github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/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 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAWSENI_attached(t *testing.T) { 38 var conf ec2.NetworkInterface 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckAWSENIDestroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: testAccAWSENIConfigWithAttachment, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckAWSENIExists("aws_network_interface.bar", &conf), 49 testAccCheckAWSENIAttributesWithAttachment(&conf), 50 resource.TestCheckResourceAttr( 51 "aws_network_interface.bar", "private_ips.#", "1"), 52 resource.TestCheckResourceAttr( 53 "aws_network_interface.bar", "tags.Name", "bar_interface"), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func testAccCheckAWSENIExists(n string, res *ec2.NetworkInterface) resource.TestCheckFunc { 61 return func(s *terraform.State) error { 62 rs, ok := s.RootModule().Resources[n] 63 if !ok { 64 return fmt.Errorf("Not found: %s", n) 65 } 66 67 if rs.Primary.ID == "" { 68 return fmt.Errorf("No ENI ID is set") 69 } 70 71 conn := testAccProvider.Meta().(*AWSClient).ec2conn 72 describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{ 73 NetworkInterfaceIDs: []*string{aws.String(rs.Primary.ID)}, 74 } 75 describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request) 76 77 if err != nil { 78 return err 79 } 80 81 if len(describeResp.NetworkInterfaces) != 1 || 82 *describeResp.NetworkInterfaces[0].NetworkInterfaceID != rs.Primary.ID { 83 return fmt.Errorf("ENI not found") 84 } 85 86 *res = *describeResp.NetworkInterfaces[0] 87 88 return nil 89 } 90 } 91 92 func testAccCheckAWSENIAttributes(conf *ec2.NetworkInterface) resource.TestCheckFunc { 93 return func(s *terraform.State) error { 94 95 if conf.Attachment != nil { 96 return fmt.Errorf("expected attachment to be nil") 97 } 98 99 if *conf.AvailabilityZone != "us-west-2a" { 100 return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone) 101 } 102 103 if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" { 104 return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups) 105 } 106 107 if *conf.PrivateIPAddress != "172.16.10.100" { 108 return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIPAddress) 109 } 110 111 if len(conf.TagSet) == 0 { 112 return fmt.Errorf("expected tags") 113 } 114 115 return nil 116 } 117 } 118 119 func testAccCheckAWSENIAttributesWithAttachment(conf *ec2.NetworkInterface) resource.TestCheckFunc { 120 return func(s *terraform.State) error { 121 122 if conf.Attachment == nil { 123 return fmt.Errorf("expected attachment to be set, but was nil") 124 } 125 126 if *conf.Attachment.DeviceIndex != 1 { 127 return fmt.Errorf("expected attachment device index to be 1, but was %d", *conf.Attachment.DeviceIndex) 128 } 129 130 if *conf.AvailabilityZone != "us-west-2a" { 131 return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone) 132 } 133 134 if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" { 135 return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups) 136 } 137 138 if *conf.PrivateIPAddress != "172.16.10.100" { 139 return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIPAddress) 140 } 141 142 return nil 143 } 144 } 145 146 func testAccCheckAWSENIDestroy(s *terraform.State) error { 147 for _, rs := range s.RootModule().Resources { 148 if rs.Type != "aws_network_interface" { 149 continue 150 } 151 152 conn := testAccProvider.Meta().(*AWSClient).ec2conn 153 describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{ 154 NetworkInterfaceIDs: []*string{aws.String(rs.Primary.ID)}, 155 } 156 _, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request) 157 158 if err != nil { 159 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidNetworkInterfaceID.NotFound" { 160 return nil 161 } 162 163 return err 164 } 165 } 166 167 return nil 168 } 169 170 const testAccAWSENIConfig = ` 171 resource "aws_vpc" "foo" { 172 cidr_block = "172.16.0.0/16" 173 } 174 175 resource "aws_subnet" "foo" { 176 vpc_id = "${aws_vpc.foo.id}" 177 cidr_block = "172.16.10.0/24" 178 availability_zone = "us-west-2a" 179 } 180 181 resource "aws_security_group" "foo" { 182 vpc_id = "${aws_vpc.foo.id}" 183 description = "foo" 184 name = "foo" 185 186 egress { 187 from_port = 0 188 to_port = 0 189 protocol = "tcp" 190 cidr_blocks = ["10.0.0.0/16"] 191 } 192 } 193 194 resource "aws_network_interface" "bar" { 195 subnet_id = "${aws_subnet.foo.id}" 196 private_ips = ["172.16.10.100"] 197 security_groups = ["${aws_security_group.foo.id}"] 198 tags { 199 Name = "bar_interface" 200 } 201 } 202 ` 203 204 const testAccAWSENIConfigWithAttachment = ` 205 resource "aws_vpc" "foo" { 206 cidr_block = "172.16.0.0/16" 207 tags { 208 Name = "tf-eni-test" 209 } 210 } 211 212 resource "aws_subnet" "foo" { 213 vpc_id = "${aws_vpc.foo.id}" 214 cidr_block = "172.16.10.0/24" 215 availability_zone = "us-west-2a" 216 tags { 217 Name = "tf-eni-test" 218 } 219 } 220 221 resource "aws_subnet" "bar" { 222 vpc_id = "${aws_vpc.foo.id}" 223 cidr_block = "172.16.11.0/24" 224 availability_zone = "us-west-2a" 225 tags { 226 Name = "tf-eni-test" 227 } 228 } 229 230 resource "aws_security_group" "foo" { 231 vpc_id = "${aws_vpc.foo.id}" 232 description = "foo" 233 name = "foo" 234 } 235 236 resource "aws_instance" "foo" { 237 ami = "ami-c5eabbf5" 238 instance_type = "t2.micro" 239 subnet_id = "${aws_subnet.bar.id}" 240 associate_public_ip_address = false 241 private_ip = "172.16.11.50" 242 tags { 243 Name = "tf-eni-test" 244 } 245 } 246 247 resource "aws_network_interface" "bar" { 248 subnet_id = "${aws_subnet.foo.id}" 249 private_ips = ["172.16.10.100"] 250 security_groups = ["${aws_security_group.foo.id}"] 251 attachment { 252 instance = "${aws_instance.foo.id}" 253 device_index = 1 254 } 255 tags { 256 Name = "bar_interface" 257 } 258 } 259 `