github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_traffic_manager_endpoint_test.go (about)

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