Skip to content

Commit

Permalink
provider/google: Updated Read(..) behavior to handle deleted resources
Browse files Browse the repository at this point in the history
  • Loading branch information
lwander authored and Lars Wander committed Jan 7, 2016
1 parent 9697622 commit b52c28c
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 4 deletions.
10 changes: 8 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,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)
}

Expand Down
8 changes: 8 additions & 0 deletions builtin/providers/google/resource_compute_url_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"

"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/googleapi"
"google.golang.org/api/compute/v1"
)

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

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

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

Expand Down
8 changes: 8 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,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
}

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

Expand Down
8 changes: 8 additions & 0 deletions builtin/providers/google/resource_storage_bucket_object.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/storage/v1"
"google.golang.org/api/googleapi"
)

func resourceStorageBucketObject() *schema.Resource {
Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions builtin/providers/google/resource_storage_object_acl.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/storage/v1"
"google.golang.org/api/googleapi"
)

func resourceStorageObjectAcl() *schema.Resource {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit b52c28c

Please sign in to comment.