github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_eip_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/ec2"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSEIP_importEc2Classic(t *testing.T) {
    17  	oldvar := os.Getenv("AWS_DEFAULT_REGION")
    18  	os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
    19  	defer os.Setenv("AWS_DEFAULT_REGION", oldvar)
    20  
    21  	resourceName := "aws_eip.bar"
    22  
    23  	resource.Test(t, resource.TestCase{
    24  		PreCheck:     func() { testAccPreCheck(t) },
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckAWSEIPDestroy,
    27  		Steps: []resource.TestStep{
    28  			{
    29  				Config: testAccAWSEIPInstanceEc2Classic,
    30  			},
    31  			{
    32  				ResourceName:      resourceName,
    33  				ImportState:       true,
    34  				ImportStateVerify: true,
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccAWSEIP_importVpc(t *testing.T) {
    41  	resourceName := "aws_eip.bar"
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckAWSEIPDestroy,
    47  		Steps: []resource.TestStep{
    48  			{
    49  				Config: testAccAWSEIPNetworkInterfaceConfig,
    50  			},
    51  			{
    52  				ResourceName:      resourceName,
    53  				ImportState:       true,
    54  				ImportStateVerify: true,
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func TestAccAWSEIP_basic(t *testing.T) {
    61  	var conf ec2.Address
    62  
    63  	resource.Test(t, resource.TestCase{
    64  		PreCheck:      func() { testAccPreCheck(t) },
    65  		IDRefreshName: "aws_eip.bar",
    66  		Providers:     testAccProviders,
    67  		CheckDestroy:  testAccCheckAWSEIPDestroy,
    68  		Steps: []resource.TestStep{
    69  			resource.TestStep{
    70  				Config: testAccAWSEIPConfig,
    71  				Check: resource.ComposeTestCheckFunc(
    72  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    73  					testAccCheckAWSEIPAttributes(&conf),
    74  				),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestAccAWSEIP_instance(t *testing.T) {
    81  	var conf ec2.Address
    82  
    83  	resource.Test(t, resource.TestCase{
    84  		PreCheck:      func() { testAccPreCheck(t) },
    85  		IDRefreshName: "aws_eip.bar",
    86  		Providers:     testAccProviders,
    87  		CheckDestroy:  testAccCheckAWSEIPDestroy,
    88  		Steps: []resource.TestStep{
    89  			resource.TestStep{
    90  				Config: testAccAWSEIPInstanceConfig,
    91  				Check: resource.ComposeTestCheckFunc(
    92  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    93  					testAccCheckAWSEIPAttributes(&conf),
    94  				),
    95  			},
    96  
    97  			resource.TestStep{
    98  				Config: testAccAWSEIPInstanceConfig2,
    99  				Check: resource.ComposeTestCheckFunc(
   100  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
   101  					testAccCheckAWSEIPAttributes(&conf),
   102  				),
   103  			},
   104  		},
   105  	})
   106  }
   107  
   108  func TestAccAWSEIP_network_interface(t *testing.T) {
   109  	var conf ec2.Address
   110  
   111  	resource.Test(t, resource.TestCase{
   112  		PreCheck:      func() { testAccPreCheck(t) },
   113  		IDRefreshName: "aws_eip.bar",
   114  		Providers:     testAccProviders,
   115  		CheckDestroy:  testAccCheckAWSEIPDestroy,
   116  		Steps: []resource.TestStep{
   117  			resource.TestStep{
   118  				Config: testAccAWSEIPNetworkInterfaceConfig,
   119  				Check: resource.ComposeTestCheckFunc(
   120  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
   121  					testAccCheckAWSEIPAttributes(&conf),
   122  					testAccCheckAWSEIPAssociated(&conf),
   123  				),
   124  			},
   125  		},
   126  	})
   127  }
   128  
   129  func TestAccAWSEIP_twoEIPsOneNetworkInterface(t *testing.T) {
   130  	var one, two ec2.Address
   131  
   132  	resource.Test(t, resource.TestCase{
   133  		PreCheck:      func() { testAccPreCheck(t) },
   134  		IDRefreshName: "aws_eip.one",
   135  		Providers:     testAccProviders,
   136  		CheckDestroy:  testAccCheckAWSEIPDestroy,
   137  		Steps: []resource.TestStep{
   138  			resource.TestStep{
   139  				Config: testAccAWSEIPMultiNetworkInterfaceConfig,
   140  				Check: resource.ComposeTestCheckFunc(
   141  					testAccCheckAWSEIPExists("aws_eip.one", &one),
   142  					testAccCheckAWSEIPAttributes(&one),
   143  					testAccCheckAWSEIPAssociated(&one),
   144  					testAccCheckAWSEIPExists("aws_eip.two", &two),
   145  					testAccCheckAWSEIPAttributes(&two),
   146  					testAccCheckAWSEIPAssociated(&two),
   147  				),
   148  			},
   149  		},
   150  	})
   151  }
   152  
   153  // This test is an expansion of TestAccAWSEIP_instance, by testing the
   154  // associated Private EIPs of two instances
   155  func TestAccAWSEIP_associated_user_private_ip(t *testing.T) {
   156  	var one ec2.Address
   157  
   158  	resource.Test(t, resource.TestCase{
   159  		PreCheck:      func() { testAccPreCheck(t) },
   160  		IDRefreshName: "aws_eip.bar",
   161  		Providers:     testAccProviders,
   162  		CheckDestroy:  testAccCheckAWSEIPDestroy,
   163  		Steps: []resource.TestStep{
   164  			resource.TestStep{
   165  				Config: testAccAWSEIPInstanceConfig_associated,
   166  				Check: resource.ComposeTestCheckFunc(
   167  					testAccCheckAWSEIPExists("aws_eip.bar", &one),
   168  					testAccCheckAWSEIPAttributes(&one),
   169  					testAccCheckAWSEIPAssociated(&one),
   170  				),
   171  			},
   172  
   173  			resource.TestStep{
   174  				Config: testAccAWSEIPInstanceConfig_associated_switch,
   175  				Check: resource.ComposeTestCheckFunc(
   176  					testAccCheckAWSEIPExists("aws_eip.bar", &one),
   177  					testAccCheckAWSEIPAttributes(&one),
   178  					testAccCheckAWSEIPAssociated(&one),
   179  				),
   180  			},
   181  		},
   182  	})
   183  }
   184  
   185  func testAccCheckAWSEIPDestroy(s *terraform.State) error {
   186  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   187  
   188  	for _, rs := range s.RootModule().Resources {
   189  		if rs.Type != "aws_eip" {
   190  			continue
   191  		}
   192  
   193  		if strings.Contains(rs.Primary.ID, "eipalloc") {
   194  			req := &ec2.DescribeAddressesInput{
   195  				AllocationIds: []*string{aws.String(rs.Primary.ID)},
   196  			}
   197  			describe, err := conn.DescribeAddresses(req)
   198  			if err != nil {
   199  				// Verify the error is what we want
   200  				if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidAllocationID.NotFound" || ae.Code() == "InvalidAddress.NotFound" {
   201  					continue
   202  				}
   203  				return err
   204  			}
   205  
   206  			if len(describe.Addresses) > 0 {
   207  				return fmt.Errorf("still exists")
   208  			}
   209  		} else {
   210  			req := &ec2.DescribeAddressesInput{
   211  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   212  			}
   213  			describe, err := conn.DescribeAddresses(req)
   214  			if err != nil {
   215  				// Verify the error is what we want
   216  				if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidAllocationID.NotFound" || ae.Code() == "InvalidAddress.NotFound" {
   217  					continue
   218  				}
   219  				return err
   220  			}
   221  
   222  			if len(describe.Addresses) > 0 {
   223  				return fmt.Errorf("still exists")
   224  			}
   225  		}
   226  	}
   227  
   228  	return nil
   229  }
   230  
   231  func testAccCheckAWSEIPAttributes(conf *ec2.Address) resource.TestCheckFunc {
   232  	return func(s *terraform.State) error {
   233  		if *conf.PublicIp == "" {
   234  			return fmt.Errorf("empty public_ip")
   235  		}
   236  
   237  		return nil
   238  	}
   239  }
   240  
   241  func testAccCheckAWSEIPAssociated(conf *ec2.Address) resource.TestCheckFunc {
   242  	return func(s *terraform.State) error {
   243  		if conf.AssociationId == nil || *conf.AssociationId == "" {
   244  			return fmt.Errorf("empty association_id")
   245  		}
   246  
   247  		return nil
   248  	}
   249  }
   250  
   251  func testAccCheckAWSEIPExists(n string, res *ec2.Address) resource.TestCheckFunc {
   252  	return func(s *terraform.State) error {
   253  		rs, ok := s.RootModule().Resources[n]
   254  		if !ok {
   255  			return fmt.Errorf("Not found: %s", n)
   256  		}
   257  
   258  		if rs.Primary.ID == "" {
   259  			return fmt.Errorf("No EIP ID is set")
   260  		}
   261  
   262  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   263  
   264  		if strings.Contains(rs.Primary.ID, "eipalloc") {
   265  			req := &ec2.DescribeAddressesInput{
   266  				AllocationIds: []*string{aws.String(rs.Primary.ID)},
   267  			}
   268  			describe, err := conn.DescribeAddresses(req)
   269  			if err != nil {
   270  				return err
   271  			}
   272  
   273  			if len(describe.Addresses) != 1 ||
   274  				*describe.Addresses[0].AllocationId != rs.Primary.ID {
   275  				return fmt.Errorf("EIP not found")
   276  			}
   277  			*res = *describe.Addresses[0]
   278  
   279  		} else {
   280  			req := &ec2.DescribeAddressesInput{
   281  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   282  			}
   283  			describe, err := conn.DescribeAddresses(req)
   284  			if err != nil {
   285  				return err
   286  			}
   287  
   288  			if len(describe.Addresses) != 1 ||
   289  				*describe.Addresses[0].PublicIp != rs.Primary.ID {
   290  				return fmt.Errorf("EIP not found")
   291  			}
   292  			*res = *describe.Addresses[0]
   293  		}
   294  
   295  		return nil
   296  	}
   297  }
   298  
   299  const testAccAWSEIPConfig = `
   300  resource "aws_eip" "bar" {
   301  }
   302  `
   303  
   304  const testAccAWSEIPInstanceEc2Classic = `
   305  provider "aws" {
   306  	region = "us-east-1"
   307  }
   308  resource "aws_instance" "foo" {
   309  	ami = "ami-5469ae3c"
   310  	instance_type = "m1.small"
   311  }
   312  
   313  resource "aws_eip" "bar" {
   314  	instance = "${aws_instance.foo.id}"
   315  }
   316  `
   317  
   318  const testAccAWSEIPInstanceConfig = `
   319  resource "aws_instance" "foo" {
   320  	# us-west-2
   321  	ami = "ami-4fccb37f"
   322  	instance_type = "m1.small"
   323  }
   324  
   325  resource "aws_eip" "bar" {
   326  	instance = "${aws_instance.foo.id}"
   327  }
   328  `
   329  
   330  const testAccAWSEIPInstanceConfig2 = `
   331  resource "aws_instance" "bar" {
   332  	# us-west-2
   333  	ami = "ami-4fccb37f"
   334  	instance_type = "m1.small"
   335  }
   336  
   337  resource "aws_eip" "bar" {
   338  	instance = "${aws_instance.bar.id}"
   339  }
   340  `
   341  
   342  const testAccAWSEIPInstanceConfig_associated = `
   343  resource "aws_vpc" "default" {
   344    cidr_block           = "10.0.0.0/16"
   345    enable_dns_hostnames = true
   346  
   347    tags {
   348      Name = "default"
   349    }
   350  }
   351  
   352  resource "aws_internet_gateway" "gw" {
   353    vpc_id = "${aws_vpc.default.id}"
   354  
   355    tags {
   356      Name = "main"
   357    }
   358  }
   359  
   360  resource "aws_subnet" "tf_test_subnet" {
   361    vpc_id                  = "${aws_vpc.default.id}"
   362    cidr_block              = "10.0.0.0/24"
   363    map_public_ip_on_launch = true
   364  
   365    depends_on = ["aws_internet_gateway.gw"]
   366  
   367    tags {
   368      Name = "tf_test_subnet"
   369    }
   370  }
   371  
   372  resource "aws_instance" "foo" {
   373    # us-west-2
   374    ami           = "ami-5189a661"
   375    instance_type = "t2.micro"
   376  
   377    private_ip = "10.0.0.12"
   378    subnet_id  = "${aws_subnet.tf_test_subnet.id}"
   379  
   380    tags {
   381      Name = "foo instance"
   382    }
   383  }
   384  
   385  resource "aws_instance" "bar" {
   386    # us-west-2
   387  
   388    ami = "ami-5189a661"
   389  
   390    instance_type = "t2.micro"
   391  
   392    private_ip = "10.0.0.19"
   393    subnet_id  = "${aws_subnet.tf_test_subnet.id}"
   394  
   395    tags {
   396      Name = "bar instance"
   397    }
   398  }
   399  
   400  resource "aws_eip" "bar" {
   401    vpc = true
   402  
   403    instance                  = "${aws_instance.bar.id}"
   404    associate_with_private_ip = "10.0.0.19"
   405  }
   406  `
   407  const testAccAWSEIPInstanceConfig_associated_switch = `
   408  resource "aws_vpc" "default" {
   409    cidr_block           = "10.0.0.0/16"
   410    enable_dns_hostnames = true
   411  
   412    tags {
   413      Name = "default"
   414    }
   415  }
   416  
   417  resource "aws_internet_gateway" "gw" {
   418    vpc_id = "${aws_vpc.default.id}"
   419  
   420    tags {
   421      Name = "main"
   422    }
   423  }
   424  
   425  resource "aws_subnet" "tf_test_subnet" {
   426    vpc_id                  = "${aws_vpc.default.id}"
   427    cidr_block              = "10.0.0.0/24"
   428    map_public_ip_on_launch = true
   429  
   430    depends_on = ["aws_internet_gateway.gw"]
   431  
   432    tags {
   433      Name = "tf_test_subnet"
   434    }
   435  }
   436  
   437  resource "aws_instance" "foo" {
   438    # us-west-2
   439    ami           = "ami-5189a661"
   440    instance_type = "t2.micro"
   441  
   442    private_ip = "10.0.0.12"
   443    subnet_id  = "${aws_subnet.tf_test_subnet.id}"
   444  
   445    tags {
   446      Name = "foo instance"
   447    }
   448  }
   449  
   450  resource "aws_instance" "bar" {
   451    # us-west-2
   452  
   453    ami = "ami-5189a661"
   454  
   455    instance_type = "t2.micro"
   456  
   457    private_ip = "10.0.0.19"
   458    subnet_id  = "${aws_subnet.tf_test_subnet.id}"
   459  
   460    tags {
   461      Name = "bar instance"
   462    }
   463  }
   464  
   465  resource "aws_eip" "bar" {
   466    vpc = true
   467  
   468    instance                  = "${aws_instance.foo.id}"
   469    associate_with_private_ip = "10.0.0.12"
   470  }
   471  `
   472  
   473  const testAccAWSEIPInstanceConfig_associated_update = `
   474  resource "aws_instance" "bar" {
   475  	# us-west-2
   476  	ami = "ami-4fccb37f"
   477  	instance_type = "m1.small"
   478  }
   479  
   480  resource "aws_eip" "bar" {
   481  	instance = "${aws_instance.bar.id}"
   482  }
   483  `
   484  
   485  const testAccAWSEIPNetworkInterfaceConfig = `
   486  resource "aws_vpc" "bar" {
   487  	cidr_block = "10.0.0.0/24"
   488  }
   489  resource "aws_internet_gateway" "bar" {
   490  	vpc_id = "${aws_vpc.bar.id}"
   491  }
   492  resource "aws_subnet" "bar" {
   493    vpc_id = "${aws_vpc.bar.id}"
   494    availability_zone = "us-west-2a"
   495    cidr_block = "10.0.0.0/24"
   496  }
   497  resource "aws_network_interface" "bar" {
   498    subnet_id = "${aws_subnet.bar.id}"
   499  	private_ips = ["10.0.0.10"]
   500    security_groups = [ "${aws_vpc.bar.default_security_group_id}" ]
   501  }
   502  resource "aws_eip" "bar" {
   503  	vpc = "true"
   504  	network_interface = "${aws_network_interface.bar.id}"
   505  }
   506  `
   507  
   508  const testAccAWSEIPMultiNetworkInterfaceConfig = `
   509  resource "aws_vpc" "bar" {
   510    cidr_block = "10.0.0.0/24"
   511  }
   512  
   513  resource "aws_internet_gateway" "bar" {
   514    vpc_id = "${aws_vpc.bar.id}"
   515  }
   516  
   517  resource "aws_subnet" "bar" {
   518    vpc_id            = "${aws_vpc.bar.id}"
   519    availability_zone = "us-west-2a"
   520    cidr_block        = "10.0.0.0/24"
   521  }
   522  
   523  resource "aws_network_interface" "bar" {
   524    subnet_id       = "${aws_subnet.bar.id}"
   525    private_ips     = ["10.0.0.10", "10.0.0.11"]
   526    security_groups = ["${aws_vpc.bar.default_security_group_id}"]
   527  }
   528  
   529  resource "aws_eip" "one" {
   530    vpc                       = "true"
   531    network_interface         = "${aws_network_interface.bar.id}"
   532    associate_with_private_ip = "10.0.0.10"
   533  }
   534  
   535  resource "aws_eip" "two" {
   536    vpc                       = "true"
   537    network_interface         = "${aws_network_interface.bar.id}"
   538    associate_with_private_ip = "10.0.0.11"
   539  }
   540  `