github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/resource_arm_cdn_endpoint_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 TestAccAzureRMCdnEndpoint_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMCdnEndpoint_basic, ri, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMCdnEndpointDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMCdnEndpoint_disappears(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	config := fmt.Sprintf(testAccAzureRMCdnEndpoint_basic, ri, ri, ri)
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testCheckAzureRMCdnEndpointDestroy,
    40  		Steps: []resource.TestStep{
    41  			{
    42  				Config: config,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
    45  					testCheckAzureRMCdnEndpointDisappears("azurerm_cdn_endpoint.test"),
    46  				),
    47  				ExpectNonEmptyPlan: true,
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccAzureRMCdnEndpoint_withTags(t *testing.T) {
    54  	ri := acctest.RandInt()
    55  	preConfig := fmt.Sprintf(testAccAzureRMCdnEndpoint_withTags, ri, ri, ri)
    56  	postConfig := fmt.Sprintf(testAccAzureRMCdnEndpoint_withTagsUpdate, ri, ri, ri)
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testCheckAzureRMCdnEndpointDestroy,
    62  		Steps: []resource.TestStep{
    63  			{
    64  				Config: preConfig,
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
    67  					resource.TestCheckResourceAttr(
    68  						"azurerm_cdn_endpoint.test", "tags.%", "2"),
    69  					resource.TestCheckResourceAttr(
    70  						"azurerm_cdn_endpoint.test", "tags.environment", "Production"),
    71  					resource.TestCheckResourceAttr(
    72  						"azurerm_cdn_endpoint.test", "tags.cost_center", "MSFT"),
    73  				),
    74  			},
    75  
    76  			{
    77  				Config: postConfig,
    78  				Check: resource.ComposeTestCheckFunc(
    79  					testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
    80  					resource.TestCheckResourceAttr(
    81  						"azurerm_cdn_endpoint.test", "tags.%", "1"),
    82  					resource.TestCheckResourceAttr(
    83  						"azurerm_cdn_endpoint.test", "tags.environment", "staging"),
    84  				),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func testCheckAzureRMCdnEndpointExists(name string) resource.TestCheckFunc {
    91  	return func(s *terraform.State) error {
    92  		// Ensure we have enough information in state to look up in API
    93  		rs, ok := s.RootModule().Resources[name]
    94  		if !ok {
    95  			return fmt.Errorf("Not found: %s", name)
    96  		}
    97  
    98  		name := rs.Primary.Attributes["name"]
    99  		profileName := rs.Primary.Attributes["profile_name"]
   100  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   101  		if !hasResourceGroup {
   102  			return fmt.Errorf("Bad: no resource group found in state for cdn endpoint: %s", name)
   103  		}
   104  
   105  		conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient
   106  
   107  		resp, err := conn.Get(resourceGroup, profileName, name)
   108  		if err != nil {
   109  			return fmt.Errorf("Bad: Get on cdnEndpointsClient: %s", err)
   110  		}
   111  
   112  		if resp.StatusCode == http.StatusNotFound {
   113  			return fmt.Errorf("Bad: CDN Endpoint %q (resource group: %q) does not exist", name, resourceGroup)
   114  		}
   115  
   116  		return nil
   117  	}
   118  }
   119  
   120  func testCheckAzureRMCdnEndpointDisappears(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  		profileName := rs.Primary.Attributes["profile_name"]
   130  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   131  		if !hasResourceGroup {
   132  			return fmt.Errorf("Bad: no resource group found in state for cdn endpoint: %s", name)
   133  		}
   134  
   135  		conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient
   136  
   137  		_, err := conn.Delete(resourceGroup, profileName, name, make(chan struct{}))
   138  		if err != nil {
   139  			return fmt.Errorf("Bad: Delete on cdnEndpointsClient: %s", err)
   140  		}
   141  
   142  		return nil
   143  	}
   144  }
   145  
   146  func testCheckAzureRMCdnEndpointDestroy(s *terraform.State) error {
   147  	conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient
   148  
   149  	for _, rs := range s.RootModule().Resources {
   150  		if rs.Type != "azurerm_cdn_endpoint" {
   151  			continue
   152  		}
   153  
   154  		name := rs.Primary.Attributes["name"]
   155  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   156  		profileName := rs.Primary.Attributes["profile_name"]
   157  
   158  		resp, err := conn.Get(resourceGroup, profileName, name)
   159  
   160  		if err != nil {
   161  			return nil
   162  		}
   163  
   164  		if resp.StatusCode != http.StatusNotFound {
   165  			return fmt.Errorf("CDN Endpoint still exists:\n%#v", resp.EndpointProperties)
   166  		}
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  var testAccAzureRMCdnEndpoint_basic = `
   173  resource "azurerm_resource_group" "test" {
   174      name = "acctestRG-%d"
   175      location = "West US"
   176  }
   177  resource "azurerm_cdn_profile" "test" {
   178      name = "acctestcdnprof%d"
   179      location = "West US"
   180      resource_group_name = "${azurerm_resource_group.test.name}"
   181      sku = "Standard_Verizon"
   182  }
   183  
   184  resource "azurerm_cdn_endpoint" "test" {
   185      name = "acctestcdnend%d"
   186      profile_name = "${azurerm_cdn_profile.test.name}"
   187      location = "West US"
   188      resource_group_name = "${azurerm_resource_group.test.name}"
   189  
   190      origin {
   191  	name = "acceptanceTestCdnOrigin1"
   192  	host_name = "www.example.com"
   193  	https_port = 443
   194  	http_port = 80
   195      }
   196  }
   197  `
   198  
   199  var testAccAzureRMCdnEndpoint_withTags = `
   200  resource "azurerm_resource_group" "test" {
   201      name = "acctestRG-%d"
   202      location = "West US"
   203  }
   204  resource "azurerm_cdn_profile" "test" {
   205      name = "acctestcdnprof%d"
   206      location = "West US"
   207      resource_group_name = "${azurerm_resource_group.test.name}"
   208      sku = "Standard_Verizon"
   209  }
   210  
   211  resource "azurerm_cdn_endpoint" "test" {
   212      name = "acctestcdnend%d"
   213      profile_name = "${azurerm_cdn_profile.test.name}"
   214      location = "West US"
   215      resource_group_name = "${azurerm_resource_group.test.name}"
   216  
   217      origin {
   218  	name = "acceptanceTestCdnOrigin2"
   219  	host_name = "www.example.com"
   220  	https_port = 443
   221  	http_port = 80
   222      }
   223  
   224      tags {
   225  	environment = "Production"
   226  	cost_center = "MSFT"
   227      }
   228  }
   229  `
   230  
   231  var testAccAzureRMCdnEndpoint_withTagsUpdate = `
   232  resource "azurerm_resource_group" "test" {
   233      name = "acctestRG-%d"
   234      location = "West US"
   235  }
   236  resource "azurerm_cdn_profile" "test" {
   237      name = "acctestcdnprof%d"
   238      location = "West US"
   239      resource_group_name = "${azurerm_resource_group.test.name}"
   240      sku = "Standard_Verizon"
   241  }
   242  
   243  resource "azurerm_cdn_endpoint" "test" {
   244      name = "acctestcdnend%d"
   245      profile_name = "${azurerm_cdn_profile.test.name}"
   246      location = "West US"
   247      resource_group_name = "${azurerm_resource_group.test.name}"
   248  
   249      origin {
   250  	name = "acceptanceTestCdnOrigin2"
   251  	host_name = "www.example.com"
   252  	https_port = 443
   253  	http_port = 80
   254      }
   255  
   256      tags {
   257  	environment = "staging"
   258      }
   259  }
   260  `