Review by @paddyforan: Add a resourceComputeSnapshotExists function

This commit is contained in:
Thomas Poindessous 2017-03-22 15:01:45 +01:00
parent 84dc163089
commit 00c3553f11
1 changed files with 24 additions and 0 deletions

View File

@ -14,6 +14,7 @@ func resourceComputeSnapshot() *schema.Resource {
Create: resourceComputeSnapshotCreate, Create: resourceComputeSnapshotCreate,
Read: resourceComputeSnapshotRead, Read: resourceComputeSnapshotRead,
Delete: resourceComputeSnapshotDelete, Delete: resourceComputeSnapshotDelete,
Exists: resourceComputeSnapshotExists,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": &schema.Schema{
@ -182,3 +183,26 @@ func resourceComputeSnapshotDelete(d *schema.ResourceData, meta interface{}) err
d.SetId("") d.SetId("")
return nil 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
}