github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/provider.go (about) 1 package google 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/pathorcontents" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 "google.golang.org/api/compute/v1" 12 "google.golang.org/api/googleapi" 13 ) 14 15 // Provider returns a terraform.ResourceProvider. 16 func Provider() terraform.ResourceProvider { 17 return &schema.Provider{ 18 Schema: map[string]*schema.Schema{ 19 "account_file": &schema.Schema{ 20 Type: schema.TypeString, 21 Optional: true, 22 DefaultFunc: schema.EnvDefaultFunc("GOOGLE_ACCOUNT_FILE", nil), 23 ValidateFunc: validateAccountFile, 24 Deprecated: "Use the credentials field instead", 25 }, 26 27 "credentials": &schema.Schema{ 28 Type: schema.TypeString, 29 Optional: true, 30 DefaultFunc: schema.MultiEnvDefaultFunc([]string{ 31 "GOOGLE_CREDENTIALS", 32 "GOOGLE_CLOUD_KEYFILE_JSON", 33 "GCLOUD_KEYFILE_JSON", 34 }, nil), 35 ValidateFunc: validateCredentials, 36 }, 37 38 "project": &schema.Schema{ 39 Type: schema.TypeString, 40 Optional: true, 41 DefaultFunc: schema.MultiEnvDefaultFunc([]string{ 42 "GOOGLE_PROJECT", 43 "GCLOUD_PROJECT", 44 "CLOUDSDK_CORE_PROJECT", 45 }, nil), 46 }, 47 48 "region": &schema.Schema{ 49 Type: schema.TypeString, 50 Required: true, 51 DefaultFunc: schema.MultiEnvDefaultFunc([]string{ 52 "GOOGLE_REGION", 53 "GCLOUD_REGION", 54 "CLOUDSDK_COMPUTE_REGION", 55 }, nil), 56 }, 57 }, 58 59 DataSourcesMap: map[string]*schema.Resource{ 60 "google_iam_policy": dataSourceGoogleIamPolicy(), 61 "google_compute_zones": dataSourceGoogleComputeZones(), 62 }, 63 64 ResourcesMap: map[string]*schema.Resource{ 65 "google_compute_autoscaler": resourceComputeAutoscaler(), 66 "google_compute_address": resourceComputeAddress(), 67 "google_compute_backend_service": resourceComputeBackendService(), 68 "google_compute_disk": resourceComputeDisk(), 69 "google_compute_firewall": resourceComputeFirewall(), 70 "google_compute_forwarding_rule": resourceComputeForwardingRule(), 71 "google_compute_global_address": resourceComputeGlobalAddress(), 72 "google_compute_global_forwarding_rule": resourceComputeGlobalForwardingRule(), 73 "google_compute_health_check": resourceComputeHealthCheck(), 74 "google_compute_http_health_check": resourceComputeHttpHealthCheck(), 75 "google_compute_https_health_check": resourceComputeHttpsHealthCheck(), 76 "google_compute_image": resourceComputeImage(), 77 "google_compute_instance": resourceComputeInstance(), 78 "google_compute_instance_group": resourceComputeInstanceGroup(), 79 "google_compute_instance_group_manager": resourceComputeInstanceGroupManager(), 80 "google_compute_instance_template": resourceComputeInstanceTemplate(), 81 "google_compute_network": resourceComputeNetwork(), 82 "google_compute_project_metadata": resourceComputeProjectMetadata(), 83 "google_compute_region_backend_service": resourceComputeRegionBackendService(), 84 "google_compute_route": resourceComputeRoute(), 85 "google_compute_ssl_certificate": resourceComputeSslCertificate(), 86 "google_compute_subnetwork": resourceComputeSubnetwork(), 87 "google_compute_target_http_proxy": resourceComputeTargetHttpProxy(), 88 "google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(), 89 "google_compute_target_pool": resourceComputeTargetPool(), 90 "google_compute_url_map": resourceComputeUrlMap(), 91 "google_compute_vpn_gateway": resourceComputeVpnGateway(), 92 "google_compute_vpn_tunnel": resourceComputeVpnTunnel(), 93 "google_container_cluster": resourceContainerCluster(), 94 "google_container_node_pool": resourceContainerNodePool(), 95 "google_dns_managed_zone": resourceDnsManagedZone(), 96 "google_dns_record_set": resourceDnsRecordSet(), 97 "google_sql_database": resourceSqlDatabase(), 98 "google_sql_database_instance": resourceSqlDatabaseInstance(), 99 "google_sql_user": resourceSqlUser(), 100 "google_project": resourceGoogleProject(), 101 "google_project_iam_policy": resourceGoogleProjectIamPolicy(), 102 "google_project_services": resourceGoogleProjectServices(), 103 "google_pubsub_topic": resourcePubsubTopic(), 104 "google_pubsub_subscription": resourcePubsubSubscription(), 105 "google_service_account": resourceGoogleServiceAccount(), 106 "google_storage_bucket": resourceStorageBucket(), 107 "google_storage_bucket_acl": resourceStorageBucketAcl(), 108 "google_storage_bucket_object": resourceStorageBucketObject(), 109 "google_storage_object_acl": resourceStorageObjectAcl(), 110 }, 111 112 ConfigureFunc: providerConfigure, 113 } 114 } 115 116 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 117 credentials := d.Get("credentials").(string) 118 if credentials == "" { 119 credentials = d.Get("account_file").(string) 120 } 121 config := Config{ 122 Credentials: credentials, 123 Project: d.Get("project").(string), 124 Region: d.Get("region").(string), 125 } 126 127 if err := config.loadAndValidate(); err != nil { 128 return nil, err 129 } 130 131 return &config, nil 132 } 133 134 func validateAccountFile(v interface{}, k string) (warnings []string, errors []error) { 135 if v == nil { 136 return 137 } 138 139 value := v.(string) 140 141 if value == "" { 142 return 143 } 144 145 contents, wasPath, err := pathorcontents.Read(value) 146 if err != nil { 147 errors = append(errors, fmt.Errorf("Error loading Account File: %s", err)) 148 } 149 if wasPath { 150 warnings = append(warnings, `account_file was provided as a path instead of 151 as file contents. This support will be removed in the future. Please update 152 your configuration to use ${file("filename.json")} instead.`) 153 } 154 155 var account accountFile 156 if err := json.Unmarshal([]byte(contents), &account); err != nil { 157 errors = append(errors, 158 fmt.Errorf("account_file not valid JSON '%s': %s", contents, err)) 159 } 160 161 return 162 } 163 164 func validateCredentials(v interface{}, k string) (warnings []string, errors []error) { 165 if v == nil || v.(string) == "" { 166 return 167 } 168 creds := v.(string) 169 var account accountFile 170 if err := json.Unmarshal([]byte(creds), &account); err != nil { 171 errors = append(errors, 172 fmt.Errorf("credentials are not valid JSON '%s': %s", creds, err)) 173 } 174 175 return 176 } 177 178 // getRegionFromZone returns the region from a zone for Google cloud. 179 func getRegionFromZone(zone string) string { 180 if zone != "" && len(zone) > 2 { 181 region := zone[:len(zone)-2] 182 return region 183 } 184 return "" 185 } 186 187 // getRegion reads the "region" field from the given resource data and falls 188 // back to the provider's value if not given. If the provider's value is not 189 // given, an error is returned. 190 func getRegion(d *schema.ResourceData, config *Config) (string, error) { 191 res, ok := d.GetOk("region") 192 if !ok { 193 if config.Region != "" { 194 return config.Region, nil 195 } 196 return "", fmt.Errorf("%q: required field is not set", "region") 197 } 198 return res.(string), nil 199 } 200 201 // getProject reads the "project" field from the given resource data and falls 202 // back to the provider's value if not given. If the provider's value is not 203 // given, an error is returned. 204 func getProject(d *schema.ResourceData, config *Config) (string, error) { 205 res, ok := d.GetOk("project") 206 if !ok { 207 if config.Project != "" { 208 return config.Project, nil 209 } 210 return "", fmt.Errorf("%q: required field is not set", "project") 211 } 212 return res.(string), nil 213 } 214 215 func getZonalResourceFromRegion(getResource func(string) (interface{}, error), region string, compute *compute.Service, project string) (interface{}, error) { 216 zoneList, err := compute.Zones.List(project).Do() 217 if err != nil { 218 return nil, err 219 } 220 var resource interface{} 221 for _, zone := range zoneList.Items { 222 if strings.Contains(zone.Name, region) { 223 resource, err = getResource(zone.Name) 224 if err != nil { 225 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 226 // Resource was not found in this zone 227 continue 228 } 229 return nil, fmt.Errorf("Error reading Resource: %s", err) 230 } 231 // Resource was found 232 return resource, nil 233 } 234 } 235 // Resource does not exist in this region 236 return nil, nil 237 } 238 239 // getNetworkLink reads the "network" field from the given resource data and if the value: 240 // - is a resource URL, returns the string unchanged 241 // - is the network name only, then looks up the resource URL using the google client 242 func getNetworkLink(d *schema.ResourceData, config *Config, field string) (string, error) { 243 if v, ok := d.GetOk(field); ok { 244 network := v.(string) 245 246 project, err := getProject(d, config) 247 if err != nil { 248 return "", err 249 } 250 251 if !strings.HasPrefix(network, "https://www.googleapis.com/compute/") { 252 // Network value provided is just the name, lookup the network SelfLink 253 networkData, err := config.clientCompute.Networks.Get( 254 project, network).Do() 255 if err != nil { 256 return "", fmt.Errorf("Error reading network: %s", err) 257 } 258 network = networkData.SelfLink 259 } 260 261 return network, nil 262 263 } else { 264 return "", nil 265 } 266 } 267 268 // getNetworkName reads the "network" field from the given resource data and if the value: 269 // - is a resource URL, extracts the network name from the URL and returns it 270 // - is the network name only (i.e not prefixed with http://www.googleapis.com/compute/...), is returned unchanged 271 func getNetworkName(d *schema.ResourceData, field string) (string, error) { 272 if v, ok := d.GetOk(field); ok { 273 network := v.(string) 274 275 if strings.HasPrefix(network, "https://www.googleapis.com/compute/") { 276 // extract the network name from SelfLink URL 277 networkName := network[strings.LastIndex(network, "/")+1:] 278 if networkName == "" { 279 return "", fmt.Errorf("network url not valid") 280 } 281 return networkName, nil 282 } 283 284 return network, nil 285 } 286 return "", nil 287 }