github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "path" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAzureRMTrafficManagerEndpoint_basic(t *testing.T) { 15 ri := acctest.RandInt() 16 config := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_basic, ri, ri, ri, ri, ri, ri, ri) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testCheckAzureRMTrafficManagerEndpointDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: config, 25 Check: resource.ComposeTestCheckFunc( 26 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testAzure"), 27 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternal"), 28 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testAzure", "endpoint_status", "Enabled"), 29 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternal", "endpoint_status", "Enabled"), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func TestAccAzureRMTrafficManagerEndpoint_disappears(t *testing.T) { 37 ri := acctest.RandInt() 38 config := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_basic, ri, ri, ri, ri, ri, ri, ri) 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testCheckAzureRMTrafficManagerEndpointDestroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: config, 47 Check: resource.ComposeTestCheckFunc( 48 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testAzure"), 49 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternal"), 50 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testAzure", "endpoint_status", "Enabled"), 51 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternal", "endpoint_status", "Enabled"), 52 testCheckAzureRMTrafficManagerEndpointDisappears("azurerm_traffic_manager_endpoint.testAzure"), 53 ), 54 ExpectNonEmptyPlan: true, 55 }, 56 }, 57 }) 58 } 59 60 func TestAccAzureRMTrafficManagerEndpoint_basicDisableExternal(t *testing.T) { 61 ri := acctest.RandInt() 62 preConfig := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_basic, ri, ri, ri, ri, ri, ri, ri) 63 postConfig := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_basicDisableExternal, ri, ri, ri, ri, ri, ri, ri) 64 65 resource.Test(t, resource.TestCase{ 66 PreCheck: func() { testAccPreCheck(t) }, 67 Providers: testAccProviders, 68 CheckDestroy: testCheckAzureRMTrafficManagerEndpointDestroy, 69 Steps: []resource.TestStep{ 70 { 71 Config: preConfig, 72 Check: resource.ComposeTestCheckFunc( 73 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testAzure"), 74 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternal"), 75 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testAzure", "endpoint_status", "Enabled"), 76 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternal", "endpoint_status", "Enabled"), 77 ), 78 }, 79 { 80 Config: postConfig, 81 Check: resource.ComposeTestCheckFunc( 82 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testAzure"), 83 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternal"), 84 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testAzure", "endpoint_status", "Enabled"), 85 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternal", "endpoint_status", "Disabled"), 86 ), 87 }, 88 }, 89 }) 90 } 91 92 // Altering weight might be used to ramp up migration traffic 93 func TestAccAzureRMTrafficManagerEndpoint_updateWeight(t *testing.T) { 94 ri := acctest.RandInt() 95 preConfig := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_weight, ri, ri, ri, ri, ri) 96 postConfig := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_updateWeight, ri, ri, ri, ri, ri) 97 98 resource.Test(t, resource.TestCase{ 99 PreCheck: func() { testAccPreCheck(t) }, 100 Providers: testAccProviders, 101 CheckDestroy: testCheckAzureRMTrafficManagerEndpointDestroy, 102 Steps: []resource.TestStep{ 103 { 104 Config: preConfig, 105 Check: resource.ComposeTestCheckFunc( 106 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternal"), 107 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternalNew"), 108 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternal", "weight", "50"), 109 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternalNew", "weight", "50"), 110 ), 111 }, 112 { 113 Config: postConfig, 114 Check: resource.ComposeTestCheckFunc( 115 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternal"), 116 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternalNew"), 117 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternal", "weight", "25"), 118 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternalNew", "weight", "75"), 119 ), 120 }, 121 }, 122 }) 123 } 124 125 // Altering priority might be used to switch failover/active roles 126 func TestAccAzureRMTrafficManagerEndpoint_updatePriority(t *testing.T) { 127 ri := acctest.RandInt() 128 preConfig := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_priority, ri, ri, ri, ri, ri) 129 postConfig := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_updatePriority, ri, ri, ri, ri, ri) 130 131 resource.Test(t, resource.TestCase{ 132 PreCheck: func() { testAccPreCheck(t) }, 133 Providers: testAccProviders, 134 CheckDestroy: testCheckAzureRMTrafficManagerEndpointDestroy, 135 Steps: []resource.TestStep{ 136 { 137 Config: preConfig, 138 Check: resource.ComposeTestCheckFunc( 139 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternal"), 140 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternalNew"), 141 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternal", "priority", "1"), 142 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternalNew", "priority", "2"), 143 ), 144 }, 145 { 146 Config: postConfig, 147 Check: resource.ComposeTestCheckFunc( 148 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternal"), 149 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.testExternalNew"), 150 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternal", "priority", "3"), 151 resource.TestCheckResourceAttr("azurerm_traffic_manager_endpoint.testExternalNew", "priority", "2"), 152 ), 153 }, 154 }, 155 }) 156 } 157 158 func TestAccAzureRMTrafficManagerEndpoint_nestedEndpoints(t *testing.T) { 159 ri := acctest.RandInt() 160 config := fmt.Sprintf(testAccAzureRMTrafficManagerEndpoint_nestedEndpoints, ri, ri, ri, ri, ri, ri, ri) 161 162 resource.Test(t, resource.TestCase{ 163 PreCheck: func() { testAccPreCheck(t) }, 164 Providers: testAccProviders, 165 CheckDestroy: testCheckAzureRMTrafficManagerEndpointDestroy, 166 Steps: []resource.TestStep{ 167 { 168 Config: config, 169 Check: resource.ComposeTestCheckFunc( 170 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.nested"), 171 testCheckAzureRMTrafficManagerEndpointExists("azurerm_traffic_manager_endpoint.externalChild"), 172 ), 173 }, 174 }, 175 }) 176 } 177 178 func testCheckAzureRMTrafficManagerEndpointExists(name string) resource.TestCheckFunc { 179 return func(s *terraform.State) error { 180 // Ensure we have enough information in state to look up in API 181 rs, ok := s.RootModule().Resources[name] 182 if !ok { 183 return fmt.Errorf("Not found: %s", name) 184 } 185 186 name := rs.Primary.Attributes["name"] 187 endpointType := rs.Primary.Attributes["type"] 188 profileName := rs.Primary.Attributes["profile_name"] 189 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 190 if !hasResourceGroup { 191 return fmt.Errorf("Bad: no resource group found in state for Traffic Manager Profile: %s", name) 192 } 193 194 // Ensure resource group/virtual network combination exists in API 195 conn := testAccProvider.Meta().(*ArmClient).trafficManagerEndpointsClient 196 197 resp, err := conn.Get(resourceGroup, profileName, path.Base(endpointType), name) 198 if err != nil { 199 return fmt.Errorf("Bad: Get on trafficManagerEndpointsClient: %s", err) 200 } 201 202 if resp.StatusCode == http.StatusNotFound { 203 return fmt.Errorf("Bad: Traffic Manager Endpoint %q (resource group: %q) does not exist", name, resourceGroup) 204 } 205 206 return nil 207 } 208 } 209 210 func testCheckAzureRMTrafficManagerEndpointDisappears(name string) resource.TestCheckFunc { 211 return func(s *terraform.State) error { 212 // Ensure we have enough information in state to look up in API 213 rs, ok := s.RootModule().Resources[name] 214 if !ok { 215 return fmt.Errorf("Not found: %s", name) 216 } 217 218 name := rs.Primary.Attributes["name"] 219 endpointType := rs.Primary.Attributes["type"] 220 profileName := rs.Primary.Attributes["profile_name"] 221 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 222 if !hasResourceGroup { 223 return fmt.Errorf("Bad: no resource group found in state for Traffic Manager Profile: %s", name) 224 } 225 226 // Ensure resource group/virtual network combination exists in API 227 conn := testAccProvider.Meta().(*ArmClient).trafficManagerEndpointsClient 228 229 _, err := conn.Delete(resourceGroup, profileName, path.Base(endpointType), name) 230 if err != nil { 231 return fmt.Errorf("Bad: Delete on trafficManagerEndpointsClient: %s", err) 232 } 233 234 return nil 235 } 236 } 237 238 func testCheckAzureRMTrafficManagerEndpointDestroy(s *terraform.State) error { 239 conn := testAccProvider.Meta().(*ArmClient).trafficManagerEndpointsClient 240 241 for _, rs := range s.RootModule().Resources { 242 if rs.Type != "azurerm_traffic_manager_endpoint" { 243 continue 244 } 245 246 name := rs.Primary.Attributes["name"] 247 endpointType := rs.Primary.Attributes["type"] 248 profileName := rs.Primary.Attributes["profile_name"] 249 resourceGroup := rs.Primary.Attributes["resource_group_name"] 250 251 resp, err := conn.Get(resourceGroup, profileName, path.Base(endpointType), name) 252 if err != nil { 253 return nil 254 } 255 256 if resp.StatusCode != http.StatusNotFound { 257 return fmt.Errorf("Traffic Manager Endpoint sitll exists:\n%#v", resp.EndpointProperties) 258 } 259 } 260 261 return nil 262 } 263 264 var testAccAzureRMTrafficManagerEndpoint_basic = ` 265 resource "azurerm_resource_group" "test" { 266 name = "acctestRG-%d" 267 location = "West US" 268 } 269 270 resource "azurerm_traffic_manager_profile" "test" { 271 name = "acctesttmp%d" 272 resource_group_name = "${azurerm_resource_group.test.name}" 273 traffic_routing_method = "Weighted" 274 275 dns_config { 276 relative_name = "acctesttmp%d" 277 ttl = 30 278 } 279 280 monitor_config { 281 protocol = "https" 282 port = 443 283 path = "/" 284 } 285 } 286 287 resource "azurerm_public_ip" "test" { 288 name = "acctestpublicip-%d" 289 location = "West US" 290 resource_group_name = "${azurerm_resource_group.test.name}" 291 public_ip_address_allocation = "static" 292 domain_name_label = "acctestpublicip-%d" 293 } 294 295 resource "azurerm_traffic_manager_endpoint" "testAzure" { 296 name = "acctestend-azure%d" 297 type = "azureEndpoints" 298 target_resource_id = "${azurerm_public_ip.test.id}" 299 weight = 3 300 profile_name = "${azurerm_traffic_manager_profile.test.name}" 301 resource_group_name = "${azurerm_resource_group.test.name}" 302 } 303 304 resource "azurerm_traffic_manager_endpoint" "testExternal" { 305 name = "acctestend-external%d" 306 type = "externalEndpoints" 307 target = "terraform.io" 308 weight = 3 309 profile_name = "${azurerm_traffic_manager_profile.test.name}" 310 resource_group_name = "${azurerm_resource_group.test.name}" 311 } 312 ` 313 314 var testAccAzureRMTrafficManagerEndpoint_basicDisableExternal = ` 315 resource "azurerm_resource_group" "test" { 316 name = "acctestRG-%d" 317 location = "West US" 318 } 319 320 resource "azurerm_traffic_manager_profile" "test" { 321 name = "acctesttmp%d" 322 resource_group_name = "${azurerm_resource_group.test.name}" 323 traffic_routing_method = "Weighted" 324 325 dns_config { 326 relative_name = "acctesttmp%d" 327 ttl = 30 328 } 329 330 monitor_config { 331 protocol = "https" 332 port = 443 333 path = "/" 334 } 335 } 336 337 resource "azurerm_public_ip" "test" { 338 name = "acctestpublicip-%d" 339 location = "West US" 340 resource_group_name = "${azurerm_resource_group.test.name}" 341 public_ip_address_allocation = "static" 342 domain_name_label = "acctestpublicip-%d" 343 } 344 345 resource "azurerm_traffic_manager_endpoint" "testAzure" { 346 name = "acctestend-azure%d" 347 type = "azureEndpoints" 348 target_resource_id = "${azurerm_public_ip.test.id}" 349 weight = 3 350 profile_name = "${azurerm_traffic_manager_profile.test.name}" 351 resource_group_name = "${azurerm_resource_group.test.name}" 352 } 353 354 resource "azurerm_traffic_manager_endpoint" "testExternal" { 355 name = "acctestend-external%d" 356 endpoint_status = "Disabled" 357 type = "externalEndpoints" 358 target = "terraform.io" 359 weight = 3 360 profile_name = "${azurerm_traffic_manager_profile.test.name}" 361 resource_group_name = "${azurerm_resource_group.test.name}" 362 } 363 ` 364 365 var testAccAzureRMTrafficManagerEndpoint_weight = ` 366 resource "azurerm_resource_group" "test" { 367 name = "acctestRG-%d" 368 location = "West US" 369 } 370 371 resource "azurerm_traffic_manager_profile" "test" { 372 name = "acctesttmp%d" 373 resource_group_name = "${azurerm_resource_group.test.name}" 374 traffic_routing_method = "Weighted" 375 376 dns_config { 377 relative_name = "acctesttmp%d" 378 ttl = 30 379 } 380 381 monitor_config { 382 protocol = "https" 383 port = 443 384 path = "/" 385 } 386 } 387 388 resource "azurerm_traffic_manager_endpoint" "testExternal" { 389 name = "acctestend-external%d" 390 type = "externalEndpoints" 391 target = "terraform.io" 392 weight = 50 393 profile_name = "${azurerm_traffic_manager_profile.test.name}" 394 resource_group_name = "${azurerm_resource_group.test.name}" 395 } 396 397 resource "azurerm_traffic_manager_endpoint" "testExternalNew" { 398 name = "acctestend-external%d-2" 399 type = "externalEndpoints" 400 target = "www.terraform.io" 401 weight = 50 402 profile_name = "${azurerm_traffic_manager_profile.test.name}" 403 resource_group_name = "${azurerm_resource_group.test.name}" 404 } 405 ` 406 407 var testAccAzureRMTrafficManagerEndpoint_updateWeight = ` 408 resource "azurerm_resource_group" "test" { 409 name = "acctestRG-%d" 410 location = "West US" 411 } 412 413 resource "azurerm_traffic_manager_profile" "test" { 414 name = "acctesttmp%d" 415 resource_group_name = "${azurerm_resource_group.test.name}" 416 traffic_routing_method = "Weighted" 417 418 dns_config { 419 relative_name = "acctesttmp%d" 420 ttl = 30 421 } 422 423 monitor_config { 424 protocol = "https" 425 port = 443 426 path = "/" 427 } 428 } 429 430 resource "azurerm_traffic_manager_endpoint" "testExternal" { 431 name = "acctestend-external%d" 432 type = "externalEndpoints" 433 target = "terraform.io" 434 weight = 25 435 profile_name = "${azurerm_traffic_manager_profile.test.name}" 436 resource_group_name = "${azurerm_resource_group.test.name}" 437 } 438 439 resource "azurerm_traffic_manager_endpoint" "testExternalNew" { 440 name = "acctestend-external%d-2" 441 type = "externalEndpoints" 442 target = "www.terraform.io" 443 weight = 75 444 profile_name = "${azurerm_traffic_manager_profile.test.name}" 445 resource_group_name = "${azurerm_resource_group.test.name}" 446 } 447 ` 448 449 var testAccAzureRMTrafficManagerEndpoint_priority = ` 450 resource "azurerm_resource_group" "test" { 451 name = "acctestRG-%d" 452 location = "West US" 453 } 454 455 resource "azurerm_traffic_manager_profile" "test" { 456 name = "acctesttmp%d" 457 resource_group_name = "${azurerm_resource_group.test.name}" 458 traffic_routing_method = "Priority" 459 460 dns_config { 461 relative_name = "acctesttmp%d" 462 ttl = 30 463 } 464 465 monitor_config { 466 protocol = "https" 467 port = 443 468 path = "/" 469 } 470 } 471 472 resource "azurerm_traffic_manager_endpoint" "testExternal" { 473 name = "acctestend-external%d" 474 type = "externalEndpoints" 475 target = "terraform.io" 476 priority = 1 477 profile_name = "${azurerm_traffic_manager_profile.test.name}" 478 resource_group_name = "${azurerm_resource_group.test.name}" 479 } 480 481 resource "azurerm_traffic_manager_endpoint" "testExternalNew" { 482 name = "acctestend-external%d-2" 483 type = "externalEndpoints" 484 target = "www.terraform.io" 485 priority = 2 486 profile_name = "${azurerm_traffic_manager_profile.test.name}" 487 resource_group_name = "${azurerm_resource_group.test.name}" 488 } 489 ` 490 491 var testAccAzureRMTrafficManagerEndpoint_updatePriority = ` 492 resource "azurerm_resource_group" "test" { 493 name = "acctestRG-%d" 494 location = "West US" 495 } 496 497 resource "azurerm_traffic_manager_profile" "test" { 498 name = "acctesttmp%d" 499 resource_group_name = "${azurerm_resource_group.test.name}" 500 traffic_routing_method = "Priority" 501 502 dns_config { 503 relative_name = "acctesttmp%d" 504 ttl = 30 505 } 506 507 monitor_config { 508 protocol = "https" 509 port = 443 510 path = "/" 511 } 512 } 513 514 resource "azurerm_traffic_manager_endpoint" "testExternal" { 515 name = "acctestend-external%d" 516 type = "externalEndpoints" 517 target = "terraform.io" 518 priority = 3 519 profile_name = "${azurerm_traffic_manager_profile.test.name}" 520 resource_group_name = "${azurerm_resource_group.test.name}" 521 } 522 523 resource "azurerm_traffic_manager_endpoint" "testExternalNew" { 524 name = "acctestend-external%d-2" 525 type = "externalEndpoints" 526 target = "www.terraform.io" 527 priority = 2 528 profile_name = "${azurerm_traffic_manager_profile.test.name}" 529 resource_group_name = "${azurerm_resource_group.test.name}" 530 } 531 ` 532 533 var testAccAzureRMTrafficManagerEndpoint_nestedEndpoints = ` 534 resource "azurerm_resource_group" "test" { 535 name = "acctestRG-%d" 536 location = "West US" 537 } 538 539 resource "azurerm_traffic_manager_profile" "parent" { 540 name = "acctesttmpparent%d" 541 resource_group_name = "${azurerm_resource_group.test.name}" 542 traffic_routing_method = "Priority" 543 544 dns_config { 545 relative_name = "acctestparent%d" 546 ttl = 30 547 } 548 549 monitor_config { 550 protocol = "https" 551 port = 443 552 path = "/" 553 } 554 } 555 556 resource "azurerm_traffic_manager_profile" "child" { 557 name = "acctesttmpchild%d" 558 resource_group_name = "${azurerm_resource_group.test.name}" 559 traffic_routing_method = "Priority" 560 561 dns_config { 562 relative_name = "acctesttmpchild%d" 563 ttl = 30 564 } 565 566 monitor_config { 567 protocol = "https" 568 port = 443 569 path = "/" 570 } 571 } 572 573 resource "azurerm_traffic_manager_endpoint" "nested" { 574 name = "acctestend-parent%d" 575 type = "nestedEndpoints" 576 target_resource_id = "${azurerm_traffic_manager_profile.child.id}" 577 priority = 1 578 profile_name = "${azurerm_traffic_manager_profile.parent.name}" 579 resource_group_name = "${azurerm_resource_group.test.name}" 580 min_child_endpoints = 1 581 } 582 583 resource "azurerm_traffic_manager_endpoint" "externalChild" { 584 name = "acctestend-child%d" 585 type = "externalEndpoints" 586 target = "terraform.io" 587 priority = 1 588 profile_name = "${azurerm_traffic_manager_profile.child.name}" 589 resource_group_name = "${azurerm_resource_group.test.name}" 590 } 591 `