Skip to content

Commit

Permalink
r/aws_vpc_dhcp_options: fix not found error handling on delete (#36933)
Browse files Browse the repository at this point in the history
The `DeleteDhcpOptions` API can return a slightly different error code
than the associated read operation. This change introduces a new error
code constant, preventing the delete operation from failing if the DHCP
options have been deleted out-of-band.

Before:

```console
% make testacc PKG=ec2 TESTS="TestAccVPCDHCPOptions_disappears|TestAccVPCDHCPOptionsAssociation_Disappears_dhcp"
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go1.21.8 test ./internal/service/ec2/... -v -count 1 -parallel 20 -run='TestAccVPCDHCPOptions_disappears|TestAccVPCDHCPOptionsAssociation_Disappears_dhcp'  -timeout 360m
=== RUN   TestAccVPCDHCPOptionsAssociation_Disappears_dhcp
=== PAUSE TestAccVPCDHCPOptionsAssociation_Disappears_dhcp
=== RUN   TestAccVPCDHCPOptions_disappears
=== PAUSE TestAccVPCDHCPOptions_disappears
=== CONT  TestAccVPCDHCPOptionsAssociation_Disappears_dhcp
=== CONT  TestAccVPCDHCPOptions_disappears
    vpc_dhcp_options_test.go:148: Error running post-test destroy, there may be dangling resources: exit status 1

        Error: deleting EC2 DHCP Options Set (dopt-0afbf75b2cde4f980): InvalidDhcpOptionsID.NotFound: The dhcpOptions ID 'dopt-0afbf75b2cde4f980' does not exist
                status code: 400, request id: fdf420ca-2934-44cd-8d0a-47d4eaceb401

--- FAIL: TestAccVPCDHCPOptions_disappears (10.52s)
=== NAME  TestAccVPCDHCPOptionsAssociation_Disappears_dhcp
    vpc_dhcp_options_association_test.go:76: Error running post-test destroy, there may be dangling resources: exit status 1

        Error: deleting EC2 DHCP Options Set (dopt-0a28e081bb7a4528e): InvalidDhcpOptionsID.NotFound: The dhcpOptions ID 'dopt-0a28e081bb7a4528e' does not exist
                status code: 400, request id: 35937877-857c-4309-b53f-48c8c2ec6dd3

--- FAIL: TestAccVPCDHCPOptionsAssociation_Disappears_dhcp (14.02s)
FAIL
FAIL    github.com/hashicorp/terraform-provider-aws/internal/service/ec2        19.930s
```

After:

```console
% make testacc PKG=ec2 TESTS="TestAccVPCDHCPOptions_disappears|TestAccVPCDHCPOptionsAssociation_Disappears_dhcp"
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go1.21.8 test ./internal/service/ec2/... -v -count 1 -parallel 20 -run='TestAccVPCDHCPOptions_disappears|TestAccVPCDHCPOptionsAssociation_Disappears_dhcp'  -timeout 360m

--- PASS: TestAccVPCDHCPOptions_disappears (10.68s)
--- PASS: TestAccVPCDHCPOptionsAssociation_Disappears_dhcp (14.40s)
PASS
ok      github.com/hashicorp/terraform-provider-aws/internal/service/ec2        20.149s
```

See the AWS EC2 error code reference for additional details:
- https://docs.aws.amazon.com/AWSEC2/latest/APIReference/errors-overview.html#api-error-codes-table-client
  • Loading branch information
jar-b authored Apr 16, 2024
1 parent 87fbdfa commit 92ed0a4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/36933.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_vpc_dhcp_options: Fix `NotFound` error handling on delete
```
1 change: 1 addition & 0 deletions internal/service/ec2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
errCodeInvalidConversionTaskIdMalformed = "InvalidConversionTaskId.Malformed"
errCodeInvalidCustomerGatewayIDNotFound = "InvalidCustomerGatewayID.NotFound"
errCodeInvalidDHCPOptionIDNotFound = "InvalidDhcpOptionID.NotFound"
errCodeInvalidDHCPOptionsIDNotFound = "InvalidDhcpOptionsID.NotFound"
errCodeInvalidFleetIdNotFound = "InvalidFleetId.NotFound"
errCodeInvalidFlowLogIdNotFound = "InvalidFlowLogId.NotFound"
errCodeInvalidGatewayIDNotFound = "InvalidGatewayID.NotFound"
Expand Down
2 changes: 1 addition & 1 deletion internal/service/ec2/vpc_dhcp_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func resourceVPCDHCPOptionsDelete(ctx context.Context, d *schema.ResourceData, m
return conn.DeleteDhcpOptionsWithContext(ctx, input)
}, errCodeDependencyViolation)

if tfawserr.ErrCodeEquals(err, errCodeInvalidDHCPOptionIDNotFound) {
if tfawserr.ErrCodeEquals(err, errCodeInvalidDHCPOptionsIDNotFound) {
return diags
}

Expand Down

0 comments on commit 92ed0a4

Please sign in to comment.