Skip to content

Commit

Permalink
Merge pull request #205 from terraform-providers/cdn-sweepers
Browse files Browse the repository at this point in the history
Adding Sweepers to the CDN's
  • Loading branch information
tombuildsstuff authored Aug 1, 2017
2 parents d3e94dd + 8357d7c commit 10cf46c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
65 changes: 65 additions & 0 deletions azurerm/azurerm_sweeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package azurerm

import (
"fmt"
"log"
"os"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestMain(m *testing.M) {
resource.TestMain(m)
}

func buildConfigForSweepers() (*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, resourceLocation string, region string) bool {
loweredName := strings.ToLower(name)

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)

if normalisedResourceLocation != normalisedRegion {
log.Printf("Region '%s' isn't '%s' - skipping", normalisedResourceLocation, normalisedRegion)
return false
}

return true
}
46 changes: 46 additions & 0 deletions azurerm/resource_arm_cdn_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"log"
"net/http"
"testing"

Expand All @@ -10,6 +11,51 @@ 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 := buildConfigForSweepers()
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 {
if !shouldSweepAcceptanceTestResource(*profile.Name, *profile.Location, region) {
continue
}

resourceId, err := parseAzureResourceID(*profile.ID)
if err != nil {
return err
}

resourceGroup := resourceId.ResourceGroup
name := resourceId.Path["profiles"]

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
Expand Down

0 comments on commit 10cf46c

Please sign in to comment.