github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/azurerm/provider.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/mutexkv"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  // Provider returns a terraform.ResourceProvider.
    14  func Provider() terraform.ResourceProvider {
    15  	return &schema.Provider{
    16  		Schema: map[string]*schema.Schema{
    17  			"subscription_id": &schema.Schema{
    18  				Type:        schema.TypeString,
    19  				Required:    true,
    20  				DefaultFunc: schema.EnvDefaultFunc("ARM_SUBSCRIPTION_ID", ""),
    21  			},
    22  
    23  			"client_id": &schema.Schema{
    24  				Type:        schema.TypeString,
    25  				Required:    true,
    26  				DefaultFunc: schema.EnvDefaultFunc("ARM_CLIENT_ID", ""),
    27  			},
    28  
    29  			"client_secret": &schema.Schema{
    30  				Type:        schema.TypeString,
    31  				Required:    true,
    32  				DefaultFunc: schema.EnvDefaultFunc("ARM_CLIENT_SECRET", ""),
    33  			},
    34  
    35  			"tenant_id": &schema.Schema{
    36  				Type:        schema.TypeString,
    37  				Required:    true,
    38  				DefaultFunc: schema.EnvDefaultFunc("ARM_TENANT_ID", ""),
    39  			},
    40  		},
    41  
    42  		ResourcesMap: map[string]*schema.Resource{
    43  			"azurerm_resource_group":         resourceArmResourceGroup(),
    44  			"azurerm_virtual_network":        resourceArmVirtualNetwork(),
    45  			"azurerm_local_network_gateway":  resourceArmLocalNetworkGateway(),
    46  			"azurerm_availability_set":       resourceArmAvailabilitySet(),
    47  			"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
    48  			"azurerm_network_security_rule":  resourceArmNetworkSecurityRule(),
    49  			"azurerm_public_ip":              resourceArmPublicIp(),
    50  			"azurerm_subnet":                 resourceArmSubnet(),
    51  			"azurerm_network_interface":      resourceArmNetworkInterface(),
    52  			"azurerm_route_table":            resourceArmRouteTable(),
    53  			"azurerm_route":                  resourceArmRoute(),
    54  		},
    55  		ConfigureFunc: providerConfigure,
    56  	}
    57  }
    58  
    59  // Config is the configuration structure used to instantiate a
    60  // new Azure management client.
    61  type Config struct {
    62  	ManagementURL string
    63  
    64  	SubscriptionID string
    65  	ClientID       string
    66  	ClientSecret   string
    67  	TenantID       string
    68  }
    69  
    70  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    71  	config := Config{
    72  		SubscriptionID: d.Get("subscription_id").(string),
    73  		ClientID:       d.Get("client_id").(string),
    74  		ClientSecret:   d.Get("client_secret").(string),
    75  		TenantID:       d.Get("tenant_id").(string),
    76  	}
    77  
    78  	client, err := config.getArmClient()
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	err = registerAzureResourceProvidersWithSubscription(&config, client)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	return client, nil
    89  }
    90  
    91  // registerAzureResourceProvidersWithSubscription uses the providers client to register
    92  // all Azure resource providers which the Terraform provider may require (regardless of
    93  // whether they are actually used by the configuration or not). It was confirmed by Microsoft
    94  // that this is the approach their own internal tools also take.
    95  func registerAzureResourceProvidersWithSubscription(config *Config, client *ArmClient) error {
    96  	providerClient := client.providers
    97  
    98  	providers := []string{"Microsoft.Network", "Microsoft.Compute"}
    99  
   100  	for _, v := range providers {
   101  		res, err := providerClient.Register(v)
   102  		if err != nil {
   103  			return err
   104  		}
   105  
   106  		if res.StatusCode != http.StatusOK {
   107  			return fmt.Errorf("Error registering provider %q with subscription %q", v, config.SubscriptionID)
   108  		}
   109  	}
   110  
   111  	return nil
   112  }
   113  
   114  func azureRMNormalizeLocation(location interface{}) string {
   115  	input := location.(string)
   116  	return strings.Replace(strings.ToLower(input), " ", "", -1)
   117  }
   118  
   119  // armMutexKV is the instance of MutexKV for ARM resources
   120  var armMutexKV = mutexkv.NewMutexKV()