From 00c3553f11a5ef88b5a6dfd0b9266b653832cd6b Mon Sep 17 00:00:00 2001 From: Thomas Poindessous Date: Wed, 22 Mar 2017 15:01:45 +0100 Subject: [PATCH] Review by @paddyforan: Add a resourceComputeSnapshotExists function --- .../google/resource_compute_snapshot.go | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/builtin/providers/google/resource_compute_snapshot.go b/builtin/providers/google/resource_compute_snapshot.go index e8c6a9264..b136db38c 100644 --- a/builtin/providers/google/resource_compute_snapshot.go +++ b/builtin/providers/google/resource_compute_snapshot.go @@ -14,6 +14,7 @@ func resourceComputeSnapshot() *schema.Resource { Create: resourceComputeSnapshotCreate, Read: resourceComputeSnapshotRead, Delete: resourceComputeSnapshotDelete, + Exists: resourceComputeSnapshotExists, Schema: map[string]*schema.Schema{ "name": &schema.Schema{ @@ -182,3 +183,26 @@ func resourceComputeSnapshotDelete(d *schema.ResourceData, meta interface{}) err d.SetId("") return nil } + +func resourceComputeSnapshotExists(d *schema.ResourceData, meta interface{}) (bool, error) { + config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return false, err + } + + _, 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 false, err + } + return true, err + } + return true, nil +}