Skip to content

Commit

Permalink
Merge pull request #3913 from lwander/b-gcp-delete-behavior
Browse files Browse the repository at this point in the history
provider/google: Updated Read(..) behavior to handle deleted resources
  • Loading branch information
lwander committed Jan 7, 2016
2 parents 26e0a34 + cef0589 commit 01a5392
Show file tree
Hide file tree
Showing 32 changed files with 138 additions and 4 deletions.
1 change: 1 addition & 0 deletions builtin/providers/google/resource_compute_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions builtin/providers/google/resource_compute_autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions builtin/providers/google/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
2 changes: 2 additions & 0 deletions builtin/providers/google/resource_compute_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"bytes"
"fmt"
"log"
"sort"

"github.com/hashicorp/terraform/helper/hashcode"
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
1 change: 1 addition & 0 deletions builtin/providers/google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -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("")

Expand Down
1 change: 1 addition & 0 deletions builtin/providers/google/resource_compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
11 changes: 9 additions & 2 deletions builtin/providers/google/resource_compute_project_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions builtin/providers/google/resource_compute_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
2 changes: 2 additions & 0 deletions builtin/providers/google/resource_compute_ssl_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
1 change: 1 addition & 0 deletions builtin/providers/google/resource_compute_target_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
10 changes: 10 additions & 0 deletions builtin/providers/google/resource_compute_url_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions builtin/providers/google/resource_compute_vpn_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
10 changes: 10 additions & 0 deletions builtin/providers/google/resource_compute_vpn_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
9 changes: 9 additions & 0 deletions builtin/providers/google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions builtin/providers/google/resource_dns_managed_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
9 changes: 9 additions & 0 deletions builtin/providers/google/resource_dns_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions builtin/providers/google/resource_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions builtin/providers/google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
Loading

0 comments on commit 01a5392

Please sign in to comment.