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