github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_traffic_manager_profile_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     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 TestAccAzureRMTrafficManagerProfile_weighted(t *testing.T) {
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccAzureRMTrafficManagerProfile_weighted, ri, ri, ri)
    17  
    18  	fqdn := fmt.Sprintf("acctesttmp%d.trafficmanager.net", ri)
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testCheckAzureRMTrafficManagerProfileDestroy,
    24  		Steps: []resource.TestStep{
    25  			resource.TestStep{
    26  				Config: config,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testCheckAzureRMTrafficManagerProfileExists("azurerm_traffic_manager_profile.test"),
    29  					resource.TestCheckResourceAttr("azurerm_traffic_manager_profile.test", "traffic_routing_method", "Weighted"),
    30  					resource.TestCheckResourceAttr("azurerm_traffic_manager_profile.test", "fqdn", fqdn),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAzureRMTrafficManagerProfile_performance(t *testing.T) {
    38  	ri := acctest.RandInt()
    39  	config := fmt.Sprintf(testAccAzureRMTrafficManagerProfile_performance, ri, ri, ri)
    40  
    41  	fqdn := fmt.Sprintf("acctesttmp%d.trafficmanager.net", ri)
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testCheckAzureRMTrafficManagerProfileDestroy,
    47  		Steps: []resource.TestStep{
    48  			resource.TestStep{
    49  				Config: config,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testCheckAzureRMTrafficManagerProfileExists("azurerm_traffic_manager_profile.test"),
    52  					resource.TestCheckResourceAttr("azurerm_traffic_manager_profile.test", "traffic_routing_method", "Performance"),
    53  					resource.TestCheckResourceAttr("azurerm_traffic_manager_profile.test", "fqdn", fqdn),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func TestAccAzureRMTrafficManagerProfile_priority(t *testing.T) {
    61  	ri := acctest.RandInt()
    62  	config := fmt.Sprintf(testAccAzureRMTrafficManagerProfile_priority, ri, ri, ri)
    63  
    64  	fqdn := fmt.Sprintf("acctesttmp%d.trafficmanager.net", ri)
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:     func() { testAccPreCheck(t) },
    68  		Providers:    testAccProviders,
    69  		CheckDestroy: testCheckAzureRMTrafficManagerProfileDestroy,
    70  		Steps: []resource.TestStep{
    71  			resource.TestStep{
    72  				Config: config,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testCheckAzureRMTrafficManagerProfileExists("azurerm_traffic_manager_profile.test"),
    75  					resource.TestCheckResourceAttr("azurerm_traffic_manager_profile.test", "traffic_routing_method", "Priority"),
    76  					resource.TestCheckResourceAttr("azurerm_traffic_manager_profile.test", "fqdn", fqdn),
    77  				),
    78  			},
    79  		},
    80  	})
    81  }
    82  
    83  func TestAccAzureRMTrafficManagerProfile_withTags(t *testing.T) {
    84  	ri := acctest.RandInt()
    85  	preConfig := fmt.Sprintf(testAccAzureRMTrafficManagerProfile_withTags, ri, ri, ri)
    86  	postConfig := fmt.Sprintf(testAccAzureRMTrafficManagerProfile_withTagsUpdated, ri, ri, ri)
    87  
    88  	resource.Test(t, resource.TestCase{
    89  		PreCheck:     func() { testAccPreCheck(t) },
    90  		Providers:    testAccProviders,
    91  		CheckDestroy: testCheckAzureRMTrafficManagerProfileDestroy,
    92  		Steps: []resource.TestStep{
    93  			resource.TestStep{
    94  				Config: preConfig,
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testCheckAzureRMTrafficManagerProfileExists("azurerm_traffic_manager_profile.test"),
    97  					resource.TestCheckResourceAttr(
    98  						"azurerm_traffic_manager_profile.test", "tags.%", "2"),
    99  					resource.TestCheckResourceAttr(
   100  						"azurerm_traffic_manager_profile.test", "tags.environment", "Production"),
   101  					resource.TestCheckResourceAttr(
   102  						"azurerm_traffic_manager_profile.test", "tags.cost_center", "MSFT"),
   103  				),
   104  			},
   105  
   106  			resource.TestStep{
   107  				Config: postConfig,
   108  				Check: resource.ComposeTestCheckFunc(
   109  					testCheckAzureRMTrafficManagerProfileExists("azurerm_traffic_manager_profile.test"),
   110  					resource.TestCheckResourceAttr(
   111  						"azurerm_traffic_manager_profile.test", "tags.%", "1"),
   112  					resource.TestCheckResourceAttr(
   113  						"azurerm_traffic_manager_profile.test", "tags.environment", "staging"),
   114  				),
   115  			},
   116  		},
   117  	})
   118  }
   119  
   120  func testCheckAzureRMTrafficManagerProfileExists(name string) resource.TestCheckFunc {
   121  	return func(s *terraform.State) error {
   122  		// Ensure we have enough information in state to look up in API
   123  		rs, ok := s.RootModule().Resources[name]
   124  		if !ok {
   125  			return fmt.Errorf("Not found: %s", name)
   126  		}
   127  
   128  		name := rs.Primary.Attributes["name"]
   129  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   130  		if !hasResourceGroup {
   131  			return fmt.Errorf("Bad: no resource group found in state for Traffic Manager Profile: %s", name)
   132  		}
   133  
   134  		// Ensure resource group/virtual network combination exists in API
   135  		conn := testAccProvider.Meta().(*ArmClient).trafficManagerProfilesClient
   136  
   137  		resp, err := conn.Get(resourceGroup, name)
   138  		if err != nil {
   139  			return fmt.Errorf("Bad: Get on trafficManagerProfilesClient: %s", err)
   140  		}
   141  
   142  		if resp.StatusCode == http.StatusNotFound {
   143  			return fmt.Errorf("Bad: Traffic Manager %q (resource group: %q) does not exist", name, resourceGroup)
   144  		}
   145  
   146  		return nil
   147  	}
   148  }
   149  
   150  func testCheckAzureRMTrafficManagerProfileDestroy(s *terraform.State) error {
   151  	conn := testAccProvider.Meta().(*ArmClient).trafficManagerProfilesClient
   152  
   153  	for _, rs := range s.RootModule().Resources {
   154  		if rs.Type != "azurerm_traffic_manager_profile" {
   155  			continue
   156  		}
   157  
   158  		log.Printf("[TRACE] test_profile %#v", rs)
   159  
   160  		name := rs.Primary.Attributes["name"]
   161  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   162  
   163  		resp, err := conn.Get(resourceGroup, name)
   164  		if err != nil {
   165  			return nil
   166  		}
   167  
   168  		if resp.StatusCode != http.StatusNotFound {
   169  			return fmt.Errorf("Traffic Manager profile sitll exists:\n%#v", resp.ProfileProperties)
   170  		}
   171  	}
   172  
   173  	return nil
   174  }
   175  
   176  var testAccAzureRMTrafficManagerProfile_weighted = `
   177  resource "azurerm_resource_group" "test" {
   178      name = "acctestRG-%d"
   179      location = "West US"
   180  }
   181  
   182  resource "azurerm_traffic_manager_profile" "test" {
   183      name = "acctesttmp%d"
   184      resource_group_name = "${azurerm_resource_group.test.name}"
   185      traffic_routing_method = "Weighted"
   186  
   187      dns_config {
   188          relative_name = "acctesttmp%d"
   189          ttl = 30
   190      }
   191  
   192      monitor_config {
   193          protocol = "https"
   194          port = 443
   195          path = "/"
   196      }
   197  }
   198  `
   199  
   200  var testAccAzureRMTrafficManagerProfile_performance = `
   201  resource "azurerm_resource_group" "test" {
   202      name = "acctestRG-%d"
   203      location = "West US"
   204  }
   205  
   206  resource "azurerm_traffic_manager_profile" "test" {
   207      name = "acctesttmp%d"
   208      resource_group_name = "${azurerm_resource_group.test.name}"
   209      traffic_routing_method = "Performance"
   210  
   211      dns_config {
   212          relative_name = "acctesttmp%d"
   213          ttl = 30
   214      }
   215  
   216      monitor_config {
   217          protocol = "https"
   218          port = 443
   219          path = "/"
   220      }
   221  }
   222  `
   223  
   224  var testAccAzureRMTrafficManagerProfile_priority = `
   225  resource "azurerm_resource_group" "test" {
   226      name = "acctestRG-%d"
   227      location = "West US"
   228  }
   229  
   230  resource "azurerm_traffic_manager_profile" "test" {
   231      name = "acctesttmp%d"
   232      resource_group_name = "${azurerm_resource_group.test.name}"
   233      traffic_routing_method = "Priority"
   234  
   235      dns_config {
   236          relative_name = "acctesttmp%d"
   237          ttl = 30
   238      }
   239  
   240      monitor_config {
   241          protocol = "https"
   242          port = 443
   243          path = "/"
   244      }
   245  }
   246  `
   247  
   248  var testAccAzureRMTrafficManagerProfile_withTags = `
   249  resource "azurerm_resource_group" "test" {
   250      name = "acctestRG-%d"
   251      location = "West US"
   252  }
   253  
   254  resource "azurerm_traffic_manager_profile" "test" {
   255      name = "acctesttmp%d"
   256      resource_group_name = "${azurerm_resource_group.test.name}"
   257      traffic_routing_method = "Priority"
   258  
   259      dns_config {
   260          relative_name = "acctesttmp%d"
   261          ttl = 30
   262      }
   263  
   264      monitor_config {
   265          protocol = "https"
   266          port = 443
   267          path = "/"
   268      }
   269      
   270      tags {
   271          environment = "Production"
   272          cost_center = "MSFT"
   273      }
   274  }
   275  `
   276  
   277  var testAccAzureRMTrafficManagerProfile_withTagsUpdated = `
   278  resource "azurerm_resource_group" "test" {
   279      name = "acctestRG-%d"
   280      location = "West US"
   281  }
   282  
   283  resource "azurerm_traffic_manager_profile" "test" {
   284      name = "acctesttmp%d"
   285      resource_group_name = "${azurerm_resource_group.test.name}"
   286      traffic_routing_method = "Priority"
   287  
   288      dns_config {
   289          relative_name = "acctesttmp%d"
   290          ttl = 30
   291      }
   292  
   293      monitor_config {
   294          protocol = "https"
   295          port = 443
   296          path = "/"
   297      }
   298      
   299      tags {
   300          environment = "staging"
   301      }
   302  }
   303  `