github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_network_interface_card_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAzureRMNetworkInterface_basic(t *testing.T) { 14 rInt := acctest.RandInt() 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy, 19 Steps: []resource.TestStep{ 20 { 21 Config: testAccAzureRMNetworkInterface_basic(rInt), 22 Check: resource.ComposeTestCheckFunc( 23 testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func TestAccAzureRMNetworkInterface_disappears(t *testing.T) { 31 rInt := acctest.RandInt() 32 resource.Test(t, resource.TestCase{ 33 PreCheck: func() { testAccPreCheck(t) }, 34 Providers: testAccProviders, 35 CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy, 36 Steps: []resource.TestStep{ 37 { 38 Config: testAccAzureRMNetworkInterface_basic(rInt), 39 Check: resource.ComposeTestCheckFunc( 40 testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"), 41 testCheckAzureRMNetworkInterfaceDisappears("azurerm_network_interface.test"), 42 ), 43 ExpectNonEmptyPlan: true, 44 }, 45 }, 46 }) 47 } 48 49 func TestAccAzureRMNetworkInterface_enableIPForwarding(t *testing.T) { 50 rInt := acctest.RandInt() 51 resource.Test(t, resource.TestCase{ 52 PreCheck: func() { testAccPreCheck(t) }, 53 Providers: testAccProviders, 54 CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy, 55 Steps: []resource.TestStep{ 56 { 57 Config: testAccAzureRMNetworkInterface_ipForwarding(rInt), 58 Check: resource.ComposeTestCheckFunc( 59 testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"), 60 resource.TestCheckResourceAttr( 61 "azurerm_network_interface.test", "enable_ip_forwarding", "true"), 62 ), 63 }, 64 }, 65 }) 66 } 67 68 func TestAccAzureRMNetworkInterface_multipleLoadBalancers(t *testing.T) { 69 rInt := acctest.RandInt() 70 resource.Test(t, resource.TestCase{ 71 PreCheck: func() { testAccPreCheck(t) }, 72 Providers: testAccProviders, 73 CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy, 74 Steps: []resource.TestStep{ 75 { 76 Config: testAccAzureRMNetworkInterface_multipleLoadBalancers(rInt), 77 Check: resource.ComposeTestCheckFunc( 78 testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test1"), 79 testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test2"), 80 ), 81 }, 82 }, 83 }) 84 } 85 86 func TestAccAzureRMNetworkInterface_withTags(t *testing.T) { 87 rInt := acctest.RandInt() 88 resource.Test(t, resource.TestCase{ 89 PreCheck: func() { testAccPreCheck(t) }, 90 Providers: testAccProviders, 91 CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy, 92 Steps: []resource.TestStep{ 93 { 94 Config: testAccAzureRMNetworkInterface_withTags(rInt), 95 Check: resource.ComposeTestCheckFunc( 96 testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"), 97 resource.TestCheckResourceAttr( 98 "azurerm_network_interface.test", "tags.%", "2"), 99 resource.TestCheckResourceAttr( 100 "azurerm_network_interface.test", "tags.environment", "Production"), 101 resource.TestCheckResourceAttr( 102 "azurerm_network_interface.test", "tags.cost_center", "MSFT"), 103 ), 104 }, 105 { 106 Config: testAccAzureRMNetworkInterface_withTagsUpdate(rInt), 107 Check: resource.ComposeTestCheckFunc( 108 testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"), 109 resource.TestCheckResourceAttr( 110 "azurerm_network_interface.test", "tags.%", "1"), 111 resource.TestCheckResourceAttr( 112 "azurerm_network_interface.test", "tags.environment", "staging"), 113 ), 114 }, 115 }, 116 }) 117 } 118 119 func testCheckAzureRMNetworkInterfaceExists(name string) resource.TestCheckFunc { 120 return func(s *terraform.State) error { 121 // Ensure we have enough information in state to look up in API 122 rs, ok := s.RootModule().Resources[name] 123 if !ok { 124 return fmt.Errorf("Not found: %s", name) 125 } 126 127 name := rs.Primary.Attributes["name"] 128 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 129 if !hasResourceGroup { 130 return fmt.Errorf("Bad: no resource group found in state for availability set: %s", name) 131 } 132 133 conn := testAccProvider.Meta().(*ArmClient).ifaceClient 134 135 resp, err := conn.Get(resourceGroup, name, "") 136 if err != nil { 137 return fmt.Errorf("Bad: Get on ifaceClient: %s", err) 138 } 139 140 if resp.StatusCode == http.StatusNotFound { 141 return fmt.Errorf("Bad: Network Interface %q (resource group: %q) does not exist", name, resourceGroup) 142 } 143 144 return nil 145 } 146 } 147 148 func testCheckAzureRMNetworkInterfaceDisappears(name string) resource.TestCheckFunc { 149 return func(s *terraform.State) error { 150 // Ensure we have enough information in state to look up in API 151 rs, ok := s.RootModule().Resources[name] 152 if !ok { 153 return fmt.Errorf("Not found: %s", name) 154 } 155 156 name := rs.Primary.Attributes["name"] 157 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 158 if !hasResourceGroup { 159 return fmt.Errorf("Bad: no resource group found in state for availability set: %s", name) 160 } 161 162 conn := testAccProvider.Meta().(*ArmClient).ifaceClient 163 164 _, err := conn.Delete(resourceGroup, name, make(chan struct{})) 165 if err != nil { 166 return fmt.Errorf("Bad: Delete on ifaceClient: %s", err) 167 } 168 169 return nil 170 } 171 } 172 173 func testCheckAzureRMNetworkInterfaceDestroy(s *terraform.State) error { 174 conn := testAccProvider.Meta().(*ArmClient).ifaceClient 175 176 for _, rs := range s.RootModule().Resources { 177 if rs.Type != "azurerm_network_interface" { 178 continue 179 } 180 181 name := rs.Primary.Attributes["name"] 182 resourceGroup := rs.Primary.Attributes["resource_group_name"] 183 184 resp, err := conn.Get(resourceGroup, name, "") 185 186 if err != nil { 187 return nil 188 } 189 190 if resp.StatusCode != http.StatusNotFound { 191 return fmt.Errorf("Network Interface still exists:\n%#v", resp.InterfacePropertiesFormat) 192 } 193 } 194 195 return nil 196 } 197 198 func testAccAzureRMNetworkInterface_basic(rInt int) string { 199 return fmt.Sprintf(` 200 resource "azurerm_resource_group" "test" { 201 name = "acctest-rg-%d" 202 location = "West US" 203 } 204 205 resource "azurerm_virtual_network" "test" { 206 name = "acceptanceTestVirtualNetwork1" 207 address_space = ["10.0.0.0/16"] 208 location = "West US" 209 resource_group_name = "${azurerm_resource_group.test.name}" 210 } 211 212 resource "azurerm_subnet" "test" { 213 name = "testsubnet" 214 resource_group_name = "${azurerm_resource_group.test.name}" 215 virtual_network_name = "${azurerm_virtual_network.test.name}" 216 address_prefix = "10.0.2.0/24" 217 } 218 219 resource "azurerm_network_interface" "test" { 220 name = "acceptanceTestNetworkInterface1" 221 location = "West US" 222 resource_group_name = "${azurerm_resource_group.test.name}" 223 224 ip_configuration { 225 name = "testconfiguration1" 226 subnet_id = "${azurerm_subnet.test.id}" 227 private_ip_address_allocation = "dynamic" 228 } 229 } 230 `, rInt) 231 } 232 233 func testAccAzureRMNetworkInterface_ipForwarding(rInt int) string { 234 return fmt.Sprintf(` 235 resource "azurerm_resource_group" "test" { 236 name = "acctest-rg-%d" 237 location = "West US" 238 } 239 240 resource "azurerm_virtual_network" "test" { 241 name = "acceptanceTestVirtualNetwork1" 242 address_space = ["10.0.0.0/16"] 243 location = "West US" 244 resource_group_name = "${azurerm_resource_group.test.name}" 245 } 246 247 resource "azurerm_subnet" "test" { 248 name = "testsubnet" 249 resource_group_name = "${azurerm_resource_group.test.name}" 250 virtual_network_name = "${azurerm_virtual_network.test.name}" 251 address_prefix = "10.0.2.0/24" 252 } 253 254 resource "azurerm_network_interface" "test" { 255 name = "acceptanceTestNetworkInterface1" 256 location = "West US" 257 resource_group_name = "${azurerm_resource_group.test.name}" 258 enable_ip_forwarding = true 259 260 ip_configuration { 261 name = "testconfiguration1" 262 subnet_id = "${azurerm_subnet.test.id}" 263 private_ip_address_allocation = "dynamic" 264 } 265 } 266 `, rInt) 267 } 268 269 func testAccAzureRMNetworkInterface_withTags(rInt int) string { 270 return fmt.Sprintf(` 271 resource "azurerm_resource_group" "test" { 272 name = "acctest-rg-%d" 273 location = "West US" 274 } 275 276 resource "azurerm_virtual_network" "test" { 277 name = "acceptanceTestVirtualNetwork1" 278 address_space = ["10.0.0.0/16"] 279 location = "West US" 280 resource_group_name = "${azurerm_resource_group.test.name}" 281 } 282 283 resource "azurerm_subnet" "test" { 284 name = "testsubnet" 285 resource_group_name = "${azurerm_resource_group.test.name}" 286 virtual_network_name = "${azurerm_virtual_network.test.name}" 287 address_prefix = "10.0.2.0/24" 288 } 289 290 resource "azurerm_network_interface" "test" { 291 name = "acceptanceTestNetworkInterface1" 292 location = "West US" 293 resource_group_name = "${azurerm_resource_group.test.name}" 294 295 ip_configuration { 296 name = "testconfiguration1" 297 subnet_id = "${azurerm_subnet.test.id}" 298 private_ip_address_allocation = "dynamic" 299 } 300 301 tags { 302 environment = "Production" 303 cost_center = "MSFT" 304 } 305 } 306 `, rInt) 307 } 308 309 func testAccAzureRMNetworkInterface_withTagsUpdate(rInt int) string { 310 return fmt.Sprintf(` 311 resource "azurerm_resource_group" "test" { 312 name = "acctest-rg-%d" 313 location = "West US" 314 } 315 316 resource "azurerm_virtual_network" "test" { 317 name = "acceptanceTestVirtualNetwork1" 318 address_space = ["10.0.0.0/16"] 319 location = "West US" 320 resource_group_name = "${azurerm_resource_group.test.name}" 321 } 322 323 resource "azurerm_subnet" "test" { 324 name = "testsubnet" 325 resource_group_name = "${azurerm_resource_group.test.name}" 326 virtual_network_name = "${azurerm_virtual_network.test.name}" 327 address_prefix = "10.0.2.0/24" 328 } 329 330 resource "azurerm_network_interface" "test" { 331 name = "acceptanceTestNetworkInterface1" 332 location = "West US" 333 resource_group_name = "${azurerm_resource_group.test.name}" 334 335 ip_configuration { 336 name = "testconfiguration1" 337 subnet_id = "${azurerm_subnet.test.id}" 338 private_ip_address_allocation = "dynamic" 339 } 340 341 tags { 342 environment = "staging" 343 } 344 } 345 `, rInt) 346 } 347 348 func testAccAzureRMNetworkInterface_multipleLoadBalancers(rInt int) string { 349 return fmt.Sprintf(` 350 resource "azurerm_resource_group" "test" { 351 name = "acctest-rg-%d" 352 location = "West US" 353 } 354 355 resource "azurerm_virtual_network" "test" { 356 name = "acceptanceTestVirtualNetwork1" 357 address_space = ["10.0.0.0/16"] 358 location = "West US" 359 resource_group_name = "${azurerm_resource_group.test.name}" 360 } 361 362 resource "azurerm_subnet" "test" { 363 name = "testsubnet" 364 resource_group_name = "${azurerm_resource_group.test.name}" 365 virtual_network_name = "${azurerm_virtual_network.test.name}" 366 address_prefix = "10.0.2.0/24" 367 } 368 369 resource "azurerm_public_ip" "testext" { 370 name = "testpublicipext" 371 location = "West US" 372 resource_group_name = "${azurerm_resource_group.test.name}" 373 public_ip_address_allocation = "static" 374 } 375 376 resource "azurerm_lb" "testext" { 377 name = "testlbext" 378 location = "West US" 379 resource_group_name = "${azurerm_resource_group.test.name}" 380 381 frontend_ip_configuration { 382 name = "publicipext" 383 public_ip_address_id = "${azurerm_public_ip.testext.id}" 384 } 385 } 386 387 resource "azurerm_lb_backend_address_pool" "testext" { 388 location = "West US" 389 resource_group_name = "${azurerm_resource_group.test.name}" 390 loadbalancer_id = "${azurerm_lb.testext.id}" 391 name = "testbackendpoolext" 392 } 393 394 resource "azurerm_lb_nat_rule" "testext" { 395 name = "testnatruleext" 396 location = "West US" 397 resource_group_name = "${azurerm_resource_group.test.name}" 398 loadbalancer_id = "${azurerm_lb.testext.id}" 399 protocol = "Tcp" 400 frontend_port = 3389 401 backend_port = 3390 402 frontend_ip_configuration_name = "publicipext" 403 } 404 405 resource "azurerm_public_ip" "testint" { 406 name = "testpublicipint" 407 location = "West US" 408 resource_group_name = "${azurerm_resource_group.test.name}" 409 public_ip_address_allocation = "static" 410 } 411 412 resource "azurerm_lb" "testint" { 413 name = "testlbint" 414 location = "West US" 415 resource_group_name = "${azurerm_resource_group.test.name}" 416 417 frontend_ip_configuration { 418 name = "publicipint" 419 subnet_id = "${azurerm_subnet.test.id}" 420 private_ip_address_allocation = "Dynamic" 421 } 422 } 423 424 resource "azurerm_lb_backend_address_pool" "testint" { 425 location = "West US" 426 resource_group_name = "${azurerm_resource_group.test.name}" 427 loadbalancer_id = "${azurerm_lb.testint.id}" 428 name = "testbackendpoolint" 429 } 430 431 resource "azurerm_lb_nat_rule" "testint" { 432 name = "testnatruleint" 433 location = "West US" 434 resource_group_name = "${azurerm_resource_group.test.name}" 435 loadbalancer_id = "${azurerm_lb.testint.id}" 436 protocol = "Tcp" 437 frontend_port = 3389 438 backend_port = 3391 439 frontend_ip_configuration_name = "publicipint" 440 } 441 442 resource "azurerm_network_interface" "test1" { 443 name = "acceptanceTestNetworkInterface1" 444 location = "West US" 445 resource_group_name = "${azurerm_resource_group.test.name}" 446 enable_ip_forwarding = true 447 448 ip_configuration { 449 name = "testconfiguration1" 450 subnet_id = "${azurerm_subnet.test.id}" 451 private_ip_address_allocation = "dynamic" 452 load_balancer_backend_address_pools_ids = [ 453 "${azurerm_lb_backend_address_pool.testext.id}", 454 "${azurerm_lb_backend_address_pool.testint.id}", 455 ] 456 } 457 } 458 459 resource "azurerm_network_interface" "test2" { 460 name = "acceptanceTestNetworkInterface2" 461 location = "West US" 462 resource_group_name = "${azurerm_resource_group.test.name}" 463 enable_ip_forwarding = true 464 465 ip_configuration { 466 name = "testconfiguration1" 467 subnet_id = "${azurerm_subnet.test.id}" 468 private_ip_address_allocation = "dynamic" 469 load_balancer_inbound_nat_rules_ids = [ 470 "${azurerm_lb_nat_rule.testext.id}", 471 "${azurerm_lb_nat_rule.testint.id}", 472 ] 473 } 474 } 475 `, rInt) 476 }