Skip to content

Commit

Permalink
Fix disk snapshot expand/encode, kmsKeyName decoder (#119)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @rileykarson
  • Loading branch information
modular-magician authored and rileykarson committed Nov 12, 2018
1 parent 38ce950 commit 3b0064d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 57 deletions.
43 changes: 15 additions & 28 deletions google-beta/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1251,25 +1251,6 @@ func resourceComputeDiskEncoder(d *schema.ResourceData, meta interface{}, obj ma
log.Printf("[DEBUG] Image name resolved to: %s", imageUrl)
}

if v, ok := d.GetOk("snapshot"); ok {
snapshotName := v.(string)
match, _ := regexp.MatchString("^https://www.googleapis.com/compute", snapshotName)
if match {
obj["sourceSnapshot"] = snapshotName
} else {
log.Printf("[DEBUG] Loading snapshot: %s", snapshotName)
snapshotData, err := config.clientCompute.Snapshots.Get(
project, snapshotName).Do()

if err != nil {
return nil, fmt.Errorf(
"Error loading snapshot '%s': %s",
snapshotName, err)
}
obj["sourceSnapshot"] = snapshotData.SelfLink
}
}

return obj, nil
}

Expand All @@ -1281,9 +1262,11 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
transformed["rawKey"] = d.Get("disk_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(original["kmsKeyName"].(string), "/cryptoKeyVersions")[0]
if kmsKeyName, ok := original["kmsKeyName"]; ok {
// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

res["diskEncryptionKey"] = transformed
}
Expand All @@ -1295,9 +1278,11 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
transformed["rawKey"] = d.Get("source_image_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(original["kmsKeyName"].(string), "/cryptoKeyVersions")[0]
if kmsKeyName, ok := original["kmsKeyName"]; ok {
// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

res["sourceImageEncryptionKey"] = transformed
}
Expand All @@ -1309,9 +1294,11 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
transformed["rawKey"] = d.Get("source_snapshot_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(original["kmsKeyName"].(string), "/cryptoKeyVersions")[0]
if kmsKeyName, ok := original["kmsKeyName"]; ok {
// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

res["sourceSnapshotEncryptionKey"] = transformed
}
Expand Down
44 changes: 15 additions & 29 deletions google-beta/resource_compute_region_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -908,25 +907,6 @@ func resourceComputeRegionDiskEncoder(d *schema.ResourceData, meta interface{},
log.Printf("[DEBUG] Image name resolved to: %s", imageUrl)
}

if v, ok := d.GetOk("snapshot"); ok {
snapshotName := v.(string)
match, _ := regexp.MatchString("^https://www.googleapis.com/compute", snapshotName)
if match {
obj["sourceSnapshot"] = snapshotName
} else {
log.Printf("[DEBUG] Loading snapshot: %s", snapshotName)
snapshotData, err := config.clientCompute.Snapshots.Get(
project, snapshotName).Do()

if err != nil {
return nil, fmt.Errorf(
"Error loading snapshot '%s': %s",
snapshotName, err)
}
obj["sourceSnapshot"] = snapshotData.SelfLink
}
}

return obj, nil
}

Expand All @@ -938,9 +918,11 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
transformed["rawKey"] = d.Get("disk_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(original["kmsKeyName"].(string), "/cryptoKeyVersions")[0]
if kmsKeyName, ok := original["kmsKeyName"]; ok {
// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

res["diskEncryptionKey"] = transformed
}
Expand All @@ -952,9 +934,11 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
transformed["rawKey"] = d.Get("source_image_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(original["kmsKeyName"].(string), "/cryptoKeyVersions")[0]
if kmsKeyName, ok := original["kmsKeyName"]; ok {
// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

res["sourceImageEncryptionKey"] = transformed
}
Expand All @@ -966,9 +950,11 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
transformed["rawKey"] = d.Get("source_snapshot_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(original["kmsKeyName"].(string), "/cryptoKeyVersions")[0]
if kmsKeyName, ok := original["kmsKeyName"]; ok {
// The response for crypto keys often includes the version of the key which needs to be removed
// format: projects/<project>/locations/<region>/keyRings/<keyring>/cryptoKeys/<key>/cryptoKeyVersions/1
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

res["sourceSnapshotEncryptionKey"] = transformed
}
Expand Down

0 comments on commit 3b0064d

Please sign in to comment.