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

Fixes EC2 capacity reservation in state not found #9862

Merged
merged 2 commits into from
Aug 26, 2019

Conversation

thlacroix
Copy link
Contributor

@thlacroix thlacroix commented Aug 23, 2019

When an EC2 Capacity Reservation is created with terraform, but is then manually deleted, the next terraform plan gives:

Error describing EC2 Capacity Reservations: InvalidCapacityReservationId.NotFound: An error occurred when calling the DescribeCapacityReservations operation: The capacity reservation ID 'cr-xxx' was not found

As the EC2 Capacity Reservation ID is still in the state, terraform tries to read it, but the error handling doesn't check the InvalidCapacityReservationId.NotFound case. It returns the error directly, instead of doing a simple d.SetId(""); return nil to notify that the resource doesn't exist anymore.

I used a structure similar to https://github.com/terraform-providers/terraform-provider-aws/blob/master/aws/resource_aws_ebs_volume.go#L242 to fix the issue.

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" comments, they generate extra noise for pull request followers and do not help prioritize the request

Release note for CHANGELOG:

Fixes error handling when an EC2 Capacity Reservation is deleted manually but is still in state.

Output from acceptance testing:

$ make testacc TEST=./aws TESTARGS='-run=TestAccAWSEc2CapacityReservation'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -parallel 20 -run=TestAccAWSEc2CapacityReservation -timeout 120m
=== RUN   TestAccAWSEc2CapacityReservation_basic
=== PAUSE TestAccAWSEc2CapacityReservation_basic
=== RUN   TestAccAWSEc2CapacityReservation_ebsOptimized
=== PAUSE TestAccAWSEc2CapacityReservation_ebsOptimized
=== RUN   TestAccAWSEc2CapacityReservation_endDate
=== PAUSE TestAccAWSEc2CapacityReservation_endDate
=== RUN   TestAccAWSEc2CapacityReservation_endDateType
=== PAUSE TestAccAWSEc2CapacityReservation_endDateType
=== RUN   TestAccAWSEc2CapacityReservation_ephemeralStorage
=== PAUSE TestAccAWSEc2CapacityReservation_ephemeralStorage
=== RUN   TestAccAWSEc2CapacityReservation_instanceCount
=== PAUSE TestAccAWSEc2CapacityReservation_instanceCount
=== RUN   TestAccAWSEc2CapacityReservation_instanceMatchCriteria
=== PAUSE TestAccAWSEc2CapacityReservation_instanceMatchCriteria
=== RUN   TestAccAWSEc2CapacityReservation_instanceType
=== PAUSE TestAccAWSEc2CapacityReservation_instanceType
=== RUN   TestAccAWSEc2CapacityReservation_tags
=== PAUSE TestAccAWSEc2CapacityReservation_tags
=== RUN   TestAccAWSEc2CapacityReservation_tenancy
--- SKIP: TestAccAWSEc2CapacityReservation_tenancy (0.00s)
    resource_aws_ec2_capacity_reservation_test.go:362: EC2 Capacity Reservations do not currently support dedicated tenancy.
=== CONT  TestAccAWSEc2CapacityReservation_instanceCount
=== CONT  TestAccAWSEc2CapacityReservation_basic
=== CONT  TestAccAWSEc2CapacityReservation_ephemeralStorage
=== CONT  TestAccAWSEc2CapacityReservation_endDateType
=== CONT  TestAccAWSEc2CapacityReservation_endDate
=== CONT  TestAccAWSEc2CapacityReservation_ebsOptimized
=== CONT  TestAccAWSEc2CapacityReservation_instanceType
=== CONT  TestAccAWSEc2CapacityReservation_tags
=== CONT  TestAccAWSEc2CapacityReservation_instanceMatchCriteria
--- PASS: TestAccAWSEc2CapacityReservation_ephemeralStorage (30.28s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceMatchCriteria (30.29s)
--- PASS: TestAccAWSEc2CapacityReservation_basic (30.34s)
--- PASS: TestAccAWSEc2CapacityReservation_ebsOptimized (30.36s)
--- PASS: TestAccAWSEc2CapacityReservation_endDate (50.49s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceCount (50.52s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceType (50.94s)
--- PASS: TestAccAWSEc2CapacityReservation_endDateType (67.30s)
--- PASS: TestAccAWSEc2CapacityReservation_tags (69.69s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	69.757s

@thlacroix thlacroix requested a review from a team August 23, 2019 12:57
@ghost ghost added size/XS Managed by automation to categorize the size of a PR. service/ec2 Issues and PRs that pertain to the ec2 service. labels Aug 23, 2019
@ryndaniels ryndaniels self-assigned this Aug 26, 2019
Copy link
Contributor

@ryndaniels ryndaniels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @thlacroix - thanks for this fix! One optional change if you'd like to make it, but otherwise looks good! 👍

Test output:

--- SKIP: TestAccAWSEc2CapacityReservation_tenancy (0.00s)
--- PASS: TestAccAWSEc2CapacityReservation_basic (9.63s)
--- PASS: TestAccAWSEc2CapacityReservation_ebsOptimized (10.78s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceMatchCriteria (10.88s)
--- PASS: TestAccAWSEc2CapacityReservation_ephemeralStorage (11.73s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceType (17.48s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceCount (17.82s)
--- PASS: TestAccAWSEc2CapacityReservation_endDate (18.29s)
--- PASS: TestAccAWSEc2CapacityReservation_tags (23.53s)
--- PASS: TestAccAWSEc2CapacityReservation_endDateType (23.56s)

@@ -165,13 +166,16 @@ func resourceAwsEc2CapacityReservationRead(d *schema.ResourceData, meta interfac
})

if err != nil {
return fmt.Errorf("Error describing EC2 Capacity Reservations: %s", err)
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCapacityReservationId.NotFound" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to do this would be the isAWSErr helper function:

Suggested change
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidCapacityReservationId.NotFound" {
if isAWSErr(err, "InvalidCapacityReservationId.NotFound", "") {

If you wanted to make this change and remove the associated awserr import (and re-run the tests 🙂) that would be super - but this is perfectly functional as it is. The isAWSErr is just a slightly neater-looking style convention we're trying to move towards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ryndaniels, thanks for the review / comment, will make the change quickly

@thlacroix
Copy link
Contributor Author

Suggested improvement added, and output from acceptance testing updated.

@ryndaniels
Copy link
Contributor

Awesome, thanks again @thlacroix! 🚀

--- SKIP: TestAccAWSEc2CapacityReservation_tenancy (0.00s)
--- PASS: TestAccAWSEc2CapacityReservation_basic (10.41s)
--- PASS: TestAccAWSEc2CapacityReservation_ebsOptimized (11.80s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceMatchCriteria (12.61s)
--- PASS: TestAccAWSEc2CapacityReservation_ephemeralStorage (12.68s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceType (17.54s)
--- PASS: TestAccAWSEc2CapacityReservation_endDate (17.82s)
--- PASS: TestAccAWSEc2CapacityReservation_instanceCount (19.31s)
--- PASS: TestAccAWSEc2CapacityReservation_tags (23.20s)
--- PASS: TestAccAWSEc2CapacityReservation_endDateType (23.88s)

@ryndaniels ryndaniels added this to the v2.26.0 milestone Aug 26, 2019
@ryndaniels ryndaniels merged commit 8b4182b into hashicorp:master Aug 26, 2019
@ghost
Copy link

ghost commented Aug 29, 2019

This has been released in version 2.26.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

@ghost
Copy link

ghost commented Nov 1, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
service/ec2 Issues and PRs that pertain to the ec2 service. size/XS Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants