github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 _, error := conn.Delete(resourceGroup, name, make(chan struct{})) 165 err := <-error 166 if err != nil { 167 return fmt.Errorf("Bad: Delete on ifaceClient: %s", err) 168 } 169 170 return nil 171 } 172 } 173 174 func testCheckAzureRMNetworkInterfaceDestroy(s *terraform.State) error { 175 conn := testAccProvider.Meta().(*ArmClient).ifaceClient 176 177 for _, rs := range s.RootModule().Resources { 178 if rs.Type != "azurerm_network_interface" { 179 continue 180 } 181 182 name := rs.Primary.Attributes["name"] 183 resourceGroup := rs.Primary.Attributes["resource_group_name"] 184 185 resp, err := conn.Get(resourceGroup, name, "") 186 187 if err != nil { 188 return nil 189 } 190 191 if resp.StatusCode != http.StatusNotFound { 192 return fmt.Errorf("Network Interface still exists:\n%#v", resp.InterfacePropertiesFormat) 193 } 194 } 195 196 return nil 197 } 198 199 func testAccAzureRMNetworkInterface_basic(rInt int) string { 200 return fmt.Sprintf(` 201 resource "azurerm_resource_group" "test" { 202 name = "acctest-rg-%d" 203 location = "West US" 204 } 205 206 resource "azurerm_virtual_network" "test" { 207 name = "acceptanceTestVirtualNetwork1" 208 address_space = ["10.0.0.0/16"] 209 location = "West US" 210 resource_group_name = "${azurerm_resource_group.test.name}" 211 } 212 213 resource "azurerm_subnet" "test" { 214 name = "testsubnet" 215 resource_group_name = "${azurerm_resource_group.test.name}" 216 virtual_network_name = "${azurerm_virtual_network.test.name}" 217 address_prefix = "10.0.2.0/24" 218 } 219 220 resource "azurerm_network_interface" "test" { 221 name = "acceptanceTestNetworkInterface1" 222 location = "West US" 223 resource_group_name = "${azurerm_resource_group.test.name}" 224 225 ip_configuration { 226 name = "testconfiguration1" 227 subnet_id = "${azurerm_subnet.test.id}" 228 private_ip_address_allocation = "dynamic" 229 } 230 } 231 `, rInt) 232 } 233 234 func testAccAzureRMNetworkInterface_ipForwarding(rInt int) string { 235 return fmt.Sprintf(` 236 resource "azurerm_resource_group" "test" { 237 name = "acctest-rg-%d" 238 location = "West US" 239 } 240 241 resource "azurerm_virtual_network" "test" { 242 name = "acceptanceTestVirtualNetwork1" 243 address_space = ["10.0.0.0/16"] 244 location = "West US" 245 resource_group_name = "${azurerm_resource_group.test.name}" 246 } 247 248 resource "azurerm_subnet" "test" { 249 name = "testsubnet" 250 resource_group_name = "${azurerm_resource_group.test.name}" 251 virtual_network_name = "${azurerm_virtual_network.test.name}" 252 address_prefix = "10.0.2.0/24" 253 } 254 255 resource "azurerm_network_interface" "test" { 256 name = "acceptanceTestNetworkInterface1" 257 location = "West US" 258 resource_group_name = "${azurerm_resource_group.test.name}" 259 enable_ip_forwarding = true 260 261 ip_configuration { 262 name = "testconfiguration1" 263 subnet_id = "${azurerm_subnet.test.id}" 264 private_ip_address_allocation = "dynamic" 265 } 266 } 267 `, rInt) 268 } 269 270 func testAccAzureRMNetworkInterface_withTags(rInt int) string { 271 return fmt.Sprintf(` 272 resource "azurerm_resource_group" "test" { 273 name = "acctest-rg-%d" 274 location = "West US" 275 } 276 277 resource "azurerm_virtual_network" "test" { 278 name = "acceptanceTestVirtualNetwork1" 279 address_space = ["10.0.0.0/16"] 280 location = "West US" 281 resource_group_name = "${azurerm_resource_group.test.name}" 282 } 283 284 resource "azurerm_subnet" "test" { 285 name = "testsubnet" 286 resource_group_name = "${azurerm_resource_group.test.name}" 287 virtual_network_name = "${azurerm_virtual_network.test.name}" 288 address_prefix = "10.0.2.0/24" 289 } 290 291 resource "azurerm_network_interface" "test" { 292 name = "acceptanceTestNetworkInterface1" 293 location = "West US" 294 resource_group_name = "${azurerm_resource_group.test.name}" 295 296 ip_configuration { 297 name = "testconfiguration1" 298 subnet_id = "${azurerm_subnet.test.id}" 299 private_ip_address_allocation = "dynamic" 300 } 301 302 tags { 303 environment = "Production" 304 cost_center = "MSFT" 305 } 306 } 307 `, rInt) 308 } 309 310 func testAccAzureRMNetworkInterface_withTagsUpdate(rInt int) string { 311 return fmt.Sprintf(` 312 resource "azurerm_resource_group" "test" { 313 name = "acctest-rg-%d" 314 location = "West US" 315 } 316 317 resource "azurerm_virtual_network" "test" { 318 name = "acceptanceTestVirtualNetwork1" 319 address_space = ["10.0.0.0/16"] 320 location = "West US" 321 resource_group_name = "${azurerm_resource_group.test.name}" 322 } 323 324 resource "azurerm_subnet" "test" { 325 name = "testsubnet" 326 resource_group_name = "${azurerm_resource_group.test.name}" 327 virtual_network_name = "${azurerm_virtual_network.test.name}" 328 address_prefix = "10.0.2.0/24" 329 } 330 331 resource "azurerm_network_interface" "test" { 332 name = "acceptanceTestNetworkInterface1" 333 location = "West US" 334 resource_group_name = "${azurerm_resource_group.test.name}" 335 336 ip_configuration { 337 name = "testconfiguration1" 338 subnet_id = "${azurerm_subnet.test.id}" 339 private_ip_address_allocation = "dynamic" 340 } 341 342 tags { 343 environment = "staging" 344 } 345 } 346 `, rInt) 347 } 348 349 func testAccAzureRMNetworkInterface_multipleLoadBalancers(rInt int) string { 350 return fmt.Sprintf(` 351 resource "azurerm_resource_group" "test" { 352 name = "acctest-rg-%d" 353 location = "West US" 354 } 355 356 resource "azurerm_virtual_network" "test" { 357 name = "acceptanceTestVirtualNetwork1" 358 address_space = ["10.0.0.0/16"] 359 location = "West US" 360 resource_group_name = "${azurerm_resource_group.test.name}" 361 } 362 363 resource "azurerm_subnet" "test" { 364 name = "testsubnet" 365 resource_group_name = "${azurerm_resource_group.test.name}" 366 virtual_network_name = "${azurerm_virtual_network.test.name}" 367 address_prefix = "10.0.2.0/24" 368 } 369 370 resource "azurerm_public_ip" "testext" { 371 name = "testpublicipext" 372 location = "West US" 373 resource_group_name = "${azurerm_resource_group.test.name}" 374 public_ip_address_allocation = "static" 375 } 376 377 resource "azurerm_lb" "testext" { 378 name = "testlbext" 379 location = "West US" 380 resource_group_name = "${azurerm_resource_group.test.name}" 381 382 frontend_ip_configuration { 383 name = "publicipext" 384 public_ip_address_id = "${azurerm_public_ip.testext.id}" 385 } 386 } 387 388 resource "azurerm_lb_backend_address_pool" "testext" { 389 location = "West US" 390 resource_group_name = "${azurerm_resource_group.test.name}" 391 loadbalancer_id = "${azurerm_lb.testext.id}" 392 name = "testbackendpoolext" 393 } 394 395 resource "azurerm_lb_nat_rule" "testext" { 396 name = "testnatruleext" 397 location = "West US" 398 resource_group_name = "${azurerm_resource_group.test.name}" 399 loadbalancer_id = "${azurerm_lb.testext.id}" 400 protocol = "Tcp" 401 frontend_port = 3389 402 backend_port = 3390 403 frontend_ip_configuration_name = "publicipext" 404 } 405 406 resource "azurerm_public_ip" "testint" { 407 name = "testpublicipint" 408 location = "West US" 409 resource_group_name = "${azurerm_resource_group.test.name}" 410 public_ip_address_allocation = "static" 411 } 412 413 resource "azurerm_lb" "testint" { 414 name = "testlbint" 415 location = "West US" 416 resource_group_name = "${azurerm_resource_group.test.name}" 417 418 frontend_ip_configuration { 419 name = "publicipint" 420 subnet_id = "${azurerm_subnet.test.id}" 421 private_ip_address_allocation = "Dynamic" 422 } 423 } 424 425 resource "azurerm_lb_backend_address_pool" "testint" { 426 location = "West US" 427 resource_group_name = "${azurerm_resource_group.test.name}" 428 loadbalancer_id = "${azurerm_lb.testint.id}" 429 name = "testbackendpoolint" 430 } 431 432 resource "azurerm_lb_nat_rule" "testint" { 433 name = "testnatruleint" 434 location = "West US" 435 resource_group_name = "${azurerm_resource_group.test.name}" 436 loadbalancer_id = "${azurerm_lb.testint.id}" 437 protocol = "Tcp" 438 frontend_port = 3389 439 backend_port = 3391 440 frontend_ip_configuration_name = "publicipint" 441 } 442 443 resource "azurerm_network_interface" "test1" { 444 name = "acceptanceTestNetworkInterface1" 445 location = "West US" 446 resource_group_name = "${azurerm_resource_group.test.name}" 447 enable_ip_forwarding = true 448 449 ip_configuration { 450 name = "testconfiguration1" 451 subnet_id = "${azurerm_subnet.test.id}" 452 private_ip_address_allocation = "dynamic" 453 load_balancer_backend_address_pools_ids = [ 454 "${azurerm_lb_backend_address_pool.testext.id}", 455 "${azurerm_lb_backend_address_pool.testint.id}", 456 ] 457 } 458 } 459 460 resource "azurerm_network_interface" "test2" { 461 name = "acceptanceTestNetworkInterface2" 462 location = "West US" 463 resource_group_name = "${azurerm_resource_group.test.name}" 464 enable_ip_forwarding = true 465 466 ip_configuration { 467 name = "testconfiguration1" 468 subnet_id = "${azurerm_subnet.test.id}" 469 private_ip_address_allocation = "dynamic" 470 load_balancer_inbound_nat_rules_ids = [ 471 "${azurerm_lb_nat_rule.testext.id}", 472 "${azurerm_lb_nat_rule.testint.id}", 473 ] 474 } 475 } 476 `, rInt) 477 }