2016-01-19 11:11:37 +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 TestResourceAzureRMCdnProfileSKU_validation(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Value string
|
|
|
|
ErrCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Value: "Random",
|
|
|
|
ErrCount: 1,
|
|
|
|
},
|
|
|
|
{
|
2016-06-30 16:36:08 +02:00
|
|
|
Value: "Standard_Verizon",
|
2016-06-02 02:44:08 +02:00
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
2016-06-30 16:36:08 +02:00
|
|
|
Value: "Premium_Verizon",
|
2016-06-02 02:44:08 +02:00
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
2016-06-30 16:36:08 +02:00
|
|
|
Value: "Standard_Akamai",
|
2016-06-02 02:44:08 +02:00
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
2016-06-30 16:36:08 +02:00
|
|
|
Value: "STANDARD_AKAMAI",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "standard_akamai",
|
2016-06-02 02:44:08 +02:00
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
_, errors := validateCdnProfileSku(tc.Value, "azurerm_cdn_profile")
|
|
|
|
|
|
|
|
if len(errors) != tc.ErrCount {
|
|
|
|
t.Fatalf("Expected the Azure RM CDN Profile SKU to trigger a validation error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureRMCdnProfile_basic(t *testing.T) {
|
|
|
|
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
config := fmt.Sprintf(testAccAzureRMCdnProfile_basic, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMCdnProfileDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureRMCdnProfile_withTags(t *testing.T) {
|
|
|
|
|
|
|
|
ri := acctest.RandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAzureRMCdnProfile_withTags, ri, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAzureRMCdnProfile_withTagsUpdate, ri, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckAzureRMCdnProfileDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: preConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_cdn_profile.test", "tags.%", "2"),
|
2016-06-02 02:44:08 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_cdn_profile.test", "tags.environment", "Production"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_cdn_profile.test", "tags.cost_center", "MSFT"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Config: postConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckAzureRMCdnProfileExists("azurerm_cdn_profile.test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-06-10 17:07:02 +02:00
|
|
|
"azurerm_cdn_profile.test", "tags.%", "1"),
|
2016-06-02 02:44:08 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azurerm_cdn_profile.test", "tags.environment", "staging"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMCdnProfileExists(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"]
|
|
|
|
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
if !hasResourceGroup {
|
|
|
|
return fmt.Errorf("Bad: no resource group found in state for cdn profile: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
resp, err := conn.Get(resourceGroup, name)
|
2016-06-02 02:44:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Get on cdnProfilesClient: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return fmt.Errorf("Bad: CDN Profile %q (resource group: %q) does not exist", name, resourceGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckAzureRMCdnProfileDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*ArmClient).cdnProfilesClient
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azurerm_cdn_profile" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
resourceGroup := rs.Primary.Attributes["resource_group_name"]
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
resp, err := conn.Get(resourceGroup, name)
|
2016-06-02 02:44:08 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
2016-12-06 09:39:47 +01:00
|
|
|
return fmt.Errorf("CDN Profile still exists:\n%#v", resp.ProfileProperties)
|
2016-06-02 02:44:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureRMCdnProfile_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
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMCdnProfile_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
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "Production"
|
|
|
|
cost_center = "MSFT"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAzureRMCdnProfile_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
|
|
|
|
|
|
|
tags {
|
|
|
|
environment = "staging"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|