provider/azurerm: Register needed Azure providers

This commit is contained in:
James Nugent 2016-01-05 16:43:52 -05:00
parent 5c6304ed57
commit a48e713fe0
2 changed files with 36 additions and 0 deletions

View File

@ -36,6 +36,7 @@ type ArmClient struct {
vnetGatewayClient network.VirtualNetworkGatewaysClient
vnetClient network.VirtualNetworksClient
providers resources.ProvidersClient
resourceGroupClient resources.GroupsClient
tagsClient resources.TagsClient
@ -160,6 +161,11 @@ func (c *Config) getArmClient() (*ArmClient, error) {
rgc.Sender = autorest.CreateSender(withRequestLogging())
client.resourceGroupClient = rgc
pc := resources.NewProvidersClient(c.SubscriptionID)
pc.Authorizer = spt
pc.Sender = autorest.CreateSender(withRequestLogging())
client.providers = pc
tc := resources.NewTagsClient(c.SubscriptionID)
tc.Authorizer = spt
tc.Sender = autorest.CreateSender(withRequestLogging())

View File

@ -1,6 +1,8 @@
package azurerm
import (
"fmt"
"net/http"
"strings"
"github.com/hashicorp/terraform/helper/schema"
@ -70,9 +72,37 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return nil, err
}
err = registerAzureResourceProvidersWithSubscription(&config, client)
if err != nil {
return nil, err
}
return client, nil
}
// 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(config *Config, client *ArmClient) error {
providerClient := client.providers
providers := []string{"Microsoft.Network"}
for _, v := range providers {
res, err := providerClient.Register(v)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("Error registering provider %q with subscription %q", v, config.SubscriptionID)
}
}
return nil
}
func azureRMNormalizeLocation(location interface{}) string {
input := location.(string)
return strings.Replace(strings.ToLower(input), " ", "", -1)