github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/aws/resource_aws_eip_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSEIP_basic(t *testing.T) {
    16  	var conf ec2.Address
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAWSEIPDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccAWSEIPConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    27  					testAccCheckAWSEIPAttributes(&conf),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAWSEIP_instance(t *testing.T) {
    35  	var conf ec2.Address
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckAWSEIPDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccAWSEIPInstanceConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    46  					testAccCheckAWSEIPAttributes(&conf),
    47  				),
    48  			},
    49  
    50  			resource.TestStep{
    51  				Config: testAccAWSEIPInstanceConfig2,
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    54  					testAccCheckAWSEIPAttributes(&conf),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccAWSEIP_network_interface(t *testing.T) {
    62  	var conf ec2.Address
    63  
    64  	resource.Test(t, resource.TestCase{
    65  		PreCheck:     func() { testAccPreCheck(t) },
    66  		Providers:    testAccProviders,
    67  		CheckDestroy: testAccCheckAWSEIPDestroy,
    68  		Steps: []resource.TestStep{
    69  			resource.TestStep{
    70  				Config: testAccAWSEIPNetworkInterfaceConfig,
    71  				Check: resource.ComposeTestCheckFunc(
    72  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    73  					testAccCheckAWSEIPAttributes(&conf),
    74  					testAccCheckAWSEIPAssociated(&conf),
    75  				),
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  func testAccCheckAWSEIPDestroy(s *terraform.State) error {
    82  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    83  
    84  	for _, rs := range s.RootModule().Resources {
    85  		if rs.Type != "aws_eip" {
    86  			continue
    87  		}
    88  
    89  		if strings.Contains(rs.Primary.ID, "eipalloc") {
    90  			req := &ec2.DescribeAddressesInput{
    91  				AllocationIds: []*string{aws.String(rs.Primary.ID)},
    92  			}
    93  			describe, err := conn.DescribeAddresses(req)
    94  			if err != nil {
    95  				// Verify the error is what we want
    96  				if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidAllocationID.NotFound" {
    97  					continue
    98  				}
    99  				return err
   100  			}
   101  
   102  			if len(describe.Addresses) > 0 {
   103  				return fmt.Errorf("still exists")
   104  			}
   105  		} else {
   106  			req := &ec2.DescribeAddressesInput{
   107  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   108  			}
   109  			describe, err := conn.DescribeAddresses(req)
   110  			if err != nil {
   111  				// Verify the error is what we want
   112  				if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidAllocationID.NotFound" {
   113  					continue
   114  				}
   115  				return err
   116  			}
   117  
   118  			if len(describe.Addresses) > 0 {
   119  				return fmt.Errorf("still exists")
   120  			}
   121  		}
   122  	}
   123  
   124  	return nil
   125  }
   126  
   127  func testAccCheckAWSEIPAttributes(conf *ec2.Address) resource.TestCheckFunc {
   128  	return func(s *terraform.State) error {
   129  		if *conf.PublicIp == "" {
   130  			return fmt.Errorf("empty public_ip")
   131  		}
   132  
   133  		return nil
   134  	}
   135  }
   136  
   137  func testAccCheckAWSEIPAssociated(conf *ec2.Address) resource.TestCheckFunc {
   138  	return func(s *terraform.State) error {
   139  		if *conf.AssociationId == "" {
   140  			return fmt.Errorf("empty association_id")
   141  		}
   142  
   143  		return nil
   144  	}
   145  }
   146  
   147  func testAccCheckAWSEIPExists(n string, res *ec2.Address) resource.TestCheckFunc {
   148  	return func(s *terraform.State) error {
   149  		rs, ok := s.RootModule().Resources[n]
   150  		if !ok {
   151  			return fmt.Errorf("Not found: %s", n)
   152  		}
   153  
   154  		if rs.Primary.ID == "" {
   155  			return fmt.Errorf("No EIP ID is set")
   156  		}
   157  
   158  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   159  
   160  		if strings.Contains(rs.Primary.ID, "eipalloc") {
   161  			req := &ec2.DescribeAddressesInput{
   162  				AllocationIds: []*string{aws.String(rs.Primary.ID)},
   163  			}
   164  			describe, err := conn.DescribeAddresses(req)
   165  			if err != nil {
   166  				return err
   167  			}
   168  
   169  			if len(describe.Addresses) != 1 ||
   170  				*describe.Addresses[0].AllocationId != rs.Primary.ID {
   171  				return fmt.Errorf("EIP not found")
   172  			}
   173  			*res = *describe.Addresses[0]
   174  
   175  		} else {
   176  			req := &ec2.DescribeAddressesInput{
   177  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   178  			}
   179  			describe, err := conn.DescribeAddresses(req)
   180  			if err != nil {
   181  				return err
   182  			}
   183  
   184  			if len(describe.Addresses) != 1 ||
   185  				*describe.Addresses[0].PublicIp != rs.Primary.ID {
   186  				return fmt.Errorf("EIP not found")
   187  			}
   188  			*res = *describe.Addresses[0]
   189  		}
   190  
   191  		return nil
   192  	}
   193  }
   194  
   195  const testAccAWSEIPConfig = `
   196  resource "aws_eip" "bar" {
   197  }
   198  `
   199  
   200  const testAccAWSEIPInstanceConfig = `
   201  resource "aws_instance" "foo" {
   202  	# us-west-2
   203  	ami = "ami-4fccb37f"
   204  	instance_type = "m1.small"
   205  }
   206  
   207  resource "aws_eip" "bar" {
   208  	instance = "${aws_instance.foo.id}"
   209  }
   210  `
   211  
   212  const testAccAWSEIPInstanceConfig2 = `
   213  resource "aws_instance" "bar" {
   214  	# us-west-2
   215  	ami = "ami-4fccb37f"
   216  	instance_type = "m1.small"
   217  }
   218  
   219  resource "aws_eip" "bar" {
   220  	instance = "${aws_instance.bar.id}"
   221  }
   222  `
   223  const testAccAWSEIPNetworkInterfaceConfig = `
   224  resource "aws_vpc" "bar" {
   225  	cidr_block = "10.0.0.0/24"
   226  }
   227  resource "aws_internet_gateway" "bar" {
   228  	vpc_id = "${aws_vpc.bar.id}"
   229  }
   230  resource "aws_subnet" "bar" {
   231    vpc_id = "${aws_vpc.bar.id}"
   232    availability_zone = "us-west-2a"
   233    cidr_block = "10.0.0.0/24"
   234  }
   235  resource "aws_network_interface" "bar" {
   236    subnet_id = "${aws_subnet.bar.id}"
   237  	private_ips = ["10.0.0.10"]
   238    security_groups = [ "${aws_vpc.bar.default_security_group_id}" ]
   239  }
   240  resource "aws_eip" "bar" {
   241  	vpc = "true"
   242  	network_interface = "${aws_network_interface.bar.id}"
   243  }
   244  `