From f2d7900e63b4714e28a1f56046322059fc0c90f2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 25 Aug 2014 22:49:14 -0700 Subject: [PATCH] providers/google: on refresh, treat 404 as resource gone --- builtin/providers/google/resource_compute_address.go | 8 ++++++++ builtin/providers/google/resource_compute_disk.go | 8 ++++++++ builtin/providers/google/resource_compute_firewall.go | 8 ++++++++ builtin/providers/google/resource_compute_instance.go | 8 ++++++++ builtin/providers/google/resource_compute_network.go | 8 ++++++++ builtin/providers/google/resource_compute_route.go | 8 ++++++++ 6 files changed, 48 insertions(+) diff --git a/builtin/providers/google/resource_compute_address.go b/builtin/providers/google/resource_compute_address.go index cd14e6c0f..a8f1ecf0c 100644 --- a/builtin/providers/google/resource_compute_address.go +++ b/builtin/providers/google/resource_compute_address.go @@ -6,6 +6,7 @@ import ( "time" "code.google.com/p/google-api-go-client/compute/v1" + "code.google.com/p/google-api-go-client/googleapi" "github.com/hashicorp/terraform/helper/schema" ) @@ -78,6 +79,13 @@ func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error addr, err := config.clientCompute.Addresses.Get( config.Project, config.Region, d.Id()).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 address: %s", err) } diff --git a/builtin/providers/google/resource_compute_disk.go b/builtin/providers/google/resource_compute_disk.go index f5e6dc4f2..e69cf9a16 100644 --- a/builtin/providers/google/resource_compute_disk.go +++ b/builtin/providers/google/resource_compute_disk.go @@ -6,6 +6,7 @@ import ( "time" "code.google.com/p/google-api-go-client/compute/v1" + "code.google.com/p/google-api-go-client/googleapi" "github.com/hashicorp/terraform/helper/schema" ) @@ -107,6 +108,13 @@ func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error { _, err := config.clientCompute.Disks.Get( config.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 + d.SetId("") + + return nil + } + return fmt.Errorf("Error reading disk: %s", err) } diff --git a/builtin/providers/google/resource_compute_firewall.go b/builtin/providers/google/resource_compute_firewall.go index 738a937ce..355c48084 100644 --- a/builtin/providers/google/resource_compute_firewall.go +++ b/builtin/providers/google/resource_compute_firewall.go @@ -7,6 +7,7 @@ import ( "time" "code.google.com/p/google-api-go-client/compute/v1" + "code.google.com/p/google-api-go-client/googleapi" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" ) @@ -193,6 +194,13 @@ func resourceComputeFirewallRead(d *schema.ResourceData, meta interface{}) error _, err := config.clientCompute.Firewalls.Get( config.Project, d.Id()).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 firewall: %s", err) } diff --git a/builtin/providers/google/resource_compute_instance.go b/builtin/providers/google/resource_compute_instance.go index 1aebc87a3..efc627f8d 100644 --- a/builtin/providers/google/resource_compute_instance.go +++ b/builtin/providers/google/resource_compute_instance.go @@ -6,6 +6,7 @@ import ( "time" "code.google.com/p/google-api-go-client/compute/v1" + "code.google.com/p/google-api-go-client/googleapi" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" ) @@ -304,6 +305,13 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error instance, err := config.clientCompute.Instances.Get( config.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 + d.SetId("") + + return nil + } + return fmt.Errorf("Error reading instance: %s", err) } diff --git a/builtin/providers/google/resource_compute_network.go b/builtin/providers/google/resource_compute_network.go index 52e6d714a..b79ac2ade 100644 --- a/builtin/providers/google/resource_compute_network.go +++ b/builtin/providers/google/resource_compute_network.go @@ -6,6 +6,7 @@ import ( "time" "code.google.com/p/google-api-go-client/compute/v1" + "code.google.com/p/google-api-go-client/googleapi" "github.com/hashicorp/terraform/helper/schema" ) @@ -86,6 +87,13 @@ func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error network, err := config.clientCompute.Networks.Get( config.Project, d.Id()).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 network: %s", err) } diff --git a/builtin/providers/google/resource_compute_route.go b/builtin/providers/google/resource_compute_route.go index 8fc5c5c44..0c15dbaa4 100644 --- a/builtin/providers/google/resource_compute_route.go +++ b/builtin/providers/google/resource_compute_route.go @@ -6,6 +6,7 @@ import ( "time" "code.google.com/p/google-api-go-client/compute/v1" + "code.google.com/p/google-api-go-client/googleapi" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" ) @@ -185,6 +186,13 @@ func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error { _, err := config.clientCompute.Routes.Get( config.Project, d.Id()).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 route: %#v", err) }