diff --git a/builtin/providers/google/resource_bigquery_dataset.go b/builtin/providers/google/resource_bigquery_dataset.go index 69cfdbb4c..8080b8dbc 100644 --- a/builtin/providers/google/resource_bigquery_dataset.go +++ b/builtin/providers/google/resource_bigquery_dataset.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" "google.golang.org/api/bigquery/v2" - "google.golang.org/api/googleapi" ) func resourceBigQueryDataset() *schema.Resource { @@ -225,15 +224,7 @@ func resourceBigQueryDatasetRead(d *schema.ResourceData, meta interface{}) error res, err := config.clientBigQuery.Datasets.Get(projectID, datasetID).Do() if err != nil { - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { - log.Printf("[WARN] Removing BigQuery dataset %q because it's gone", datasetID) - // The resource doesn't exist anymore - d.SetId("") - - return nil - } - - return err + return handleNotFoundError(err, d, fmt.Sprintf("BigQuery dataset %q", datasetID)) } d.Set("etag", res.Etag) diff --git a/builtin/providers/google/resource_compute_address.go b/builtin/providers/google/resource_compute_address.go index 27b4c1805..d23b9549b 100644 --- a/builtin/providers/google/resource_compute_address.go +++ b/builtin/providers/google/resource_compute_address.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeAddress() *schema.Resource { @@ -97,15 +96,7 @@ func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error addr, err := config.clientCompute.Addresses.Get( project, region, d.Id()).Do() 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 - } - - return fmt.Errorf("Error reading address: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Address %q", d.Get("name").(string))) } d.Set("address", addr.Address) diff --git a/builtin/providers/google/resource_compute_backend_bucket.go b/builtin/providers/google/resource_compute_backend_bucket.go index 8741f7f07..9849402e8 100644 --- a/builtin/providers/google/resource_compute_backend_bucket.go +++ b/builtin/providers/google/resource_compute_backend_bucket.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeBackendBucket() *schema.Resource { @@ -118,15 +117,7 @@ func resourceComputeBackendBucketRead(d *schema.ResourceData, meta interface{}) bucket, err := config.clientCompute.BackendBuckets.Get( project, d.Id()).Do() if err != nil { - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { - // The resource doesn't exist anymore - log.Printf("[WARN] Removing Backend Bucket %q because it's gone", d.Get("name").(string)) - d.SetId("") - - return nil - } - - return fmt.Errorf("Error reading bucket: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Backend Bucket %q", d.Get("name").(string))) } d.Set("bucket_name", bucket.BucketName) diff --git a/builtin/providers/google/resource_compute_backend_service.go b/builtin/providers/google/resource_compute_backend_service.go index 16ff99219..64d3fa84c 100644 --- a/builtin/providers/google/resource_compute_backend_service.go +++ b/builtin/providers/google/resource_compute_backend_service.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeBackendService() *schema.Resource { @@ -225,15 +224,7 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{}) service, err := config.clientCompute.BackendServices.Get( project, d.Id()).Do() 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 - } - - return fmt.Errorf("Error reading service: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Backend Service %q", d.Get("name").(string))) } d.Set("description", service.Description) diff --git a/builtin/providers/google/resource_compute_disk.go b/builtin/providers/google/resource_compute_disk.go index 36554ca73..2b4148baa 100644 --- a/builtin/providers/google/resource_compute_disk.go +++ b/builtin/providers/google/resource_compute_disk.go @@ -174,15 +174,7 @@ func resourceComputeDiskRead(d *schema.ResourceData, meta interface{}) error { disk, err := config.clientCompute.Disks.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading disk: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Disk %q", d.Get("name").(string))) } d.Set("self_link", disk.SelfLink) diff --git a/builtin/providers/google/resource_compute_firewall.go b/builtin/providers/google/resource_compute_firewall.go index a47da5573..c276d86c1 100644 --- a/builtin/providers/google/resource_compute_firewall.go +++ b/builtin/providers/google/resource_compute_firewall.go @@ -3,14 +3,12 @@ package google import ( "bytes" "fmt" - "log" "sort" "strings" "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeFirewall() *schema.Resource { @@ -171,15 +169,7 @@ func resourceComputeFirewallRead(d *schema.ResourceData, meta interface{}) error firewall, err := config.clientCompute.Firewalls.Get( project, d.Id()).Do() 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 - } - - return fmt.Errorf("Error reading firewall: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Firewall %q", d.Get("name").(string))) } networkUrl := strings.Split(firewall.Network, "/") diff --git a/builtin/providers/google/resource_compute_forwarding_rule.go b/builtin/providers/google/resource_compute_forwarding_rule.go index 529588fdc..696bd62a3 100644 --- a/builtin/providers/google/resource_compute_forwarding_rule.go +++ b/builtin/providers/google/resource_compute_forwarding_rule.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeForwardingRule() *schema.Resource { @@ -226,15 +225,7 @@ func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) frule, err := config.clientCompute.ForwardingRules.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading ForwardingRule: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Forwarding Rule %q", d.Get("name").(string))) } d.Set("name", frule.Name) diff --git a/builtin/providers/google/resource_compute_global_address.go b/builtin/providers/google/resource_compute_global_address.go index bf6a6a6d6..db3a1798e 100644 --- a/builtin/providers/google/resource_compute_global_address.go +++ b/builtin/providers/google/resource_compute_global_address.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeGlobalAddress() *schema.Resource { @@ -81,15 +80,7 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) addr, err := config.clientCompute.GlobalAddresses.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading address: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Global Address %q", d.Get("name").(string))) } d.Set("address", addr.Address) diff --git a/builtin/providers/google/resource_compute_global_forwarding_rule.go b/builtin/providers/google/resource_compute_global_forwarding_rule.go index e70c88377..7f86adbb1 100644 --- a/builtin/providers/google/resource_compute_global_forwarding_rule.go +++ b/builtin/providers/google/resource_compute_global_forwarding_rule.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeGlobalForwardingRule() *schema.Resource { @@ -152,15 +151,7 @@ func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interf frule, err := config.clientCompute.GlobalForwardingRules.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Global Forwarding Rule %q", d.Get("name").(string))) } d.Set("ip_address", frule.IPAddress) diff --git a/builtin/providers/google/resource_compute_health_check.go b/builtin/providers/google/resource_compute_health_check.go index de8d7d428..a0ac940d6 100644 --- a/builtin/providers/google/resource_compute_health_check.go +++ b/builtin/providers/google/resource_compute_health_check.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeHealthCheck() *schema.Resource { @@ -442,15 +441,7 @@ func resourceComputeHealthCheckRead(d *schema.ResourceData, meta interface{}) er hchk, err := config.clientCompute.HealthChecks.Get( project, d.Id()).Do() if err != nil { - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { - // The resource doesn't exist anymore - log.Printf("[WARN] Removing Health Check %q because it's gone", d.Get("name").(string)) - d.SetId("") - - return nil - } - - return fmt.Errorf("Error reading HealthCheck: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Health Check %q", d.Get("name").(string))) } d.Set("check_interval_sec", hchk.CheckIntervalSec) diff --git a/builtin/providers/google/resource_compute_http_health_check.go b/builtin/providers/google/resource_compute_http_health_check.go index 0802db8bc..e3e8235a4 100644 --- a/builtin/providers/google/resource_compute_http_health_check.go +++ b/builtin/providers/google/resource_compute_http_health_check.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeHttpHealthCheck() *schema.Resource { @@ -210,15 +209,7 @@ func resourceComputeHttpHealthCheckRead(d *schema.ResourceData, meta interface{} hchk, err := config.clientCompute.HttpHealthChecks.Get( project, d.Id()).Do() 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 - } - - return fmt.Errorf("Error reading HttpHealthCheck: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("HTTP Health Check %q", d.Get("name").(string))) } d.Set("host", hchk.Host) diff --git a/builtin/providers/google/resource_compute_https_health_check.go b/builtin/providers/google/resource_compute_https_health_check.go index 0746d542b..769606268 100644 --- a/builtin/providers/google/resource_compute_https_health_check.go +++ b/builtin/providers/google/resource_compute_https_health_check.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeHttpsHealthCheck() *schema.Resource { @@ -206,15 +205,7 @@ func resourceComputeHttpsHealthCheckRead(d *schema.ResourceData, meta interface{ hchk, err := config.clientCompute.HttpsHealthChecks.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading HttpsHealthCheck: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("HTTPS Health Check %q", d.Get("name").(string))) } d.Set("host", hchk.Host) diff --git a/builtin/providers/google/resource_compute_image.go b/builtin/providers/google/resource_compute_image.go index 9cf172669..9e5b14197 100644 --- a/builtin/providers/google/resource_compute_image.go +++ b/builtin/providers/google/resource_compute_image.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeImage() *schema.Resource { @@ -164,15 +163,7 @@ func resourceComputeImageRead(d *schema.ResourceData, meta interface{}) error { image, err := config.clientCompute.Images.Get( project, d.Id()).Do() if err != nil { - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { - // The resource doesn't exist anymore - log.Printf("[WARN] Removing Image %q because it's gone", d.Get("name").(string)) - d.SetId("") - - return nil - } - - return fmt.Errorf("Error reading image: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Image %q", d.Get("name").(string))) } d.Set("self_link", image.SelfLink) diff --git a/builtin/providers/google/resource_compute_instance_group.go b/builtin/providers/google/resource_compute_instance_group.go index 1f2b93e06..6241196ca 100644 --- a/builtin/providers/google/resource_compute_instance_group.go +++ b/builtin/providers/google/resource_compute_instance_group.go @@ -183,14 +183,7 @@ func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{}) instanceGroup, err := config.clientCompute.InstanceGroups.Get( 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 InstanceGroup: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Instance Group %q", d.Get("name").(string))) } // retreive instance group members diff --git a/builtin/providers/google/resource_compute_instance_template.go b/builtin/providers/google/resource_compute_instance_template.go index f4c2dd327..7b38a5b0c 100644 --- a/builtin/providers/google/resource_compute_instance_template.go +++ b/builtin/providers/google/resource_compute_instance_template.go @@ -2,13 +2,11 @@ package google import ( "fmt" - "log" "strings" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeInstanceTemplate() *schema.Resource { @@ -721,14 +719,7 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{ instanceTemplate, err := config.clientCompute.InstanceTemplates.Get( 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("") - return nil - } - - return fmt.Errorf("Error reading instance template: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Instance Template %q", d.Get("name").(string))) } // Set the metadata fingerprint if there is one. diff --git a/builtin/providers/google/resource_compute_network.go b/builtin/providers/google/resource_compute_network.go index ccd75ae08..d0fef1753 100644 --- a/builtin/providers/google/resource_compute_network.go +++ b/builtin/providers/google/resource_compute_network.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeNetwork() *schema.Resource { @@ -132,15 +131,7 @@ func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error network, err := config.clientCompute.Networks.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading network: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Network %q", d.Get("name").(string))) } d.Set("gateway_ipv4", network.GatewayIPv4) diff --git a/builtin/providers/google/resource_compute_project_metadata.go b/builtin/providers/google/resource_compute_project_metadata.go index 6b867e1ad..07e3ee1cb 100644 --- a/builtin/providers/google/resource_compute_project_metadata.go +++ b/builtin/providers/google/resource_compute_project_metadata.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeProjectMetadata() *schema.Resource { @@ -100,15 +99,7 @@ func resourceComputeProjectMetadataRead(d *schema.ResourceData, meta interface{} log.Printf("[DEBUG] Loading project service: %s", projectID) project, err := config.clientCompute.Projects.Get(projectID).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", projectID, err) + return handleNotFoundError(err, d, fmt.Sprintf("Project metadata for project %q", projectID)) } md := project.CommonInstanceMetadata diff --git a/builtin/providers/google/resource_compute_region_backend_service.go b/builtin/providers/google/resource_compute_region_backend_service.go index 8fd3950f7..682cd0fab 100644 --- a/builtin/providers/google/resource_compute_region_backend_service.go +++ b/builtin/providers/google/resource_compute_region_backend_service.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeRegionBackendService() *schema.Resource { @@ -189,15 +188,7 @@ func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interf service, err := config.clientCompute.RegionBackendServices.Get( project, region, d.Id()).Do() 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 - } - - return fmt.Errorf("Error reading service: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Region Backend Service %q", d.Get("name").(string))) } d.Set("description", service.Description) diff --git a/builtin/providers/google/resource_compute_route.go b/builtin/providers/google/resource_compute_route.go index 3d7b80616..90b5a2e8b 100644 --- a/builtin/providers/google/resource_compute_route.go +++ b/builtin/providers/google/resource_compute_route.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeRoute() *schema.Resource { @@ -192,15 +191,7 @@ func resourceComputeRouteRead(d *schema.ResourceData, meta interface{}) error { route, err := config.clientCompute.Routes.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading route: %#v", err) + return handleNotFoundError(err, d, fmt.Sprintf("Route %q", d.Get("name").(string))) } d.Set("next_hop_network", route.NextHopNetwork) diff --git a/builtin/providers/google/resource_compute_snapshot.go b/builtin/providers/google/resource_compute_snapshot.go index e482c86f9..794d98904 100644 --- a/builtin/providers/google/resource_compute_snapshot.go +++ b/builtin/providers/google/resource_compute_snapshot.go @@ -130,15 +130,7 @@ func resourceComputeSnapshotRead(d *schema.ResourceData, meta interface{}) error snapshot, err := config.clientCompute.Snapshots.Get( project, d.Id()).Do() if err != nil { - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { - log.Printf("[WARN] Removing Snapshot %q because it's gone", d.Get("name").(string)) - // The resource doesn't exist anymore - d.SetId("") - - return nil - } - - return fmt.Errorf("Error reading snapshot: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Snapshot %q", d.Get("name").(string))) } d.Set("self_link", snapshot.SelfLink) diff --git a/builtin/providers/google/resource_compute_ssl_certificate.go b/builtin/providers/google/resource_compute_ssl_certificate.go index ea37e141f..5b64ebbf7 100644 --- a/builtin/providers/google/resource_compute_ssl_certificate.go +++ b/builtin/providers/google/resource_compute_ssl_certificate.go @@ -2,13 +2,11 @@ package google import ( "fmt" - "log" "strconv" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeSslCertificate() *schema.Resource { @@ -144,15 +142,7 @@ func resourceComputeSslCertificateRead(d *schema.ResourceData, meta interface{}) cert, err := config.clientCompute.SslCertificates.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading ssl certificate: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("SSL Certificate %q", d.Get("name").(string))) } d.Set("self_link", cert.SelfLink) diff --git a/builtin/providers/google/resource_compute_subnetwork.go b/builtin/providers/google/resource_compute_subnetwork.go index 94c7a9dd8..53f0c0dae 100644 --- a/builtin/providers/google/resource_compute_subnetwork.go +++ b/builtin/providers/google/resource_compute_subnetwork.go @@ -8,7 +8,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeSubnetwork() *schema.Resource { @@ -146,15 +145,7 @@ func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) err subnetwork, err := config.clientCompute.Subnetworks.Get( project, region, name).Do() if err != nil { - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { - log.Printf("[WARN] Removing Subnetwork %q because it's gone", name) - // The resource doesn't exist anymore - d.SetId("") - - return nil - } - - return fmt.Errorf("Error reading subnetwork: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name)) } d.Set("gateway_address", subnetwork.GatewayAddress) diff --git a/builtin/providers/google/resource_compute_target_http_proxy.go b/builtin/providers/google/resource_compute_target_http_proxy.go index 72c68eb51..602c38b7c 100644 --- a/builtin/providers/google/resource_compute_target_http_proxy.go +++ b/builtin/providers/google/resource_compute_target_http_proxy.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeTargetHttpProxy() *schema.Resource { @@ -131,15 +130,7 @@ func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{} proxy, err := config.clientCompute.TargetHttpProxies.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading TargetHttpProxy: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Target HTTP Proxy %q", d.Get("name").(string))) } d.Set("self_link", proxy.SelfLink) diff --git a/builtin/providers/google/resource_compute_target_https_proxy.go b/builtin/providers/google/resource_compute_target_https_proxy.go index 5e8bf58c2..7ba080e4c 100644 --- a/builtin/providers/google/resource_compute_target_https_proxy.go +++ b/builtin/providers/google/resource_compute_target_https_proxy.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeTargetHttpsProxy() *schema.Resource { @@ -206,15 +205,7 @@ func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{ proxy, err := config.clientCompute.TargetHttpsProxies.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading TargetHttpsProxy: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Target HTTPS proxy %q", d.Get("name").(string))) } _certs := d.Get("ssl_certificates").([]interface{}) diff --git a/builtin/providers/google/resource_compute_target_pool.go b/builtin/providers/google/resource_compute_target_pool.go index 1680be90f..3a40c151d 100644 --- a/builtin/providers/google/resource_compute_target_pool.go +++ b/builtin/providers/google/resource_compute_target_pool.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeTargetPool() *schema.Resource { @@ -391,15 +390,7 @@ func resourceComputeTargetPoolRead(d *schema.ResourceData, meta interface{}) err tpool, err := config.clientCompute.TargetPools.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading TargetPool: %s", err) + return handleNotFoundError(err, d, fmt.Sprintf("Target Pool %q", d.Get("name").(string))) } regionUrl := strings.Split(tpool.Region, "/") diff --git a/builtin/providers/google/resource_compute_url_map.go b/builtin/providers/google/resource_compute_url_map.go index 56c19ddc3..3c5740e06 100644 --- a/builtin/providers/google/resource_compute_url_map.go +++ b/builtin/providers/google/resource_compute_url_map.go @@ -2,12 +2,10 @@ 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 { @@ -312,15 +310,7 @@ func resourceComputeUrlMapRead(d *schema.ResourceData, meta interface{}) error { urlMap, err := config.clientCompute.UrlMaps.Get(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) + return handleNotFoundError(err, d, fmt.Sprintf("URL Map %q", d.Get("name").(string))) } d.SetId(name) diff --git a/builtin/providers/google/resource_compute_vpn_gateway.go b/builtin/providers/google/resource_compute_vpn_gateway.go index fe716198d..5b23eaa49 100644 --- a/builtin/providers/google/resource_compute_vpn_gateway.go +++ b/builtin/providers/google/resource_compute_vpn_gateway.go @@ -2,12 +2,10 @@ 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 { @@ -119,15 +117,7 @@ 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) + return handleNotFoundError(err, d, fmt.Sprintf("VPN Gateway %q", d.Get("name").(string))) } d.Set("self_link", vpnGateway.SelfLink) diff --git a/builtin/providers/google/resource_compute_vpn_tunnel.go b/builtin/providers/google/resource_compute_vpn_tunnel.go index 42f477d92..a5120c991 100644 --- a/builtin/providers/google/resource_compute_vpn_tunnel.go +++ b/builtin/providers/google/resource_compute_vpn_tunnel.go @@ -3,13 +3,11 @@ package google import ( "bytes" "fmt" - "log" "net" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/compute/v1" - "google.golang.org/api/googleapi" ) func resourceComputeVpnTunnel() *schema.Resource { @@ -189,15 +187,7 @@ 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) + return handleNotFoundError(err, d, fmt.Sprintf("VPN Tunnel %q", d.Get("name").(string))) } localTrafficSelectors := []string{} diff --git a/builtin/providers/google/resource_container_cluster.go b/builtin/providers/google/resource_container_cluster.go index 55805541c..91c994826 100644 --- a/builtin/providers/google/resource_container_cluster.go +++ b/builtin/providers/google/resource_container_cluster.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/container/v1" - "google.golang.org/api/googleapi" ) var ( @@ -535,15 +534,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro cluster, err := config.clientContainer.Projects.Zones.Clusters.Get( 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 + return handleNotFoundError(err, d, fmt.Sprintf("Container Cluster %q", d.Get("name").(string))) } d.Set("name", cluster.Name) diff --git a/builtin/providers/google/resource_dns_managed_zone.go b/builtin/providers/google/resource_dns_managed_zone.go index f35e7dd87..a934460c0 100644 --- a/builtin/providers/google/resource_dns_managed_zone.go +++ b/builtin/providers/google/resource_dns_managed_zone.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/dns/v1" - "google.golang.org/api/googleapi" ) func resourceDnsManagedZone() *schema.Resource { @@ -99,15 +98,7 @@ func resourceDnsManagedZoneRead(d *schema.ResourceData, meta interface{}) error zone, err := config.clientDns.ManagedZones.Get( 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("") - - return nil - } - - return fmt.Errorf("Error reading DNS ManagedZone: %#v", err) + return handleNotFoundError(err, d, fmt.Sprintf("DNS Managed Zone %q", d.Get("name").(string))) } d.Set("name_servers", zone.NameServers) diff --git a/builtin/providers/google/resource_dns_record_set.go b/builtin/providers/google/resource_dns_record_set.go index 49a56d9b1..0f322bd86 100644 --- a/builtin/providers/google/resource_dns_record_set.go +++ b/builtin/providers/google/resource_dns_record_set.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/dns/v1" - "google.golang.org/api/googleapi" ) func resourceDnsRecordSet() *schema.Resource { @@ -117,15 +116,7 @@ func resourceDnsRecordSetRead(d *schema.ResourceData, meta interface{}) error { resp, err := config.clientDns.ResourceRecordSets.List( 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) + return handleNotFoundError(err, d, fmt.Sprintf("DNS Record Set %q", d.Get("name").(string))) } if len(resp.Rrsets) == 0 { // The resource doesn't exist anymore diff --git a/builtin/providers/google/resource_google_project.go b/builtin/providers/google/resource_google_project.go index d894166fb..4e71d0d46 100644 --- a/builtin/providers/google/resource_google_project.go +++ b/builtin/providers/google/resource_google_project.go @@ -136,10 +136,7 @@ func resourceGoogleProjectRead(d *schema.ResourceData, meta interface{}) error { // Read the project p, err := config.clientResourceManager.Projects.Get(pid).Do() if err != nil { - if v, ok := err.(*googleapi.Error); ok && v.Code == http.StatusNotFound { - return fmt.Errorf("Project %q does not exist.", pid) - } - return fmt.Errorf("Error checking project %q: %s", pid, err) + return handleNotFoundError(err, d, fmt.Sprintf("Project %q", pid)) } d.Set("project_id", pid) diff --git a/builtin/providers/google/resource_google_service_account.go b/builtin/providers/google/resource_google_service_account.go index 101702bb4..6e3e6abe1 100644 --- a/builtin/providers/google/resource_google_service_account.go +++ b/builtin/providers/google/resource_google_service_account.go @@ -6,7 +6,6 @@ import ( "log" "github.com/hashicorp/terraform/helper/schema" - "google.golang.org/api/googleapi" "google.golang.org/api/iam/v1" ) @@ -116,14 +115,7 @@ func resourceGoogleServiceAccountRead(d *schema.ResourceData, meta interface{}) // Confirm the service account exists sa, err := config.clientIAM.Projects.ServiceAccounts.Get(d.Id()).Do() if err != nil { - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { - log.Printf("[WARN] Removing reference to service account %q because it no longer exists", d.Id()) - // The resource doesn't exist anymore - d.SetId("") - - return nil - } - return fmt.Errorf("Error reading service account %q: %q", d.Id(), err) + return handleNotFoundError(err, d, fmt.Sprintf("Service Account %q", d.Id())) } d.Set("email", sa.Email) diff --git a/builtin/providers/google/resource_pubsub_subscription.go b/builtin/providers/google/resource_pubsub_subscription.go index 6afd7c5cd..04c0414b0 100644 --- a/builtin/providers/google/resource_pubsub_subscription.go +++ b/builtin/providers/google/resource_pubsub_subscription.go @@ -130,7 +130,7 @@ func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) er call := config.clientPubsub.Projects.Subscriptions.Get(name) _, err := call.Do() if err != nil { - return err + return handleNotFoundError(err, d, fmt.Sprintf("Pubsub Subscription %q", name)) } return nil diff --git a/builtin/providers/google/resource_pubsub_topic.go b/builtin/providers/google/resource_pubsub_topic.go index 84932e4e9..ba78a6f74 100644 --- a/builtin/providers/google/resource_pubsub_topic.go +++ b/builtin/providers/google/resource_pubsub_topic.go @@ -58,7 +58,7 @@ func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error { call := config.clientPubsub.Projects.Topics.Get(name) _, err := call.Do() if err != nil { - return err + return handleNotFoundError(err, d, fmt.Sprintf("Pubsub Topic %q", name)) } return nil diff --git a/builtin/providers/google/resource_sql_database.go b/builtin/providers/google/resource_sql_database.go index c15e49ced..e8df24a70 100644 --- a/builtin/providers/google/resource_sql_database.go +++ b/builtin/providers/google/resource_sql_database.go @@ -2,11 +2,9 @@ package google import ( "fmt" - "log" "github.com/hashicorp/terraform/helper/schema" - "google.golang.org/api/googleapi" "google.golang.org/api/sqladmin/v1beta4" ) @@ -93,17 +91,7 @@ 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) + return handleNotFoundError(err, d, fmt.Sprintf("SQL Database %q in instance %q", database_name, instance_name)) } d.Set("self_link", db.SelfLink) diff --git a/builtin/providers/google/resource_sql_database_instance.go b/builtin/providers/google/resource_sql_database_instance.go index 2a1fa2f30..e946e6267 100644 --- a/builtin/providers/google/resource_sql_database_instance.go +++ b/builtin/providers/google/resource_sql_database_instance.go @@ -2,7 +2,6 @@ package google import ( "fmt" - "log" "strings" "github.com/hashicorp/terraform/helper/resource" @@ -610,16 +609,7 @@ 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) + return handleNotFoundError(err, d, fmt.Sprintf("SQL Database Instance %q", d.Get("name").(string))) } _settingsList := d.Get("settings").([]interface{}) diff --git a/builtin/providers/google/resource_sql_user.go b/builtin/providers/google/resource_sql_user.go index 2aaf1bd7a..23daf461a 100644 --- a/builtin/providers/google/resource_sql_user.go +++ b/builtin/providers/google/resource_sql_user.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" - "google.golang.org/api/googleapi" "google.golang.org/api/sqladmin/v1beta4" ) @@ -102,14 +101,7 @@ func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error { users, err := config.clientSqlAdmin.Users.List(project, instance).Do() if err != nil { - if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { - log.Printf("[WARN] Removing SQL User %q because it's gone", d.Get("name").(string)) - d.SetId("") - - return nil - } - - return fmt.Errorf("Error, failed to get user %s in instance %s: %s", name, instance, err) + return handleNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance)) } found := false diff --git a/builtin/providers/google/resource_storage_bucket.go b/builtin/providers/google/resource_storage_bucket.go index afd2ad493..a5990c0fe 100644 --- a/builtin/providers/google/resource_storage_bucket.go +++ b/builtin/providers/google/resource_storage_bucket.go @@ -215,15 +215,7 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error { res, err := config.clientStorage.Buckets.Get(bucket).Do() if err != nil { - 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) + return handleNotFoundError(err, d, fmt.Sprintf("Storage Bucket %q", d.Get("name").(string))) } 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 aa996cb9f..428c1cecf 100644 --- a/builtin/providers/google/resource_storage_bucket_acl.go +++ b/builtin/providers/google/resource_storage_bucket_acl.go @@ -7,7 +7,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" - "google.golang.org/api/googleapi" "google.golang.org/api/storage/v1" ) @@ -170,15 +169,7 @@ 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 + return handleNotFoundError(err, d, fmt.Sprintf("Storage Bucket ACL for bucket %q", d.Get("bucket").(string))) } for _, v := range res.Items { diff --git a/builtin/providers/google/resource_storage_bucket_object.go b/builtin/providers/google/resource_storage_bucket_object.go index 14db8d21c..bbf9c1f28 100644 --- a/builtin/providers/google/resource_storage_bucket_object.go +++ b/builtin/providers/google/resource_storage_bucket_object.go @@ -182,15 +182,7 @@ 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) + return handleNotFoundError(err, d, fmt.Sprintf("Storage Bucket Object %q", d.Get("name").(string))) } d.Set("md5hash", res.Md5Hash) diff --git a/builtin/providers/google/resource_storage_object_acl.go b/builtin/providers/google/resource_storage_object_acl.go index 9795305b6..718260d98 100644 --- a/builtin/providers/google/resource_storage_object_acl.go +++ b/builtin/providers/google/resource_storage_object_acl.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/schema" - "google.golang.org/api/googleapi" "google.golang.org/api/storage/v1" ) @@ -138,15 +137,7 @@ 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 + return handleNotFoundError(err, d, fmt.Sprintf("Storage Object ACL for Bucket %q", d.Get("bucket").(string))) } for _, v := range res.Items {