From cb437ad9b599859beecdd72694596d972b36cc60 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Wed, 12 Apr 2017 18:51:17 -0400 Subject: [PATCH] Remove aws_vpc_dhcp_options if not found. --- .../aws/resource_aws_vpc_dhcp_options.go | 13 +++++- .../aws/resource_aws_vpc_dhcp_options_test.go | 40 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_vpc_dhcp_options.go b/builtin/providers/aws/resource_aws_vpc_dhcp_options.go index 16c33fd4d9b4..66de6dbeb44f 100644 --- a/builtin/providers/aws/resource_aws_vpc_dhcp_options.go +++ b/builtin/providers/aws/resource_aws_vpc_dhcp_options.go @@ -147,7 +147,18 @@ func resourceAwsVpcDhcpOptionsRead(d *schema.ResourceData, meta interface{}) err resp, err := conn.DescribeDhcpOptions(req) if err != nil { - return fmt.Errorf("Error retrieving DHCP Options: %s", err) + ec2err, ok := err.(awserr.Error) + if !ok { + return fmt.Errorf("Error retrieving DHCP Options: %s", err.Error()) + } + + if ec2err.Code() == "InvalidDhcpOptionID.NotFound" { + log.Printf("[WARN] DHCP Options (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + + return fmt.Errorf("Error retrieving DHCP Options: %s", err.Error()) } if len(resp.DhcpOptions) == 0 { diff --git a/builtin/providers/aws/resource_aws_vpc_dhcp_options_test.go b/builtin/providers/aws/resource_aws_vpc_dhcp_options_test.go index baa86f7d7d96..f101f95f3c73 100644 --- a/builtin/providers/aws/resource_aws_vpc_dhcp_options_test.go +++ b/builtin/providers/aws/resource_aws_vpc_dhcp_options_test.go @@ -36,6 +36,26 @@ func TestAccAWSDHCPOptions_basic(t *testing.T) { }) } +func TestAccAWSDHCPOptions_deleteOptions(t *testing.T) { + var d ec2.DhcpOptions + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDHCPOptionsDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDHCPOptionsConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckDHCPOptionsExists("aws_vpc_dhcp_options.foo", &d), + testAccCheckDHCPOptionsDelete("aws_vpc_dhcp_options.foo"), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckDHCPOptionsDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).ec2conn @@ -104,6 +124,26 @@ func testAccCheckDHCPOptionsExists(n string, d *ec2.DhcpOptions) resource.TestCh } } +func testAccCheckDHCPOptionsDelete(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).ec2conn + _, err := conn.DeleteDhcpOptions(&ec2.DeleteDhcpOptionsInput{ + DhcpOptionsId: aws.String(rs.Primary.ID), + }) + + return err + } +} + const testAccDHCPOptionsConfig = ` resource "aws_vpc_dhcp_options" "foo" { domain_name = "service.consul"