Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Sweepers to the CDN's #205

Merged
merged 4 commits into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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