2015-12-08 23:30:12 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
2016-01-05 22:43:52 +01:00
|
|
|
"fmt"
|
2016-02-18 22:33:01 +01:00
|
|
|
"reflect"
|
2015-12-08 23:30:12 +01:00
|
|
|
"strings"
|
|
|
|
|
2016-06-11 00:37:14 +02:00
|
|
|
"sync"
|
|
|
|
|
2016-01-29 16:48:20 +01:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-01-08 16:55:46 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/mutexkv"
|
2016-02-18 22:33:01 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-12-08 23:30:12 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2016-02-18 22:33:01 +01:00
|
|
|
riviera "github.com/jen20/riviera/azure"
|
2015-12-08 23:30:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Provider returns a terraform.ResourceProvider.
|
|
|
|
func Provider() terraform.ResourceProvider {
|
|
|
|
return &schema.Provider{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-06-01 22:17:21 +02:00
|
|
|
"subscription_id": {
|
2015-12-08 23:30:12 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-09 02:25:05 +01:00
|
|
|
Required: true,
|
2015-12-09 00:50:48 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_SUBSCRIPTION_ID", ""),
|
2015-12-08 23:30:12 +01:00
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"client_id": {
|
2015-12-08 23:30:12 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-09 02:25:05 +01:00
|
|
|
Required: true,
|
2015-12-08 23:30:12 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_CLIENT_ID", ""),
|
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"client_secret": {
|
2015-12-08 23:30:12 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-09 02:25:05 +01:00
|
|
|
Required: true,
|
2015-12-08 23:30:12 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_CLIENT_SECRET", ""),
|
|
|
|
},
|
|
|
|
|
2016-06-01 22:17:21 +02:00
|
|
|
"tenant_id": {
|
2015-12-08 23:30:12 +01:00
|
|
|
Type: schema.TypeString,
|
2015-12-09 02:25:05 +01:00
|
|
|
Required: true,
|
2015-12-08 23:30:12 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("ARM_TENANT_ID", ""),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
2016-06-01 22:17:21 +02:00
|
|
|
// These resources use the Azure ARM SDK
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_availability_set": resourceArmAvailabilitySet(),
|
|
|
|
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
|
|
|
|
"azurerm_cdn_profile": resourceArmCdnProfile(),
|
|
|
|
"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
|
|
|
|
"azurerm_network_interface": resourceArmNetworkInterface(),
|
|
|
|
"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
|
|
|
|
"azurerm_network_security_rule": resourceArmNetworkSecurityRule(),
|
|
|
|
"azurerm_public_ip": resourceArmPublicIp(),
|
|
|
|
"azurerm_route": resourceArmRoute(),
|
|
|
|
"azurerm_route_table": resourceArmRouteTable(),
|
2016-08-15 19:00:00 +02:00
|
|
|
"azurerm_servicebus_namespace": resourceArmServiceBusNamespace(),
|
2016-10-03 12:22:18 +02:00
|
|
|
"azurerm_servicebus_subscription": resourceArmServiceBusSubscription(),
|
2016-09-29 19:07:25 +02:00
|
|
|
"azurerm_servicebus_topic": resourceArmServiceBusTopic(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_storage_account": resourceArmStorageAccount(),
|
|
|
|
"azurerm_storage_blob": resourceArmStorageBlob(),
|
|
|
|
"azurerm_storage_container": resourceArmStorageContainer(),
|
|
|
|
"azurerm_storage_queue": resourceArmStorageQueue(),
|
2016-07-27 23:49:43 +02:00
|
|
|
"azurerm_storage_table": resourceArmStorageTable(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_subnet": resourceArmSubnet(),
|
|
|
|
"azurerm_template_deployment": resourceArmTemplateDeployment(),
|
2016-08-01 00:46:15 +02:00
|
|
|
"azurerm_traffic_manager_endpoint": resourceArmTrafficManagerEndpoint(),
|
|
|
|
"azurerm_traffic_manager_profile": resourceArmTrafficManagerProfile(),
|
2016-06-11 00:37:14 +02:00
|
|
|
"azurerm_virtual_machine": resourceArmVirtualMachine(),
|
|
|
|
"azurerm_virtual_machine_scale_set": resourceArmVirtualMachineScaleSet(),
|
|
|
|
"azurerm_virtual_network": resourceArmVirtualNetwork(),
|
2016-08-13 20:37:46 +02:00
|
|
|
"azurerm_virtual_network_peering": resourceArmVirtualNetworkPeering(),
|
2016-06-01 22:17:21 +02:00
|
|
|
|
|
|
|
// These resources use the Riviera SDK
|
|
|
|
"azurerm_dns_a_record": resourceArmDnsARecord(),
|
|
|
|
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
|
|
|
|
"azurerm_dns_cname_record": resourceArmDnsCNameRecord(),
|
|
|
|
"azurerm_dns_mx_record": resourceArmDnsMxRecord(),
|
|
|
|
"azurerm_dns_ns_record": resourceArmDnsNsRecord(),
|
|
|
|
"azurerm_dns_srv_record": resourceArmDnsSrvRecord(),
|
|
|
|
"azurerm_dns_txt_record": resourceArmDnsTxtRecord(),
|
|
|
|
"azurerm_dns_zone": resourceArmDnsZone(),
|
|
|
|
"azurerm_resource_group": resourceArmResourceGroup(),
|
|
|
|
"azurerm_search_service": resourceArmSearchService(),
|
|
|
|
"azurerm_sql_database": resourceArmSqlDatabase(),
|
|
|
|
"azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(),
|
|
|
|
"azurerm_sql_server": resourceArmSqlServer(),
|
2015-12-08 23:30:12 +01:00
|
|
|
},
|
|
|
|
ConfigureFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the configuration structure used to instantiate a
|
|
|
|
// new Azure management client.
|
|
|
|
type Config struct {
|
|
|
|
ManagementURL string
|
|
|
|
|
|
|
|
SubscriptionID string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
TenantID string
|
2016-04-22 01:50:47 +02:00
|
|
|
|
|
|
|
validateCredentialsOnce sync.Once
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
2016-04-22 01:50:47 +02:00
|
|
|
func (c *Config) validate() error {
|
2016-01-29 16:48:20 +01:00
|
|
|
var err *multierror.Error
|
|
|
|
|
|
|
|
if c.SubscriptionID == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Subscription ID must be configured for the AzureRM provider"))
|
|
|
|
}
|
|
|
|
if c.ClientID == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Client ID must be configured for the AzureRM provider"))
|
|
|
|
}
|
|
|
|
if c.ClientSecret == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Client Secret must be configured for the AzureRM provider"))
|
|
|
|
}
|
|
|
|
if c.TenantID == "" {
|
|
|
|
err = multierror.Append(err, fmt.Errorf("Tenant ID must be configured for the AzureRM provider"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return err.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
2016-04-22 01:50:47 +02:00
|
|
|
config := &Config{
|
2015-12-08 23:30:12 +01:00
|
|
|
SubscriptionID: d.Get("subscription_id").(string),
|
|
|
|
ClientID: d.Get("client_id").(string),
|
|
|
|
ClientSecret: d.Get("client_secret").(string),
|
|
|
|
TenantID: d.Get("tenant_id").(string),
|
|
|
|
}
|
|
|
|
|
2016-01-29 16:48:20 +01:00
|
|
|
if err := config.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
client, err := config.getArmClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-22 01:50:47 +02:00
|
|
|
err = registerAzureResourceProvidersWithSubscription(client.rivieraClient)
|
2016-01-05 22:43:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2016-04-22 01:50:47 +02:00
|
|
|
func registerProviderWithSubscription(providerName string, client *riviera.Client) error {
|
|
|
|
request := client.NewRequest()
|
|
|
|
request.Command = riviera.RegisterResourceProvider{
|
|
|
|
Namespace: providerName,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := request.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot request provider registration for Azure Resource Manager: %s.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response.IsSuccessful() {
|
2016-09-16 17:09:20 +02:00
|
|
|
return fmt.Errorf("Credentials for accessing the Azure Resource Manager API are likely " +
|
2016-04-22 01:50:47 +02:00
|
|
|
"to be incorrect, or\n the service principal does not have permission to use " +
|
|
|
|
"the Azure Service Management\n API.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var providerRegistrationOnce sync.Once
|
|
|
|
|
2016-01-05 22:43:52 +01:00
|
|
|
// 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.
|
2016-04-22 01:50:47 +02:00
|
|
|
func registerAzureResourceProvidersWithSubscription(client *riviera.Client) error {
|
|
|
|
var err error
|
|
|
|
providerRegistrationOnce.Do(func() {
|
|
|
|
// We register Microsoft.Compute during client initialization
|
2016-08-15 19:00:00 +02:00
|
|
|
providers := []string{"Microsoft.Network", "Microsoft.Cdn", "Microsoft.Storage", "Microsoft.Sql", "Microsoft.Search", "Microsoft.Resources", "Microsoft.ServiceBus"}
|
2016-04-22 01:50:47 +02:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(len(providers))
|
|
|
|
for _, providerName := range providers {
|
|
|
|
go func(p string) {
|
|
|
|
defer wg.Done()
|
|
|
|
if innerErr := registerProviderWithSubscription(p, client); err != nil {
|
|
|
|
err = innerErr
|
|
|
|
}
|
|
|
|
}(providerName)
|
2016-01-05 22:43:52 +01:00
|
|
|
}
|
2016-04-22 01:50:47 +02:00
|
|
|
wg.Wait()
|
|
|
|
})
|
2016-01-05 22:43:52 +01:00
|
|
|
|
2016-04-22 01:50:47 +02:00
|
|
|
return err
|
2016-01-05 22:43:52 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 16:25:26 +01:00
|
|
|
// azureRMNormalizeLocation is a function which normalises human-readable region/location
|
|
|
|
// names (e.g. "West US") to the values used and returned by the Azure API (e.g. "westus").
|
|
|
|
// In state we track the API internal version as it is easier to go from the human form
|
|
|
|
// to the canonical form than the other way around.
|
2015-12-08 23:30:12 +01:00
|
|
|
func azureRMNormalizeLocation(location interface{}) string {
|
|
|
|
input := location.(string)
|
|
|
|
return strings.Replace(strings.ToLower(input), " ", "", -1)
|
|
|
|
}
|
2016-01-08 16:55:46 +01:00
|
|
|
|
|
|
|
// armMutexKV is the instance of MutexKV for ARM resources
|
|
|
|
var armMutexKV = mutexkv.NewMutexKV()
|
2016-02-18 22:33:01 +01:00
|
|
|
|
|
|
|
func azureStateRefreshFunc(resourceURI string, client *ArmClient, command riviera.APICall) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
req := client.rivieraClient.NewRequestForURI(resourceURI)
|
|
|
|
req.Command = command
|
|
|
|
|
|
|
|
res, err := req.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", fmt.Errorf("Error executing %T command in azureStateRefreshFunc", req.Command)
|
|
|
|
}
|
|
|
|
|
|
|
|
var value reflect.Value
|
|
|
|
if reflect.ValueOf(res.Parsed).Kind() == reflect.Ptr {
|
|
|
|
value = reflect.ValueOf(res.Parsed).Elem()
|
|
|
|
} else {
|
|
|
|
value = reflect.ValueOf(res.Parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < value.NumField(); i++ { // iterates through every struct type field
|
|
|
|
tag := value.Type().Field(i).Tag // returns the tag string
|
|
|
|
tagValue := tag.Get("mapstructure")
|
|
|
|
if tagValue == "provisioningState" {
|
|
|
|
return res.Parsed, value.Field(i).Elem().String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf("azureStateRefreshFunc called on structure %T with no mapstructure:provisioningState tag. This is a bug", res.Parsed))
|
|
|
|
}
|
|
|
|
}
|