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

r/aws_eip fix case when aws returns multiple eips from read request #5331

Merged
merged 1 commit into from Jul 30, 2018
Merged

r/aws_eip fix case when aws returns multiple eips from read request #5331

merged 1 commit into from Jul 30, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jul 25, 2018

Fixes #5330

Changes proposed in this pull request:

  • instead of just checking the first element in the addresses response, loop through all of them to see if the one we want is returned

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccAWSAvailabilityZones'

...

Wasn't entirely sure how to add an acceptance test for this, given that the acceptance tests hit the AWS api, and the bug in question seems like an inconsistency in AWS's api rather than what terraform itself is doing. If there's a good way to test this via an acceptance test please let me know though 😄

@ghost ghost added size/S Managed by automation to categorize the size of a PR. labels Jul 25, 2018
@ghost ghost changed the title [WIP] r/aws_eip fix case when aws returns multiple eips from read request r/aws_eip fix case when aws returns multiple eips from read request Jul 25, 2018
// Sometimes AWS returns EIPs that *aren't* the EIP we're looking for
// so we loop over the returned addresses to see if it's in the list of results
for _, addr := range describeAddresses.Addresses {
if (domain == "vpc" && *addr.AllocationId == id) || *address.PublicIp == id {
Copy link
Contributor

Choose a reason for hiding this comment

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

This new logic can cause Terraform to crash due to a typo:

=== RUN   TestAccAWSEIP_importEc2Classic

------- Stderr: -------
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x1ec009b]

goroutine 215 [running]:
github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsEipRead(0xc420a6f8f0, 0x307b000, 0xc420592c00, 0x2f3d4e0, 0xc420952600)
	/opt/teamcity-agent/work/7be301907ad6c6ab/src/github.com/terraform-providers/terraform-provider-aws/aws/resource_aws_eip.go:189 +0x3bb

While we're fixing this, we should also switch the * dereferencing to the SDK provided aws.StringValue() function to further prevent panics:

if (domain == "vpc" && aws.StringValue(addr.AllocationId) == id) || aws.StringValue(addr.PublicIp) == id {

}
}

address := describeAddresses.Addresses[0]
if address == nil {
return fmt.Errorf("Unable to find EIP: %#v", describeAddresses.Addresses)
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of returning an error in these cases, we generally prefer to remove the resource from the Terraform state to trigger a resource recreation:

if address == nil {
	log.Printf("[WARN] EIP %q not found, removing from state", d.Id())
	d.SetId("")
	return nil
}

@bflad bflad added bug Addresses a defect in current functionality. upstream Addresses functionality related to the cloud provider. service/ec2 Issues and PRs that pertain to the ec2 service. waiting-response Maintainers are waiting on response from community or contributor. labels Jul 25, 2018
@ghost ghost added the size/S Managed by automation to categorize the size of a PR. label Jul 26, 2018
@ghost
Copy link
Author

ghost commented Jul 26, 2018

@bflad sorry for the delay! Made the changes you requested.

@bflad bflad added this to the v1.30.0 milestone Jul 27, 2018
@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Jul 27, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @rv-aburdine! 🚀

10 tests passed (all tests)
=== RUN   TestAccAWSEIP_disappears
--- PASS: TestAccAWSEIP_disappears (5.74s)
=== RUN   TestAccAWSEIP_basic
--- PASS: TestAccAWSEIP_basic (7.59s)
=== RUN   TestAccAWSEIP_tags
--- PASS: TestAccAWSEIP_tags (12.45s)
=== RUN   TestAccAWSEIP_network_interface
--- PASS: TestAccAWSEIP_network_interface (21.67s)
=== RUN   TestAccAWSEIP_twoEIPsOneNetworkInterface
--- PASS: TestAccAWSEIP_twoEIPsOneNetworkInterface (22.13s)
=== RUN   TestAccAWSEIP_importVpc
--- PASS: TestAccAWSEIP_importVpc (22.72s)
=== RUN   TestAccAWSEIP_associated_user_private_ip
--- PASS: TestAccAWSEIP_associated_user_private_ip (108.01s)
=== RUN   TestAccAWSEIP_importEc2Classic
--- PASS: TestAccAWSEIP_importEc2Classic (244.25s)
=== RUN   TestAccAWSEIP_classic_disassociate
--- PASS: TestAccAWSEIP_classic_disassociate (253.92s)
=== RUN   TestAccAWSEIP_instance
--- PASS: TestAccAWSEIP_instance (343.30s)

@bflad bflad merged commit 2ae5370 into hashicorp:master Jul 30, 2018
bflad added a commit that referenced this pull request Jul 30, 2018
@bflad
Copy link
Contributor

bflad commented Aug 2, 2018

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

@ghost ghost deleted the fix/eip-filter branch August 2, 2018 14:32
@ghost
Copy link

ghost commented Apr 4, 2020

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 Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/ec2 Issues and PRs that pertain to the ec2 service. size/S Managed by automation to categorize the size of a PR. upstream Addresses functionality related to the cloud provider.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EIP scope change incorrectly detected
2 participants