github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  // This test is an expansion of TestAccAWSEIP_instance, by testing the
   109  // associated Private EIPs of two instances
   110  func TestAccAWSEIP_associated_user_private_ip(t *testing.T) {
   111  	var one ec2.Address
   112  
   113  	resource.Test(t, resource.TestCase{
   114  		PreCheck:      func() { testAccPreCheck(t) },
   115  		IDRefreshName: "aws_eip.bar",
   116  		Providers:     testAccProviders,
   117  		CheckDestroy:  testAccCheckAWSEIPDestroy,
   118  		Steps: []resource.TestStep{
   119  			resource.TestStep{
   120  				Config: testAccAWSEIPInstanceConfig_associated,
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testAccCheckAWSEIPExists("aws_eip.bar", &one),
   123  					testAccCheckAWSEIPAttributes(&one),
   124  					testAccCheckAWSEIPAssociated(&one),
   125  				),
   126  			},
   127  
   128  			resource.TestStep{
   129  				Config: testAccAWSEIPInstanceConfig_associated_switch,
   130  				Check: resource.ComposeTestCheckFunc(
   131  					testAccCheckAWSEIPExists("aws_eip.bar", &one),
   132  					testAccCheckAWSEIPAttributes(&one),
   133  					testAccCheckAWSEIPAssociated(&one),
   134  				),
   135  			},
   136  		},
   137  	})
   138  }
   139  
   140  func testAccCheckAWSEIPDestroy(s *terraform.State) error {
   141  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
   142  
   143  	for _, rs := range s.RootModule().Resources {
   144  		if rs.Type != "aws_eip" {
   145  			continue
   146  		}
   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  				// Verify the error is what we want
   155  				if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidAllocationID.NotFound" {
   156  					continue
   157  				}
   158  				return err
   159  			}
   160  
   161  			if len(describe.Addresses) > 0 {
   162  				return fmt.Errorf("still exists")
   163  			}
   164  		} else {
   165  			req := &ec2.DescribeAddressesInput{
   166  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   167  			}
   168  			describe, err := conn.DescribeAddresses(req)
   169  			if err != nil {
   170  				// Verify the error is what we want
   171  				if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidAllocationID.NotFound" {
   172  					continue
   173  				}
   174  				return err
   175  			}
   176  
   177  			if len(describe.Addresses) > 0 {
   178  				return fmt.Errorf("still exists")
   179  			}
   180  		}
   181  	}
   182  
   183  	return nil
   184  }
   185  
   186  func testAccCheckAWSEIPAttributes(conf *ec2.Address) resource.TestCheckFunc {
   187  	return func(s *terraform.State) error {
   188  		if *conf.PublicIp == "" {
   189  			return fmt.Errorf("empty public_ip")
   190  		}
   191  
   192  		return nil
   193  	}
   194  }
   195  
   196  func testAccCheckAWSEIPAssociated(conf *ec2.Address) resource.TestCheckFunc {
   197  	return func(s *terraform.State) error {
   198  		if conf.AssociationId == nil || *conf.AssociationId == "" {
   199  			return fmt.Errorf("empty association_id")
   200  		}
   201  
   202  		return nil
   203  	}
   204  }
   205  
   206  func testAccCheckAWSEIPExists(n string, res *ec2.Address) resource.TestCheckFunc {
   207  	return func(s *terraform.State) error {
   208  		rs, ok := s.RootModule().Resources[n]
   209  		if !ok {
   210  			return fmt.Errorf("Not found: %s", n)
   211  		}
   212  
   213  		if rs.Primary.ID == "" {
   214  			return fmt.Errorf("No EIP ID is set")
   215  		}
   216  
   217  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   218  
   219  		if strings.Contains(rs.Primary.ID, "eipalloc") {
   220  			req := &ec2.DescribeAddressesInput{
   221  				AllocationIds: []*string{aws.String(rs.Primary.ID)},
   222  			}
   223  			describe, err := conn.DescribeAddresses(req)
   224  			if err != nil {
   225  				return err
   226  			}
   227  
   228  			if len(describe.Addresses) != 1 ||
   229  				*describe.Addresses[0].AllocationId != rs.Primary.ID {
   230  				return fmt.Errorf("EIP not found")
   231  			}
   232  			*res = *describe.Addresses[0]
   233  
   234  		} else {
   235  			req := &ec2.DescribeAddressesInput{
   236  				PublicIps: []*string{aws.String(rs.Primary.ID)},
   237  			}
   238  			describe, err := conn.DescribeAddresses(req)
   239  			if err != nil {
   240  				return err
   241  			}
   242  
   243  			if len(describe.Addresses) != 1 ||
   244  				*describe.Addresses[0].PublicIp != rs.Primary.ID {
   245  				return fmt.Errorf("EIP not found")
   246  			}
   247  			*res = *describe.Addresses[0]
   248  		}
   249  
   250  		return nil
   251  	}
   252  }
   253  
   254  const testAccAWSEIPConfig = `
   255  resource "aws_eip" "bar" {
   256  }
   257  `
   258  
   259  const testAccAWSEIPInstanceConfig = `
   260  resource "aws_instance" "foo" {
   261  	# us-west-2
   262  	ami = "ami-4fccb37f"
   263  	instance_type = "m1.small"
   264  }
   265  
   266  resource "aws_eip" "bar" {
   267  	instance = "${aws_instance.foo.id}"
   268  }
   269  `
   270  
   271  const testAccAWSEIPInstanceConfig2 = `
   272  resource "aws_instance" "bar" {
   273  	# us-west-2
   274  	ami = "ami-4fccb37f"
   275  	instance_type = "m1.small"
   276  }
   277  
   278  resource "aws_eip" "bar" {
   279  	instance = "${aws_instance.bar.id}"
   280  }
   281  `
   282  
   283  const testAccAWSEIPInstanceConfig_associated = `
   284  resource "aws_vpc" "default" {
   285    cidr_block           = "10.0.0.0/16"
   286    enable_dns_hostnames = true
   287  
   288    tags {
   289      Name = "default"
   290    }
   291  }
   292  
   293  resource "aws_internet_gateway" "gw" {
   294    vpc_id = "${aws_vpc.default.id}"
   295  
   296    tags {
   297      Name = "main"
   298    }
   299  }
   300  
   301  resource "aws_subnet" "tf_test_subnet" {
   302    vpc_id                  = "${aws_vpc.default.id}"
   303    cidr_block              = "10.0.0.0/24"
   304    map_public_ip_on_launch = true
   305  
   306    depends_on = ["aws_internet_gateway.gw"]
   307  
   308    tags {
   309      Name = "tf_test_subnet"
   310    }
   311  }
   312  
   313  resource "aws_instance" "foo" {
   314    # us-west-2
   315    ami           = "ami-5189a661"
   316    instance_type = "t2.micro"
   317  
   318    private_ip = "10.0.0.12"
   319    subnet_id  = "${aws_subnet.tf_test_subnet.id}"
   320  
   321    tags {
   322      Name = "foo instance"
   323    }
   324  }
   325  
   326  resource "aws_instance" "bar" {
   327    # us-west-2
   328  
   329    ami = "ami-5189a661"
   330  
   331    instance_type = "t2.micro"
   332  
   333    private_ip = "10.0.0.19"
   334    subnet_id  = "${aws_subnet.tf_test_subnet.id}"
   335  
   336    tags {
   337      Name = "bar instance"
   338    }
   339  }
   340  
   341  resource "aws_eip" "bar" {
   342    vpc = true
   343  
   344    instance                  = "${aws_instance.bar.id}"
   345    associate_with_private_ip = "10.0.0.19"
   346  }
   347  `
   348  const testAccAWSEIPInstanceConfig_associated_switch = `
   349  resource "aws_vpc" "default" {
   350    cidr_block           = "10.0.0.0/16"
   351    enable_dns_hostnames = true
   352  
   353    tags {
   354      Name = "default"
   355    }
   356  }
   357  
   358  resource "aws_internet_gateway" "gw" {
   359    vpc_id = "${aws_vpc.default.id}"
   360  
   361    tags {
   362      Name = "main"
   363    }
   364  }
   365  
   366  resource "aws_subnet" "tf_test_subnet" {
   367    vpc_id                  = "${aws_vpc.default.id}"
   368    cidr_block              = "10.0.0.0/24"
   369    map_public_ip_on_launch = true
   370  
   371    depends_on = ["aws_internet_gateway.gw"]
   372  
   373    tags {
   374      Name = "tf_test_subnet"
   375    }
   376  }
   377  
   378  resource "aws_instance" "foo" {
   379    # us-west-2
   380    ami           = "ami-5189a661"
   381    instance_type = "t2.micro"
   382  
   383    private_ip = "10.0.0.12"
   384    subnet_id  = "${aws_subnet.tf_test_subnet.id}"
   385  
   386    tags {
   387      Name = "foo instance"
   388    }
   389  }
   390  
   391  resource "aws_instance" "bar" {
   392    # us-west-2
   393  
   394    ami = "ami-5189a661"
   395  
   396    instance_type = "t2.micro"
   397  
   398    private_ip = "10.0.0.19"
   399    subnet_id  = "${aws_subnet.tf_test_subnet.id}"
   400  
   401    tags {
   402      Name = "bar instance"
   403    }
   404  }
   405  
   406  resource "aws_eip" "bar" {
   407    vpc = true
   408  
   409    instance                  = "${aws_instance.foo.id}"
   410    associate_with_private_ip = "10.0.0.12"
   411  }
   412  `
   413  
   414  const testAccAWSEIPInstanceConfig_associated_update = `
   415  resource "aws_instance" "bar" {
   416  	# us-west-2
   417  	ami = "ami-4fccb37f"
   418  	instance_type = "m1.small"
   419  }
   420  
   421  resource "aws_eip" "bar" {
   422  	instance = "${aws_instance.bar.id}"
   423  }
   424  `
   425  
   426  const testAccAWSEIPNetworkInterfaceConfig = `
   427  resource "aws_vpc" "bar" {
   428  	cidr_block = "10.0.0.0/24"
   429  }
   430  resource "aws_internet_gateway" "bar" {
   431  	vpc_id = "${aws_vpc.bar.id}"
   432  }
   433  resource "aws_subnet" "bar" {
   434    vpc_id = "${aws_vpc.bar.id}"
   435    availability_zone = "us-west-2a"
   436    cidr_block = "10.0.0.0/24"
   437  }
   438  resource "aws_network_interface" "bar" {
   439    subnet_id = "${aws_subnet.bar.id}"
   440  	private_ips = ["10.0.0.10"]
   441    security_groups = [ "${aws_vpc.bar.default_security_group_id}" ]
   442  }
   443  resource "aws_eip" "bar" {
   444  	vpc = "true"
   445  	network_interface = "${aws_network_interface.bar.id}"
   446  }
   447  `
   448  
   449  const testAccAWSEIPMultiNetworkInterfaceConfig = `
   450  resource "aws_vpc" "bar" {
   451    cidr_block = "10.0.0.0/24"
   452  }
   453  
   454  resource "aws_internet_gateway" "bar" {
   455    vpc_id = "${aws_vpc.bar.id}"
   456  }
   457  
   458  resource "aws_subnet" "bar" {
   459    vpc_id            = "${aws_vpc.bar.id}"
   460    availability_zone = "us-west-2a"
   461    cidr_block        = "10.0.0.0/24"
   462  }
   463  
   464  resource "aws_network_interface" "bar" {
   465    subnet_id       = "${aws_subnet.bar.id}"
   466    private_ips     = ["10.0.0.10", "10.0.0.11"]
   467    security_groups = ["${aws_vpc.bar.default_security_group_id}"]
   468  }
   469  
   470  resource "aws_eip" "one" {
   471    vpc                       = "true"
   472    network_interface         = "${aws_network_interface.bar.id}"
   473    associate_with_private_ip = "10.0.0.10"
   474  }
   475  
   476  resource "aws_eip" "two" {
   477    vpc                       = "true"
   478    network_interface         = "${aws_network_interface.bar.id}"
   479    associate_with_private_ip = "10.0.0.11"
   480  }
   481  `