2016-01-19 19:08:56 +01:00
|
|
|
package azurerm
|
|
|
|
|
2016-06-02 02:44:08 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAzureRMCdnEndpoint_basic(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMCdnEndpoint_basic, ri, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMCdnEndpointDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func TestAccAzureRMCdnEndpoint_disappears(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMCdnEndpoint_basic, ri, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMCdnEndpointDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
|
|
|
|
testCheckAzureRMCdnEndpointDisappears("azurerm_cdn_endpoint.test"),
|
|
|
|
),
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-02 02:44:08 +02:00
|
|
|
func TestAccAzureRMCdnEndpoint_withTags(t *testing.T) {
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMCdnEndpoint_withTags, ri, ri, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMCdnEndpoint_withTagsUpdate, ri, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMCdnEndpointDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: preConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_cdn_endpoint.test", "tags.%", "2"),
|
2016-06-02 02:44:08 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_cdn_endpoint.test", "tags.environment", "Production"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_cdn_endpoint.test", "tags.cost_center", "MSFT"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Config: postConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMCdnEndpointExists("azurerm_cdn_endpoint.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_cdn_endpoint.test", "tags.%", "1"),
|
2016-06-02 02:44:08 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_cdn_endpoint.test", "tags.environment", "staging"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMCdnEndpointExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Ensure we have enough information in state to look up in API
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
profileName := rs.Primary.Attributes["profile_name"]
|
|
|
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
if !hasResourceGroup {
|
|
|
|
return fmt.Errorf("Bad: no resource group found in state for cdn endpoint: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient
|
|
|
|
|
|
|
|
resp, err := conn.Get(name, profileName, resourceGroup)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Get on cdnEndpointsClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Bad: CDN Endpoint %q (resource group: %q) does not exist", name, resourceGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 18:14:20 +02:00
|
|
|
func testCheckAzureRMCdnEndpointDisappears(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Ensure we have enough information in state to look up in API
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
profileName := rs.Primary.Attributes["profile_name"]
|
|
|
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
if !hasResourceGroup {
|
|
|
|
return fmt.Errorf("Bad: no resource group found in state for cdn endpoint: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient
|
|
|
|
|
|
|
|
_, err := conn.DeleteIfExists(name, profileName, resourceGroup, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Delete on cdnEndpointsClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 02:44:08 +02:00
|
|
|
func testCheckAzureRMCdnEndpointDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).cdnEndpointsClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_cdn_endpoint" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
profileName := rs.Primary.Attributes["profile_name"]
|
|
|
|
|
|
|
|
resp, err := conn.Get(name, profileName, resourceGroup)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
|
|
return fmt.Errorf("CDN Endpoint still exists:\n%#v", resp.Properties)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMCdnEndpoint_basic = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG-%d"
|
2016-06-02 02:44:08 +02:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_cdn_profile" "test" {
|
|
|
|
name = "acctestcdnprof%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
2016-06-30 16:36:08 +02:00
|
|
|
sku = "Standard_Verizon"
|
2016-06-02 02:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_cdn_endpoint" "test" {
|
|
|
|
name = "acctestcdnend%d"
|
|
|
|
profile_name = "${azurerm_cdn_profile.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
origin {
|
|
|
|
name = "acceptanceTestCdnOrigin1"
|
|
|
|
host_name = "www.example.com"
|
|
|
|
https_port = 443
|
|
|
|
http_port = 80
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMCdnEndpoint_withTags = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG-%d"
|
2016-06-02 02:44:08 +02:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_cdn_profile" "test" {
|
|
|
|
name = "acctestcdnprof%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
2016-06-30 16:36:08 +02:00
|
|
|
sku = "Standard_Verizon"
|
2016-06-02 02:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_cdn_endpoint" "test" {
|
|
|
|
name = "acctestcdnend%d"
|
|
|
|
profile_name = "${azurerm_cdn_profile.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
origin {
|
|
|
|
name = "acceptanceTestCdnOrigin2"
|
|
|
|
host_name = "www.example.com"
|
|
|
|
https_port = 443
|
|
|
|
http_port = 80
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "Production"
|
|
|
|
cost_center = "MSFT"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMCdnEndpoint_withTagsUpdate = `
|
|
|
|
resource "azurerm_resource_group" "test" {
|
2016-09-28 12:27:07 +02:00
|
|
|
name = "acctestRG-%d"
|
2016-06-02 02:44:08 +02:00
|
|
|
location = "West US"
|
|
|
|
}
|
|
|
|
resource "azurerm_cdn_profile" "test" {
|
|
|
|
name = "acctestcdnprof%d"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
2016-06-30 16:36:08 +02:00
|
|
|
sku = "Standard_Verizon"
|
2016-06-02 02:44:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azurerm_cdn_endpoint" "test" {
|
|
|
|
name = "acctestcdnend%d"
|
|
|
|
profile_name = "${azurerm_cdn_profile.test.name}"
|
|
|
|
location = "West US"
|
|
|
|
resource_group_name = "${azurerm_resource_group.test.name}"
|
|
|
|
|
|
|
|
origin {
|
|
|
|
name = "acceptanceTestCdnOrigin2"
|
|
|
|
host_name = "www.example.com"
|
|
|
|
https_port = 443
|
|
|
|
http_port = 80
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "staging"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|