Skip to content

Commit

Permalink
Adding tests covering the resource registration
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Aug 29, 2017
1 parent 04562f7 commit 22908b5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 30 deletions.
65 changes: 36 additions & 29 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,43 +251,50 @@ func registerProviderWithSubscription(providerName string, client resources.Prov

var providerRegistrationOnce sync.Once

func determineAzureResourceProvidersToRegister(providerList []resources.Provider) map[string]struct{} {
providers := map[string]struct{}{
"Microsoft.Cache": struct{}{},
"Microsoft.Cdn": struct{}{},
"Microsoft.Compute": struct{}{},
"Microsoft.ContainerRegistry": struct{}{},
"Microsoft.ContainerService": struct{}{},
"Microsoft.DocumentDB": struct{}{},
"Microsoft.EventGrid": struct{}{},
"Microsoft.EventHub": struct{}{},
"Microsoft.KeyVault": struct{}{},
"microsoft.insights": struct{}{},
"Microsoft.Network": struct{}{},
"Microsoft.Resources": struct{}{},
"Microsoft.Search": struct{}{},
"Microsoft.ServiceBus": struct{}{},
"Microsoft.Sql": struct{}{},
"Microsoft.Storage": struct{}{},
}

// filter out any providers already registered
for _, p := range providerList {
if _, ok := providers[*p.Namespace]; !ok {
continue
}

if strings.ToLower(*p.RegistrationState) == "registered" {
log.Printf("[DEBUG] Skipping provider registration for namespace %s\n", *p.Namespace)
delete(providers, *p.Namespace)
}
}

return providers
}

// registerAzureResourceProvidersWithSubscription uses the providers client to register
// all Azure resource providers which the Terraform provider may require (regardless of
// whether they are actually used by the configuration or not). It was confirmed by Microsoft
// that this is the approach their own internal tools also take.
func registerAzureResourceProvidersWithSubscription(providerList []resources.Provider, client resources.ProvidersClient) error {
var err error
providerRegistrationOnce.Do(func() {
providers := map[string]struct{}{
"Microsoft.Cache": struct{}{},
"Microsoft.Cdn": struct{}{},
"Microsoft.Compute": struct{}{},
"Microsoft.ContainerRegistry": struct{}{},
"Microsoft.ContainerService": struct{}{},
"Microsoft.DocumentDB": struct{}{},
"Microsoft.EventGrid": struct{}{},
"Microsoft.EventHub": struct{}{},
"Microsoft.KeyVault": struct{}{},
"microsoft.insights": struct{}{},
"Microsoft.Network": struct{}{},
"Microsoft.Resources": struct{}{},
"Microsoft.Search": struct{}{},
"Microsoft.ServiceBus": struct{}{},
"Microsoft.Sql": struct{}{},
"Microsoft.Storage": struct{}{},
}

// filter out any providers already registered
for _, p := range providerList {
if _, ok := providers[*p.Namespace]; !ok {
continue
}

if strings.ToLower(*p.RegistrationState) == "registered" {
log.Printf("[DEBUG] Skipping provider registration for namespace %s\n", *p.Namespace)
delete(providers, *p.Namespace)
}
}
providers := determineAzureResourceProvidersToRegister(providerList)

var wg sync.WaitGroup
wg.Add(len(providers))
Expand Down
46 changes: 45 additions & 1 deletion azurerm/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/davecgh/go-spew/spew"
)

var testAccProviders map[string]terraform.ResourceProvider
Expand Down Expand Up @@ -56,12 +57,18 @@ func testAltLocation() string {
return os.Getenv("ARM_TEST_LOCATION_ALT")
}

func testArmEnvironment() (*azure.Environment, error) {
func testArmEnvironmentName() string {
envName, exists := os.LookupEnv("ARM_ENVIRONMENT")
if !exists {
envName = "public"
}

return envName
}

func testArmEnvironment() (*azure.Environment, error) {
envName := testArmEnvironmentName()

// detect cloud from environment
env, envErr := azure.EnvironmentFromName(envName)
if envErr != nil {
Expand All @@ -75,3 +82,40 @@ func testArmEnvironment() (*azure.Environment, error) {

return &env, nil
}

func TestAzureRMResourceProviderRegistration(t *testing.T) {
testAccPreCheck(t)

environment := testArmEnvironmentName()
config := Config{
SubscriptionID: os.Getenv("ARM_SUBSCRIPTION_ID"),
ClientID: os.Getenv("ARM_CLIENT_ID"),
TenantID: os.Getenv("ARM_TENANT_ID"),
ClientSecret: os.Getenv("ARM_CLIENT_SECRET"),
Environment: environment,
SkipProviderRegistration: false,
}

armClient, err := config.getArmClient()
if err != nil {
t.Fatalf("Error building ARM Client: %+v", err)
}

client := armClient.providers
providerList, err := client.List(nil, "")
if err != nil {
t.Fatalf("Unable to list provider registration status, it is possible that this is due to invalid "+
"credentials or the service principal does not have permission to use the Resource Manager API, Azure "+
"error: %s", err)
}

err = registerAzureResourceProvidersWithSubscription(*providerList.Value, client)
if err != nil {
t.Fatalf("Error registering Resource Providers: %+v", err)
}

needingRegistration := determineAzureResourceProvidersToRegister(*providerList.Value)
if len(needingRegistration) > 0 {
t.Fatalf("'%d' Resource Providers are still Pending Registration: %s", len(needingRegistration), spew.Sprint(needingRegistration))
}
}

0 comments on commit 22908b5

Please sign in to comment.