2015-04-24 18:18:24 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2015-08-03 22:12:30 +02:00
|
|
|
"encoding/xml"
|
2015-05-29 00:10:21 +02:00
|
|
|
"fmt"
|
|
|
|
|
2015-11-12 22:51:39 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/pathorcontents"
|
2015-04-24 18:18:24 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Provider returns a terraform.ResourceProvider.
|
|
|
|
func Provider() terraform.ResourceProvider {
|
|
|
|
return &schema.Provider{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"settings_file": &schema.Schema{
|
2015-08-03 22:12:30 +02:00
|
|
|
Type: schema.TypeString,
|
2015-08-03 22:34:34 +02:00
|
|
|
Optional: true,
|
2015-08-03 22:12:30 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("AZURE_SETTINGS_FILE", nil),
|
|
|
|
ValidateFunc: validateSettingsFile,
|
2015-11-12 22:51:39 +01:00
|
|
|
Deprecated: "Use the publish_settings field instead",
|
|
|
|
},
|
|
|
|
|
|
|
|
"publish_settings": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("AZURE_PUBLISH_SETTINGS", nil),
|
|
|
|
ValidateFunc: validatePublishSettings,
|
2015-04-24 18:18:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"subscription_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("AZURE_SUBSCRIPTION_ID", ""),
|
|
|
|
},
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
"certificate": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("AZURE_CERTIFICATE", ""),
|
|
|
|
},
|
2015-04-24 18:18:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
2015-06-26 23:07:35 +02:00
|
|
|
"azure_instance": resourceAzureInstance(),
|
|
|
|
"azure_affinity_group": resourceAzureAffinityGroup(),
|
|
|
|
"azure_data_disk": resourceAzureDataDisk(),
|
|
|
|
"azure_sql_database_server": resourceAzureSqlDatabaseServer(),
|
|
|
|
"azure_sql_database_server_firewall_rule": resourceAzureSqlDatabaseServerFirewallRule(),
|
|
|
|
"azure_sql_database_service": resourceAzureSqlDatabaseService(),
|
|
|
|
"azure_hosted_service": resourceAzureHostedService(),
|
|
|
|
"azure_storage_service": resourceAzureStorageService(),
|
|
|
|
"azure_storage_container": resourceAzureStorageContainer(),
|
|
|
|
"azure_storage_blob": resourceAzureStorageBlob(),
|
|
|
|
"azure_storage_queue": resourceAzureStorageQueue(),
|
|
|
|
"azure_virtual_network": resourceAzureVirtualNetwork(),
|
|
|
|
"azure_dns_server": resourceAzureDnsServer(),
|
|
|
|
"azure_local_network_connection": resourceAzureLocalNetworkConnection(),
|
|
|
|
"azure_security_group": resourceAzureSecurityGroup(),
|
|
|
|
"azure_security_group_rule": resourceAzureSecurityGroupRule(),
|
2015-04-24 18:18:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ConfigureFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|
|
|
config := Config{
|
|
|
|
SubscriptionID: d.Get("subscription_id").(string),
|
2015-05-28 00:50:45 +02:00
|
|
|
Certificate: []byte(d.Get("certificate").(string)),
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
|
|
|
|
2015-11-12 22:51:39 +01:00
|
|
|
publishSettings := d.Get("publish_settings").(string)
|
|
|
|
if publishSettings == "" {
|
|
|
|
publishSettings = d.Get("settings_file").(string)
|
|
|
|
}
|
|
|
|
if publishSettings != "" {
|
2015-10-13 23:57:11 +02:00
|
|
|
// any errors from readSettings would have been caught at the validate
|
|
|
|
// step, so we can avoid handling them now
|
2015-11-12 22:51:39 +01:00
|
|
|
settings, _, _ := readSettings(publishSettings)
|
2015-10-13 23:57:11 +02:00
|
|
|
config.Settings = settings
|
2015-08-03 22:12:30 +02:00
|
|
|
return config.NewClientFromSettingsData()
|
2015-05-29 00:10:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.SubscriptionID != "" && len(config.Certificate) > 0 {
|
|
|
|
return config.NewClient()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Insufficient configuration data. Please specify either a 'settings_file'\n" +
|
|
|
|
"or both a 'subscription_id' and 'certificate'.")
|
2015-04-24 18:18:24 +02:00
|
|
|
}
|
2015-08-03 22:12:30 +02:00
|
|
|
|
2015-10-13 23:57:11 +02:00
|
|
|
func validateSettingsFile(v interface{}, k string) ([]string, []error) {
|
2015-08-03 22:12:30 +02:00
|
|
|
value := v.(string)
|
|
|
|
if value == "" {
|
2015-10-13 23:57:11 +02:00
|
|
|
return nil, nil
|
2015-08-03 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-13 23:57:11 +02:00
|
|
|
_, warnings, errors := readSettings(value)
|
|
|
|
return warnings, errors
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:51:39 +01:00
|
|
|
func validatePublishSettings(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if value == "" {
|
2015-08-03 22:12:30 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:51:39 +01:00
|
|
|
var settings settingsData
|
|
|
|
if err := xml.Unmarshal([]byte(value), &settings); err != nil {
|
|
|
|
es = append(es, fmt.Errorf("error parsing publish_settings as XML: %s", err))
|
2015-08-03 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:51:39 +01:00
|
|
|
const settingsPathWarnMsg = `
|
|
|
|
settings_file was provided as a file path. This support
|
|
|
|
will be removed in the future. Please update your configuration
|
|
|
|
to use ${file("filename.publishsettings")} instead.`
|
|
|
|
|
|
|
|
func readSettings(pathOrContents string) (s []byte, ws []string, es []error) {
|
|
|
|
contents, wasPath, err := pathorcontents.Read(pathOrContents)
|
|
|
|
if err != nil {
|
|
|
|
es = append(es, fmt.Errorf("error reading settings_file: %s", err))
|
|
|
|
}
|
|
|
|
if wasPath {
|
|
|
|
ws = append(ws, settingsPathWarnMsg)
|
2015-08-03 22:12:30 +02:00
|
|
|
}
|
2015-11-12 22:51:39 +01:00
|
|
|
|
|
|
|
var settings settingsData
|
|
|
|
if err := xml.Unmarshal([]byte(contents), &settings); err != nil {
|
|
|
|
es = append(es, fmt.Errorf("error parsing settings_file as XML: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
s = []byte(contents)
|
|
|
|
|
|
|
|
return
|
2015-08-03 22:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// settingsData is a private struct used to test the unmarshalling of the
|
|
|
|
// settingsFile contents, to determine if the contents are valid XML
|
|
|
|
type settingsData struct {
|
|
|
|
XMLName xml.Name `xml:"PublishData"`
|
|
|
|
}
|