From bacf5abf3cbf853868ce04b7da1ad644b66f8f40 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 10 Apr 2016 12:59:57 -0400 Subject: [PATCH] Accept "project" as an attribute to GCP resources This is the first step in removing the config dependency on "project". This change is backwards-compatible because the value for this new attribute defaults to the value from the provider. --- builtin/providers/google/provider.go | 32 ++++++- .../google/resource_compute_address.go | 52 ++++++++--- .../google/resource_compute_autoscaler.go | 37 ++++++-- .../resource_compute_backend_service.go | 34 +++++++- .../providers/google/resource_compute_disk.go | 31 +++++-- .../google/resource_compute_firewall.go | 38 ++++++-- .../resource_compute_forwarding_rule.go | 66 +++++++++++--- .../google/resource_compute_global_address.go | 27 +++++- ...resource_compute_global_forwarding_rule.go | 34 +++++++- .../resource_compute_http_health_check.go | 34 +++++++- .../resource_compute_https_health_check.go | 34 +++++++- .../google/resource_compute_instance.go | 54 +++++++++--- .../google/resource_compute_instance_group.go | 44 ++++++++-- ...resource_compute_instance_group_manager.go | 46 +++++++--- .../resource_compute_instance_template.go | 50 ++++++++--- .../google/resource_compute_network.go | 27 +++++- .../resource_compute_project_metadata.go | 62 +++++++++---- .../google/resource_compute_route.go | 31 +++++-- .../resource_compute_ssl_certificate.go | 27 +++++- .../google/resource_compute_subnetwork.go | 32 ++++++- .../resource_compute_target_http_proxy.go | 34 +++++++- .../resource_compute_target_https_proxy.go | 38 ++++++-- .../google/resource_compute_target_pool.go | 86 ++++++++++++++----- .../google/resource_compute_url_map.go | 37 ++++++-- .../google/resource_compute_vpn_gateway.go | 45 ++++++++-- .../google/resource_compute_vpn_tunnel.go | 51 ++++++++--- .../google/resource_container_cluster.go | 40 +++++++-- .../google/resource_dns_managed_zone.go | 27 +++++- .../google/resource_dns_record_set.go | 31 +++++-- .../google/resource_pubsub_subscription.go | 15 +++- .../providers/google/resource_pubsub_topic.go | 13 ++- .../providers/google/resource_sql_database.go | 23 ++++- .../google/resource_sql_database_instance.go | 37 ++++++-- builtin/providers/google/resource_sql_user.go | 30 ++++++- .../google/resource_storage_bucket.go | 12 ++- 35 files changed, 1081 insertions(+), 230 deletions(-) diff --git a/builtin/providers/google/provider.go b/builtin/providers/google/provider.go index 7af7e628c..8fd5339f5 100644 --- a/builtin/providers/google/provider.go +++ b/builtin/providers/google/provider.go @@ -33,8 +33,8 @@ func Provider() terraform.ResourceProvider { "project": &schema.Schema{ Type: schema.TypeString, - Required: false, - DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", nil), + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("GOOGLE_PROJECT", ""), }, "region": &schema.Schema{ @@ -158,3 +158,31 @@ func getRegionFromZone(zone string) string { } return "" } + +// 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 +} diff --git a/builtin/providers/google/resource_compute_address.go b/builtin/providers/google/resource_compute_address.go index 15fa13272..4567e4280 100644 --- a/builtin/providers/google/resource_compute_address.go +++ b/builtin/providers/google/resource_compute_address.go @@ -37,26 +37,33 @@ func resourceComputeAddress() *schema.Resource { Optional: true, ForceNew: true, }, - }, - } -} -func getOptionalRegion(d *schema.ResourceData, config *Config) string { - if res, ok := d.GetOk("region"); !ok { - return config.Region - } else { - return res.(string) + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + }, } } func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } // Build the address parameter addr := &compute.Address{Name: d.Get("name").(string)} op, err := config.clientCompute.Addresses.Insert( - config.Project, region, addr).Do() + project, region, addr).Do() if err != nil { return fmt.Errorf("Error creating address: %s", err) } @@ -75,10 +82,18 @@ func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) erro func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } addr, err := config.clientCompute.Addresses.Get( - config.Project, region, d.Id()).Do() + project, region, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore @@ -100,11 +115,20 @@ func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error func resourceComputeAddressDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the address log.Printf("[DEBUG] address delete request") op, err := config.clientCompute.Addresses.Delete( - config.Project, region, d.Id()).Do() + project, region, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting address: %s", err) } diff --git a/builtin/providers/google/resource_compute_autoscaler.go b/builtin/providers/google/resource_compute_autoscaler.go index 89cc41b07..7fd8819de 100644 --- a/builtin/providers/google/resource_compute_autoscaler.go +++ b/builtin/providers/google/resource_compute_autoscaler.go @@ -115,12 +115,17 @@ func resourceComputeAutoscaler() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } func buildAutoscaler(d *schema.ResourceData) (*compute.Autoscaler, error) { - // Build the parameter scaler := &compute.Autoscaler{ Name: d.Get("name").(string), @@ -200,10 +205,15 @@ func buildAutoscaler(d *schema.ResourceData) (*compute.Autoscaler, error) { func resourceComputeAutoscalerCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Get the zone log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string)) zone, err := config.clientCompute.Zones.Get( - config.Project, d.Get("zone").(string)).Do() + project, d.Get("zone").(string)).Do() if err != nil { return fmt.Errorf( "Error loading zone '%s': %s", d.Get("zone").(string), err) @@ -215,7 +225,7 @@ func resourceComputeAutoscalerCreate(d *schema.ResourceData, meta interface{}) e } op, err := config.clientCompute.Autoscalers.Insert( - config.Project, zone.Name, scaler).Do() + project, zone.Name, scaler).Do() if err != nil { return fmt.Errorf("Error creating Autoscaler: %s", err) } @@ -234,9 +244,14 @@ func resourceComputeAutoscalerCreate(d *schema.ResourceData, meta interface{}) e func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("zone").(string) scaler, err := config.clientCompute.Autoscalers.Get( - config.Project, zone, d.Id()).Do() + project, zone, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore @@ -257,6 +272,11 @@ func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) err func resourceComputeAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("zone").(string) scaler, err := buildAutoscaler(d) @@ -265,7 +285,7 @@ func resourceComputeAutoscalerUpdate(d *schema.ResourceData, meta interface{}) e } op, err := config.clientCompute.Autoscalers.Patch( - config.Project, zone, d.Id(), scaler).Do() + project, zone, d.Id(), scaler).Do() if err != nil { return fmt.Errorf("Error updating Autoscaler: %s", err) } @@ -284,9 +304,14 @@ func resourceComputeAutoscalerUpdate(d *schema.ResourceData, meta interface{}) e func resourceComputeAutoscalerDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("zone").(string) op, err := config.clientCompute.Autoscalers.Delete( - config.Project, zone, d.Id()).Do() + project, zone, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting autoscaler: %s", err) } diff --git a/builtin/providers/google/resource_compute_backend_service.go b/builtin/providers/google/resource_compute_backend_service.go index 2159073c2..f04024781 100644 --- a/builtin/providers/google/resource_compute_backend_service.go +++ b/builtin/providers/google/resource_compute_backend_service.go @@ -121,6 +121,12 @@ func resourceComputeBackendService() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -159,9 +165,14 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{ service.TimeoutSec = int64(v.(int)) } + project, err := getProject(d, config) + if err != nil { + return err + } + log.Printf("[DEBUG] Creating new Backend Service: %#v", service) op, err := config.clientCompute.BackendServices.Insert( - config.Project, &service).Do() + project, &service).Do() if err != nil { return fmt.Errorf("Error creating backend service: %s", err) } @@ -181,8 +192,13 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + service, err := config.clientCompute.BackendServices.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore @@ -211,6 +227,11 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + hc := d.Get("health_checks").(*schema.Set).List() healthChecks := make([]string, 0, len(hc)) for _, v := range hc { @@ -241,7 +262,7 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{ log.Printf("[DEBUG] Updating existing Backend Service %q: %#v", d.Id(), service) op, err := config.clientCompute.BackendServices.Update( - config.Project, d.Id(), &service).Do() + project, d.Id(), &service).Do() if err != nil { return fmt.Errorf("Error updating backend service: %s", err) } @@ -259,9 +280,14 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{ func resourceComputeBackendServiceDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + log.Printf("[DEBUG] Deleting backend service %s", d.Id()) op, err := config.clientCompute.BackendServices.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting backend service: %s", err) } diff --git a/builtin/providers/google/resource_compute_disk.go b/builtin/providers/google/resource_compute_disk.go index 1df66b9bb..62d0ea3e1 100644 --- a/builtin/providers/google/resource_compute_disk.go +++ b/builtin/providers/google/resource_compute_disk.go @@ -56,6 +56,12 @@ func resourceComputeDisk() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -63,10 +69,15 @@ func resourceComputeDisk() *schema.Resource { func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Get the zone log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string)) zone, err := config.clientCompute.Zones.Get( - config.Project, d.Get("zone").(string)).Do() + project, d.Get("zone").(string)).Do() if err != nil { return fmt.Errorf( "Error loading zone '%s': %s", d.Get("zone").(string), err) @@ -107,7 +118,7 @@ func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { snapshotName := v.(string) log.Printf("[DEBUG] Loading snapshot: %s", snapshotName) snapshotData, err := config.clientCompute.Snapshots.Get( - config.Project, snapshotName).Do() + project, snapshotName).Do() if err != nil { return fmt.Errorf( @@ -119,7 +130,7 @@ func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { } op, err := config.clientCompute.Disks.Insert( - config.Project, d.Get("zone").(string), disk).Do() + project, d.Get("zone").(string), disk).Do() if err != nil { return fmt.Errorf("Error creating disk: %s", err) } @@ -137,8 +148,13 @@ func resourceComputeDiskCreate(d *schema.ResourceData, meta interface{}) error { func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + disk, err := config.clientCompute.Disks.Get( - config.Project, d.Get("zone").(string), d.Id()).Do() + project, d.Get("zone").(string), d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Disk %q because it's gone", d.Get("name").(string)) @@ -159,9 +175,14 @@ func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error { func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the disk op, err := config.clientCompute.Disks.Delete( - config.Project, d.Get("zone").(string), d.Id()).Do() + project, d.Get("zone").(string), d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting disk: %s", err) } diff --git a/builtin/providers/google/resource_compute_firewall.go b/builtin/providers/google/resource_compute_firewall.go index 3d5d8e595..1676b22a6 100644 --- a/builtin/providers/google/resource_compute_firewall.go +++ b/builtin/providers/google/resource_compute_firewall.go @@ -83,6 +83,12 @@ func resourceComputeFirewall() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -113,13 +119,18 @@ func resourceComputeFirewallAllowHash(v interface{}) int { func resourceComputeFirewallCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + firewall, err := resourceFirewall(d, meta) if err != nil { return err } op, err := config.clientCompute.Firewalls.Insert( - config.Project, firewall).Do() + project, firewall).Do() if err != nil { return fmt.Errorf("Error creating firewall: %s", err) } @@ -138,8 +149,13 @@ func resourceComputeFirewallCreate(d *schema.ResourceData, meta interface{}) err func resourceComputeFirewallRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + firewall, err := config.clientCompute.Firewalls.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore @@ -160,6 +176,11 @@ func resourceComputeFirewallRead(d *schema.ResourceData, meta interface{}) error func resourceComputeFirewallUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + d.Partial(true) firewall, err := resourceFirewall(d, meta) @@ -168,7 +189,7 @@ func resourceComputeFirewallUpdate(d *schema.ResourceData, meta interface{}) err } op, err := config.clientCompute.Firewalls.Update( - config.Project, d.Id(), firewall).Do() + project, d.Id(), firewall).Do() if err != nil { return fmt.Errorf("Error updating firewall: %s", err) } @@ -186,9 +207,14 @@ func resourceComputeFirewallUpdate(d *schema.ResourceData, meta interface{}) err func resourceComputeFirewallDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the firewall op, err := config.clientCompute.Firewalls.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting firewall: %s", err) } @@ -207,9 +233,11 @@ func resourceFirewall( meta interface{}) (*compute.Firewall, error) { config := meta.(*Config) + project, _ := getProject(d, config) + // Look up the network to attach the firewall to network, err := config.clientCompute.Networks.Get( - config.Project, d.Get("network").(string)).Do() + project, d.Get("network").(string)).Do() if err != nil { return nil, fmt.Errorf("Error reading network: %s", err) } diff --git a/builtin/providers/google/resource_compute_forwarding_rule.go b/builtin/providers/google/resource_compute_forwarding_rule.go index e1cbdc46c..0f7162737 100644 --- a/builtin/providers/google/resource_compute_forwarding_rule.go +++ b/builtin/providers/google/resource_compute_forwarding_rule.go @@ -49,12 +49,6 @@ func resourceComputeForwardingRule() *schema.Resource { ForceNew: true, }, - "region": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "self_link": &schema.Schema{ Type: schema.TypeString, Computed: true, @@ -65,6 +59,18 @@ func resourceComputeForwardingRule() *schema.Resource { Required: true, ForceNew: false, }, + + "region": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -72,7 +78,15 @@ func resourceComputeForwardingRule() *schema.Resource { func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } frule := &compute.ForwardingRule{ IPAddress: d.Get("ip_address").(string), @@ -85,7 +99,7 @@ func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{ log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule) op, err := config.clientCompute.ForwardingRules.Insert( - config.Project, region, frule).Do() + project, region, frule).Do() if err != nil { return fmt.Errorf("Error creating ForwardingRule: %s", err) } @@ -104,7 +118,15 @@ func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{ func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } d.Partial(true) @@ -112,7 +134,7 @@ func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{ target_name := d.Get("target").(string) target_ref := &compute.TargetReference{Target: target_name} op, err := config.clientCompute.ForwardingRules.SetTarget( - config.Project, region, d.Id(), target_ref).Do() + project, region, d.Id(), target_ref).Do() if err != nil { return fmt.Errorf("Error updating target: %s", err) } @@ -133,10 +155,18 @@ func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{ func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } frule, err := config.clientCompute.ForwardingRules.Get( - config.Project, region, d.Id()).Do() + project, region, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Forwarding Rule %q because it's gone", d.Get("name").(string)) @@ -159,12 +189,20 @@ func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } // Delete the ForwardingRule log.Printf("[DEBUG] ForwardingRule delete request") op, err := config.clientCompute.ForwardingRules.Delete( - config.Project, region, d.Id()).Do() + project, region, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting ForwardingRule: %s", err) } diff --git a/builtin/providers/google/resource_compute_global_address.go b/builtin/providers/google/resource_compute_global_address.go index 58d3f5e8e..554902239 100644 --- a/builtin/providers/google/resource_compute_global_address.go +++ b/builtin/providers/google/resource_compute_global_address.go @@ -31,6 +31,12 @@ func resourceComputeGlobalAddress() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -38,10 +44,15 @@ func resourceComputeGlobalAddress() *schema.Resource { func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the address parameter addr := &compute.Address{Name: d.Get("name").(string)} op, err := config.clientCompute.GlobalAddresses.Insert( - config.Project, addr).Do() + project, addr).Do() if err != nil { return fmt.Errorf("Error creating address: %s", err) } @@ -60,8 +71,13 @@ func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{} func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + addr, err := config.clientCompute.GlobalAddresses.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Global Address %q because it's gone", d.Get("name").(string)) @@ -83,10 +99,15 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the address log.Printf("[DEBUG] address delete request") op, err := config.clientCompute.GlobalAddresses.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting address: %s", err) } diff --git a/builtin/providers/google/resource_compute_global_forwarding_rule.go b/builtin/providers/google/resource_compute_global_forwarding_rule.go index dc7a852c7..5c41675e1 100644 --- a/builtin/providers/google/resource_compute_global_forwarding_rule.go +++ b/builtin/providers/google/resource_compute_global_forwarding_rule.go @@ -65,6 +65,12 @@ func resourceComputeGlobalForwardingRule() *schema.Resource { ForceNew: true, Deprecated: "Please remove this attribute (it was never used)", }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -72,6 +78,11 @@ func resourceComputeGlobalForwardingRule() *schema.Resource { func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + frule := &compute.ForwardingRule{ IPAddress: d.Get("ip_address").(string), IPProtocol: d.Get("ip_protocol").(string), @@ -82,7 +93,7 @@ func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta inte } op, err := config.clientCompute.GlobalForwardingRules.Insert( - config.Project, frule).Do() + project, frule).Do() if err != nil { return fmt.Errorf("Error creating Global Forwarding Rule: %s", err) } @@ -101,13 +112,18 @@ func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta inte func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + d.Partial(true) if d.HasChange("target") { target_name := d.Get("target").(string) target_ref := &compute.TargetReference{Target: target_name} op, err := config.clientCompute.GlobalForwardingRules.SetTarget( - config.Project, d.Id(), target_ref).Do() + project, d.Id(), target_ref).Do() if err != nil { return fmt.Errorf("Error updating target: %s", err) } @@ -128,8 +144,13 @@ func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta inte func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + frule, err := config.clientCompute.GlobalForwardingRules.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Global Forwarding Rule %q because it's gone", d.Get("name").(string)) @@ -152,10 +173,15 @@ func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interf func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the GlobalForwardingRule log.Printf("[DEBUG] GlobalForwardingRule delete request") op, err := config.clientCompute.GlobalForwardingRules.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err) } diff --git a/builtin/providers/google/resource_compute_http_health_check.go b/builtin/providers/google/resource_compute_http_health_check.go index 8ddae0b70..0d8eaed0b 100644 --- a/builtin/providers/google/resource_compute_http_health_check.go +++ b/builtin/providers/google/resource_compute_http_health_check.go @@ -73,6 +73,12 @@ func resourceComputeHttpHealthCheck() *schema.Resource { Optional: true, Default: 2, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -80,6 +86,11 @@ func resourceComputeHttpHealthCheck() *schema.Resource { func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the parameter hchk := &compute.HttpHealthCheck{ Name: d.Get("name").(string), @@ -112,7 +123,7 @@ func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface log.Printf("[DEBUG] HttpHealthCheck insert request: %#v", hchk) op, err := config.clientCompute.HttpHealthChecks.Insert( - config.Project, hchk).Do() + project, hchk).Do() if err != nil { return fmt.Errorf("Error creating HttpHealthCheck: %s", err) } @@ -131,6 +142,11 @@ func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the parameter hchk := &compute.HttpHealthCheck{ Name: d.Get("name").(string), @@ -163,7 +179,7 @@ func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface log.Printf("[DEBUG] HttpHealthCheck patch request: %#v", hchk) op, err := config.clientCompute.HttpHealthChecks.Patch( - config.Project, hchk.Name, hchk).Do() + project, hchk.Name, hchk).Do() if err != nil { return fmt.Errorf("Error patching HttpHealthCheck: %s", err) } @@ -182,8 +198,13 @@ func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface func resourceComputeHttpHealthCheckRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + hchk, err := config.clientCompute.HttpHealthChecks.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore @@ -211,9 +232,14 @@ func resourceComputeHttpHealthCheckRead(d *schema.ResourceData, meta interface{} func resourceComputeHttpHealthCheckDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the HttpHealthCheck op, err := config.clientCompute.HttpHealthChecks.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting HttpHealthCheck: %s", err) } diff --git a/builtin/providers/google/resource_compute_https_health_check.go b/builtin/providers/google/resource_compute_https_health_check.go index 46affdd9e..64b50483d 100644 --- a/builtin/providers/google/resource_compute_https_health_check.go +++ b/builtin/providers/google/resource_compute_https_health_check.go @@ -73,6 +73,12 @@ func resourceComputeHttpsHealthCheck() *schema.Resource { Optional: true, Default: 2, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -80,6 +86,11 @@ func resourceComputeHttpsHealthCheck() *schema.Resource { func resourceComputeHttpsHealthCheckCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the parameter hchk := &compute.HttpsHealthCheck{ Name: d.Get("name").(string), @@ -112,7 +123,7 @@ func resourceComputeHttpsHealthCheckCreate(d *schema.ResourceData, meta interfac log.Printf("[DEBUG] HttpsHealthCheck insert request: %#v", hchk) op, err := config.clientCompute.HttpsHealthChecks.Insert( - config.Project, hchk).Do() + project, hchk).Do() if err != nil { return fmt.Errorf("Error creating HttpsHealthCheck: %s", err) } @@ -131,6 +142,11 @@ func resourceComputeHttpsHealthCheckCreate(d *schema.ResourceData, meta interfac func resourceComputeHttpsHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the parameter hchk := &compute.HttpsHealthCheck{ Name: d.Get("name").(string), @@ -163,7 +179,7 @@ func resourceComputeHttpsHealthCheckUpdate(d *schema.ResourceData, meta interfac log.Printf("[DEBUG] HttpsHealthCheck patch request: %#v", hchk) op, err := config.clientCompute.HttpsHealthChecks.Patch( - config.Project, hchk.Name, hchk).Do() + project, hchk.Name, hchk).Do() if err != nil { return fmt.Errorf("Error patching HttpsHealthCheck: %s", err) } @@ -182,8 +198,13 @@ func resourceComputeHttpsHealthCheckUpdate(d *schema.ResourceData, meta interfac func resourceComputeHttpsHealthCheckRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + hchk, err := config.clientCompute.HttpsHealthChecks.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing HTTPS Health Check %q because it's gone", d.Get("name").(string)) @@ -211,9 +232,14 @@ func resourceComputeHttpsHealthCheckRead(d *schema.ResourceData, meta interface{ func resourceComputeHttpsHealthCheckDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the HttpsHealthCheck op, err := config.clientCompute.HttpsHealthChecks.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting HttpsHealthCheck: %s", err) } diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 4c4632125..a50e1c105 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -281,13 +281,24 @@ func resourceComputeInstance() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } func getInstance(config *Config, d *schema.ResourceData) (*compute.Instance, error) { + project, err := getProject(d, config) + if err != nil { + return nil, err + } + instance, err := config.clientCompute.Instances.Get( - config.Project, d.Get("zone").(string), d.Id()).Do() + project, d.Get("zone").(string), d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Instance %q because it's gone", d.Get("name").(string)) @@ -307,10 +318,15 @@ func getInstance(config *Config, d *schema.ResourceData) (*compute.Instance, err func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Get the zone log.Printf("[DEBUG] Loading zone: %s", d.Get("zone").(string)) zone, err := config.clientCompute.Zones.Get( - config.Project, d.Get("zone").(string)).Do() + project, d.Get("zone").(string)).Do() if err != nil { return fmt.Errorf( "Error loading zone '%s': %s", d.Get("zone").(string), err) @@ -319,7 +335,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err // Get the machine type log.Printf("[DEBUG] Loading machine type: %s", d.Get("machine_type").(string)) machineType, err := config.clientCompute.MachineTypes.Get( - config.Project, zone.Name, d.Get("machine_type").(string)).Do() + project, zone.Name, d.Get("machine_type").(string)).Do() if err != nil { return fmt.Errorf( "Error loading machine type: %s", @@ -345,7 +361,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err if v, ok := d.GetOk(prefix + ".disk"); ok { diskName := v.(string) diskData, err := config.clientCompute.Disks.Get( - config.Project, zone.Name, diskName).Do() + project, zone.Name, diskName).Do() if err != nil { return fmt.Errorf( "Error loading disk '%s': %s", @@ -423,7 +439,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err // Load up the name of this network networkName := d.Get(prefix + ".source").(string) network, err := config.clientCompute.Networks.Get( - config.Project, networkName).Do() + project, networkName).Do() if err != nil { return fmt.Errorf( "Error loading network '%s': %s", @@ -458,7 +474,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("Cannot specify both network and subnetwork values.") } else if networkName != "" { network, err := config.clientCompute.Networks.Get( - config.Project, networkName).Do() + project, networkName).Do() if err != nil { return fmt.Errorf( "Error referencing network '%s': %s", @@ -468,7 +484,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err } else { region := getRegionFromZone(d.Get("zone").(string)) subnetwork, err := config.clientCompute.Subnetworks.Get( - config.Project, region, subnetworkName).Do() + project, region, subnetworkName).Do() if err != nil { return fmt.Errorf( "Error referencing subnetwork '%s' in region '%s': %s", @@ -552,7 +568,7 @@ func resourceComputeInstanceCreate(d *schema.ResourceData, meta interface{}) err log.Printf("[INFO] Requesting instance creation") op, err := config.clientCompute.Instances.Insert( - config.Project, zone.Name, &instance).Do() + project, zone.Name, &instance).Do() if err != nil { return fmt.Errorf("Error creating instance: %s", err) } @@ -724,6 +740,11 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("zone").(string) instance, err := getInstance(config, d) @@ -760,7 +781,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("Error updating metadata: %s", err) } op, err := config.clientCompute.Instances.SetMetadata( - config.Project, zone, d.Id(), md).Do() + project, zone, d.Id(), md).Do() if err != nil { return fmt.Errorf("Error updating metadata: %s", err) } @@ -780,7 +801,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err if d.HasChange("tags") { tags := resourceInstanceTags(d) op, err := config.clientCompute.Instances.SetTags( - config.Project, zone, d.Id(), tags).Do() + project, zone, d.Id(), tags).Do() if err != nil { return fmt.Errorf("Error updating tags: %s", err) } @@ -809,7 +830,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err scheduling.OnHostMaintenance = val.(string) } - op, err := config.clientCompute.Instances.SetScheduling(config.Project, + op, err := config.clientCompute.Instances.SetScheduling(project, zone, d.Id(), scheduling).Do() if err != nil { @@ -854,7 +875,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err // Delete any accessConfig that currently exists in instNetworkInterface for _, ac := range instNetworkInterface.AccessConfigs { op, err := config.clientCompute.Instances.DeleteAccessConfig( - config.Project, zone, d.Id(), ac.Name, networkName).Do() + project, zone, d.Id(), ac.Name, networkName).Do() if err != nil { return fmt.Errorf("Error deleting old access_config: %s", err) } @@ -873,7 +894,7 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err NatIP: d.Get(acPrefix + ".nat_ip").(string), } op, err := config.clientCompute.Instances.AddAccessConfig( - config.Project, zone, d.Id(), networkName, ac).Do() + project, zone, d.Id(), networkName, ac).Do() if err != nil { return fmt.Errorf("Error adding new access_config: %s", err) } @@ -895,9 +916,14 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("zone").(string) log.Printf("[INFO] Requesting instance deletion: %s", d.Id()) - op, err := config.clientCompute.Instances.Delete(config.Project, zone, d.Id()).Do() + op, err := config.clientCompute.Instances.Delete(project, zone, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting instance: %s", err) } diff --git a/builtin/providers/google/resource_compute_instance_group.go b/builtin/providers/google/resource_compute_instance_group.go index 284fc1631..cd6d31088 100644 --- a/builtin/providers/google/resource_compute_instance_group.go +++ b/builtin/providers/google/resource_compute_instance_group.go @@ -75,6 +75,12 @@ func resourceComputeInstanceGroup() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -100,6 +106,11 @@ func validInstanceURLs(instanceUrls []string) bool { func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the parameter instanceGroup := &compute.InstanceGroup{ Name: d.Get("name").(string), @@ -116,7 +127,7 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] InstanceGroup insert request: %#v", instanceGroup) op, err := config.clientCompute.InstanceGroups.Insert( - config.Project, d.Get("zone").(string), instanceGroup).Do() + project, d.Get("zone").(string), instanceGroup).Do() if err != nil { return fmt.Errorf("Error creating InstanceGroup: %s", err) } @@ -142,7 +153,7 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] InstanceGroup add instances request: %#v", addInstanceReq) op, err := config.clientCompute.InstanceGroups.AddInstances( - config.Project, d.Get("zone").(string), d.Id(), addInstanceReq).Do() + project, d.Get("zone").(string), d.Id(), addInstanceReq).Do() if err != nil { return fmt.Errorf("Error adding instances to InstanceGroup: %s", err) } @@ -160,9 +171,14 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{} func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // retreive instance group instanceGroup, err := config.clientCompute.InstanceGroups.Get( - config.Project, d.Get("zone").(string), d.Id()).Do() + project, d.Get("zone").(string), d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore @@ -177,7 +193,7 @@ func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{}) // retreive instance group members var memberUrls []string members, err := config.clientCompute.InstanceGroups.ListInstances( - config.Project, d.Get("zone").(string), d.Id(), &compute.InstanceGroupsListInstancesRequest{ + project, d.Get("zone").(string), d.Id(), &compute.InstanceGroupsListInstancesRequest{ InstanceState: "ALL", }).Do() if err != nil { @@ -206,8 +222,13 @@ func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{}) func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // refresh the state incase referenced instances have been removed earlier in the run - err := resourceComputeInstanceGroupRead(d, meta) + err = resourceComputeInstanceGroupRead(d, meta) if err != nil { return fmt.Errorf("Error reading InstanceGroup: %s", err) } @@ -237,7 +258,7 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] InstanceGroup remove instances request: %#v", removeReq) removeOp, err := config.clientCompute.InstanceGroups.RemoveInstances( - config.Project, d.Get("zone").(string), d.Id(), removeReq).Do() + project, d.Get("zone").(string), d.Id(), removeReq).Do() if err != nil { return fmt.Errorf("Error removing instances from InstanceGroup: %s", err) } @@ -257,7 +278,7 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] InstanceGroup adding instances request: %#v", addReq) addOp, err := config.clientCompute.InstanceGroups.AddInstances( - config.Project, d.Get("zone").(string), d.Id(), addReq).Do() + project, d.Get("zone").(string), d.Id(), addReq).Do() if err != nil { return fmt.Errorf("Error adding instances from InstanceGroup: %s", err) } @@ -281,7 +302,7 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] InstanceGroup updating named ports request: %#v", namedPortsReq) op, err := config.clientCompute.InstanceGroups.SetNamedPorts( - config.Project, d.Get("zone").(string), d.Id(), namedPortsReq).Do() + project, d.Get("zone").(string), d.Id(), namedPortsReq).Do() if err != nil { return fmt.Errorf("Error updating named ports for InstanceGroup: %s", err) } @@ -301,8 +322,13 @@ func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{} func resourceComputeInstanceGroupDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("zone").(string) - op, err := config.clientCompute.InstanceGroups.Delete(config.Project, zone, d.Id()).Do() + op, err := config.clientCompute.InstanceGroups.Delete(project, zone, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting InstanceGroup: %s", err) } diff --git a/builtin/providers/google/resource_compute_instance_group_manager.go b/builtin/providers/google/resource_compute_instance_group_manager.go index 3e4e49863..970722ae5 100644 --- a/builtin/providers/google/resource_compute_instance_group_manager.go +++ b/builtin/providers/google/resource_compute_instance_group_manager.go @@ -100,6 +100,12 @@ func resourceComputeInstanceGroupManager() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -119,6 +125,11 @@ func getNamedPorts(nps []interface{}) []*compute.NamedPort { func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Get group size, default to 1 if not given var target_size int64 = 1 if v, ok := d.GetOk("target_size"); ok { @@ -157,7 +168,7 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte log.Printf("[DEBUG] InstanceGroupManager insert request: %#v", manager) op, err := config.clientCompute.InstanceGroupManagers.Insert( - config.Project, d.Get("zone").(string), manager).Do() + project, d.Get("zone").(string), manager).Do() if err != nil { return fmt.Errorf("Error creating InstanceGroupManager: %s", err) } @@ -177,8 +188,13 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + manager, err := config.clientCompute.InstanceGroupManagers.Get( - config.Project, d.Get("zone").(string), d.Id()).Do() + project, d.Get("zone").(string), d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Instance Group Manager %q because it's gone", d.Get("name").(string)) @@ -203,6 +219,11 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + d.Partial(true) // If target_pools changes then update @@ -221,7 +242,7 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte } op, err := config.clientCompute.InstanceGroupManagers.SetTargetPools( - config.Project, d.Get("zone").(string), d.Id(), setTargetPools).Do() + project, d.Get("zone").(string), d.Id(), setTargetPools).Do() if err != nil { return fmt.Errorf("Error updating InstanceGroupManager: %s", err) } @@ -243,7 +264,7 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte } op, err := config.clientCompute.InstanceGroupManagers.SetInstanceTemplate( - config.Project, d.Get("zone").(string), d.Id(), setInstanceTemplate).Do() + project, d.Get("zone").(string), d.Id(), setInstanceTemplate).Do() if err != nil { return fmt.Errorf("Error updating InstanceGroupManager: %s", err) } @@ -256,7 +277,7 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte if d.Get("update_strategy").(string) == "RESTART" { managedInstances, err := config.clientCompute.InstanceGroupManagers.ListManagedInstances( - config.Project, d.Get("zone").(string), d.Id()).Do() + project, d.Get("zone").(string), d.Id()).Do() managedInstanceCount := len(managedInstances.ManagedInstances) instances := make([]string, managedInstanceCount) @@ -269,7 +290,7 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte } op, err = config.clientCompute.InstanceGroupManagers.RecreateInstances( - config.Project, d.Get("zone").(string), d.Id(), recreateInstances).Do() + project, d.Get("zone").(string), d.Id(), recreateInstances).Do() if err != nil { return fmt.Errorf("Error restarting instance group managers instances: %s", err) @@ -297,7 +318,7 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte // Make the request: op, err := config.clientCompute.InstanceGroups.SetNamedPorts( - config.Project, d.Get("zone").(string), d.Id(), setNamedPorts).Do() + project, d.Get("zone").(string), d.Id(), setNamedPorts).Do() if err != nil { return fmt.Errorf("Error updating InstanceGroupManager: %s", err) } @@ -318,7 +339,7 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte target_size := int64(v.(int)) op, err := config.clientCompute.InstanceGroupManagers.Resize( - config.Project, d.Get("zone").(string), d.Id(), target_size).Do() + project, d.Get("zone").(string), d.Id(), target_size).Do() if err != nil { return fmt.Errorf("Error updating InstanceGroupManager: %s", err) } @@ -341,8 +362,13 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("zone").(string) - op, err := config.clientCompute.InstanceGroupManagers.Delete(config.Project, zone, d.Id()).Do() + op, err := config.clientCompute.InstanceGroupManagers.Delete(project, zone, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting instance group manager: %s", err) } @@ -358,7 +384,7 @@ func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta inte } instanceGroup, err := config.clientCompute.InstanceGroups.Get( - config.Project, d.Get("zone").(string), d.Id()).Do() + project, d.Get("zone").(string), d.Id()).Do() if err != nil { return fmt.Errorf("Error getting instance group size: %s", err) diff --git a/builtin/providers/google/resource_compute_instance_template.go b/builtin/providers/google/resource_compute_instance_template.go index ea5ed35d6..5805fd2bb 100644 --- a/builtin/providers/google/resource_compute_instance_template.go +++ b/builtin/providers/google/resource_compute_instance_template.go @@ -179,12 +179,6 @@ func resourceComputeInstanceTemplate() *schema.Resource { Deprecated: "Please use `scheduling.on_host_maintenance` instead", }, - "region": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "scheduling": &schema.Schema{ Type: schema.TypeList, Optional: true, @@ -262,6 +256,18 @@ func resourceComputeInstanceTemplate() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "region": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -341,6 +347,11 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network // Build up the list of networks config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return nil, err + } + networksCount := d.Get("network_interface.#").(int) networkInterfaces := make([]*compute.NetworkInterface, 0, networksCount) for i := 0; i < networksCount; i++ { @@ -372,9 +383,9 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network networkLink = network.SelfLink } else { // lookup subnetwork link using region and subnetwork name - region := d.Get("region").(string) - if region == "" { - region = config.Region + region, err := getRegion(d, config) + if err != nil { + return nil, err } subnetwork, err := config.clientCompute.Subnetworks.Get( project, region, subnetworkName).Do() @@ -409,6 +420,11 @@ func buildNetworks(d *schema.ResourceData, meta interface{}) ([]*compute.Network func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + instanceProperties := &compute.InstanceProperties{} instanceProperties.CanIpForward = d.Get("can_ip_forward").(bool) @@ -503,7 +519,7 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac } op, err := config.clientCompute.InstanceTemplates.Insert( - config.Project, &instanceTemplate).Do() + project, &instanceTemplate).Do() if err != nil { return fmt.Errorf("Error creating instance: %s", err) } @@ -522,8 +538,13 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + instanceTemplate, err := config.clientCompute.InstanceTemplates.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Instance Template %q because it's gone", d.Get("name").(string)) @@ -553,8 +574,13 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{ func resourceComputeInstanceTemplateDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + op, err := config.clientCompute.InstanceTemplates.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting instance template: %s", err) } diff --git a/builtin/providers/google/resource_compute_network.go b/builtin/providers/google/resource_compute_network.go index 573c72f40..b3182ab14 100644 --- a/builtin/providers/google/resource_compute_network.go +++ b/builtin/providers/google/resource_compute_network.go @@ -56,6 +56,12 @@ func resourceComputeNetwork() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -63,6 +69,11 @@ func resourceComputeNetwork() *schema.Resource { func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // // Possible modes: // - 1 Legacy mode - Create a network in the legacy mode. ipv4_range is set. auto_create_subnetworks must not be @@ -91,7 +102,7 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro log.Printf("[DEBUG] Network insert request: %#v", network) op, err := config.clientCompute.Networks.Insert( - config.Project, network).Do() + project, network).Do() if err != nil { return fmt.Errorf("Error creating network: %s", err) } @@ -110,8 +121,13 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + network, err := config.clientCompute.Networks.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Network %q because it's gone", d.Get("name").(string)) @@ -133,9 +149,14 @@ func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the network op, err := config.clientCompute.Networks.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting network: %s", err) } diff --git a/builtin/providers/google/resource_compute_project_metadata.go b/builtin/providers/google/resource_compute_project_metadata.go index c2508c8f3..39f3ba2b0 100644 --- a/builtin/providers/google/resource_compute_project_metadata.go +++ b/builtin/providers/google/resource_compute_project_metadata.go @@ -24,6 +24,12 @@ func resourceComputeProjectMetadata() *schema.Resource { Type: schema.TypeMap, Required: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -31,12 +37,17 @@ func resourceComputeProjectMetadata() *schema.Resource { func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + projectID, err := getProject(d, config) + if err != nil { + return err + } + createMD := func() error { // Load project service - log.Printf("[DEBUG] Loading project service: %s", config.Project) - project, err := config.clientCompute.Projects.Get(config.Project).Do() + log.Printf("[DEBUG] Loading project service: %s", projectID) + project, err := config.clientCompute.Projects.Get(projectID).Do() if err != nil { - return fmt.Errorf("Error loading project '%s': %s", config.Project, err) + return fmt.Errorf("Error loading project '%s': %s", projectID, err) } md := project.CommonInstanceMetadata @@ -45,7 +56,7 @@ func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface // Ensure that we aren't overwriting entries that already exist for _, kv := range md.Items { if _, ok := newMDMap[kv.Key]; ok { - return fmt.Errorf("Error, key '%s' already exists in project '%s'", kv.Key, config.Project) + return fmt.Errorf("Error, key '%s' already exists in project '%s'", kv.Key, projectID) } } @@ -58,7 +69,7 @@ func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface }) } - op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(config.Project, md).Do() + op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do() if err != nil { return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err) @@ -69,7 +80,7 @@ func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface return computeOperationWaitGlobal(config, op, "SetCommonMetadata") } - err := MetadataRetryWrapper(createMD) + err = MetadataRetryWrapper(createMD) if err != nil { return err } @@ -80,9 +91,14 @@ func resourceComputeProjectMetadataCreate(d *schema.ResourceData, meta interface func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + projectID, err := getProject(d, config) + if err != nil { + return err + } + // Load project service - log.Printf("[DEBUG] Loading project service: %s", config.Project) - project, err := config.clientCompute.Projects.Get(config.Project).Do() + log.Printf("[DEBUG] Loading project service: %s", projectID) + project, err := config.clientCompute.Projects.Get(projectID).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Project Metadata because it's gone") @@ -92,7 +108,7 @@ func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{} return nil } - return fmt.Errorf("Error loading project '%s': %s", config.Project, err) + return fmt.Errorf("Error loading project '%s': %s", projectID, err) } md := project.CommonInstanceMetadata @@ -109,22 +125,27 @@ func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{} func resourceComputeProjectMetadataUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + projectID, err := getProject(d, config) + if err != nil { + return err + } + if d.HasChange("metadata") { o, n := d.GetChange("metadata") updateMD := func() error { // Load project service - log.Printf("[DEBUG] Loading project service: %s", config.Project) - project, err := config.clientCompute.Projects.Get(config.Project).Do() + log.Printf("[DEBUG] Loading project service: %s", projectID) + project, err := config.clientCompute.Projects.Get(projectID).Do() if err != nil { - return fmt.Errorf("Error loading project '%s': %s", config.Project, err) + return fmt.Errorf("Error loading project '%s': %s", projectID, err) } md := project.CommonInstanceMetadata MetadataUpdate(o.(map[string]interface{}), n.(map[string]interface{}), md) - op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(config.Project, md).Do() + op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do() if err != nil { return fmt.Errorf("SetCommonInstanceMetadata failed: %s", err) @@ -152,11 +173,16 @@ func resourceComputeProjectMetadataUpdate(d *schema.ResourceData, meta interface func resourceComputeProjectMetadataDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - // Load project service - log.Printf("[DEBUG] Loading project service: %s", config.Project) - project, err := config.clientCompute.Projects.Get(config.Project).Do() + projectID, err := getProject(d, config) if err != nil { - return fmt.Errorf("Error loading project '%s': %s", config.Project, err) + return err + } + + // Load project service + log.Printf("[DEBUG] Loading project service: %s", projectID) + project, err := config.clientCompute.Projects.Get(projectID).Do() + if err != nil { + return fmt.Errorf("Error loading project '%s': %s", projectID, err) } md := project.CommonInstanceMetadata @@ -164,7 +190,7 @@ func resourceComputeProjectMetadataDelete(d *schema.ResourceData, meta interface // Remove all items md.Items = nil - op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(config.Project, md).Do() + op, err := config.clientCompute.Projects.SetCommonInstanceMetadata(projectID, md).Do() log.Printf("[DEBUG] SetCommonMetadata: %d (%s)", op.Id, op.SelfLink) diff --git a/builtin/providers/google/resource_compute_route.go b/builtin/providers/google/resource_compute_route.go index 603373142..0e177c895 100644 --- a/builtin/providers/google/resource_compute_route.go +++ b/builtin/providers/google/resource_compute_route.go @@ -87,6 +87,12 @@ func resourceComputeRoute() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -94,9 +100,14 @@ func resourceComputeRoute() *schema.Resource { func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Look up the network to attach the route to network, err := config.clientCompute.Networks.Get( - config.Project, d.Get("network").(string)).Do() + project, d.Get("network").(string)).Do() if err != nil { return fmt.Errorf("Error reading network: %s", err) } @@ -115,7 +126,7 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error } if v, ok := d.GetOk("next_hop_instance"); ok { nextInstance, err := config.clientCompute.Instances.Get( - config.Project, + project, d.Get("next_hop_instance_zone").(string), v.(string)).Do() if err != nil { @@ -148,7 +159,7 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error } log.Printf("[DEBUG] Route insert request: %#v", route) op, err := config.clientCompute.Routes.Insert( - config.Project, route).Do() + project, route).Do() if err != nil { return fmt.Errorf("Error creating route: %s", err) } @@ -167,8 +178,13 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + route, err := config.clientCompute.Routes.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Route %q because it's gone", d.Get("name").(string)) @@ -190,9 +206,14 @@ func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error { func resourceComputeRouteDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the route op, err := config.clientCompute.Routes.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting route: %s", err) } diff --git a/builtin/providers/google/resource_compute_ssl_certificate.go b/builtin/providers/google/resource_compute_ssl_certificate.go index a80bc2fb2..8d7a4048b 100644 --- a/builtin/providers/google/resource_compute_ssl_certificate.go +++ b/builtin/providers/google/resource_compute_ssl_certificate.go @@ -50,6 +50,12 @@ func resourceComputeSslCertificate() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -57,6 +63,11 @@ func resourceComputeSslCertificate() *schema.Resource { func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the certificate parameter cert := &compute.SslCertificate{ Name: d.Get("name").(string), @@ -69,7 +80,7 @@ func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{ } op, err := config.clientCompute.SslCertificates.Insert( - config.Project, cert).Do() + project, cert).Do() if err != nil { return fmt.Errorf("Error creating ssl certificate: %s", err) @@ -88,8 +99,13 @@ func resourceComputeSslCertificateCreate(d *schema.ResourceData, meta interface{ func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + cert, err := config.clientCompute.SslCertificates.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing SSL Certificate %q because it's gone", d.Get("name").(string)) @@ -111,8 +127,13 @@ func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) func resourceComputeSslCertificateDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + op, err := config.clientCompute.SslCertificates.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting ssl certificate: %s", err) } diff --git a/builtin/providers/google/resource_compute_subnetwork.go b/builtin/providers/google/resource_compute_subnetwork.go index 61e8caa6e..9a0d2b423 100644 --- a/builtin/providers/google/resource_compute_subnetwork.go +++ b/builtin/providers/google/resource_compute_subnetwork.go @@ -4,10 +4,11 @@ import ( "fmt" "log" + "strings" + "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" "google.golang.org/api/googleapi" - "strings" ) func resourceComputeSubnetwork() *schema.Resource { @@ -56,6 +57,12 @@ func resourceComputeSubnetwork() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -74,6 +81,11 @@ func splitSubnetID(id string) (region string, name string) { func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the subnetwork parameters subnetwork := &compute.Subnetwork{ Name: d.Get("name").(string), @@ -85,7 +97,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e log.Printf("[DEBUG] Subnetwork insert request: %#v", subnetwork) op, err := config.clientCompute.Subnetworks.Insert( - config.Project, region, subnetwork).Do() + project, region, subnetwork).Do() if err != nil { return fmt.Errorf("Error creating subnetwork: %s", err) @@ -109,11 +121,17 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) region := d.Get("region").(string) subnetwork, err := config.clientCompute.Subnetworks.Get( - config.Project, region, name).Do() + project, region, name).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Subnetwork %q because it's gone", name) @@ -134,11 +152,17 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + region := d.Get("region").(string) // Delete the subnetwork op, err := config.clientCompute.Subnetworks.Delete( - config.Project, region, d.Get("name").(string)).Do() + project, region, d.Get("name").(string)).Do() if err != nil { return fmt.Errorf("Error deleting subnetwork: %s", err) } diff --git a/builtin/providers/google/resource_compute_target_http_proxy.go b/builtin/providers/google/resource_compute_target_http_proxy.go index 72644fb01..cec71954d 100644 --- a/builtin/providers/google/resource_compute_target_http_proxy.go +++ b/builtin/providers/google/resource_compute_target_http_proxy.go @@ -44,6 +44,12 @@ func resourceComputeTargetHttpProxy() *schema.Resource { Type: schema.TypeString, Required: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -51,6 +57,11 @@ func resourceComputeTargetHttpProxy() *schema.Resource { func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + proxy := &compute.TargetHttpProxy{ Name: d.Get("name").(string), UrlMap: d.Get("url_map").(string), @@ -62,7 +73,7 @@ func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface log.Printf("[DEBUG] TargetHttpProxy insert request: %#v", proxy) op, err := config.clientCompute.TargetHttpProxies.Insert( - config.Project, proxy).Do() + project, proxy).Do() if err != nil { return fmt.Errorf("Error creating TargetHttpProxy: %s", err) } @@ -80,13 +91,18 @@ func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + d.Partial(true) if d.HasChange("url_map") { url_map := d.Get("url_map").(string) url_map_ref := &compute.UrlMapReference{UrlMap: url_map} op, err := config.clientCompute.TargetHttpProxies.SetUrlMap( - config.Project, d.Id(), url_map_ref).Do() + project, d.Id(), url_map_ref).Do() if err != nil { return fmt.Errorf("Error updating target: %s", err) } @@ -107,8 +123,13 @@ func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + proxy, err := config.clientCompute.TargetHttpProxies.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Target HTTP Proxy %q because it's gone", d.Get("name").(string)) @@ -130,10 +151,15 @@ func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{} func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the TargetHttpProxy log.Printf("[DEBUG] TargetHttpProxy delete request") op, err := config.clientCompute.TargetHttpProxies.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting TargetHttpProxy: %s", err) } diff --git a/builtin/providers/google/resource_compute_target_https_proxy.go b/builtin/providers/google/resource_compute_target_https_proxy.go index b30fd1eab..b505b0227 100644 --- a/builtin/providers/google/resource_compute_target_https_proxy.go +++ b/builtin/providers/google/resource_compute_target_https_proxy.go @@ -50,6 +50,12 @@ func resourceComputeTargetHttpsProxy() *schema.Resource { Required: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -57,6 +63,11 @@ func resourceComputeTargetHttpsProxy() *schema.Resource { func resourceComputeTargetHttpsProxyCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + _sslCertificates := d.Get("ssl_certificates").([]interface{}) sslCertificates := make([]string, len(_sslCertificates)) @@ -76,7 +87,7 @@ func resourceComputeTargetHttpsProxyCreate(d *schema.ResourceData, meta interfac log.Printf("[DEBUG] TargetHttpsProxy insert request: %#v", proxy) op, err := config.clientCompute.TargetHttpsProxies.Insert( - config.Project, proxy).Do() + project, proxy).Do() if err != nil { return fmt.Errorf("Error creating TargetHttpsProxy: %s", err) } @@ -94,13 +105,18 @@ func resourceComputeTargetHttpsProxyCreate(d *schema.ResourceData, meta interfac func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + d.Partial(true) if d.HasChange("url_map") { url_map := d.Get("url_map").(string) url_map_ref := &compute.UrlMapReference{UrlMap: url_map} op, err := config.clientCompute.TargetHttpsProxies.SetUrlMap( - config.Project, d.Id(), url_map_ref).Do() + project, d.Id(), url_map_ref).Do() if err != nil { return fmt.Errorf("Error updating Target HTTPS proxy URL map: %s", err) } @@ -115,7 +131,7 @@ func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interfac if d.HasChange("ssl_certificates") { proxy, err := config.clientCompute.TargetHttpsProxies.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() _old, _new := d.GetChange("ssl_certificates") _oldCerts := _old.([]interface{}) @@ -161,7 +177,7 @@ func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interfac SslCertificates: sslCertificates, } op, err := config.clientCompute.TargetHttpsProxies.SetSslCertificates( - config.Project, d.Id(), cert_ref).Do() + project, d.Id(), cert_ref).Do() if err != nil { return fmt.Errorf("Error updating Target Https Proxy SSL Certificates: %s", err) } @@ -182,8 +198,13 @@ func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interfac func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + proxy, err := config.clientCompute.TargetHttpsProxies.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Target HTTPS Proxy %q because it's gone", d.Get("name").(string)) @@ -223,10 +244,15 @@ func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{ func resourceComputeTargetHttpsProxyDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Delete the TargetHttpsProxy log.Printf("[DEBUG] TargetHttpsProxy delete request") op, err := config.clientCompute.TargetHttpsProxies.Delete( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting TargetHttpsProxy: %s", err) } diff --git a/builtin/providers/google/resource_compute_target_pool.go b/builtin/providers/google/resource_compute_target_pool.go index fa25a1b72..8ececab41 100644 --- a/builtin/providers/google/resource_compute_target_pool.go +++ b/builtin/providers/google/resource_compute_target_pool.go @@ -72,6 +72,12 @@ func resourceComputeTargetPool() *schema.Resource { Optional: true, ForceNew: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -85,11 +91,11 @@ func convertStringArr(ifaceArr []interface{}) []string { } // Healthchecks need to exist before being referred to from the target pool. -func convertHealthChecks(config *Config, names []string) ([]string, error) { +func convertHealthChecks(config *Config, project string, names []string) ([]string, error) { urls := make([]string, len(names)) for i, name := range names { // Look up the healthcheck - res, err := config.clientCompute.HttpHealthChecks.Get(config.Project, name).Do() + res, err := config.clientCompute.HttpHealthChecks.Get(project, name).Do() if err != nil { return nil, fmt.Errorf("Error reading HealthCheck: %s", err) } @@ -100,7 +106,7 @@ func convertHealthChecks(config *Config, names []string) ([]string, error) { // Instances do not need to exist yet, so we simply generate URLs. // Instances can be full URLS or zone/name -func convertInstances(config *Config, names []string) ([]string, error) { +func convertInstances(config *Config, project string, names []string) ([]string, error) { urls := make([]string, len(names)) for i, name := range names { if strings.HasPrefix(name, "https://www.googleapis.com/compute/v1/") { @@ -112,7 +118,7 @@ func convertInstances(config *Config, names []string) ([]string, error) { } else { urls[i] = fmt.Sprintf( "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances/%s", - config.Project, splitName[0], splitName[1]) + project, splitName[0], splitName[1]) } } } @@ -121,16 +127,25 @@ func convertInstances(config *Config, names []string) ([]string, error) { func resourceComputeTargetPoolCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } hchkUrls, err := convertHealthChecks( - config, convertStringArr(d.Get("health_checks").([]interface{}))) + config, project, convertStringArr(d.Get("health_checks").([]interface{}))) if err != nil { return err } instanceUrls, err := convertInstances( - config, convertStringArr(d.Get("instances").([]interface{}))) + config, project, convertStringArr(d.Get("instances").([]interface{}))) if err != nil { return err } @@ -149,7 +164,7 @@ func resourceComputeTargetPoolCreate(d *schema.ResourceData, meta interface{}) e } log.Printf("[DEBUG] TargetPool insert request: %#v", tpool) op, err := config.clientCompute.TargetPools.Insert( - config.Project, region, tpool).Do() + project, region, tpool).Do() if err != nil { return fmt.Errorf("Error creating TargetPool: %s", err) } @@ -196,7 +211,16 @@ func calcAddRemove(from []string, to []string) ([]string, []string) { func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } d.Partial(true) @@ -205,11 +229,11 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e from_, to_ := d.GetChange("health_checks") from := convertStringArr(from_.([]interface{})) to := convertStringArr(to_.([]interface{})) - fromUrls, err := convertHealthChecks(config, from) + fromUrls, err := convertHealthChecks(config, project, from) if err != nil { return err } - toUrls, err := convertHealthChecks(config, to) + toUrls, err := convertHealthChecks(config, project, to) if err != nil { return err } @@ -222,7 +246,7 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e removeReq.HealthChecks[i] = &compute.HealthCheckReference{HealthCheck: v} } op, err := config.clientCompute.TargetPools.RemoveHealthCheck( - config.Project, region, d.Id(), removeReq).Do() + project, region, d.Id(), removeReq).Do() if err != nil { return fmt.Errorf("Error updating health_check: %s", err) } @@ -238,7 +262,7 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e addReq.HealthChecks[i] = &compute.HealthCheckReference{HealthCheck: v} } op, err = config.clientCompute.TargetPools.AddHealthCheck( - config.Project, region, d.Id(), addReq).Do() + project, region, d.Id(), addReq).Do() if err != nil { return fmt.Errorf("Error updating health_check: %s", err) } @@ -255,11 +279,11 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e from_, to_ := d.GetChange("instances") from := convertStringArr(from_.([]interface{})) to := convertStringArr(to_.([]interface{})) - fromUrls, err := convertInstances(config, from) + fromUrls, err := convertInstances(config, project, from) if err != nil { return err } - toUrls, err := convertInstances(config, to) + toUrls, err := convertInstances(config, project, to) if err != nil { return err } @@ -272,7 +296,7 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e addReq.Instances[i] = &compute.InstanceReference{Instance: v} } op, err := config.clientCompute.TargetPools.AddInstance( - config.Project, region, d.Id(), addReq).Do() + project, region, d.Id(), addReq).Do() if err != nil { return fmt.Errorf("Error updating instances: %s", err) } @@ -288,7 +312,7 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e removeReq.Instances[i] = &compute.InstanceReference{Instance: v} } op, err = config.clientCompute.TargetPools.RemoveInstance( - config.Project, region, d.Id(), removeReq).Do() + project, region, d.Id(), removeReq).Do() if err != nil { return fmt.Errorf("Error updating instances: %s", err) } @@ -305,7 +329,7 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e Target: bpool_name, } op, err := config.clientCompute.TargetPools.SetBackup( - config.Project, region, d.Id(), tref).Do() + project, region, d.Id(), tref).Do() if err != nil { return fmt.Errorf("Error updating backup_pool: %s", err) } @@ -324,10 +348,19 @@ func resourceComputeTargetPoolUpdate(d *schema.ResourceData, meta interface{}) e func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } tpool, err := config.clientCompute.TargetPools.Get( - config.Project, region, d.Id()).Do() + project, region, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Target Pool %q because it's gone", d.Get("name").(string)) @@ -347,11 +380,20 @@ func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) err func resourceComputeTargetPoolDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - region := getOptionalRegion(d, config) + + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } // Delete the TargetPool op, err := config.clientCompute.TargetPools.Delete( - config.Project, region, d.Id()).Do() + project, region, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting TargetPool: %s", err) } diff --git a/builtin/providers/google/resource_compute_url_map.go b/builtin/providers/google/resource_compute_url_map.go index 47a38431f..381ad9205 100644 --- a/builtin/providers/google/resource_compute_url_map.go +++ b/builtin/providers/google/resource_compute_url_map.go @@ -142,6 +142,12 @@ func resourceComputeUrlMap() *schema.Resource { }, }, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -235,6 +241,11 @@ func createUrlMapTest(v interface{}) *compute.UrlMapTest { func resourceComputeUrlMapCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) defaultService := d.Get("default_service").(string) @@ -271,7 +282,7 @@ func resourceComputeUrlMapCreate(d *schema.ResourceData, meta interface{}) error urlMap.Tests[i] = createUrlMapTest(v) } - op, err := config.clientCompute.UrlMaps.Insert(config.Project, urlMap).Do() + op, err := config.clientCompute.UrlMaps.Insert(project, urlMap).Do() if err != nil { return fmt.Errorf("Error, failed to insert Url Map %s: %s", name, err) @@ -289,9 +300,14 @@ func resourceComputeUrlMapCreate(d *schema.ResourceData, meta interface{}) error func resourceComputeUrlMapRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) - urlMap, err := config.clientCompute.UrlMaps.Get(config.Project, name).Do() + urlMap, err := config.clientCompute.UrlMaps.Get(project, name).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { @@ -425,8 +441,13 @@ func resourceComputeUrlMapRead(d *schema.ResourceData, meta interface{}) error { func resourceComputeUrlMapUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) - urlMap, err := config.clientCompute.UrlMaps.Get(config.Project, name).Do() + urlMap, err := config.clientCompute.UrlMaps.Get(project, name).Do() if err != nil { return fmt.Errorf("Error, failed to get Url Map %s: %s", name, err) } @@ -624,7 +645,7 @@ func resourceComputeUrlMapUpdate(d *schema.ResourceData, meta interface{}) error urlMap.Tests = newTests } - op, err := config.clientCompute.UrlMaps.Update(config.Project, urlMap.Name, urlMap).Do() + op, err := config.clientCompute.UrlMaps.Update(project, urlMap.Name, urlMap).Do() if err != nil { return fmt.Errorf("Error, failed to update Url Map %s: %s", name, err) @@ -641,9 +662,15 @@ func resourceComputeUrlMapUpdate(d *schema.ResourceData, meta interface{}) error func resourceComputeUrlMapDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) - op, err := config.clientCompute.UrlMaps.Delete(config.Project, name).Do() + op, err := config.clientCompute.UrlMaps.Delete(project, name).Do() if err != nil { return fmt.Errorf("Error, failed to delete Url Map %s: %s", name, err) diff --git a/builtin/providers/google/resource_compute_vpn_gateway.go b/builtin/providers/google/resource_compute_vpn_gateway.go index 562e3dfac..1e7de64b9 100644 --- a/builtin/providers/google/resource_compute_vpn_gateway.go +++ b/builtin/providers/google/resource_compute_vpn_gateway.go @@ -34,14 +34,19 @@ func resourceComputeVpnGateway() *schema.Resource { Required: true, ForceNew: true, }, + "self_link": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, "region": &schema.Schema{ Type: schema.TypeString, Optional: true, ForceNew: true, }, - "self_link": &schema.Schema{ + "project": &schema.Schema{ Type: schema.TypeString, - Computed: true, + Optional: true, + ForceNew: true, }, }, } @@ -50,10 +55,18 @@ func resourceComputeVpnGateway() *schema.Resource { func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) network := d.Get("network").(string) - region := getOptionalRegion(d, config) - project := config.Project vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) @@ -82,9 +95,17 @@ func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) e func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) - region := getOptionalRegion(d, config) - project := config.Project vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) vpnGateway, err := vpnGatewaysService.Get(project, region, name).Do() @@ -110,9 +131,17 @@ func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) err func resourceComputeVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) - region := getOptionalRegion(d, config) - project := config.Project vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute) diff --git a/builtin/providers/google/resource_compute_vpn_tunnel.go b/builtin/providers/google/resource_compute_vpn_tunnel.go index 2788dda8d..4e94e4f05 100644 --- a/builtin/providers/google/resource_compute_vpn_tunnel.go +++ b/builtin/providers/google/resource_compute_vpn_tunnel.go @@ -31,11 +31,6 @@ func resourceComputeVpnTunnel() *schema.Resource { Optional: true, ForceNew: true, }, - "region": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, "peer_ip": &schema.Schema{ Type: schema.TypeString, Required: true, @@ -73,6 +68,16 @@ func resourceComputeVpnTunnel() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "region": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -80,13 +85,21 @@ func resourceComputeVpnTunnel() *schema.Resource { func resourceComputeVpnTunnelCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) - region := getOptionalRegion(d, config) peerIp := d.Get("peer_ip").(string) sharedSecret := d.Get("shared_secret").(string) targetVpnGateway := d.Get("target_vpn_gateway").(string) ikeVersion := d.Get("ike_version").(int) - project := config.Project if ikeVersion < 1 || ikeVersion > 2 { return fmt.Errorf("Only IKE version 1 or 2 supported, not %d", ikeVersion) @@ -132,9 +145,17 @@ func resourceComputeVpnTunnelCreate(d *schema.ResourceData, meta interface{}) er func resourceComputeVpnTunnelRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) - region := getOptionalRegion(d, config) - project := config.Project vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute) @@ -162,9 +183,17 @@ func resourceComputeVpnTunnelRead(d *schema.ResourceData, meta interface{}) erro func resourceComputeVpnTunnelDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + region, err := getRegion(d, config) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) - region := getOptionalRegion(d, config) - project := config.Project vpnTunnelsService := compute.NewVpnTunnelsService(config.clientCompute) diff --git a/builtin/providers/google/resource_container_cluster.go b/builtin/providers/google/resource_container_cluster.go index 841644015..08dddaf27 100644 --- a/builtin/providers/google/resource_container_cluster.go +++ b/builtin/providers/google/resource_container_cluster.go @@ -195,6 +195,12 @@ func resourceContainerCluster() *schema.Resource { Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -202,6 +208,11 @@ func resourceContainerCluster() *schema.Resource { func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zoneName := d.Get("zone").(string) clusterName := d.Get("name").(string) @@ -273,7 +284,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er } op, err := config.clientContainer.Projects.Zones.Clusters.Create( - config.Project, zoneName, req).Do() + project, zoneName, req).Do() if err != nil { return err } @@ -286,7 +297,7 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er MinTimeout: 3 * time.Second, Refresh: func() (interface{}, string, error) { resp, err := config.clientContainer.Projects.Zones.Operations.Get( - config.Project, zoneName, op.Name).Do() + project, zoneName, op.Name).Do() log.Printf("[DEBUG] Progress of creating GKE cluster %s: %s", clusterName, resp.Status) return resp, resp.Status, err @@ -308,10 +319,15 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zoneName := d.Get("zone").(string) cluster, err := config.clientContainer.Projects.Zones.Clusters.Get( - config.Project, zoneName, d.Get("name").(string)).Do() + project, zoneName, d.Get("name").(string)).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing Container Cluster %q because it's gone", d.Get("name").(string)) @@ -355,6 +371,11 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zoneName := d.Get("zone").(string) clusterName := d.Get("name").(string) desiredNodeVersion := d.Get("node_version").(string) @@ -365,7 +386,7 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er }, } op, err := config.clientContainer.Projects.Zones.Clusters.Update( - config.Project, zoneName, clusterName, req).Do() + project, zoneName, clusterName, req).Do() if err != nil { return err } @@ -379,7 +400,7 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er Refresh: func() (interface{}, string, error) { log.Printf("[DEBUG] Checking if GKE cluster %s is updated", clusterName) resp, err := config.clientContainer.Projects.Zones.Operations.Get( - config.Project, zoneName, op.Name).Do() + project, zoneName, op.Name).Do() log.Printf("[DEBUG] Progress of updating GKE cluster %s: %s", clusterName, resp.Status) return resp, resp.Status, err @@ -400,12 +421,17 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er func resourceContainerClusterDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zoneName := d.Get("zone").(string) clusterName := d.Get("name").(string) log.Printf("[DEBUG] Deleting GKE cluster %s", d.Get("name").(string)) op, err := config.clientContainer.Projects.Zones.Clusters.Delete( - config.Project, zoneName, clusterName).Do() + project, zoneName, clusterName).Do() if err != nil { return err } @@ -419,7 +445,7 @@ func resourceContainerClusterDelete(d *schema.ResourceData, meta interface{}) er Refresh: func() (interface{}, string, error) { log.Printf("[DEBUG] Checking if GKE cluster %s is deleted", clusterName) resp, err := config.clientContainer.Projects.Zones.Operations.Get( - config.Project, zoneName, op.Name).Do() + project, zoneName, op.Name).Do() log.Printf("[DEBUG] Progress of deleting GKE cluster %s: %s", clusterName, resp.Status) return resp, resp.Status, err diff --git a/builtin/providers/google/resource_dns_managed_zone.go b/builtin/providers/google/resource_dns_managed_zone.go index 0ef813ef2..913353593 100644 --- a/builtin/providers/google/resource_dns_managed_zone.go +++ b/builtin/providers/google/resource_dns_managed_zone.go @@ -44,6 +44,12 @@ func resourceDnsManagedZone() *schema.Resource { }, // Google Cloud DNS ManagedZone resources do not have a SelfLink attribute. + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -51,6 +57,11 @@ func resourceDnsManagedZone() *schema.Resource { func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Build the parameter zone := &dns.ManagedZone{ Name: d.Get("name").(string), @@ -65,7 +76,7 @@ func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) erro } log.Printf("[DEBUG] DNS ManagedZone create request: %#v", zone) - zone, err := config.clientDns.ManagedZones.Create(config.Project, zone).Do() + zone, err = config.clientDns.ManagedZones.Create(project, zone).Do() if err != nil { return fmt.Errorf("Error creating DNS ManagedZone: %s", err) } @@ -78,8 +89,13 @@ func resourceDnsManagedZoneCreate(d *schema.ResourceData, meta interface{}) erro func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone, err := config.clientDns.ManagedZones.Get( - config.Project, d.Id()).Do() + project, d.Id()).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing DNS Managed Zone %q because it's gone", d.Get("name").(string)) @@ -100,7 +116,12 @@ func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error func resourceDnsManagedZoneDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - err := config.clientDns.ManagedZones.Delete(config.Project, d.Id()).Do() + project, err := getProject(d, config) + if err != nil { + return err + } + + err = config.clientDns.ManagedZones.Delete(project, d.Id()).Do() if err != nil { return fmt.Errorf("Error deleting DNS ManagedZone: %s", err) } diff --git a/builtin/providers/google/resource_dns_record_set.go b/builtin/providers/google/resource_dns_record_set.go index 49b1fce71..5f0b7a51e 100644 --- a/builtin/providers/google/resource_dns_record_set.go +++ b/builtin/providers/google/resource_dns_record_set.go @@ -49,6 +49,12 @@ func resourceDnsRecordSet() *schema.Resource { Type: schema.TypeString, }, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -56,6 +62,11 @@ func resourceDnsRecordSet() *schema.Resource { func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("managed_zone").(string) rrdatasCount := d.Get("rrdatas.#").(int) @@ -78,7 +89,7 @@ func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error } log.Printf("[DEBUG] DNS Record create request: %#v", chg) - chg, err := config.clientDns.Changes.Create(config.Project, zone, chg).Do() + chg, err = config.clientDns.Changes.Create(project, zone, chg).Do() if err != nil { return fmt.Errorf("Error creating DNS RecordSet: %s", err) } @@ -88,7 +99,7 @@ func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error w := &DnsChangeWaiter{ Service: config.clientDns, Change: chg, - Project: config.Project, + Project: project, ManagedZone: zone, } state := w.Conf() @@ -106,6 +117,11 @@ func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("managed_zone").(string) // name and type are effectively the 'key' @@ -113,7 +129,7 @@ func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error { dnsType := d.Get("type").(string) resp, err := config.clientDns.ResourceRecordSets.List( - config.Project, zone).Name(name).Type(dnsType).Do() + project, zone).Name(name).Type(dnsType).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { log.Printf("[WARN] Removing DNS Record Set %q because it's gone", d.Get("name").(string)) @@ -144,6 +160,11 @@ func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error { func resourceDnsRecordSetDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + zone := d.Get("managed_zone").(string) rrdatasCount := d.Get("rrdatas.#").(int) @@ -165,7 +186,7 @@ func resourceDnsRecordSetDelete(d *schema.ResourceData, meta interface{}) error chg.Deletions[0].Rrdatas[i] = d.Get(rrdata).(string) } log.Printf("[DEBUG] DNS Record delete request: %#v", chg) - chg, err := config.clientDns.Changes.Create(config.Project, zone, chg).Do() + chg, err = config.clientDns.Changes.Create(project, zone, chg).Do() if err != nil { return fmt.Errorf("Error deleting DNS RecordSet: %s", err) } @@ -173,7 +194,7 @@ func resourceDnsRecordSetDelete(d *schema.ResourceData, meta interface{}) error w := &DnsChangeWaiter{ Service: config.clientDns, Change: chg, - Project: config.Project, + Project: project, ManagedZone: zone, } state := w.Conf() diff --git a/builtin/providers/google/resource_pubsub_subscription.go b/builtin/providers/google/resource_pubsub_subscription.go index c006818f6..19f3f38e7 100644 --- a/builtin/providers/google/resource_pubsub_subscription.go +++ b/builtin/providers/google/resource_pubsub_subscription.go @@ -53,6 +53,12 @@ func resourcePubsubSubscription() *schema.Resource { Required: true, ForceNew: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -68,8 +74,13 @@ func cleanAdditionalArgs(args map[string]interface{}) map[string]string { func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - name := fmt.Sprintf("projects/%s/subscriptions/%s", config.Project, d.Get("name").(string)) - computed_topic_name := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Get("topic").(string)) + project, err := getProject(d, config) + if err != nil { + return err + } + + name := fmt.Sprintf("projects/%s/subscriptions/%s", project, d.Get("name").(string)) + computed_topic_name := fmt.Sprintf("projects/%s/topics/%s", project, d.Get("topic").(string)) // process optional parameters var ackDeadlineSeconds int64 diff --git a/builtin/providers/google/resource_pubsub_topic.go b/builtin/providers/google/resource_pubsub_topic.go index 9d6a6a879..84932e4e9 100644 --- a/builtin/providers/google/resource_pubsub_topic.go +++ b/builtin/providers/google/resource_pubsub_topic.go @@ -19,6 +19,12 @@ func resourcePubsubTopic() *schema.Resource { Required: true, ForceNew: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -26,7 +32,12 @@ func resourcePubsubTopic() *schema.Resource { func resourcePubsubTopicCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - name := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Get("name").(string)) + project, err := getProject(d, config) + if err != nil { + return err + } + + name := fmt.Sprintf("projects/%s/topics/%s", project, d.Get("name").(string)) topic := &pubsub.Topic{} call := config.clientPubsub.Projects.Topics.Create(name, topic) diff --git a/builtin/providers/google/resource_sql_database.go b/builtin/providers/google/resource_sql_database.go index f66d3c584..8ef245b1d 100644 --- a/builtin/providers/google/resource_sql_database.go +++ b/builtin/providers/google/resource_sql_database.go @@ -31,6 +31,11 @@ func resourceSqlDatabase() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -38,9 +43,13 @@ func resourceSqlDatabase() *schema.Resource { func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + database_name := d.Get("name").(string) instance_name := d.Get("instance").(string) - project := config.Project db := &sqladmin.Database{ Name: database_name, @@ -69,9 +78,13 @@ func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error { func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + database_name := d.Get("name").(string) instance_name := d.Get("instance").(string) - project := config.Project db, err := config.clientSqlAdmin.Databases.Get(project, instance_name, database_name).Do() @@ -99,9 +112,13 @@ func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + database_name := d.Get("name").(string) instance_name := d.Get("instance").(string) - project := config.Project op, err := config.clientSqlAdmin.Databases.Delete(project, instance_name, database_name).Do() diff --git a/builtin/providers/google/resource_sql_database_instance.go b/builtin/providers/google/resource_sql_database_instance.go index e4d1c3087..a8945caad 100644 --- a/builtin/providers/google/resource_sql_database_instance.go +++ b/builtin/providers/google/resource_sql_database_instance.go @@ -245,6 +245,12 @@ func resourceSqlDatabaseInstance() *schema.Resource { }, }, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -252,6 +258,11 @@ func resourceSqlDatabaseInstance() *schema.Resource { func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + region := d.Get("region").(string) databaseVersion := d.Get("database_version").(string) @@ -468,7 +479,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) instance.MasterInstanceName = v.(string) } - op, err := config.clientSqlAdmin.Instances.Insert(config.Project, instance).Do() + op, err := config.clientSqlAdmin.Instances.Insert(project, instance).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 409 { return fmt.Errorf("Error, the name %s is unavailable because it was used recently", instance.Name) @@ -488,7 +499,12 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{}) func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - instance, err := config.clientSqlAdmin.Instances.Get(config.Project, + project, err := getProject(d, config) + if err != nil { + return err + } + + instance, err := config.clientSqlAdmin.Instances.Get(project, d.Get("name").(string)).Do() if err != nil { @@ -742,9 +758,15 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + d.Partial(true) - instance, err := config.clientSqlAdmin.Instances.Get(config.Project, + instance, err := config.clientSqlAdmin.Instances.Get(project, d.Get("name").(string)).Do() if err != nil { @@ -963,7 +985,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) d.Partial(false) - op, err := config.clientSqlAdmin.Instances.Update(config.Project, instance.Name, instance).Do() + op, err := config.clientSqlAdmin.Instances.Update(project, instance.Name, instance).Do() if err != nil { return fmt.Errorf("Error, failed to update instance %s: %s", instance.Name, err) } @@ -979,7 +1001,12 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{}) func resourceSqlDatabaseInstanceDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - op, err := config.clientSqlAdmin.Instances.Delete(config.Project, d.Get("name").(string)).Do() + project, err := getProject(d, config) + if err != nil { + return err + } + + op, err := config.clientSqlAdmin.Instances.Delete(project, d.Get("name").(string)).Do() if err != nil { return fmt.Errorf("Error, failed to delete instance %s: %s", d.Get("name").(string), err) diff --git a/builtin/providers/google/resource_sql_user.go b/builtin/providers/google/resource_sql_user.go index 06e76becc..b787ed040 100644 --- a/builtin/providers/google/resource_sql_user.go +++ b/builtin/providers/google/resource_sql_user.go @@ -40,6 +40,12 @@ func resourceSqlUser() *schema.Resource { Required: true, ForceNew: true, }, + + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -47,11 +53,15 @@ func resourceSqlUser() *schema.Resource { func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) instance := d.Get("instance").(string) password := d.Get("password").(string) host := d.Get("host").(string) - project := config.Project user := &sqladmin.User{ Name: name, @@ -81,9 +91,13 @@ func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error { func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) instance := d.Get("instance").(string) - project := config.Project users, err := config.clientSqlAdmin.Users.List(project, instance).Do() @@ -122,11 +136,15 @@ func resourceSqlUserUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) if d.HasChange("password") { + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) instance := d.Get("instance").(string) host := d.Get("host").(string) password := d.Get("password").(string) - project := config.Project user := &sqladmin.User{ Name: name, @@ -159,10 +177,14 @@ func resourceSqlUserUpdate(d *schema.ResourceData, meta interface{}) error { func resourceSqlUserDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + name := d.Get("name").(string) instance := d.Get("instance").(string) host := d.Get("host").(string) - project := config.Project op, err := config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do() diff --git a/builtin/providers/google/resource_storage_bucket.go b/builtin/providers/google/resource_storage_bucket.go index c4e64244f..105430765 100644 --- a/builtin/providers/google/resource_storage_bucket.go +++ b/builtin/providers/google/resource_storage_bucket.go @@ -61,6 +61,11 @@ func resourceStorageBucket() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "project": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }, } } @@ -68,6 +73,11 @@ func resourceStorageBucket() *schema.Resource { func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + project, err := getProject(d, config) + if err != nil { + return err + } + // Get the bucket and acl bucket := d.Get("name").(string) location := d.Get("location").(string) @@ -95,7 +105,7 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error } } - call := config.clientStorage.Buckets.Insert(config.Project, sb) + call := config.clientStorage.Buckets.Insert(project, sb) if v, ok := d.GetOk("predefined_acl"); ok { call = call.PredefinedAcl(v.(string)) }