2014-08-25 20:48:20 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2015-07-27 21:35:52 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-05-05 01:15:36 +02:00
|
|
|
"log"
|
2016-08-04 22:14:43 +02:00
|
|
|
"strings"
|
2015-07-27 21:35:52 +02:00
|
|
|
|
2017-01-27 15:32:42 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/mutexkv"
|
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"
|
2016-08-04 22:14:43 +02:00
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
2014-08-25 20:48:20 +02:00
|
|
|
)
|
|
|
|
|
2017-01-27 15:32:42 +01:00
|
|
|
// Global MutexKV
|
|
|
|
var mutexKV = mutexkv.NewMutexKV()
|
|
|
|
|
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{
|
2015-11-12 23:13:07 +01:00
|
|
|
"credentials": &schema.Schema{
|
2016-04-04 23:56:35 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
|
|
|
|
"GOOGLE_CREDENTIALS",
|
|
|
|
"GOOGLE_CLOUD_KEYFILE_JSON",
|
2016-04-11 01:31:40 +02:00
|
|
|
"GCLOUD_KEYFILE_JSON",
|
2016-04-04 23:56:35 +02:00
|
|
|
}, nil),
|
2015-11-12 23:13:07 +01:00
|
|
|
ValidateFunc: validateCredentials,
|
2015-07-23 22:53:44 +02:00
|
|
|
},
|
|
|
|
|
2014-08-25 21:55:08 +02:00
|
|
|
"project": &schema.Schema{
|
2016-04-11 01:31:40 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
|
|
|
|
"GOOGLE_PROJECT",
|
|
|
|
"GCLOUD_PROJECT",
|
|
|
|
"CLOUDSDK_CORE_PROJECT",
|
|
|
|
}, nil),
|
2014-08-25 21:55:08 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"region": &schema.Schema{
|
2016-04-11 01:31:40 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
|
|
|
|
"GOOGLE_REGION",
|
|
|
|
"GCLOUD_REGION",
|
|
|
|
"CLOUDSDK_COMPUTE_REGION",
|
|
|
|
}, nil),
|
2014-08-25 21:55:08 +02:00
|
|
|
},
|
2014-08-25 20:48:20 +02:00
|
|
|
},
|
|
|
|
|
2016-08-10 06:44:53 +02:00
|
|
|
DataSourcesMap: map[string]*schema.Resource{
|
2017-04-13 22:25:29 +02:00
|
|
|
"google_compute_network": dataSourceGoogleComputeNetwork(),
|
|
|
|
"google_compute_subnetwork": dataSourceGoogleComputeSubnetwork(),
|
|
|
|
"google_compute_zones": dataSourceGoogleComputeZones(),
|
|
|
|
"google_iam_policy": dataSourceGoogleIamPolicy(),
|
2016-08-10 06:44:53 +02:00
|
|
|
},
|
|
|
|
|
2014-08-25 20:48:20 +02:00
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
2017-04-20 18:47:38 +02:00
|
|
|
"google_bigquery_dataset": resourceBigQueryDataset(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_autoscaler": resourceComputeAutoscaler(),
|
|
|
|
"google_compute_address": resourceComputeAddress(),
|
2017-04-26 22:01:48 +02:00
|
|
|
"google_compute_backend_bucket": resourceComputeBackendBucket(),
|
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(),
|
2017-03-06 23:59:46 +01:00
|
|
|
"google_compute_snapshot": resourceComputeSnapshot(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"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(),
|
2016-11-18 17:08:26 +01:00
|
|
|
"google_compute_health_check": resourceComputeHealthCheck(),
|
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(),
|
2016-08-12 04:35:33 +02:00
|
|
|
"google_compute_image": resourceComputeImage(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_instance": resourceComputeInstance(),
|
2016-02-26 19:41:35 +01:00
|
|
|
"google_compute_instance_group": resourceComputeInstanceGroup(),
|
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(),
|
2016-11-18 17:08:26 +01:00
|
|
|
"google_compute_region_backend_service": resourceComputeRegionBackendService(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"google_compute_route": resourceComputeRoute(),
|
2017-01-27 15:32:42 +01:00
|
|
|
"google_compute_router": resourceComputeRouter(),
|
|
|
|
"google_compute_router_interface": resourceComputeRouterInterface(),
|
|
|
|
"google_compute_router_peer": resourceComputeRouterPeer(),
|
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(),
|
2017-03-06 23:59:24 +01:00
|
|
|
"google_container_node_pool": resourceContainerNodePool(),
|
2015-08-14 13:06:06 +02:00
|
|
|
"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(),
|
2016-08-10 06:44:53 +02:00
|
|
|
"google_project": resourceGoogleProject(),
|
2016-11-23 07:55:40 +01:00
|
|
|
"google_project_iam_policy": resourceGoogleProjectIamPolicy(),
|
|
|
|
"google_project_services": resourceGoogleProjectServices(),
|
2015-12-11 18:59:13 +01:00
|
|
|
"google_pubsub_topic": resourcePubsubTopic(),
|
|
|
|
"google_pubsub_subscription": resourcePubsubSubscription(),
|
2016-11-08 08:27:32 +01:00
|
|
|
"google_service_account": resourceGoogleServiceAccount(),
|
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)
|
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
|
|
|
|
2015-11-12 23:13:07 +01:00
|
|
|
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 ""
|
|
|
|
}
|
2016-04-10 18:59:57 +02:00
|
|
|
|
|
|
|
// getRegion reads the "region" field from the given resource data and falls
|
|
|
|
// back to the provider's value if not given. If the provider's value is not
|
|
|
|
// given, an error is returned.
|
|
|
|
func getRegion(d *schema.ResourceData, config *Config) (string, error) {
|
|
|
|
res, ok := d.GetOk("region")
|
|
|
|
if !ok {
|
|
|
|
if config.Region != "" {
|
|
|
|
return config.Region, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("%q: required field is not set", "region")
|
|
|
|
}
|
|
|
|
return res.(string), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getProject reads the "project" field from the given resource data and falls
|
|
|
|
// back to the provider's value if not given. If the provider's value is not
|
|
|
|
// given, an error is returned.
|
|
|
|
func getProject(d *schema.ResourceData, config *Config) (string, error) {
|
|
|
|
res, ok := d.GetOk("project")
|
|
|
|
if !ok {
|
|
|
|
if config.Project != "" {
|
|
|
|
return config.Project, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("%q: required field is not set", "project")
|
|
|
|
}
|
|
|
|
return res.(string), nil
|
|
|
|
}
|
2016-08-04 22:14:43 +02:00
|
|
|
|
|
|
|
func getZonalResourceFromRegion(getResource func(string) (interface{}, error), region string, compute *compute.Service, project string) (interface{}, error) {
|
|
|
|
zoneList, err := compute.Zones.List(project).Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var resource interface{}
|
|
|
|
for _, zone := range zoneList.Items {
|
|
|
|
if strings.Contains(zone.Name, region) {
|
|
|
|
resource, err = getResource(zone.Name)
|
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
// Resource was not found in this zone
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Error reading Resource: %s", err)
|
|
|
|
}
|
|
|
|
// Resource was found
|
|
|
|
return resource, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Resource does not exist in this region
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-09-03 11:51:20 +02:00
|
|
|
|
|
|
|
// getNetworkLink reads the "network" field from the given resource data and if the value:
|
|
|
|
// - is a resource URL, returns the string unchanged
|
|
|
|
// - is the network name only, then looks up the resource URL using the google client
|
|
|
|
func getNetworkLink(d *schema.ResourceData, config *Config, field string) (string, error) {
|
|
|
|
if v, ok := d.GetOk(field); ok {
|
|
|
|
network := v.(string)
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(network, "https://www.googleapis.com/compute/") {
|
|
|
|
// Network value provided is just the name, lookup the network SelfLink
|
|
|
|
networkData, err := config.clientCompute.Networks.Get(
|
|
|
|
project, network).Do()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error reading network: %s", err)
|
|
|
|
}
|
|
|
|
network = networkData.SelfLink
|
|
|
|
}
|
|
|
|
|
|
|
|
return network, nil
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNetworkName reads the "network" field from the given resource data and if the value:
|
|
|
|
// - is a resource URL, extracts the network name from the URL and returns it
|
|
|
|
// - is the network name only (i.e not prefixed with http://www.googleapis.com/compute/...), is returned unchanged
|
|
|
|
func getNetworkName(d *schema.ResourceData, field string) (string, error) {
|
|
|
|
if v, ok := d.GetOk(field); ok {
|
|
|
|
network := v.(string)
|
2017-03-15 01:50:58 +01:00
|
|
|
return getNetworkNameFromSelfLink(network)
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
2016-09-03 11:51:20 +02:00
|
|
|
|
2017-03-15 01:50:58 +01:00
|
|
|
func getNetworkNameFromSelfLink(network string) (string, error) {
|
|
|
|
if strings.HasPrefix(network, "https://www.googleapis.com/compute/") {
|
|
|
|
// extract the network name from SelfLink URL
|
|
|
|
networkName := network[strings.LastIndex(network, "/")+1:]
|
|
|
|
if networkName == "" {
|
|
|
|
return "", fmt.Errorf("network url not valid")
|
2016-09-03 11:51:20 +02:00
|
|
|
}
|
2017-03-15 01:50:58 +01:00
|
|
|
return networkName, nil
|
2016-09-03 11:51:20 +02:00
|
|
|
}
|
2017-03-15 01:50:58 +01:00
|
|
|
|
|
|
|
return network, nil
|
2016-09-03 11:51:20 +02:00
|
|
|
}
|
2017-04-28 21:17:08 +02:00
|
|
|
|
|
|
|
func getRouterLockName(region string, router string) string {
|
|
|
|
return fmt.Sprintf("router/%s/%s", region, router)
|
|
|
|
}
|
2017-05-05 11:43:41 +02:00
|
|
|
|
2017-05-05 01:15:36 +02:00
|
|
|
func handleNotFoundError(err error, d *schema.ResourceData, resource string) error {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
log.Printf("[WARN] Removing %s because it's gone", resource)
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading %s: %s", resource, err)
|
|
|
|
}
|