2014-08-25 20:48:20 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2015-07-27 21:35:52 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2015-11-12 23:13:07 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/pathorcontents"
|
2014-08-25 20:48:20 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-09-28 20:51:39 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-08-25 20:48:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Provider returns a terraform.ResourceProvider.
|
2014-09-28 20:51:39 +02:00
|
|
|
func Provider() terraform.ResourceProvider {
|
2014-08-25 20:48:20 +02:00
|
|
|
return &schema.Provider{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"account_file": &schema.Schema{
|
2015-07-27 21:35:52 +02:00
|
|
|
Type: schema.TypeString,
|
2015-10-19 21:27:41 +02:00
|
|
|
Optional: true,
|
2015-07-27 21:35:52 +02:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil),
|
|
|
|
ValidateFunc: validateAccountFile,
|
2015-11-12 23:13:07 +01:00
|
|
|
Deprecated: "Use the credentials field instead",
|
|
|
|
},
|
|
|
|
|
|
|
|
"credentials": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_CREDENTIALS", nil),
|
|
|
|
ValidateFunc: validateCredentials,
|
2015-07-23 22:53:44 +02:00
|
|
|
},
|
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
"project": &schema.Schema{
|
2014-11-20 11:25:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-01-16 18:22:09 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", nil),
|
2014-08-25 21:55:08 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"region": &schema.Schema{
|
2014-11-20 11:25:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-01-16 18:22:09 +01:00
|
|
|
DefaultFunc: schema.EnvDefaultFunc("GOOGLE_REGION", nil),
|
2014-08-25 21:55:08 +02:00
|
|
|
},
|
2014-08-25 20:48:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_autoscaler": resourceComputeAutoscaler(),
|
|
|
|
"google_compute_address": resourceComputeAddress(),
|
2015-08-20 21:52:30 +02:00
|
|
|
"google_compute_backend_service": resourceComputeBackendService(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_disk": resourceComputeDisk(),
|
|
|
|
"google_compute_firewall": resourceComputeFirewall(),
|
|
|
|
"google_compute_forwarding_rule": resourceComputeForwardingRule(),
|
2015-10-30 21:14:36 +01:00
|
|
|
"google_compute_global_address": resourceComputeGlobalAddress(),
|
2015-10-30 21:34:14 +01:00
|
|
|
"google_compute_global_forwarding_rule": resourceComputeGlobalForwardingRule(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_http_health_check": resourceComputeHttpHealthCheck(),
|
2015-11-12 16:48:26 +01:00
|
|
|
"google_compute_https_health_check": resourceComputeHttpsHealthCheck(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_instance": resourceComputeInstance(),
|
2015-10-30 22:27:12 +01:00
|
|
|
"google_compute_instance_group_manager": resourceComputeInstanceGroupManager(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_instance_template": resourceComputeInstanceTemplate(),
|
|
|
|
"google_compute_network": resourceComputeNetwork(),
|
2015-08-20 21:18:41 +02:00
|
|
|
"google_compute_project_metadata": resourceComputeProjectMetadata(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_route": resourceComputeRoute(),
|
2015-11-02 16:15:19 +01:00
|
|
|
"google_compute_ssl_certificate": resourceComputeSslCertificate(),
|
2016-02-14 23:27:17 +01:00
|
|
|
"google_compute_subnetwork": resourceComputeSubnetwork(),
|
2015-11-01 16:39:08 +01:00
|
|
|
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
|
2015-11-02 20:32:07 +01:00
|
|
|
"google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_target_pool": resourceComputeTargetPool(),
|
2015-10-30 22:27:12 +01:00
|
|
|
"google_compute_url_map": resourceComputeUrlMap(),
|
2015-09-04 22:54:18 +02:00
|
|
|
"google_compute_vpn_gateway": resourceComputeVpnGateway(),
|
|
|
|
"google_compute_vpn_tunnel": resourceComputeVpnTunnel(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_container_cluster": resourceContainerCluster(),
|
|
|
|
"google_dns_managed_zone": resourceDnsManagedZone(),
|
|
|
|
"google_dns_record_set": resourceDnsRecordSet(),
|
2015-10-23 16:10:41 +02:00
|
|
|
"google_sql_database": resourceSqlDatabase(),
|
|
|
|
"google_sql_database_instance": resourceSqlDatabaseInstance(),
|
2016-01-13 22:33:08 +01:00
|
|
|
"google_sql_user": resourceSqlUser(),
|
2015-12-11 18:59:13 +01:00
|
|
|
"google_pubsub_topic": resourcePubsubTopic(),
|
|
|
|
"google_pubsub_subscription": resourcePubsubSubscription(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_storage_bucket": resourceStorageBucket(),
|
2015-09-16 20:46:46 +02:00
|
|
|
"google_storage_bucket_acl": resourceStorageBucketAcl(),
|
2015-09-03 20:47:51 +02:00
|
|
|
"google_storage_bucket_object": resourceStorageBucketObject(),
|
2015-09-16 20:46:46 +02:00
|
|
|
"google_storage_object_acl": resourceStorageObjectAcl(),
|
2014-08-25 20:48:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ConfigureFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
2015-11-12 23:13:07 +01:00
|
|
|
credentials := d.Get("credentials").(string)
|
|
|
|
if credentials == "" {
|
|
|
|
credentials = d.Get("account_file").(string)
|
|
|
|
}
|
2014-08-25 20:48:20 +02:00
|
|
|
config := Config{
|
2015-11-12 23:13:07 +01:00
|
|
|
Credentials: credentials,
|
2015-07-27 21:35:52 +02:00
|
|
|
Project: d.Get("project").(string),
|
|
|
|
Region: d.Get("region").(string),
|
2014-08-25 20:48:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := config.loadAndValidate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
return &config, nil
|
2014-08-25 20:48:20 +02:00
|
|
|
}
|
2015-07-27 21:35:52 +02:00
|
|
|
|
|
|
|
func validateAccountFile(v interface{}, k string) (warnings []string, errors []error) {
|
2015-10-19 21:27:41 +02:00
|
|
|
if v == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:35:52 +02:00
|
|
|
value := v.(string)
|
|
|
|
|
|
|
|
if value == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-12 23:13:07 +01:00
|
|
|
contents, wasPath, err := pathorcontents.Read(value)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, fmt.Errorf("Error loading Account File: %s", err))
|
|
|
|
}
|
|
|
|
if wasPath {
|
|
|
|
warnings = append(warnings, `account_file was provided as a path instead of
|
|
|
|
as file contents. This support will be removed in the future. Please update
|
|
|
|
your configuration to use ${file("filename.json")} instead.`)
|
|
|
|
}
|
|
|
|
|
2015-07-27 21:35:52 +02:00
|
|
|
var account accountFile
|
2015-11-12 23:13:07 +01:00
|
|
|
if err := json.Unmarshal([]byte(contents), &account); err != nil {
|
|
|
|
errors = append(errors,
|
|
|
|
fmt.Errorf("account_file not valid JSON '%s': %s", contents, err))
|
2015-07-27 21:35:52 +02:00
|
|
|
}
|
|
|
|
|
2015-11-12 23:13:07 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateCredentials(v interface{}, k string) (warnings []string, errors []error) {
|
|
|
|
if v == nil || v.(string) == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
creds := v.(string)
|
|
|
|
var account accountFile
|
|
|
|
if err := json.Unmarshal([]byte(creds), &account); err != nil {
|
2015-07-27 23:07:38 +02:00
|
|
|
errors = append(errors,
|
2015-11-12 23:13:07 +01:00
|
|
|
fmt.Errorf("credentials are not valid JSON '%s': %s", creds, err))
|
2015-07-27 21:35:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2016-02-15 04:17:55 +01:00
|
|
|
|
2016-02-18 17:23:20 +01:00
|
|
|
// getRegionFromZone returns the region from a zone for Google cloud.
|
2016-02-15 04:17:55 +01:00
|
|
|
func getRegionFromZone(zone string) string {
|
|
|
|
if zone != "" && len(zone) > 2 {
|
|
|
|
region := zone[:len(zone)-2]
|
|
|
|
return region
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|