Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_launch_template: Properly recreate existing resource when deleted #5967

Merged
merged 1 commit into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,19 +496,30 @@ func resourceAwsLaunchTemplateRead(d *schema.ResourceData, meta interface{}) err
dlt, err := conn.DescribeLaunchTemplates(&ec2.DescribeLaunchTemplatesInput{
LaunchTemplateIds: []*string{aws.String(d.Id())},
})

if isAWSErr(err, ec2.LaunchTemplateErrorCodeLaunchTemplateIdDoesNotExist, "") {
log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
}

// AWS SDK constant above is currently incorrect
if isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") {
log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
if isAWSErr(err, ec2.LaunchTemplateErrorCodeLaunchTemplateIdDoesNotExist, "") {
log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error getting launch template: %s", err)
}
if len(dlt.LaunchTemplates) == 0 {

if dlt == nil || len(dlt.LaunchTemplates) == 0 {
log.Printf("[WARN] launch template (%s) not found - removing from state", d.Id())
d.SetId("")
return nil
}

if *dlt.LaunchTemplates[0].LaunchTemplateId != d.Id() {
return fmt.Errorf("Unable to find launch template: %#v", dlt.LaunchTemplates)
}
Expand Down Expand Up @@ -641,10 +652,15 @@ func resourceAwsLaunchTemplateDelete(d *schema.ResourceData, meta interface{}) e
_, err := conn.DeleteLaunchTemplate(&ec2.DeleteLaunchTemplateInput{
LaunchTemplateId: aws.String(d.Id()),
})

if isAWSErr(err, ec2.LaunchTemplateErrorCodeLaunchTemplateIdDoesNotExist, "") {
return nil
}
// AWS SDK constant above is currently incorrect
if isAWSErr(err, "InvalidLaunchTemplateId.NotFound", "") {
return nil
}
if err != nil {
if isAWSErr(err, ec2.LaunchTemplateErrorCodeLaunchTemplateIdDoesNotExist, "") {
return nil
}
return err
}

Expand Down
36 changes: 36 additions & 0 deletions aws/resource_aws_launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ func TestAccAWSLaunchTemplate_basic(t *testing.T) {
})
}

func TestAccAWSLaunchTemplate_disappears(t *testing.T) {
var launchTemplate ec2.LaunchTemplate
resourceName := "aws_launch_template.foo"
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchTemplateConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchTemplateExists(resourceName, &launchTemplate),
testAccCheckAWSLaunchTemplateDisappears(&launchTemplate),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAWSLaunchTemplate_BlockDeviceMappings_EBS(t *testing.T) {
var template ec2.LaunchTemplate
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -437,6 +459,20 @@ func testAccCheckAWSLaunchTemplateDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSLaunchTemplateDisappears(launchTemplate *ec2.LaunchTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

input := &ec2.DeleteLaunchTemplateInput{
LaunchTemplateId: launchTemplate.LaunchTemplateId,
}

_, err := conn.DeleteLaunchTemplate(input)

return err
}
}

func testAccAWSLaunchTemplateConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "aws_launch_template" "foo" {
Expand Down