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

Fixing the Microsoft.Insights Resource Provider Registration #282

Merged
merged 3 commits into from
Aug 30, 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: 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
54 changes: 53 additions & 1 deletion azurerm/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"github.com/Azure/go-autorest/autorest/azure"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
Expand Down Expand Up @@ -56,12 +58,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 +83,47 @@ func testArmEnvironment() (*azure.Environment, error) {

return &env, nil
}

func TestAccAzureRMResourceProviderRegistration(t *testing.T) {
environment := testArmEnvironmentName()

if os.Getenv(resource.TestEnvVar) == "" {
t.Skip(fmt.Sprintf(
"Integration test skipped unless env '%s' set",
resource.TestEnvVar))
return
}

// we deliberately don't use the main config - since we care about
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))
}
}