Skip to content

Commit

Permalink
Merge pull request #3 from hpamukcu/handle_out_of_band_tag_delete
Browse files Browse the repository at this point in the history
Handle out of band tag delete in resourceAwsEc2TagRead
  • Loading branch information
Joe Stump authored Sep 30, 2019
2 parents 1e82903 + a8b754e commit 90f2979
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
36 changes: 30 additions & 6 deletions aws/resource_aws_ec2_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"log"
"strings"
"time"

Expand All @@ -11,6 +12,19 @@ import (
"github.com/hashicorp/terraform/helper/schema"
)

type ResourceNotExists struct {
message string
}

func NewResourceNotExists(message string) *ResourceNotExists {
return &ResourceNotExists{
message: message,
}
}
func (e *ResourceNotExists) Error() string {
return e.message
}

func resourceAwsEc2Tag() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEc2TagCreate,
Expand Down Expand Up @@ -103,20 +117,30 @@ func resourceAwsEc2TagRead(d *schema.ResourceData, meta interface{}) error {
},
})

// tag not found _yet_
if len(tags.Tags) == 0 {
return resource.RetryableError(fmt.Errorf("tag not found"))
}

if err != nil {
return resource.RetryableError(err)
}

// tag not found _yet_
if len(tags.Tags) == 0 {
return resource.RetryableError(NewResourceNotExists("tag not found"))
}

return nil
})

if retryError != nil {
return fmt.Errorf("[ERROR] Tag %s not found on resource %s", key, id)
if _, ok := retryError.(*ResourceNotExists); !ok {
return fmt.Errorf("[ERROR] Could not read tag %s on resource %s", key, id)
}
}

if len(tags.Tags) == 0 {
// The API call did not fail but the tag does not exists on resource
// Did not find the tag, as per contract with TF report:https://www.terraform.io/docs/extend/writing-custom-providers.html
log.Printf("[WARN]There are no tags on resource %s", id)
d.SetId("")
return nil
}

if len(tags.Tags) != 1 {
Expand Down
64 changes: 64 additions & 0 deletions aws/resource_aws_ec2_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,66 @@ func TestAccAWSEc2ResourceTag_subnet(t *testing.T) {
})
}

func TestAccAWSEc2ResourceTag_out_of_band_delete(t *testing.T) {
var tag ec2.TagDescription

testCheck := func(*terraform.State) error {
key := aws.StringValue(tag.Key)
if key != "Name" {
return fmt.Errorf("Expected Key to be 'Name'; got '%s'", key)
}

value := aws.StringValue(tag.Value)
if value != "Hello World" {
return fmt.Errorf("Expected Value to be 'Hello World'; got '%s'", value)
}

return nil
}

resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccEc2ResourceTagConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckEc2ResourceTagExists(
"aws_ec2_tag.test", &tag),
testCheck,
),
},
{
PreConfig: deleteTag(t, &tag), // Simulate out of band delete
Config: testAccEc2ResourceTagConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckEc2ResourceTagExists(
"aws_ec2_tag.test", &tag),
testCheck,
),
},
},
})
}

func deleteTag(t *testing.T, tag *ec2.TagDescription) func() {
return func() {
conn := testAccProvider.Meta().(*AWSClient).ec2conn
_, err := conn.DeleteTags(&ec2.DeleteTagsInput{
Resources: []*string{tag.ResourceId},
Tags: []*ec2.Tag{
{
Key: tag.Key,
Value: tag.Value,
},
},
})

if err != nil {
t.Errorf("Failed to delete the tag: %v", tag)
}
}
}

func testAccCheckEc2ResourceTagExists(n string, tag *ec2.TagDescription) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -127,6 +187,10 @@ resource "aws_ec2_tag" "test" {
resource_id = "${aws_vpc.test.id}"
key = "Name"
value = "Hello World"
timeouts {
create = "2s"
}
}
`

Expand Down

0 comments on commit 90f2979

Please sign in to comment.