github.com/anfernee/terraform@v0.6.16-0.20160430000239-06e5085a92f2/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  		IDRefreshName: "aws_eip.bar",
    21  		Providers:     testAccProviders,
    22  		CheckDestroy:  testAccCheckAWSEIPDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccAWSEIPConfig,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    28  					testAccCheckAWSEIPAttributes(&conf),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccAWSEIP_instance(t *testing.T) {
    36  	var conf ec2.Address
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:      func() { testAccPreCheck(t) },
    40  		IDRefreshName: "aws_eip.bar",
    41  		Providers:     testAccProviders,
    42  		CheckDestroy:  testAccCheckAWSEIPDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccAWSEIPInstanceConfig,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    48  					testAccCheckAWSEIPAttributes(&conf),
    49  				),
    50  			},
    51  
    52  			resource.TestStep{
    53  				Config: testAccAWSEIPInstanceConfig2,
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    56  					testAccCheckAWSEIPAttributes(&conf),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccAWSEIP_network_interface(t *testing.T) {
    64  	var conf ec2.Address
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:      func() { testAccPreCheck(t) },
    68  		IDRefreshName: "aws_eip.bar",
    69  		Providers:     testAccProviders,
    70  		CheckDestroy:  testAccCheckAWSEIPDestroy,
    71  		Steps: []resource.TestStep{
    72  			resource.TestStep{
    73  				Config: testAccAWSEIPNetworkInterfaceConfig,
    74  				Check: resource.ComposeTestCheckFunc(
    75  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    76  					testAccCheckAWSEIPAttributes(&conf),
    77  					testAccCheckAWSEIPAssociated(&conf),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccAWSEIP_twoEIPsOneNetworkInterface(t *testing.T) {
    85  	var one, two ec2.Address
    86  
    87  	resource.Test(t, resource.TestCase{
    88  		PreCheck:      func() { testAccPreCheck(t) },
    89  		IDRefreshName: "aws_eip.one",
    90  		Providers:     testAccProviders,
    91  		CheckDestroy:  testAccCheckAWSEIPDestroy,
    92  		Steps: []resource.TestStep{
    93  			resource.TestStep{
    94  				Config: testAccAWSEIPMultiNetworkInterfaceConfig,
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testAccCheckAWSEIPExists("aws_eip.one", &one),
    97  					testAccCheckAWSEIPAttributes(&one),
    98  					testAccCheckAWSEIPAssociated(&one),
    99  					testAccCheckAWSEIPExists("aws_eip.two", &two),
   100  					testAccCheckAWSEIPAttributes(&two),
   101  					testAccCheckAWSEIPAssociated(&two),
   102  				),
   103  			},
   104  		},
   105  	})
   106  }
   107  
   108  func testAccCheckAWSEIPDestroy(s *terraform.State) error {
   109  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   110  
   111  	for _, rs := range s.RootModule().Resources {
   112  		if rs.Type != "aws_eip" {
   113  			continue
   114  		}
   115  
   116  		if strings.Contains(rs.Primary.ID, "eipalloc") {
   117  			req := &ec2.DescribeAddressesInput{
   118  				AllocationIds: []*string{aws.String(rs.Primary.ID)},
   119  			}
   120  			describe, err := conn.DescribeAddresses(req)
   121  			if err != nil {
   122  				// Verify the error is what we want
   123  				if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidAllocationID.NotFound" {
   124  					continue
   125  				}
   126  				return err
   127  			}
   128  
   129  			if len(describe.Addresses) > 0 {
   130  				return fmt.Errorf("still exists")
   131  			}
   132  		} else {
   133  			req := &ec2.DescribeAddressesInput{
   134  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   135  			}
   136  			describe, err := conn.DescribeAddresses(req)
   137  			if err != nil {
   138  				// Verify the error is what we want
   139  				if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidAllocationID.NotFound" {
   140  					continue
   141  				}
   142  				return err
   143  			}
   144  
   145  			if len(describe.Addresses) > 0 {
   146  				return fmt.Errorf("still exists")
   147  			}
   148  		}
   149  	}
   150  
   151  	return nil
   152  }
   153  
   154  func testAccCheckAWSEIPAttributes(conf *ec2.Address) resource.TestCheckFunc {
   155  	return func(s *terraform.State) error {
   156  		if *conf.PublicIp == "" {
   157  			return fmt.Errorf("empty public_ip")
   158  		}
   159  
   160  		return nil
   161  	}
   162  }
   163  
   164  func testAccCheckAWSEIPAssociated(conf *ec2.Address) resource.TestCheckFunc {
   165  	return func(s *terraform.State) error {
   166  		if conf.AssociationId == nil || *conf.AssociationId == "" {
   167  			return fmt.Errorf("empty association_id")
   168  		}
   169  
   170  		return nil
   171  	}
   172  }
   173  
   174  func testAccCheckAWSEIPExists(n string, res *ec2.Address) resource.TestCheckFunc {
   175  	return func(s *terraform.State) error {
   176  		rs, ok := s.RootModule().Resources[n]
   177  		if !ok {
   178  			return fmt.Errorf("Not found: %s", n)
   179  		}
   180  
   181  		if rs.Primary.ID == "" {
   182  			return fmt.Errorf("No EIP ID is set")
   183  		}
   184  
   185  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   186  
   187  		if strings.Contains(rs.Primary.ID, "eipalloc") {
   188  			req := &ec2.DescribeAddressesInput{
   189  				AllocationIds: []*string{aws.String(rs.Primary.ID)},
   190  			}
   191  			describe, err := conn.DescribeAddresses(req)
   192  			if err != nil {
   193  				return err
   194  			}
   195  
   196  			if len(describe.Addresses) != 1 ||
   197  				*describe.Addresses[0].AllocationId != rs.Primary.ID {
   198  				return fmt.Errorf("EIP not found")
   199  			}
   200  			*res = *describe.Addresses[0]
   201  
   202  		} else {
   203  			req := &ec2.DescribeAddressesInput{
   204  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   205  			}
   206  			describe, err := conn.DescribeAddresses(req)
   207  			if err != nil {
   208  				return err
   209  			}
   210  
   211  			if len(describe.Addresses) != 1 ||
   212  				*describe.Addresses[0].PublicIp != rs.Primary.ID {
   213  				return fmt.Errorf("EIP not found")
   214  			}
   215  			*res = *describe.Addresses[0]
   216  		}
   217  
   218  		return nil
   219  	}
   220  }
   221  
   222  const testAccAWSEIPConfig = `
   223  resource "aws_eip" "bar" {
   224  }
   225  `
   226  
   227  const testAccAWSEIPInstanceConfig = `
   228  resource "aws_instance" "foo" {
   229  	# us-west-2
   230  	ami = "ami-4fccb37f"
   231  	instance_type = "m1.small"
   232  }
   233  
   234  resource "aws_eip" "bar" {
   235  	instance = "${aws_instance.foo.id}"
   236  }
   237  `
   238  
   239  const testAccAWSEIPInstanceConfig2 = `
   240  resource "aws_instance" "bar" {
   241  	# us-west-2
   242  	ami = "ami-4fccb37f"
   243  	instance_type = "m1.small"
   244  }
   245  
   246  resource "aws_eip" "bar" {
   247  	instance = "${aws_instance.bar.id}"
   248  }
   249  `
   250  
   251  const testAccAWSEIPNetworkInterfaceConfig = `
   252  resource "aws_vpc" "bar" {
   253  	cidr_block = "10.0.0.0/24"
   254  }
   255  resource "aws_internet_gateway" "bar" {
   256  	vpc_id = "${aws_vpc.bar.id}"
   257  }
   258  resource "aws_subnet" "bar" {
   259    vpc_id = "${aws_vpc.bar.id}"
   260    availability_zone = "us-west-2a"
   261    cidr_block = "10.0.0.0/24"
   262  }
   263  resource "aws_network_interface" "bar" {
   264    subnet_id = "${aws_subnet.bar.id}"
   265  	private_ips = ["10.0.0.10"]
   266    security_groups = [ "${aws_vpc.bar.default_security_group_id}" ]
   267  }
   268  resource "aws_eip" "bar" {
   269  	vpc = "true"
   270  	network_interface = "${aws_network_interface.bar.id}"
   271  }
   272  `
   273  
   274  const testAccAWSEIPMultiNetworkInterfaceConfig = `
   275  resource "aws_vpc" "bar" {
   276  	cidr_block = "10.0.0.0/24"
   277  }
   278  resource "aws_internet_gateway" "bar" {
   279  	vpc_id = "${aws_vpc.bar.id}"
   280  }
   281  resource "aws_subnet" "bar" {
   282    vpc_id            = "${aws_vpc.bar.id}"
   283    availability_zone = "us-west-2a"
   284    cidr_block        = "10.0.0.0/24"
   285  }
   286  resource "aws_network_interface" "bar" {
   287    subnet_id       = "${aws_subnet.bar.id}"
   288  	private_ips     = ["10.0.0.10", "10.0.0.11"]
   289    security_groups = [ "${aws_vpc.bar.default_security_group_id}" ]
   290  }
   291  resource "aws_eip" "one" {
   292  	vpc               = "true"
   293  	network_interface = "${aws_network_interface.bar.id}"
   294  	private_ip        = "10.0.0.10"
   295  }
   296  resource "aws_eip" "two" {
   297  	vpc               = "true"
   298  	network_interface = "${aws_network_interface.bar.id}"
   299  	private_ip        = "10.0.0.11"
   300  }
   301  `