diff --git a/builtin/providers/google/resource_compute_project_metadata.go b/builtin/providers/google/resource_compute_project_metadata.go index c2f8a4a5fa2e..a48a00fd6461 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,13 @@ 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 { + // 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_url_map.go b/builtin/providers/google/resource_compute_url_map.go index 4b29c4360d45..b28ac55a6050 100644 --- a/builtin/providers/google/resource_compute_url_map.go +++ b/builtin/providers/google/resource_compute_url_map.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/googleapi" "google.golang.org/api/compute/v1" ) @@ -292,6 +293,13 @@ 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 { + // 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 bd5350b9c312..3a7b4876cf71 100644 --- a/builtin/providers/google/resource_compute_vpn_gateway.go +++ b/builtin/providers/google/resource_compute_vpn_gateway.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" + "google.golang.org/api/googleapi" ) func resourceComputeVpnGateway() *schema.Resource { @@ -88,6 +89,13 @@ 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 { + // 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 172f96a907ab..8b83181b303b 100644 --- a/builtin/providers/google/resource_compute_vpn_tunnel.go +++ b/builtin/providers/google/resource_compute_vpn_tunnel.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" + "google.golang.org/api/googleapi" ) func resourceComputeVpnTunnel() *schema.Resource { @@ -118,6 +119,13 @@ 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 { + // 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 68c0b96ad0dd..2e084b37076d 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,13 @@ 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 { + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return err } diff --git a/builtin/providers/google/resource_dns_record_set.go b/builtin/providers/google/resource_dns_record_set.go index 05fa547f72a6..f07d64702b6d 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,13 @@ 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 { + // 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 e8715f9b0ccb..5af05a861db0 100644 --- a/builtin/providers/google/resource_sql_database.go +++ b/builtin/providers/google/resource_sql_database.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/sqladmin/v1beta4" + "google.golang.org/api/googleapi" ) func resourceSqlDatabase() *schema.Resource { @@ -75,6 +76,13 @@ func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { database_name).Do() if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + // 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 d684839283a4..44c40f3c33ca 100644 --- a/builtin/providers/google/resource_sql_database_instance.go +++ b/builtin/providers/google/resource_sql_database_instance.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/sqladmin/v1beta4" + "google.golang.org/api/googleapi" ) func resourceSqlDatabaseInstance() *schema.Resource { @@ -462,6 +463,13 @@ 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 { + // 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 9118119a8f8d..f7ce219bfe80 100644 --- a/builtin/providers/google/resource_storage_bucket.go +++ b/builtin/providers/google/resource_storage_bucket.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/storage/v1" + "google.golang.org/api/googleapi" ) func resourceStorageBucket() *schema.Resource { @@ -174,8 +175,14 @@ 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 { + // 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 3b866e0ad279..62a7f8529026 100644 --- a/builtin/providers/google/resource_storage_bucket_acl.go +++ b/builtin/providers/google/resource_storage_bucket_acl.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/storage/v1" + "google.golang.org/api/googleapi" ) func resourceStorageBucketAcl() *schema.Resource { @@ -166,6 +167,13 @@ 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 { + // 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 231153a85cd8..81f8f050f4a7 100644 --- a/builtin/providers/google/resource_storage_bucket_object.go +++ b/builtin/providers/google/resource_storage_bucket_object.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/storage/v1" + "google.golang.org/api/googleapi" ) func resourceStorageBucketObject() *schema.Resource { @@ -96,6 +97,13 @@ 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 { + // 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 5212f81db28c..9b86424b089e 100644 --- a/builtin/providers/google/resource_storage_object_acl.go +++ b/builtin/providers/google/resource_storage_object_acl.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/storage/v1" + "google.golang.org/api/googleapi" ) func resourceStorageObjectAcl() *schema.Resource { @@ -134,6 +135,13 @@ 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 { + // The resource doesn't exist anymore + d.SetId("") + + return nil + } + return err }