github.com/joshgarnett/terraform@v0.5.4-0.20160219181435-92dc20bb3594/builtin/providers/google/provider.go (about) 1 package google 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/hashicorp/terraform/helper/pathorcontents" 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 // Provider returns a terraform.ResourceProvider. 13 func Provider() terraform.ResourceProvider { 14 return &schema.Provider{ 15 Schema: map[string]*schema.Schema{ 16 "account_file": &schema.Schema{ 17 Type: schema.TypeString, 18 Optional: true, 19 DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil), 20 ValidateFunc: validateAccountFile, 21 Deprecated: "Use the credentials field instead", 22 }, 23 24 "credentials": &schema.Schema{ 25 Type: schema.TypeString, 26 Optional: true, 27 DefaultFunc: schema.EnvDefaultFunc("GOOGLE_CREDENTIALS", nil), 28 ValidateFunc: validateCredentials, 29 }, 30 31 "project": &schema.Schema{ 32 Type: schema.TypeString, 33 Required: true, 34 DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", nil), 35 }, 36 37 "region": &schema.Schema{ 38 Type: schema.TypeString, 39 Required: true, 40 DefaultFunc: schema.EnvDefaultFunc("GOOGLE_REGION", nil), 41 }, 42 }, 43 44 ResourcesMap: map[string]*schema.Resource{ 45 "google_compute_autoscaler": resourceComputeAutoscaler(), 46 "google_compute_address": resourceComputeAddress(), 47 "google_compute_backend_service": resourceComputeBackendService(), 48 "google_compute_disk": resourceComputeDisk(), 49 "google_compute_firewall": resourceComputeFirewall(), 50 "google_compute_forwarding_rule": resourceComputeForwardingRule(), 51 "google_compute_global_address": resourceComputeGlobalAddress(), 52 "google_compute_global_forwarding_rule": resourceComputeGlobalForwardingRule(), 53 "google_compute_http_health_check": resourceComputeHttpHealthCheck(), 54 "google_compute_https_health_check": resourceComputeHttpsHealthCheck(), 55 "google_compute_instance": resourceComputeInstance(), 56 "google_compute_instance_group_manager": resourceComputeInstanceGroupManager(), 57 "google_compute_instance_template": resourceComputeInstanceTemplate(), 58 "google_compute_network": resourceComputeNetwork(), 59 "google_compute_project_metadata": resourceComputeProjectMetadata(), 60 "google_compute_route": resourceComputeRoute(), 61 "google_compute_ssl_certificate": resourceComputeSslCertificate(), 62 "google_compute_subnetwork": resourceComputeSubnetwork(), 63 "google_compute_target_http_proxy": resourceComputeTargetHttpProxy(), 64 "google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(), 65 "google_compute_target_pool": resourceComputeTargetPool(), 66 "google_compute_url_map": resourceComputeUrlMap(), 67 "google_compute_vpn_gateway": resourceComputeVpnGateway(), 68 "google_compute_vpn_tunnel": resourceComputeVpnTunnel(), 69 "google_container_cluster": resourceContainerCluster(), 70 "google_dns_managed_zone": resourceDnsManagedZone(), 71 "google_dns_record_set": resourceDnsRecordSet(), 72 "google_sql_database": resourceSqlDatabase(), 73 "google_sql_database_instance": resourceSqlDatabaseInstance(), 74 "google_sql_user": resourceSqlUser(), 75 "google_pubsub_topic": resourcePubsubTopic(), 76 "google_pubsub_subscription": resourcePubsubSubscription(), 77 "google_storage_bucket": resourceStorageBucket(), 78 "google_storage_bucket_acl": resourceStorageBucketAcl(), 79 "google_storage_bucket_object": resourceStorageBucketObject(), 80 "google_storage_object_acl": resourceStorageObjectAcl(), 81 }, 82 83 ConfigureFunc: providerConfigure, 84 } 85 } 86 87 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 88 credentials := d.Get("credentials").(string) 89 if credentials == "" { 90 credentials = d.Get("account_file").(string) 91 } 92 config := Config{ 93 Credentials: credentials, 94 Project: d.Get("project").(string), 95 Region: d.Get("region").(string), 96 } 97 98 if err := config.loadAndValidate(); err != nil { 99 return nil, err 100 } 101 102 return &config, nil 103 } 104 105 func validateAccountFile(v interface{}, k string) (warnings []string, errors []error) { 106 if v == nil { 107 return 108 } 109 110 value := v.(string) 111 112 if value == "" { 113 return 114 } 115 116 contents, wasPath, err := pathorcontents.Read(value) 117 if err != nil { 118 errors = append(errors, fmt.Errorf("Error loading Account File: %s", err)) 119 } 120 if wasPath { 121 warnings = append(warnings, `account_file was provided as a path instead of 122 as file contents. This support will be removed in the future. Please update 123 your configuration to use ${file("filename.json")} instead.`) 124 } 125 126 var account accountFile 127 if err := json.Unmarshal([]byte(contents), &account); err != nil { 128 errors = append(errors, 129 fmt.Errorf("account_file not valid JSON '%s': %s", contents, err)) 130 } 131 132 return 133 } 134 135 func validateCredentials(v interface{}, k string) (warnings []string, errors []error) { 136 if v == nil || v.(string) == "" { 137 return 138 } 139 creds := v.(string) 140 var account accountFile 141 if err := json.Unmarshal([]byte(creds), &account); err != nil { 142 errors = append(errors, 143 fmt.Errorf("credentials are not valid JSON '%s': %s", creds, err)) 144 } 145 146 return 147 } 148 149 // getRegionFromZone returns the region from a zone for Google cloud. 150 func getRegionFromZone(zone string) string { 151 if zone != "" && len(zone) > 2 { 152 region := zone[:len(zone)-2] 153 return region 154 } 155 return "" 156 }