github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_eip_association_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSEIPAssociation_basic(t *testing.T) {
    14  	var a ec2.Address
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSEIPAssociationDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSEIPAssociationConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSEIPExists(
    25  						"aws_eip.bar.0", &a),
    26  					testAccCheckAWSEIPAssociationExists(
    27  						"aws_eip_association.by_allocation_id", &a),
    28  					testAccCheckAWSEIPExists(
    29  						"aws_eip.bar.1", &a),
    30  					testAccCheckAWSEIPAssociationExists(
    31  						"aws_eip_association.by_public_ip", &a),
    32  					testAccCheckAWSEIPExists(
    33  						"aws_eip.bar.2", &a),
    34  					testAccCheckAWSEIPAssociationExists(
    35  						"aws_eip_association.to_eni", &a),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckAWSEIPAssociationExists(name string, res *ec2.Address) resource.TestCheckFunc {
    43  	return func(s *terraform.State) error {
    44  		rs, ok := s.RootModule().Resources[name]
    45  		if !ok {
    46  			return fmt.Errorf("Not found: %s", name)
    47  		}
    48  
    49  		if rs.Primary.ID == "" {
    50  			return fmt.Errorf("No EIP Association ID is set")
    51  		}
    52  
    53  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    54  
    55  		request := &ec2.DescribeAddressesInput{
    56  			Filters: []*ec2.Filter{
    57  				&ec2.Filter{
    58  					Name:   aws.String("association-id"),
    59  					Values: []*string{res.AssociationId},
    60  				},
    61  			},
    62  		}
    63  		describe, err := conn.DescribeAddresses(request)
    64  		if err != nil {
    65  			return err
    66  		}
    67  
    68  		if len(describe.Addresses) != 1 ||
    69  			*describe.Addresses[0].AssociationId != *res.AssociationId {
    70  			return fmt.Errorf("EIP Association not found")
    71  		}
    72  
    73  		return nil
    74  	}
    75  }
    76  
    77  func testAccCheckAWSEIPAssociationDestroy(s *terraform.State) error {
    78  	for _, rs := range s.RootModule().Resources {
    79  		if rs.Type != "aws_eip_association" {
    80  			continue
    81  		}
    82  
    83  		if rs.Primary.ID == "" {
    84  			return fmt.Errorf("No EIP Association ID is set")
    85  		}
    86  
    87  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    88  
    89  		request := &ec2.DescribeAddressesInput{
    90  			Filters: []*ec2.Filter{
    91  				&ec2.Filter{
    92  					Name:   aws.String("association-id"),
    93  					Values: []*string{aws.String(rs.Primary.ID)},
    94  				},
    95  			},
    96  		}
    97  		describe, err := conn.DescribeAddresses(request)
    98  		if err != nil {
    99  			return err
   100  		}
   101  
   102  		if len(describe.Addresses) > 0 {
   103  			return fmt.Errorf("EIP Association still exists")
   104  		}
   105  	}
   106  	return nil
   107  }
   108  
   109  const testAccAWSEIPAssociationConfig = `
   110  resource "aws_vpc" "main" {
   111  	cidr_block = "192.168.0.0/24"
   112  }
   113  resource "aws_subnet" "sub" {
   114  	vpc_id = "${aws_vpc.main.id}"
   115  	cidr_block = "192.168.0.0/25"
   116  	availability_zone = "us-west-2a"
   117  }
   118  resource "aws_internet_gateway" "igw" {
   119  	vpc_id = "${aws_vpc.main.id}"
   120  }
   121  resource "aws_instance" "foo" {
   122  	count = 2
   123  	ami = "ami-21f78e11"
   124  	availability_zone = "us-west-2a"
   125  	instance_type = "t1.micro"
   126  	subnet_id = "${aws_subnet.sub.id}"
   127  }
   128  resource "aws_eip" "bar" {
   129  	count = 3
   130  	vpc = true
   131  }
   132  resource "aws_eip_association" "by_allocation_id" {
   133  	allocation_id = "${aws_eip.bar.0.id}"
   134  	instance_id = "${aws_instance.foo.0.id}"
   135  }
   136  resource "aws_eip_association" "by_public_ip" {
   137  	public_ip = "${aws_eip.bar.1.public_ip}"
   138  	instance_id = "${aws_instance.foo.1.id}"
   139  }
   140  resource "aws_eip_association" "to_eni" {
   141  	allocation_id = "${aws_eip.bar.2.id}"
   142  	network_interface_id = "${aws_network_interface.baz.id}"
   143  }
   144  resource "aws_network_interface" "baz" {
   145  	subnet_id = "${aws_subnet.sub.id}"
   146  	private_ips = ["192.168.0.10"]
   147  	attachment {
   148  		instance = "${aws_instance.foo.0.id}"
   149  		device_index = 1
   150  	}
   151  }
   152  `