From cef05894987ef1bdd332b509e16e92c8b809a7be Mon Sep 17 00:00:00 2001 From: Lars Wander Date: Fri, 13 Nov 2015 15:36:03 -0500 Subject: [PATCH] provider/google: Updated Read(..) behavior to handle deleted resources --- builtin/providers/google/resource_compute_address.go | 1 + .../providers/google/resource_compute_autoscaler.go | 1 + .../google/resource_compute_backend_service.go | 1 + builtin/providers/google/resource_compute_disk.go | 1 + .../providers/google/resource_compute_firewall.go | 2 ++ .../google/resource_compute_forwarding_rule.go | 1 + .../google/resource_compute_global_address.go | 1 + .../resource_compute_global_forwarding_rule.go | 1 + .../google/resource_compute_http_health_check.go | 1 + .../google/resource_compute_https_health_check.go | 1 + .../providers/google/resource_compute_instance.go | 1 + .../resource_compute_instance_group_manager.go | 1 + .../google/resource_compute_instance_template.go | 2 ++ builtin/providers/google/resource_compute_network.go | 1 + .../google/resource_compute_project_metadata.go | 11 +++++++++-- builtin/providers/google/resource_compute_route.go | 1 + .../google/resource_compute_ssl_certificate.go | 2 ++ .../google/resource_compute_target_http_proxy.go | 1 + .../google/resource_compute_target_https_proxy.go | 1 + .../providers/google/resource_compute_target_pool.go | 1 + builtin/providers/google/resource_compute_url_map.go | 10 ++++++++++ .../providers/google/resource_compute_vpn_gateway.go | 10 ++++++++++ .../providers/google/resource_compute_vpn_tunnel.go | 10 ++++++++++ .../providers/google/resource_container_cluster.go | 9 +++++++++ .../providers/google/resource_dns_managed_zone.go | 1 + builtin/providers/google/resource_dns_record_set.go | 9 +++++++++ builtin/providers/google/resource_sql_database.go | 10 ++++++++++ .../google/resource_sql_database_instance.go | 10 ++++++++++ builtin/providers/google/resource_storage_bucket.go | 12 ++++++++++-- .../providers/google/resource_storage_bucket_acl.go | 9 +++++++++ .../google/resource_storage_bucket_object.go | 10 ++++++++++ .../providers/google/resource_storage_object_acl.go | 9 +++++++++ 32 files changed, 138 insertions(+), 4 deletions(-) diff --git a/builtin/providers/google/resource_compute_address.go b/builtin/providers/google/resource_compute_address.go index 0027df230..15fa13272 100644 --- a/builtin/providers/google/resource_compute_address.go +++ b/builtin/providers/google/resource_compute_address.go @@ -82,6 +82,7 @@ func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore + log.Printf("[WARN] Removing Address %q because it's gone", d.Get("name").(string)) d.SetId("") return nil diff --git a/builtin/providers/google/resource_compute_autoscaler.go b/builtin/providers/google/resource_compute_autoscaler.go index 8539c62b3..89cc41b07 100644 --- a/builtin/providers/google/resource_compute_autoscaler.go +++ b/builtin/providers/google/resource_compute_autoscaler.go @@ -240,6 +240,7 @@ func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) err if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore + log.Printf("[WARN] Removing Autoscalar %q because it's gone", d.Get("name").(string)) d.SetId("") return nil diff --git a/builtin/providers/google/resource_compute_backend_service.go b/builtin/providers/google/resource_compute_backend_service.go index ead6e2402..e4c1586d7 100644 --- a/builtin/providers/google/resource_compute_backend_service.go +++ b/builtin/providers/google/resource_compute_backend_service.go @@ -186,6 +186,7 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore + log.Printf("[WARN] Removing Backend Service %q because it's gone", d.Get("name").(string)) d.SetId("") return nil diff --git a/builtin/providers/google/resource_compute_disk.go b/builtin/providers/google/resource_compute_disk.go index 1118702d6..1df66b9bb 100644 --- a/builtin/providers/google/resource_compute_disk.go +++ b/builtin/providers/google/resource_compute_disk.go @@ -141,6 +141,7 @@ func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error { config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_firewall.go b/builtin/providers/google/resource_compute_firewall.go index 1cec2c826..f2f4fa73d 100644 --- a/builtin/providers/google/resource_compute_firewall.go +++ b/builtin/providers/google/resource_compute_firewall.go @@ -3,6 +3,7 @@ package google import ( "bytes" "fmt" + "log" "sort" "github.com/hashicorp/terraform/helper/hashcode" @@ -150,6 +151,7 @@ func resourceComputeFirewallRead(d *schema.ResourceData, meta interface{}) error if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore + log.Printf("[WARN] Removing Firewall %q because it's gone", d.Get("name").(string)) d.SetId("") return nil diff --git a/builtin/providers/google/resource_compute_forwarding_rule.go b/builtin/providers/google/resource_compute_forwarding_rule.go index ac4851e51..e1cbdc46c 100644 --- a/builtin/providers/google/resource_compute_forwarding_rule.go +++ b/builtin/providers/google/resource_compute_forwarding_rule.go @@ -139,6 +139,7 @@ func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_global_address.go b/builtin/providers/google/resource_compute_global_address.go index 74c0633cd..58d3f5e8e 100644 --- a/builtin/providers/google/resource_compute_global_address.go +++ b/builtin/providers/google/resource_compute_global_address.go @@ -64,6 +64,7 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_global_forwarding_rule.go b/builtin/providers/google/resource_compute_global_forwarding_rule.go index f4d3c21bf..ce987f716 100644 --- a/builtin/providers/google/resource_compute_global_forwarding_rule.go +++ b/builtin/providers/google/resource_compute_global_forwarding_rule.go @@ -131,6 +131,7 @@ func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interf config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_http_health_check.go b/builtin/providers/google/resource_compute_http_health_check.go index c53267afd..8ddae0b70 100644 --- a/builtin/providers/google/resource_compute_http_health_check.go +++ b/builtin/providers/google/resource_compute_http_health_check.go @@ -187,6 +187,7 @@ func resourceComputeHttpHealthCheckRead(d *schema.ResourceData, meta interface{} if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { // The resource doesn't exist anymore + log.Printf("[WARN] Removing HTTP Health Check %q because it's gone", d.Get("name").(string)) d.SetId("") return nil diff --git a/builtin/providers/google/resource_compute_https_health_check.go b/builtin/providers/google/resource_compute_https_health_check.go index 32a8dfb38..46affdd9e 100644 --- a/builtin/providers/google/resource_compute_https_health_check.go +++ b/builtin/providers/google/resource_compute_https_health_check.go @@ -186,6 +186,7 @@ func resourceComputeHttpsHealthCheckRead(d *schema.ResourceData, meta interface{ config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 66e0b5e85..56026d3b3 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -285,6 +285,7 @@ func getInstance(config *Config, d *schema.ResourceData) (*compute.Instance, err config.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)) // The resource doesn't exist anymore id := d.Id() d.SetId("") diff --git a/builtin/providers/google/resource_compute_instance_group_manager.go b/builtin/providers/google/resource_compute_instance_group_manager.go index e8e6b33a5..25a1ced50 100644 --- a/builtin/providers/google/resource_compute_instance_group_manager.go +++ b/builtin/providers/google/resource_compute_instance_group_manager.go @@ -149,6 +149,7 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_instance_template.go b/builtin/providers/google/resource_compute_instance_template.go index 48be445cb..07bcb5f4c 100644 --- a/builtin/providers/google/resource_compute_instance_template.go +++ b/builtin/providers/google/resource_compute_instance_template.go @@ -2,6 +2,7 @@ package google import ( "fmt" + "log" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" @@ -466,6 +467,7 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{ config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_network.go b/builtin/providers/google/resource_compute_network.go index 5a61f2ad6..a3c72aa11 100644 --- a/builtin/providers/google/resource_compute_network.go +++ b/builtin/providers/google/resource_compute_network.go @@ -74,6 +74,7 @@ func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_project_metadata.go b/builtin/providers/google/resource_compute_project_metadata.go index c549415c2..c2508c8f3 100644 --- a/builtin/providers/google/resource_compute_project_metadata.go +++ b/builtin/providers/google/resource_compute_project_metadata.go @@ -4,10 +4,9 @@ import ( "fmt" "log" - // "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - // "google.golang.org/api/googleapi" + "google.golang.org/api/googleapi" ) func resourceComputeProjectMetadata() *schema.Resource { @@ -85,6 +84,14 @@ func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] Loading project service: %s", config.Project) project, err := config.clientCompute.Projects.Get(config.Project).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing Project Metadata because it's gone") + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return fmt.Errorf("Error loading project '%s': %s", config.Project, err) } diff --git a/builtin/providers/google/resource_compute_route.go b/builtin/providers/google/resource_compute_route.go index 82b43d358..9b5b5292f 100644 --- a/builtin/providers/google/resource_compute_route.go +++ b/builtin/providers/google/resource_compute_route.go @@ -185,6 +185,7 @@ func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error { config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_ssl_certificate.go b/builtin/providers/google/resource_compute_ssl_certificate.go index 05de350fa..a80bc2fb2 100644 --- a/builtin/providers/google/resource_compute_ssl_certificate.go +++ b/builtin/providers/google/resource_compute_ssl_certificate.go @@ -2,6 +2,7 @@ package google import ( "fmt" + "log" "strconv" "github.com/hashicorp/terraform/helper/schema" @@ -91,6 +92,7 @@ func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_target_http_proxy.go b/builtin/providers/google/resource_compute_target_http_proxy.go index 6cf2ccf5d..72644fb01 100644 --- a/builtin/providers/google/resource_compute_target_http_proxy.go +++ b/builtin/providers/google/resource_compute_target_http_proxy.go @@ -111,6 +111,7 @@ func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{} config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_target_https_proxy.go b/builtin/providers/google/resource_compute_target_https_proxy.go index 1ea844441..b30fd1eab 100644 --- a/builtin/providers/google/resource_compute_target_https_proxy.go +++ b/builtin/providers/google/resource_compute_target_https_proxy.go @@ -186,6 +186,7 @@ func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{ config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_target_pool.go b/builtin/providers/google/resource_compute_target_pool.go index 91e83a46a..fa25a1b72 100644 --- a/builtin/providers/google/resource_compute_target_pool.go +++ b/builtin/providers/google/resource_compute_target_pool.go @@ -330,6 +330,7 @@ func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) err config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_compute_url_map.go b/builtin/providers/google/resource_compute_url_map.go index 4b29c4360..47a38431f 100644 --- a/builtin/providers/google/resource_compute_url_map.go +++ b/builtin/providers/google/resource_compute_url_map.go @@ -2,10 +2,12 @@ package google import ( "fmt" + "log" "strconv" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" + "google.golang.org/api/googleapi" ) func resourceComputeUrlMap() *schema.Resource { @@ -292,6 +294,14 @@ func resourceComputeUrlMapRead(d *schema.ResourceData, meta interface{}) error { urlMap, err := config.clientCompute.UrlMaps.Get(config.Project, name).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing URL Map %q because it's gone", d.Get("name").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return fmt.Errorf("Error, failed to get 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 bd5350b9c..697ec8b64 100644 --- a/builtin/providers/google/resource_compute_vpn_gateway.go +++ b/builtin/providers/google/resource_compute_vpn_gateway.go @@ -2,10 +2,12 @@ package google import ( "fmt" + "log" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" + "google.golang.org/api/googleapi" ) func resourceComputeVpnGateway() *schema.Resource { @@ -88,6 +90,14 @@ func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) err vpnGateway, err := vpnGatewaysService.Get(project, region, name).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing VPN Gateway %q because it's gone", d.Get("name").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err) } diff --git a/builtin/providers/google/resource_compute_vpn_tunnel.go b/builtin/providers/google/resource_compute_vpn_tunnel.go index 172f96a90..f6290504b 100644 --- a/builtin/providers/google/resource_compute_vpn_tunnel.go +++ b/builtin/providers/google/resource_compute_vpn_tunnel.go @@ -2,10 +2,12 @@ package google import ( "fmt" + "log" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" + "google.golang.org/api/googleapi" ) func resourceComputeVpnTunnel() *schema.Resource { @@ -118,6 +120,14 @@ func resourceComputeVpnTunnelRead(d *schema.ResourceData, meta interface{}) erro vpnTunnel, err := vpnTunnelsService.Get(project, region, name).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing VPN Tunnel %q because it's gone", d.Get("name").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return fmt.Errorf("Error Reading VPN Tunnel %s: %s", name, err) } diff --git a/builtin/providers/google/resource_container_cluster.go b/builtin/providers/google/resource_container_cluster.go index 68c0b96ad..447583b9e 100644 --- a/builtin/providers/google/resource_container_cluster.go +++ b/builtin/providers/google/resource_container_cluster.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/container/v1" + "google.golang.org/api/googleapi" ) func resourceContainerCluster() *schema.Resource { @@ -312,6 +313,14 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro cluster, err := config.clientContainer.Projects.Zones.Clusters.Get( config.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)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return err } diff --git a/builtin/providers/google/resource_dns_managed_zone.go b/builtin/providers/google/resource_dns_managed_zone.go index 7253297e6..6d76c0c44 100644 --- a/builtin/providers/google/resource_dns_managed_zone.go +++ b/builtin/providers/google/resource_dns_managed_zone.go @@ -81,6 +81,7 @@ func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error config.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)) // The resource doesn't exist anymore d.SetId("") diff --git a/builtin/providers/google/resource_dns_record_set.go b/builtin/providers/google/resource_dns_record_set.go index 05fa547f7..49b1fce71 100644 --- a/builtin/providers/google/resource_dns_record_set.go +++ b/builtin/providers/google/resource_dns_record_set.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/dns/v1" + "google.golang.org/api/googleapi" ) func resourceDnsRecordSet() *schema.Resource { @@ -114,6 +115,14 @@ func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error { resp, err := config.clientDns.ResourceRecordSets.List( config.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)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return fmt.Errorf("Error reading DNS RecordSet: %#v", err) } if len(resp.Rrsets) == 0 { diff --git a/builtin/providers/google/resource_sql_database.go b/builtin/providers/google/resource_sql_database.go index e8715f9b0..f66d3c584 100644 --- a/builtin/providers/google/resource_sql_database.go +++ b/builtin/providers/google/resource_sql_database.go @@ -2,9 +2,11 @@ package google import ( "fmt" + "log" "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/googleapi" "google.golang.org/api/sqladmin/v1beta4" ) @@ -75,6 +77,14 @@ func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { database_name).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing SQL Database %q because it's gone", d.Get("name").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return fmt.Errorf("Error, failed to get"+ "database %s in instance %s: %s", database_name, instance_name, err) diff --git a/builtin/providers/google/resource_sql_database_instance.go b/builtin/providers/google/resource_sql_database_instance.go index d68483928..ff8529944 100644 --- a/builtin/providers/google/resource_sql_database_instance.go +++ b/builtin/providers/google/resource_sql_database_instance.go @@ -2,9 +2,11 @@ package google import ( "fmt" + "log" "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/googleapi" "google.golang.org/api/sqladmin/v1beta4" ) @@ -462,6 +464,14 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e d.Get("name").(string)).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing SQL Database %q because it's gone", d.Get("name").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return fmt.Errorf("Error retrieving instance %s: %s", d.Get("name").(string), err) } diff --git a/builtin/providers/google/resource_storage_bucket.go b/builtin/providers/google/resource_storage_bucket.go index 9118119a8..c4e64244f 100644 --- a/builtin/providers/google/resource_storage_bucket.go +++ b/builtin/providers/google/resource_storage_bucket.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/googleapi" "google.golang.org/api/storage/v1" ) @@ -174,8 +175,15 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error { res, err := config.clientStorage.Buckets.Get(bucket).Do() if err != nil { - fmt.Printf("Error reading bucket %s: %v", bucket, err) - return err + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing Bucket %q because it's gone", d.Get("name").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + + return fmt.Errorf("Error reading bucket %s: %v", bucket, err) } log.Printf("[DEBUG] Read bucket %v at location %v\n\n", res.Name, res.SelfLink) diff --git a/builtin/providers/google/resource_storage_bucket_acl.go b/builtin/providers/google/resource_storage_bucket_acl.go index 3b866e0ad..488fd85f4 100644 --- a/builtin/providers/google/resource_storage_bucket_acl.go +++ b/builtin/providers/google/resource_storage_bucket_acl.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/googleapi" "google.golang.org/api/storage/v1" ) @@ -166,6 +167,14 @@ func resourceStorageBucketAclRead(d *schema.ResourceData, meta interface{}) erro res, err := config.clientStorage.BucketAccessControls.List(bucket).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing Bucket ACL for bucket %q because it's gone", d.Get("bucket").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return err } diff --git a/builtin/providers/google/resource_storage_bucket_object.go b/builtin/providers/google/resource_storage_bucket_object.go index 231153a85..198d7b685 100644 --- a/builtin/providers/google/resource_storage_bucket_object.go +++ b/builtin/providers/google/resource_storage_bucket_object.go @@ -2,10 +2,12 @@ package google import ( "fmt" + "log" "os" "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/googleapi" "google.golang.org/api/storage/v1" ) @@ -96,6 +98,14 @@ func resourceStorageBucketObjectRead(d *schema.ResourceData, meta interface{}) e res, err := getCall.Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing Bucket Object %q because it's gone", d.Get("name").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return fmt.Errorf("Error retrieving contents of object %s: %s", name, err) } diff --git a/builtin/providers/google/resource_storage_object_acl.go b/builtin/providers/google/resource_storage_object_acl.go index 5212f81db..e4968265f 100644 --- a/builtin/providers/google/resource_storage_object_acl.go +++ b/builtin/providers/google/resource_storage_object_acl.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/googleapi" "google.golang.org/api/storage/v1" ) @@ -134,6 +135,14 @@ func resourceStorageObjectAclRead(d *schema.ResourceData, meta interface{}) erro res, err := config.clientStorage.ObjectAccessControls.List(bucket, object).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + log.Printf("[WARN] Removing Storage Object ACL for Bucket %q because it's gone", d.Get("bucket").(string)) + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return err }