github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_network_interface_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSENI_basic(t *testing.T) {
    15  	var conf ec2.NetworkInterface
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSENIDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSENIConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    26  					testAccCheckAWSENIAttributes(&conf),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_network_interface.bar", "private_ips.#", "1"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_network_interface.bar", "tags.Name", "bar_interface"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_network_interface.bar", "description", "Managed by Terraform"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSENI_updatedDescription(t *testing.T) {
    40  	var conf ec2.NetworkInterface
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckAWSENIDestroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: testAccAWSENIConfig,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    51  					resource.TestCheckResourceAttr(
    52  						"aws_network_interface.bar", "description", "Managed by Terraform"),
    53  				),
    54  			},
    55  
    56  			resource.TestStep{
    57  				Config: testAccAWSENIConfigUpdatedDescription,
    58  				Check: resource.ComposeTestCheckFunc(
    59  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    60  					resource.TestCheckResourceAttr(
    61  						"aws_network_interface.bar", "description", "Updated ENI Description"),
    62  				),
    63  			},
    64  		},
    65  	})
    66  }
    67  
    68  func TestAccAWSENI_attached(t *testing.T) {
    69  	var conf ec2.NetworkInterface
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:     func() { testAccPreCheck(t) },
    73  		Providers:    testAccProviders,
    74  		CheckDestroy: testAccCheckAWSENIDestroy,
    75  		Steps: []resource.TestStep{
    76  			resource.TestStep{
    77  				Config: testAccAWSENIConfigWithAttachment,
    78  				Check: resource.ComposeTestCheckFunc(
    79  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    80  					testAccCheckAWSENIAttributesWithAttachment(&conf),
    81  					resource.TestCheckResourceAttr(
    82  						"aws_network_interface.bar", "private_ips.#", "1"),
    83  					resource.TestCheckResourceAttr(
    84  						"aws_network_interface.bar", "tags.Name", "bar_interface"),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func TestAccAWSENI_ignoreExternalAttachment(t *testing.T) {
    92  	var conf ec2.NetworkInterface
    93  
    94  	resource.Test(t, resource.TestCase{
    95  		PreCheck:     func() { testAccPreCheck(t) },
    96  		Providers:    testAccProviders,
    97  		CheckDestroy: testAccCheckAWSENIDestroy,
    98  		Steps: []resource.TestStep{
    99  			resource.TestStep{
   100  				Config: testAccAWSENIConfigExternalAttachment,
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
   103  					testAccCheckAWSENIAttributes(&conf),
   104  					testAccCheckAWSENIMakeExternalAttachment("aws_instance.foo", &conf),
   105  				),
   106  			},
   107  		},
   108  	})
   109  }
   110  
   111  func TestAccAWSENI_sourceDestCheck(t *testing.T) {
   112  	var conf ec2.NetworkInterface
   113  
   114  	resource.Test(t, resource.TestCase{
   115  		PreCheck:     func() { testAccPreCheck(t) },
   116  		Providers:    testAccProviders,
   117  		CheckDestroy: testAccCheckAWSENIDestroy,
   118  		Steps: []resource.TestStep{
   119  			resource.TestStep{
   120  				Config: testAccAWSENIConfigWithSourceDestCheck,
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
   123  					resource.TestCheckResourceAttr(
   124  						"aws_network_interface.bar", "source_dest_check", "false"),
   125  				),
   126  			},
   127  		},
   128  	})
   129  }
   130  
   131  func TestAccAWSENI_computedIPs(t *testing.T) {
   132  	var conf ec2.NetworkInterface
   133  
   134  	resource.Test(t, resource.TestCase{
   135  		PreCheck:     func() { testAccPreCheck(t) },
   136  		Providers:    testAccProviders,
   137  		CheckDestroy: testAccCheckAWSENIDestroy,
   138  		Steps: []resource.TestStep{
   139  			resource.TestStep{
   140  				Config: testAccAWSENIConfigWithNoPrivateIPs,
   141  				Check: resource.ComposeTestCheckFunc(
   142  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
   143  					resource.TestCheckResourceAttr(
   144  						"aws_network_interface.bar", "private_ips.#", "1"),
   145  				),
   146  			},
   147  		},
   148  	})
   149  }
   150  
   151  func testAccCheckAWSENIExists(n string, res *ec2.NetworkInterface) resource.TestCheckFunc {
   152  	return func(s *terraform.State) error {
   153  		rs, ok := s.RootModule().Resources[n]
   154  		if !ok {
   155  			return fmt.Errorf("Not found: %s", n)
   156  		}
   157  
   158  		if rs.Primary.ID == "" {
   159  			return fmt.Errorf("No ENI ID is set")
   160  		}
   161  
   162  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   163  		describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{
   164  			NetworkInterfaceIds: []*string{aws.String(rs.Primary.ID)},
   165  		}
   166  		describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request)
   167  
   168  		if err != nil {
   169  			return err
   170  		}
   171  
   172  		if len(describeResp.NetworkInterfaces) != 1 ||
   173  			*describeResp.NetworkInterfaces[0].NetworkInterfaceId != rs.Primary.ID {
   174  			return fmt.Errorf("ENI not found")
   175  		}
   176  
   177  		*res = *describeResp.NetworkInterfaces[0]
   178  
   179  		return nil
   180  	}
   181  }
   182  
   183  func testAccCheckAWSENIAttributes(conf *ec2.NetworkInterface) resource.TestCheckFunc {
   184  	return func(s *terraform.State) error {
   185  
   186  		if conf.Attachment != nil {
   187  			return fmt.Errorf("expected attachment to be nil")
   188  		}
   189  
   190  		if *conf.AvailabilityZone != "us-west-2a" {
   191  			return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone)
   192  		}
   193  
   194  		if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" {
   195  			return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups)
   196  		}
   197  
   198  		if *conf.PrivateIpAddress != "172.16.10.100" {
   199  			return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIpAddress)
   200  		}
   201  
   202  		if *conf.SourceDestCheck != true {
   203  			return fmt.Errorf("expected source_dest_check to be true, but was %t", *conf.SourceDestCheck)
   204  		}
   205  
   206  		if len(conf.TagSet) == 0 {
   207  			return fmt.Errorf("expected tags")
   208  		}
   209  
   210  		return nil
   211  	}
   212  }
   213  
   214  func testAccCheckAWSENIAttributesWithAttachment(conf *ec2.NetworkInterface) resource.TestCheckFunc {
   215  	return func(s *terraform.State) error {
   216  
   217  		if conf.Attachment == nil {
   218  			return fmt.Errorf("expected attachment to be set, but was nil")
   219  		}
   220  
   221  		if *conf.Attachment.DeviceIndex != 1 {
   222  			return fmt.Errorf("expected attachment device index to be 1, but was %d", *conf.Attachment.DeviceIndex)
   223  		}
   224  
   225  		if *conf.AvailabilityZone != "us-west-2a" {
   226  			return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone)
   227  		}
   228  
   229  		if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" {
   230  			return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups)
   231  		}
   232  
   233  		if *conf.PrivateIpAddress != "172.16.10.100" {
   234  			return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIpAddress)
   235  		}
   236  
   237  		return nil
   238  	}
   239  }
   240  
   241  func testAccCheckAWSENIDestroy(s *terraform.State) error {
   242  	for _, rs := range s.RootModule().Resources {
   243  		if rs.Type != "aws_network_interface" {
   244  			continue
   245  		}
   246  
   247  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   248  		describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{
   249  			NetworkInterfaceIds: []*string{aws.String(rs.Primary.ID)},
   250  		}
   251  		_, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request)
   252  
   253  		if err != nil {
   254  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidNetworkInterfaceID.NotFound" {
   255  				return nil
   256  			}
   257  
   258  			return err
   259  		}
   260  	}
   261  
   262  	return nil
   263  }
   264  
   265  func testAccCheckAWSENIMakeExternalAttachment(n string, conf *ec2.NetworkInterface) resource.TestCheckFunc {
   266  	return func(s *terraform.State) error {
   267  		rs, ok := s.RootModule().Resources[n]
   268  		if !ok || rs.Primary.ID == "" {
   269  			return fmt.Errorf("Not found: %s", n)
   270  		}
   271  		attach_request := &ec2.AttachNetworkInterfaceInput{
   272  			DeviceIndex:        aws.Int64(2),
   273  			InstanceId:         aws.String(rs.Primary.ID),
   274  			NetworkInterfaceId: conf.NetworkInterfaceId,
   275  		}
   276  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   277  		_, attach_err := conn.AttachNetworkInterface(attach_request)
   278  		if attach_err != nil {
   279  			return fmt.Errorf("Error attaching ENI: %s", attach_err)
   280  		}
   281  		return nil
   282  	}
   283  }
   284  
   285  const testAccAWSENIConfig = `
   286  resource "aws_vpc" "foo" {
   287      cidr_block = "172.16.0.0/16"
   288  }
   289  
   290  resource "aws_subnet" "foo" {
   291      vpc_id = "${aws_vpc.foo.id}"
   292      cidr_block = "172.16.10.0/24"
   293      availability_zone = "us-west-2a"
   294  }
   295  
   296  resource "aws_security_group" "foo" {
   297    vpc_id = "${aws_vpc.foo.id}"
   298    description = "foo"
   299    name = "foo"
   300  
   301          egress {
   302                  from_port = 0
   303                  to_port = 0
   304                  protocol = "tcp"
   305                  cidr_blocks = ["10.0.0.0/16"]
   306          }
   307  }
   308  
   309  resource "aws_network_interface" "bar" {
   310      subnet_id = "${aws_subnet.foo.id}"
   311      private_ips = ["172.16.10.100"]
   312      security_groups = ["${aws_security_group.foo.id}"]
   313      description = "Managed by Terraform"
   314      tags {
   315          Name = "bar_interface"
   316      }
   317  }
   318  `
   319  
   320  const testAccAWSENIConfigUpdatedDescription = `
   321  resource "aws_vpc" "foo" {
   322      cidr_block = "172.16.0.0/16"
   323  }
   324  
   325  resource "aws_subnet" "foo" {
   326      vpc_id = "${aws_vpc.foo.id}"
   327      cidr_block = "172.16.10.0/24"
   328      availability_zone = "us-west-2a"
   329  }
   330  
   331  resource "aws_security_group" "foo" {
   332    vpc_id = "${aws_vpc.foo.id}"
   333    description = "foo"
   334    name = "foo"
   335  
   336          egress {
   337                  from_port = 0
   338                  to_port = 0
   339                  protocol = "tcp"
   340                  cidr_blocks = ["10.0.0.0/16"]
   341          }
   342  }
   343  
   344  resource "aws_network_interface" "bar" {
   345      subnet_id = "${aws_subnet.foo.id}"
   346      private_ips = ["172.16.10.100"]
   347      security_groups = ["${aws_security_group.foo.id}"]
   348      description = "Updated ENI Description"
   349      tags {
   350          Name = "bar_interface"
   351      }
   352  }
   353  `
   354  
   355  const testAccAWSENIConfigWithSourceDestCheck = `
   356  resource "aws_vpc" "foo" {
   357      cidr_block = "172.16.0.0/16"
   358  }
   359  
   360  resource "aws_subnet" "foo" {
   361      vpc_id = "${aws_vpc.foo.id}"
   362      cidr_block = "172.16.10.0/24"
   363      availability_zone = "us-west-2a"
   364  }
   365  
   366  resource "aws_network_interface" "bar" {
   367      subnet_id = "${aws_subnet.foo.id}"
   368          source_dest_check = false
   369      private_ips = ["172.16.10.100"]
   370  }
   371  `
   372  
   373  const testAccAWSENIConfigWithNoPrivateIPs = `
   374  resource "aws_vpc" "foo" {
   375      cidr_block = "172.16.0.0/16"
   376  }
   377  
   378  resource "aws_subnet" "foo" {
   379      vpc_id = "${aws_vpc.foo.id}"
   380      cidr_block = "172.16.10.0/24"
   381      availability_zone = "us-west-2a"
   382  }
   383  
   384  resource "aws_network_interface" "bar" {
   385      subnet_id = "${aws_subnet.foo.id}"
   386          source_dest_check = false
   387  }
   388  `
   389  
   390  const testAccAWSENIConfigWithAttachment = `
   391  resource "aws_vpc" "foo" {
   392      cidr_block = "172.16.0.0/16"
   393          tags {
   394              Name = "tf-eni-test"
   395          }
   396  }
   397  
   398  resource "aws_subnet" "foo" {
   399      vpc_id = "${aws_vpc.foo.id}"
   400      cidr_block = "172.16.10.0/24"
   401      availability_zone = "us-west-2a"
   402          tags {
   403              Name = "tf-foo-eni-test"
   404          }
   405  }
   406  
   407  resource "aws_subnet" "bar" {
   408      vpc_id = "${aws_vpc.foo.id}"
   409      cidr_block = "172.16.11.0/24"
   410      availability_zone = "us-west-2a"
   411          tags {
   412              Name = "tf-bar-eni-test"
   413          }
   414  }
   415  
   416  resource "aws_security_group" "foo" {
   417    vpc_id = "${aws_vpc.foo.id}"
   418    description = "foo"
   419    name = "foo"
   420  }
   421  
   422  resource "aws_instance" "foo" {
   423      ami = "ami-c5eabbf5"
   424      instance_type = "t2.micro"
   425      subnet_id = "${aws_subnet.bar.id}"
   426      associate_public_ip_address = false
   427      private_ip = "172.16.11.50"
   428      tags {
   429          Name = "foo-tf-eni-test"
   430      }
   431  }
   432  
   433  resource "aws_network_interface" "bar" {
   434      subnet_id = "${aws_subnet.foo.id}"
   435      private_ips = ["172.16.10.100"]
   436      security_groups = ["${aws_security_group.foo.id}"]
   437      attachment {
   438          instance = "${aws_instance.foo.id}"
   439          device_index = 1
   440      }
   441      tags {
   442          Name = "bar_interface"
   443      }
   444  }
   445  `
   446  
   447  const testAccAWSENIConfigExternalAttachment = `
   448  resource "aws_vpc" "foo" {
   449      cidr_block = "172.16.0.0/16"
   450          tags {
   451              Name = "tf-eni-test"
   452          }
   453  }
   454  
   455  resource "aws_subnet" "foo" {
   456      vpc_id = "${aws_vpc.foo.id}"
   457      cidr_block = "172.16.10.0/24"
   458      availability_zone = "us-west-2a"
   459          tags {
   460              Name = "tf-eni-test"
   461          }
   462  }
   463  
   464  resource "aws_subnet" "bar" {
   465      vpc_id = "${aws_vpc.foo.id}"
   466      cidr_block = "172.16.11.0/24"
   467      availability_zone = "us-west-2a"
   468          tags {
   469              Name = "tf-eni-test"
   470          }
   471  }
   472  
   473  resource "aws_security_group" "foo" {
   474    vpc_id = "${aws_vpc.foo.id}"
   475    description = "foo"
   476    name = "foo"
   477  }
   478  
   479  resource "aws_instance" "foo" {
   480      ami = "ami-c5eabbf5"
   481      instance_type = "t2.micro"
   482      subnet_id = "${aws_subnet.bar.id}"
   483      associate_public_ip_address = false
   484      private_ip = "172.16.11.50"
   485      tags {
   486          Name = "tf-eni-test"
   487      }
   488  }
   489  
   490  resource "aws_network_interface" "bar" {
   491      subnet_id = "${aws_subnet.foo.id}"
   492      private_ips = ["172.16.10.100"]
   493      security_groups = ["${aws_security_group.foo.id}"]
   494      tags {
   495          Name = "bar_interface"
   496      }
   497  }
   498  `