Skip to content

Commit

Permalink
Merge pull request #322 from svanharmelen/f-registration
Browse files Browse the repository at this point in the history
Improve the provider initialization
  • Loading branch information
tombuildsstuff authored Sep 27, 2017
2 parents d520011 + 21ab99a commit 524e770
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
93 changes: 49 additions & 44 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("ARM_ENVIRONMENT", "public"),
},

"skip_credentials_validation": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("ARM_SKIP_CREDENTIALS_VALIDATION", false),
},

"skip_provider_registration": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -165,20 +171,19 @@ type Config struct {
ManagementURL string

// Core
ClientID string
SubscriptionID string
TenantID string
Environment string
SkipProviderRegistration bool
ClientID string
SubscriptionID string
TenantID string
Environment string
SkipCredentialsValidation bool
SkipProviderRegistration bool

// Service Principal Auth
ClientSecret string

// Bearer Auth
AccessToken *adal.Token
IsCloudShell bool

validateCredentialsOnce sync.Once
}

func (c *Config) validateServicePrincipal() error {
Expand Down Expand Up @@ -316,12 +321,13 @@ func normalizeEnvironmentName(input string) string {
func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
return func(d *schema.ResourceData) (interface{}, error) {
config := &Config{
SubscriptionID: d.Get("subscription_id").(string),
ClientID: d.Get("client_id").(string),
ClientSecret: d.Get("client_secret").(string),
TenantID: d.Get("tenant_id").(string),
Environment: d.Get("environment").(string),
SkipProviderRegistration: d.Get("skip_provider_registration").(bool),
SubscriptionID: d.Get("subscription_id").(string),
ClientID: d.Get("client_id").(string),
ClientSecret: d.Get("client_secret").(string),
TenantID: d.Get("tenant_id").(string),
Environment: d.Get("environment").(string),
SkipCredentialsValidation: d.Get("skip_credentials_validation").(bool),
SkipProviderRegistration: d.Get("skip_provider_registration").(bool),
}

if config.ClientSecret != "" {
Expand Down Expand Up @@ -353,19 +359,21 @@ func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
return nil
}

// List all the available providers and their registration state to avoid unnecessary
// requests. This also lets us check if the provider credentials are correct.
providerList, err := client.providers.List(nil, "")
if err != nil {
return nil, fmt.Errorf("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)
}

if !config.SkipProviderRegistration {
err = registerAzureResourceProvidersWithSubscription(*providerList.Value, client.providers)
if !config.SkipCredentialsValidation {
// List all the available providers and their registration state to avoid unnecessary
// requests. This also lets us check if the provider credentials are correct.
providerList, err := client.providers.List(nil, "")
if err != nil {
return nil, err
return nil, fmt.Errorf("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)
}

if !config.SkipProviderRegistration {
err = registerAzureResourceProvidersWithSubscription(*providerList.Value, client.providers)
if err != nil {
return nil, err
}
}
}

Expand All @@ -382,8 +390,6 @@ func registerProviderWithSubscription(providerName string, client resources.Prov
return nil
}

var providerRegistrationOnce sync.Once

func determineAzureResourceProvidersToRegister(providerList []resources.Provider) map[string]struct{} {
providers := map[string]struct{}{
"Microsoft.Automation": {},
Expand Down Expand Up @@ -428,24 +434,23 @@ func determineAzureResourceProvidersToRegister(providerList []resources.Provider
// 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 {
providers := determineAzureResourceProvidersToRegister(providerList)

var err error
providerRegistrationOnce.Do(func() {

providers := determineAzureResourceProvidersToRegister(providerList)

var wg sync.WaitGroup
wg.Add(len(providers))
for providerName := range providers {
go func(p string) {
defer wg.Done()
log.Printf("[DEBUG] Registering provider with namespace %s\n", p)
if innerErr := registerProviderWithSubscription(p, client); err != nil {
err = innerErr
}
}(providerName)
}
wg.Wait()
})
var wg sync.WaitGroup
wg.Add(len(providers))

for providerName := range providers {
go func(p string) {
defer wg.Done()
log.Printf("[DEBUG] Registering provider with namespace %s\n", p)
if innerErr := registerProviderWithSubscription(p, client); err != nil {
err = innerErr
}
}(providerName)
}

wg.Wait()

return err
}
Expand Down
5 changes: 5 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ The following arguments are supported:
* `german`
* `china`

* `skip_credentials_validation` - (Optional) Prevents the provider from validating
the given credentials. When set to `true`, `skip_provider_registration` is assumed.
It can also be sourced from the `ARM_SKIP_CREDENTIALS_VALIDATION` environment
variable, defaults to `false`.

* `skip_provider_registration` - (Optional) Prevents the provider from registering
the ARM provider namespaces, this can be used if you don't wish to give the Active
Directory Application permission to register resource providers. It can also be
Expand Down

0 comments on commit 524e770

Please sign in to comment.