From ce36bff1385a85646fede6c753705b9578e18f50 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 1 Aug 2017 12:52:39 +0100 Subject: [PATCH 1/4] Adding Sweepers to the CDN's --- azurerm/azurerm_sweeper_test.go | 52 ++++++++++++++++++++++++ azurerm/provider_test.go | 2 +- azurerm/resource_arm_cdn_profile_test.go | 52 ++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 azurerm/azurerm_sweeper_test.go diff --git a/azurerm/azurerm_sweeper_test.go b/azurerm/azurerm_sweeper_test.go new file mode 100644 index 000000000000..a9cdc473d342 --- /dev/null +++ b/azurerm/azurerm_sweeper_test.go @@ -0,0 +1,52 @@ +package azurerm + +import ( + "fmt" + "os" + "strings" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestMain(m *testing.M) { + resource.TestMain(m) +} + +func sharedConfigForRegion(region string) (*ArmClient, error) { + subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") + clientID := os.Getenv("ARM_CLIENT_ID") + clientSecret := os.Getenv("ARM_CLIENT_SECRET") + tenantID := os.Getenv("ARM_TENANT_ID") + environment := os.Getenv("ARM_ENVIRONMENT") + + if environment == "" { + environment = "public" + } + + if subscriptionID == "" || clientID == "" || clientSecret == "" || tenantID == "" { + return nil, fmt.Errorf("ARM_SUBSCRIPTION_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET and ARM_TENANT_ID must be set for acceptance tests") + } + + config := &Config{ + SubscriptionID: subscriptionID, + ClientID: clientID, + ClientSecret: clientSecret, + TenantID: tenantID, + Environment: environment, + SkipProviderRegistration: false, + } + + return config.getArmClient() +} + +func shouldSweepAcceptanceTestResource(name string) bool { + loweredName := strings.ToLower(name) + return strings.HasPrefix(loweredName, "acctest") +} + +func resourceLocatedInRegion(resourceLocation string, region string) bool { + normalisedResourceLocation := azureRMNormalizeLocation(resourceLocation) + normalisedRegion := azureRMNormalizeLocation(region) + return normalisedResourceLocation == normalisedRegion +} \ No newline at end of file diff --git a/azurerm/provider_test.go b/azurerm/provider_test.go index a26249f58884..e2ff2833c15e 100644 --- a/azurerm/provider_test.go +++ b/azurerm/provider_test.go @@ -37,4 +37,4 @@ func testAccPreCheck(t *testing.T) { if subscriptionID == "" || clientID == "" || clientSecret == "" || tenantID == "" { t.Fatal("ARM_SUBSCRIPTION_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET and ARM_TENANT_ID must be set for acceptance tests") } -} +} \ No newline at end of file diff --git a/azurerm/resource_arm_cdn_profile_test.go b/azurerm/resource_arm_cdn_profile_test.go index 0e80fcb2b626..cd47c4622914 100644 --- a/azurerm/resource_arm_cdn_profile_test.go +++ b/azurerm/resource_arm_cdn_profile_test.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "log" "net/http" "testing" @@ -10,6 +11,57 @@ import ( "github.com/hashicorp/terraform/terraform" ) +func init() { + resource.AddTestSweepers("azurerm_cdn_profile", &resource.Sweeper{ + Name: "azurerm_cdn_profile", + F: testSweepCDNProfiles, + }) +} + +func testSweepCDNProfiles(region string) error { + armClient, err := sharedConfigForRegion(region) + if err != nil { + return err + } + + client := (*armClient).cdnProfilesClient + + log.Printf("Retrieving the CDN Profiles..") + results, err := client.List() + if err != nil { + return fmt.Errorf("Error Listing on CDN Profiles: %+v", err) + } + + for _, profile := range *results.Value { + resourceId, err := parseAzureResourceID(*profile.ID) + if err != nil { + return err + } + + resourceGroup := resourceId.ResourceGroup + name := resourceId.Path["profiles"] + + if !resourceLocatedInRegion(*profile.Location, region) { + log.Printf("Region '%s' isn't '%s' - skipping", *profile.Location, region) + continue + } + + if !shouldSweepAcceptanceTestResource(*profile.Name) { + log.Printf("Ignoring Resource '%s'", profile.Name) + continue + } + + log.Printf("Deleting CDN Profile '%s' in Resource Group '%s'", name, resourceGroup) + _, error := client.Delete(resourceGroup, name, make(chan struct{})) + err = <- error + if err != nil { + return err + } + } + + return nil +} + func TestResourceAzureRMCdnProfileSKU_validation(t *testing.T) { cases := []struct { Value string From 631c6310f4898fc2f9328d58dd7c458ce7bb90c7 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 1 Aug 2017 13:04:00 +0100 Subject: [PATCH 2/4] Refactoring to reduce code duplication --- azurerm/azurerm_sweeper_test.go | 25 ++++++++++++++++++------ azurerm/resource_arm_cdn_profile_test.go | 14 ++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/azurerm/azurerm_sweeper_test.go b/azurerm/azurerm_sweeper_test.go index a9cdc473d342..20b8fdd7e067 100644 --- a/azurerm/azurerm_sweeper_test.go +++ b/azurerm/azurerm_sweeper_test.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "log" "os" "strings" "testing" @@ -40,13 +41,25 @@ func sharedConfigForRegion(region string) (*ArmClient, error) { return config.getArmClient() } -func shouldSweepAcceptanceTestResource(name string) bool { +func shouldSweepAcceptanceTestResource(name string, resourceLocation string, region string) bool { loweredName := strings.ToLower(name) - return strings.HasPrefix(loweredName, "acctest") -} -func resourceLocatedInRegion(resourceLocation string, region string) bool { + prefixesToIgnore := []string { "acctest" } + + for _, prefix := range prefixesToIgnore { + if !strings.HasPrefix(loweredName, prefix) { + log.Printf("Ignoring Resource '%s' due to prefix '%s'", name, prefix) + return false + } + } + normalisedResourceLocation := azureRMNormalizeLocation(resourceLocation) normalisedRegion := azureRMNormalizeLocation(region) - return normalisedResourceLocation == normalisedRegion -} \ No newline at end of file + + if normalisedResourceLocation != normalisedRegion { + log.Printf("Region '%s' isn't '%s' - skipping", normalisedResourceLocation, normalisedRegion) + return false + } + + return true +} diff --git a/azurerm/resource_arm_cdn_profile_test.go b/azurerm/resource_arm_cdn_profile_test.go index cd47c4622914..9bf8ba2872e5 100644 --- a/azurerm/resource_arm_cdn_profile_test.go +++ b/azurerm/resource_arm_cdn_profile_test.go @@ -33,6 +33,10 @@ func testSweepCDNProfiles(region string) error { } for _, profile := range *results.Value { + if !shouldSweepAcceptanceTestResource(*profile.Name, *profile.Location, region) { + continue + } + resourceId, err := parseAzureResourceID(*profile.ID) if err != nil { return err @@ -41,16 +45,6 @@ func testSweepCDNProfiles(region string) error { resourceGroup := resourceId.ResourceGroup name := resourceId.Path["profiles"] - if !resourceLocatedInRegion(*profile.Location, region) { - log.Printf("Region '%s' isn't '%s' - skipping", *profile.Location, region) - continue - } - - if !shouldSweepAcceptanceTestResource(*profile.Name) { - log.Printf("Ignoring Resource '%s'", profile.Name) - continue - } - log.Printf("Deleting CDN Profile '%s' in Resource Group '%s'", name, resourceGroup) _, error := client.Delete(resourceGroup, name, make(chan struct{})) err = <- error From 7aee6e6a75ea406f82c21029718ce0101d746dda Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 1 Aug 2017 13:10:32 +0100 Subject: [PATCH 3/4] Removing the Region argument as it's unused --- azurerm/azurerm_sweeper_test.go | 2 +- azurerm/resource_arm_cdn_profile_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/azurerm_sweeper_test.go b/azurerm/azurerm_sweeper_test.go index 20b8fdd7e067..4b6d2a69ce03 100644 --- a/azurerm/azurerm_sweeper_test.go +++ b/azurerm/azurerm_sweeper_test.go @@ -14,7 +14,7 @@ func TestMain(m *testing.M) { resource.TestMain(m) } -func sharedConfigForRegion(region string) (*ArmClient, error) { +func buildConfigForSweepers() (*ArmClient, error) { subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") clientID := os.Getenv("ARM_CLIENT_ID") clientSecret := os.Getenv("ARM_CLIENT_SECRET") diff --git a/azurerm/resource_arm_cdn_profile_test.go b/azurerm/resource_arm_cdn_profile_test.go index 9bf8ba2872e5..5b2ea3712728 100644 --- a/azurerm/resource_arm_cdn_profile_test.go +++ b/azurerm/resource_arm_cdn_profile_test.go @@ -19,7 +19,7 @@ func init() { } func testSweepCDNProfiles(region string) error { - armClient, err := sharedConfigForRegion(region) + armClient, err := buildConfigForSweepers() if err != nil { return err } From 8357d7ce599c4cb03ec8c2f74c90e2f49dc709b9 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 1 Aug 2017 15:45:33 +0100 Subject: [PATCH 4/4] Linting --- azurerm/azurerm_sweeper_test.go | 2 +- azurerm/provider_test.go | 2 +- azurerm/resource_arm_cdn_profile_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/azurerm/azurerm_sweeper_test.go b/azurerm/azurerm_sweeper_test.go index 4b6d2a69ce03..849a6912c885 100644 --- a/azurerm/azurerm_sweeper_test.go +++ b/azurerm/azurerm_sweeper_test.go @@ -44,7 +44,7 @@ func buildConfigForSweepers() (*ArmClient, error) { func shouldSweepAcceptanceTestResource(name string, resourceLocation string, region string) bool { loweredName := strings.ToLower(name) - prefixesToIgnore := []string { "acctest" } + prefixesToIgnore := []string{"acctest"} for _, prefix := range prefixesToIgnore { if !strings.HasPrefix(loweredName, prefix) { diff --git a/azurerm/provider_test.go b/azurerm/provider_test.go index e2ff2833c15e..a26249f58884 100644 --- a/azurerm/provider_test.go +++ b/azurerm/provider_test.go @@ -37,4 +37,4 @@ func testAccPreCheck(t *testing.T) { if subscriptionID == "" || clientID == "" || clientSecret == "" || tenantID == "" { t.Fatal("ARM_SUBSCRIPTION_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET and ARM_TENANT_ID must be set for acceptance tests") } -} \ No newline at end of file +} diff --git a/azurerm/resource_arm_cdn_profile_test.go b/azurerm/resource_arm_cdn_profile_test.go index 5b2ea3712728..eb68d9ce300b 100644 --- a/azurerm/resource_arm_cdn_profile_test.go +++ b/azurerm/resource_arm_cdn_profile_test.go @@ -47,7 +47,7 @@ func testSweepCDNProfiles(region string) error { log.Printf("Deleting CDN Profile '%s' in Resource Group '%s'", name, resourceGroup) _, error := client.Delete(resourceGroup, name, make(chan struct{})) - err = <- error + err = <-error if err != nil { return err }