From 3d08cd07dd52e3a85501d1fc13bb0f9475ff872d Mon Sep 17 00:00:00 2001 From: Thomas Poindessous Date: Thu, 23 Mar 2017 10:38:57 +0100 Subject: [PATCH] Review by @paddyforan: Set attributes returned by API --- .../google/resource_compute_snapshot.go | 9 +++ .../google/resource_compute_snapshot_test.go | 56 ++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/builtin/providers/google/resource_compute_snapshot.go b/builtin/providers/google/resource_compute_snapshot.go index b136db38c..9f5643a12 100644 --- a/builtin/providers/google/resource_compute_snapshot.go +++ b/builtin/providers/google/resource_compute_snapshot.go @@ -147,10 +147,19 @@ func resourceComputeSnapshotRead(d *schema.ResourceData, meta interface{}) error } d.Set("self_link", snapshot.SelfLink) + if snapshot.SnapshotEncryptionKey != nil && snapshot.SnapshotEncryptionKey.Sha256 != "" { d.Set("snapshot_encryption_key_sha256", snapshot.SnapshotEncryptionKey.Sha256) } + if snapshot.SourceDiskEncryptionKey != nil && snapshot.SourceDiskEncryptionKey.Sha256 != "" { + d.Set("source_disk_encryption_key_sha256", snapshot.SourceDiskEncryptionKey.Sha256) + } + + d.Set("source_disk_id", snapshot.SourceDiskId) + + d.Set("source_disk", snapshot.SourceDisk) + return nil } diff --git a/builtin/providers/google/resource_compute_snapshot_test.go b/builtin/providers/google/resource_compute_snapshot_test.go index e074de4c6..2460e8459 100644 --- a/builtin/providers/google/resource_compute_snapshot_test.go +++ b/builtin/providers/google/resource_compute_snapshot_test.go @@ -92,7 +92,61 @@ func testAccCheckComputeSnapshotExists(n string, snapshot *compute.Snapshot) res } if found.Name != rs.Primary.ID { - return fmt.Errorf("Snapshot not found") + return fmt.Errorf("Snapshot %s not found", n) + } + + attr := rs.Primary.Attributes["snapshot_encryption_key_sha256"] + if found.SnapshotEncryptionKey != nil && found.SnapshotEncryptionKey.Sha256 != attr { + return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SnapshotEncryptionKey.Sha256) + } else if found.SnapshotEncryptionKey == nil && attr != "" { + return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SnapshotEncryptionKey) + } + + attr = rs.Primary.Attributes["snapshot_encryption_key_raw"] + if found.SnapshotEncryptionKey != nil && found.SnapshotEncryptionKey.RawKey != attr { + return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SnapshotEncryptionKey.RawKey) + } else if found.SnapshotEncryptionKey == nil && attr != "" { + return fmt.Errorf("Snapshot %s has mismatched encryption key.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SnapshotEncryptionKey) + } + + attr = rs.Primary.Attributes["source_disk_encryption_key_sha256"] + if found.SourceDiskEncryptionKey != nil && found.SourceDiskEncryptionKey.Sha256 != attr { + return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SourceDiskEncryptionKey.Sha256) + } else if found.SourceDiskEncryptionKey == nil && attr != "" { + return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SourceDiskEncryptionKey) + } + + attr = rs.Primary.Attributes["source_disk_encryption_key_raw"] + if found.SourceDiskEncryptionKey != nil && found.SourceDiskEncryptionKey.RawKey != attr { + return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SourceDiskEncryptionKey.RawKey) + } else if found.SourceDiskEncryptionKey == nil && attr != "" { + return fmt.Errorf("Snapshot %s has mismatched source disk encryption key.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SourceDiskEncryptionKey) + } + + attr = rs.Primary.Attributes["source_disk_id"] + if found.SourceDiskId != attr { + return fmt.Errorf("Snapshot %s has mismatched source disk id.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SourceDiskId) + } + + attr = rs.Primary.Attributes["source_disk"] + if found.SourceDisk != attr { + return fmt.Errorf("Snapshot %s has mismatched source disk.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SourceDisk) + } + + attr = rs.Primary.Attributes["self_link"] + if found.SelfLink != attr { + return fmt.Errorf("Snapshot %s has mismatched self link.\nTF State: %+v.\nGCP State: %+v", + n, attr, found.SelfLink) } *snapshot = *found