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

Remove aws_vpc_dhcp_options if not found #13610

Merged
merged 1 commit into from
Apr 16, 2017
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
13 changes: 12 additions & 1 deletion builtin/providers/aws/resource_aws_vpc_dhcp_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 40 additions & 0 deletions builtin/providers/aws/resource_aws_vpc_dhcp_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down