github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  		req := &ec2.DescribeAddressesInput{
    90  			PublicIps: []*string{aws.String(rs.Primary.ID)},
    91  		}
    92  		describe, err := conn.DescribeAddresses(req)
    93  
    94  		if err == nil {
    95  			if len(describe.Addresses) != 0 &&
    96  				*describe.Addresses[0].PublicIp == rs.Primary.ID {
    97  				return fmt.Errorf("EIP still exists")
    98  			}
    99  		}
   100  
   101  		// Verify the error
   102  		providerErr, ok := err.(awserr.Error)
   103  		if !ok {
   104  			return err
   105  		}
   106  
   107  		if providerErr.Code() != "InvalidAllocationID.NotFound" {
   108  			return fmt.Errorf("Unexpected error: %s", err)
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func testAccCheckAWSEIPAttributes(conf *ec2.Address) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  		if *conf.PublicIp == "" {
   118  			return fmt.Errorf("empty public_ip")
   119  		}
   120  
   121  		return nil
   122  	}
   123  }
   124  
   125  func testAccCheckAWSEIPAssociated(conf *ec2.Address) resource.TestCheckFunc {
   126  	return func(s *terraform.State) error {
   127  		if *conf.AssociationId == "" {
   128  			return fmt.Errorf("empty association_id")
   129  		}
   130  
   131  		return nil
   132  	}
   133  }
   134  
   135  func testAccCheckAWSEIPExists(n string, res *ec2.Address) resource.TestCheckFunc {
   136  	return func(s *terraform.State) error {
   137  		rs, ok := s.RootModule().Resources[n]
   138  		if !ok {
   139  			return fmt.Errorf("Not found: %s", n)
   140  		}
   141  
   142  		if rs.Primary.ID == "" {
   143  			return fmt.Errorf("No EIP ID is set")
   144  		}
   145  
   146  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   147  
   148  		if strings.Contains(rs.Primary.ID, "eipalloc") {
   149  			req := &ec2.DescribeAddressesInput{
   150  				AllocationIds: []*string{aws.String(rs.Primary.ID)},
   151  			}
   152  			describe, err := conn.DescribeAddresses(req)
   153  			if err != nil {
   154  				return err
   155  			}
   156  
   157  			if len(describe.Addresses) != 1 ||
   158  				*describe.Addresses[0].AllocationId != rs.Primary.ID {
   159  				return fmt.Errorf("EIP not found")
   160  			}
   161  			*res = *describe.Addresses[0]
   162  
   163  		} else {
   164  			req := &ec2.DescribeAddressesInput{
   165  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   166  			}
   167  			describe, err := conn.DescribeAddresses(req)
   168  			if err != nil {
   169  				return err
   170  			}
   171  
   172  			if len(describe.Addresses) != 1 ||
   173  				*describe.Addresses[0].PublicIp != rs.Primary.ID {
   174  				return fmt.Errorf("EIP not found")
   175  			}
   176  			*res = *describe.Addresses[0]
   177  		}
   178  
   179  		return nil
   180  	}
   181  }
   182  
   183  const testAccAWSEIPConfig = `
   184  resource "aws_eip" "bar" {
   185  }
   186  `
   187  
   188  const testAccAWSEIPInstanceConfig = `
   189  resource "aws_instance" "foo" {
   190  	# us-west-2
   191  	ami = "ami-4fccb37f"
   192  	instance_type = "m1.small"
   193  }
   194  
   195  resource "aws_eip" "bar" {
   196  	instance = "${aws_instance.foo.id}"
   197  }
   198  `
   199  
   200  const testAccAWSEIPInstanceConfig2 = `
   201  resource "aws_instance" "bar" {
   202  	# us-west-2
   203  	ami = "ami-4fccb37f"
   204  	instance_type = "m1.small"
   205  }
   206  
   207  resource "aws_eip" "bar" {
   208  	instance = "${aws_instance.bar.id}"
   209  }
   210  `
   211  const testAccAWSEIPNetworkInterfaceConfig = `
   212  resource "aws_vpc" "bar" {
   213  	cidr_block = "10.0.0.0/24"
   214  }
   215  resource "aws_internet_gateway" "bar" {
   216  	vpc_id = "${aws_vpc.bar.id}"
   217  }
   218  resource "aws_subnet" "bar" {
   219    vpc_id = "${aws_vpc.bar.id}"
   220    availability_zone = "us-west-2a"
   221    cidr_block = "10.0.0.0/24"
   222  }
   223  resource "aws_network_interface" "bar" {
   224    subnet_id = "${aws_subnet.bar.id}"
   225  	private_ips = ["10.0.0.10"]
   226    security_groups = [ "${aws_vpc.bar.default_security_group_id}" ]
   227  }
   228  resource "aws_eip" "bar" {
   229  	vpc = "true"
   230  	network_interface = "${aws_network_interface.bar.id}"
   231  }
   232  `