Skip to content

Commit

Permalink
provider/aws: Change aws_elastic_ip_association to have computed
Browse files Browse the repository at this point in the history
parameters

The AWS API was send ing more parameters than we had set. Therefore,
Terraform was showing constant changes when plans were being formed
  • Loading branch information
stack72 committed May 9, 2016
1 parent b7615a6 commit 8201f1d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 58 deletions.
50 changes: 18 additions & 32 deletions builtin/providers/aws/resource_aws_eip_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func resourceAwsEipAssociation() *schema.Resource {
"allocation_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

Expand All @@ -32,24 +33,28 @@ func resourceAwsEipAssociation() *schema.Resource {
"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,
},
},
Expand Down Expand Up @@ -93,49 +98,30 @@ func resourceAwsEipAssociationCreate(d *schema.ResourceData, meta interface{}) e

d.SetId(*resp.AssociationId)

daRequest := &ec2.DescribeAddressesInput{
Filters: []*ec2.Filter{},
}

if v, ok := d.GetOk("allocation_id"); ok {
daRequest.Filters = append(daRequest.Filters,
&ec2.Filter{
Name: aws.String("allocation-id"),
Values: []*string{aws.String(v.(string))},
},
)
}
if v, ok := d.GetOk("public_ip"); ok {
daRequest.Filters = append(daRequest.Filters,
&ec2.Filter{
Name: aws.String("public-ip"),
Values: []*string{aws.String(v.(string))},
},
)
}

daResp, err := conn.DescribeAddresses(daRequest)

if err != nil {
return fmt.Errorf("Error reading EC2 Elastic IP %s: %#v", d.Get("allocation_id").(string), err)
}

return readAwsEipAssociation(d, daResp.Addresses[0])
return resourceAwsEipAssociationRead(d, meta)
}

func resourceAwsEipAssociationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

request := &ec2.DescribeAddressesInput{
AllocationIds: []*string{aws.String(d.Get("allocation_id").(string))},
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])
}

Expand All @@ -155,10 +141,10 @@ func resourceAwsEipAssociationDelete(d *schema.ResourceData, meta interface{}) e
}

func readAwsEipAssociation(d *schema.ResourceData, address *ec2.Address) error {
if err := d.Set("allocation_id", *address.AllocationId); err != nil {
if err := d.Set("allocation_id", address.AllocationId); err != nil {
return err
}
if err := d.Set("instance_id", *address.InstanceId); err != nil {
if err := d.Set("instance_id", address.InstanceId); err != nil {
return err
}
if err := d.Set("network_interface_id", address.NetworkInterfaceId); err != nil {
Expand Down
86 changes: 60 additions & 26 deletions builtin/providers/aws/resource_aws_eip_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package aws

import (
"fmt"
"log"
"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 i ec2.Instance
var a ec2.Address

resource.Test(t, resource.TestCase{
Expand All @@ -22,52 +21,87 @@ func TestAccAWSEIPAssociation_basic(t *testing.T) {
resource.TestStep{
Config: testAccAWSEIPAssociationConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(
"aws_instance.foo.0", &i),
testAccCheckAWSEIPExists(
"aws_eip.bar.0", &a),
testAccCheckAWSEIPAssociationExists(
"aws_eip_association.by_allocation_id", &i, &a),
testAccCheckInstanceExists(
"aws_instance.foo.1", &i),
"aws_eip_association.by_allocation_id", &a),
testAccCheckAWSEIPExists(
"aws_eip.bar.1", &a),
testAccCheckAWSEIPAssociationExists(
"aws_eip_association.by_public_ip", &i, &a),
"aws_eip_association.by_public_ip", &a),
testAccCheckAWSEIPExists(
"aws_eip.bar.2", &a),
testAccCheckAWSEIPAssociationExists(
"aws_eip_association.to_eni", &a),
),
},
},
})
}

func testAccCheckAWSEIPAssociationExists(name string, i *ec2.Instance, a *ec2.Address) resource.TestCheckFunc {
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 ID is set")
return fmt.Errorf("No EIP Association ID is set")
}

if rs.Primary.Attributes["allocation_id"] == *a.AllocationId {
if a.InstanceId != nil && rs.Primary.Attributes["instance_id"] == *a.InstanceId {
// success
return nil
}
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
}

return fmt.Errorf("Error finding instance/address")
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 {
log.Printf("\n\n----- This is never called")
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
}
Expand All @@ -78,7 +112,7 @@ resource "aws_vpc" "main" {
}
resource "aws_subnet" "sub" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "192.168.0.1/25"
cidr_block = "192.168.0.0/25"
availability_zone = "us-west-2a"
}
resource "aws_internet_gateway" "igw" {
Expand All @@ -95,14 +129,6 @@ resource "aws_eip" "bar" {
count = 3
vpc = true
}
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
}
}
resource "aws_eip_association" "by_allocation_id" {
allocation_id = "${aws_eip.bar.0.id}"
instance_id = "${aws_instance.foo.0.id}"
Expand All @@ -115,4 +141,12 @@ 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
}
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ description: |-
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

```
Expand Down
4 changes: 4 additions & 0 deletions website/source/layouts/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@
<a href="/docs/providers/aws/r/ebs_volume.html">aws_ebs_volume</a>
</li>

<li<%= sidebar_current("docs-aws-resource-eip-association") %>>
<a href="/docs/providers/aws/r/eip_association.html">aws_eip_association</a>
</li>

<li<%= sidebar_current("docs-aws-resource-eip") %>>
<a href="/docs/providers/aws/r/eip.html">aws_eip</a>
</li>
Expand Down

0 comments on commit 8201f1d

Please sign in to comment.