github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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_withTags(t *testing.T) { 33 ri := acctest.RandInt() 34 preConfig := fmt.Sprintf(testAccAzureRMCdnEndpoint_withTags, ri, ri, ri) 35 postConfig := fmt.Sprintf(testAccAzureRMCdnEndpoint_withTagsUpdate, ri, ri, ri) 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testCheckAzureRMCdnEndpointDestroy, 41 Steps: []resource.TestStep{ 42 { 43 Config: preConfig, 44 Check: resource.ComposeTestCheckFunc( 45 testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"), 46 resource.TestCheckResourceAttr( 47 "azurerm_cdn_endpoint.test", "tags.%", "2"), 48 resource.TestCheckResourceAttr( 49 "azurerm_cdn_endpoint.test", "tags.environment", "Production"), 50 resource.TestCheckResourceAttr( 51 "azurerm_cdn_endpoint.test", "tags.cost_center", "MSFT"), 52 ), 53 }, 54 55 { 56 Config: postConfig, 57 Check: resource.ComposeTestCheckFunc( 58 testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"), 59 resource.TestCheckResourceAttr( 60 "azurerm_cdn_endpoint.test", "tags.%", "1"), 61 resource.TestCheckResourceAttr( 62 "azurerm_cdn_endpoint.test", "tags.environment", "staging"), 63 ), 64 }, 65 }, 66 }) 67 } 68 69 func testCheckAzureRMCdnEndpointExists(name string) resource.TestCheckFunc { 70 return func(s *terraform.State) error { 71 // Ensure we have enough information in state to look up in API 72 rs, ok := s.RootModule().Resources[name] 73 if !ok { 74 return fmt.Errorf("Not found: %s", name) 75 } 76 77 name := rs.Primary.Attributes["name"] 78 profileName := rs.Primary.Attributes["profile_name"] 79 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 80 if !hasResourceGroup { 81 return fmt.Errorf("Bad: no resource group found in state for cdn endpoint: %s", name) 82 } 83 84 conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient 85 86 resp, err := conn.Get(name, profileName, resourceGroup) 87 if err != nil { 88 return fmt.Errorf("Bad: Get on cdnEndpointsClient: %s", err) 89 } 90 91 if resp.StatusCode == http.StatusNotFound { 92 return fmt.Errorf("Bad: CDN Endpoint %q (resource group: %q) does not exist", name, resourceGroup) 93 } 94 95 return nil 96 } 97 } 98 99 func testCheckAzureRMCdnEndpointDestroy(s *terraform.State) error { 100 conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient 101 102 for _, rs := range s.RootModule().Resources { 103 if rs.Type != "azurerm_cdn_endpoint" { 104 continue 105 } 106 107 name := rs.Primary.Attributes["name"] 108 resourceGroup := rs.Primary.Attributes["resource_group_name"] 109 profileName := rs.Primary.Attributes["profile_name"] 110 111 resp, err := conn.Get(name, profileName, resourceGroup) 112 113 if err != nil { 114 return nil 115 } 116 117 if resp.StatusCode != http.StatusNotFound { 118 return fmt.Errorf("CDN Endpoint still exists:\n%#v", resp.Properties) 119 } 120 } 121 122 return nil 123 } 124 125 var testAccAzureRMCdnEndpoint_basic = ` 126 resource "azurerm_resource_group" "test" { 127 name = "acctestrg-%d" 128 location = "West US" 129 } 130 resource "azurerm_cdn_profile" "test" { 131 name = "acctestcdnprof%d" 132 location = "West US" 133 resource_group_name = "${azurerm_resource_group.test.name}" 134 sku = "Standard_Verizon" 135 } 136 137 resource "azurerm_cdn_endpoint" "test" { 138 name = "acctestcdnend%d" 139 profile_name = "${azurerm_cdn_profile.test.name}" 140 location = "West US" 141 resource_group_name = "${azurerm_resource_group.test.name}" 142 143 origin { 144 name = "acceptanceTestCdnOrigin1" 145 host_name = "www.example.com" 146 https_port = 443 147 http_port = 80 148 } 149 } 150 ` 151 152 var testAccAzureRMCdnEndpoint_withTags = ` 153 resource "azurerm_resource_group" "test" { 154 name = "acctestrg-%d" 155 location = "West US" 156 } 157 resource "azurerm_cdn_profile" "test" { 158 name = "acctestcdnprof%d" 159 location = "West US" 160 resource_group_name = "${azurerm_resource_group.test.name}" 161 sku = "Standard_Verizon" 162 } 163 164 resource "azurerm_cdn_endpoint" "test" { 165 name = "acctestcdnend%d" 166 profile_name = "${azurerm_cdn_profile.test.name}" 167 location = "West US" 168 resource_group_name = "${azurerm_resource_group.test.name}" 169 170 origin { 171 name = "acceptanceTestCdnOrigin2" 172 host_name = "www.example.com" 173 https_port = 443 174 http_port = 80 175 } 176 177 tags { 178 environment = "Production" 179 cost_center = "MSFT" 180 } 181 } 182 ` 183 184 var testAccAzureRMCdnEndpoint_withTagsUpdate = ` 185 resource "azurerm_resource_group" "test" { 186 name = "acctestrg-%d" 187 location = "West US" 188 } 189 resource "azurerm_cdn_profile" "test" { 190 name = "acctestcdnprof%d" 191 location = "West US" 192 resource_group_name = "${azurerm_resource_group.test.name}" 193 sku = "Standard_Verizon" 194 } 195 196 resource "azurerm_cdn_endpoint" "test" { 197 name = "acctestcdnend%d" 198 profile_name = "${azurerm_cdn_profile.test.name}" 199 location = "West US" 200 resource_group_name = "${azurerm_resource_group.test.name}" 201 202 origin { 203 name = "acceptanceTestCdnOrigin2" 204 host_name = "www.example.com" 205 https_port = 443 206 http_port = 80 207 } 208 209 tags { 210 environment = "staging" 211 } 212 } 213 `