-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/aws: Change aws_elastic_ip_association
to have computed parameters
#6552
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8eef469
Add documentation for aws_eip_association
c952f17
New top level AWS resource aws_eip_association
cd87db3
Add tests for aws_eip_association
b7615a6
Merge branch 'f-2680-aws-eip-association' of https://github.com/jkinr…
stack72 8201f1d
provider/aws: Change `aws_elastic_ip_association` to have computed
stack72 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsEipAssociation() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsEipAssociationCreate, | ||
Read: resourceAwsEipAssociationRead, | ||
Delete: resourceAwsEipAssociationDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"allocation_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"allow_reassociation": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"instance_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"network_interface_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"private_ip_address": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"public_ip": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsEipAssociationCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
request := &ec2.AssociateAddressInput{} | ||
|
||
if v, ok := d.GetOk("allocation_id"); ok { | ||
request.AllocationId = aws.String(v.(string)) | ||
} | ||
if v, ok := d.GetOk("allow_reassociation"); ok { | ||
request.AllowReassociation = aws.Bool(v.(bool)) | ||
} | ||
if v, ok := d.GetOk("instance_id"); ok { | ||
request.InstanceId = aws.String(v.(string)) | ||
} | ||
if v, ok := d.GetOk("network_interface_id"); ok { | ||
request.NetworkInterfaceId = aws.String(v.(string)) | ||
} | ||
if v, ok := d.GetOk("private_ip_address"); ok { | ||
request.PrivateIpAddress = aws.String(v.(string)) | ||
} | ||
if v, ok := d.GetOk("public_ip"); ok { | ||
request.PublicIp = aws.String(v.(string)) | ||
} | ||
|
||
log.Printf("[DEBUG] EIP association configuration: %#v", request) | ||
|
||
resp, err := conn.AssociateAddress(request) | ||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok { | ||
return fmt.Errorf("[WARN] Error attaching EIP, message: \"%s\", code: \"%s\"", | ||
awsErr.Message(), awsErr.Code()) | ||
} | ||
return err | ||
} | ||
|
||
d.SetId(*resp.AssociationId) | ||
|
||
return resourceAwsEipAssociationRead(d, meta) | ||
} | ||
|
||
func resourceAwsEipAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
request := &ec2.DescribeAddressesInput{ | ||
Filters: []*ec2.Filter{ | ||
&ec2.Filter{ | ||
Name: aws.String("association-id"), | ||
Values: []*string{aws.String(d.Id())}, | ||
}, | ||
}, | ||
} | ||
|
||
response, err := conn.DescribeAddresses(request) | ||
if err != nil { | ||
return fmt.Errorf("Error reading EC2 Elastic IP %s: %#v", d.Get("allocation_id").(string), err) | ||
} | ||
|
||
if response.Addresses == nil || len(response.Addresses) == 0 { | ||
return fmt.Errorf("Unable to find EIP Association: %s", d.Id()) | ||
} | ||
|
||
return readAwsEipAssociation(d, response.Addresses[0]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should check if |
||
} | ||
|
||
func resourceAwsEipAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
opts := &ec2.DisassociateAddressInput{ | ||
AssociationId: aws.String(d.Id()), | ||
} | ||
|
||
_, err := conn.DisassociateAddress(opts) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting Elastic IP association: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readAwsEipAssociation(d *schema.ResourceData, address *ec2.Address) error { | ||
if err := d.Set("allocation_id", address.AllocationId); err != nil { | ||
return err | ||
} | ||
if err := d.Set("instance_id", address.InstanceId); err != nil { | ||
return err | ||
} | ||
if err := d.Set("network_interface_id", address.NetworkInterfaceId); err != nil { | ||
return err | ||
} | ||
if err := d.Set("private_ip_address", address.PrivateIpAddress); err != nil { | ||
return err | ||
} | ||
if err := d.Set("public_ip", address.PublicIp); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
152 changes: 152 additions & 0 deletions
152
builtin/providers/aws/resource_aws_eip_association_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSEIPAssociation_basic(t *testing.T) { | ||
var a ec2.Address | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSEIPAssociationDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSEIPAssociationConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSEIPExists( | ||
"aws_eip.bar.0", &a), | ||
testAccCheckAWSEIPAssociationExists( | ||
"aws_eip_association.by_allocation_id", &a), | ||
testAccCheckAWSEIPExists( | ||
"aws_eip.bar.1", &a), | ||
testAccCheckAWSEIPAssociationExists( | ||
"aws_eip_association.by_public_ip", &a), | ||
testAccCheckAWSEIPExists( | ||
"aws_eip.bar.2", &a), | ||
testAccCheckAWSEIPAssociationExists( | ||
"aws_eip_association.to_eni", &a), | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we check the ENI association as well? |
||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSEIPAssociationExists(name string, res *ec2.Address) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No EIP Association ID is set") | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).ec2conn | ||
|
||
request := &ec2.DescribeAddressesInput{ | ||
Filters: []*ec2.Filter{ | ||
&ec2.Filter{ | ||
Name: aws.String("association-id"), | ||
Values: []*string{res.AssociationId}, | ||
}, | ||
}, | ||
} | ||
describe, err := conn.DescribeAddresses(request) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(describe.Addresses) != 1 || | ||
*describe.Addresses[0].AssociationId != *res.AssociationId { | ||
return fmt.Errorf("EIP Association not found") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckAWSEIPAssociationDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_eip_association" { | ||
continue | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No EIP Association ID is set") | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).ec2conn | ||
|
||
request := &ec2.DescribeAddressesInput{ | ||
Filters: []*ec2.Filter{ | ||
&ec2.Filter{ | ||
Name: aws.String("association-id"), | ||
Values: []*string{aws.String(rs.Primary.ID)}, | ||
}, | ||
}, | ||
} | ||
describe, err := conn.DescribeAddresses(request) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(describe.Addresses) > 0 { | ||
return fmt.Errorf("EIP Association still exists") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
const testAccAWSEIPAssociationConfig = ` | ||
resource "aws_vpc" "main" { | ||
cidr_block = "192.168.0.0/24" | ||
} | ||
resource "aws_subnet" "sub" { | ||
vpc_id = "${aws_vpc.main.id}" | ||
cidr_block = "192.168.0.0/25" | ||
availability_zone = "us-west-2a" | ||
} | ||
resource "aws_internet_gateway" "igw" { | ||
vpc_id = "${aws_vpc.main.id}" | ||
} | ||
resource "aws_instance" "foo" { | ||
count = 2 | ||
ami = "ami-21f78e11" | ||
availability_zone = "us-west-2a" | ||
instance_type = "t1.micro" | ||
subnet_id = "${aws_subnet.sub.id}" | ||
} | ||
resource "aws_eip" "bar" { | ||
count = 3 | ||
vpc = true | ||
} | ||
resource "aws_eip_association" "by_allocation_id" { | ||
allocation_id = "${aws_eip.bar.0.id}" | ||
instance_id = "${aws_instance.foo.0.id}" | ||
} | ||
resource "aws_eip_association" "by_public_ip" { | ||
public_ip = "${aws_eip.bar.1.public_ip}" | ||
instance_id = "${aws_instance.foo.1.id}" | ||
} | ||
resource "aws_eip_association" "to_eni" { | ||
allocation_id = "${aws_eip.bar.2.id}" | ||
network_interface_id = "${aws_network_interface.baz.id}" | ||
} | ||
resource "aws_network_interface" "baz" { | ||
subnet_id = "${aws_subnet.sub.id}" | ||
private_ips = ["192.168.0.10"] | ||
attachment { | ||
instance = "${aws_instance.foo.0.id}" | ||
device_index = 1 | ||
} | ||
} | ||
` |
67 changes: 67 additions & 0 deletions
67
website/source/docs/providers/aws/r/eip_association.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_eip_association" | ||
sidebar_current: "docs-aws-resource-eip-association" | ||
description: |- | ||
Provides an AWS EIP Association | ||
--- | ||
|
||
# aws\_eip\_association | ||
|
||
Provides an AWS EIP Association as a top level resource, to associate and | ||
disassociate Elastic IPs from AWS Instances and Network Interfaces. | ||
|
||
~> **NOTE:** `aws_eip_association` is useful in scenarios where EIPs are either | ||
pre-existing or distributed to customers or users and therefore cannot be changed. | ||
|
||
## Example Usage | ||
|
||
``` | ||
resource "aws_eip_association" "eip_assoc" { | ||
instance_id = "${aws_instance.web.id}" | ||
allocation_id = "${aws_eip.example.allocation_id}" | ||
} | ||
|
||
resource "aws_instance" "web" { | ||
ami = "ami-21f78e11" | ||
availability_zone = "us-west-2a" | ||
instance_type = "t1.micro" | ||
tags { | ||
Name = "HelloWorld" | ||
} | ||
} | ||
|
||
resource "aws_eip" "example" { | ||
vpc = true | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `allocation_id` - (Optional) The allocation ID. This is required for EC2-VPC. | ||
* `allow_reassociation` - (Optional, Boolean) Whether to allow an Elastic IP to | ||
be re-associated. Defaults to `true` in VPC. | ||
* `instance_id` - (Optional) The ID of the instance. This is required for | ||
EC2-Classic. For EC2-VPC, you can specify either the instance ID or the | ||
network interface ID, but not both. The operation fails if you specify an | ||
instance ID unless exactly one network interface is attached. | ||
* `network_interface_id` - (Optional) The ID of the network interface. If the | ||
instance has more than one network interface, you must specify a network | ||
interface ID. | ||
* `private_ip_address` - (Optional) The primary or secondary private IP address | ||
to associate with the Elastic IP address. If no private IP address is | ||
specified, the Elastic IP address is associated with the primary private IP | ||
address. | ||
* `public_ip` - (Optional) The Elastic IP address. This is required for EC2-Classic. | ||
|
||
## Attributes Reference | ||
|
||
* `association_id` - The ID that represents the association of the Elastic IP | ||
address with an instance. | ||
* `allocation_id` - As above | ||
* `instance_id` - As above | ||
* `network_interface_id` - As above | ||
* `private_ip_address` - As above | ||
* `public_ip` - As above |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
allocation-id
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an EIP is associated with something (ENI, Instance, Public IP etc), it get's an association-id. This was used as the ID. Therefore, the call to get the association uses the ID rather than the different scenarios it can be. An association_id will always be specific AFAICT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I misread and thought allocation was the ID