Skip to content

Commit

Permalink
Fix disk snapshot expand/encode, kmsKeyName decoder (GoogleCloudPlatf…
Browse files Browse the repository at this point in the history
…orm#119)

<!-- This change is generated by MagicModules. -->
/cc @rileykarson
  • Loading branch information
modular-magician authored and nat-henderson committed Dec 12, 2018
1 parent 96f73ca commit 0c58094
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 44 deletions.
42 changes: 20 additions & 22 deletions google-beta/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,25 +1175,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 @@ -1204,10 +1185,13 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("disk_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]
if v, ok := d.GetOk("disk_encryption_key_raw"); ok {
transformed["rawKey"] = v

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]
}
d.Set("disk_encryption_key_sha256", original["sha256"])

res["diskEncryptionKey"] = transformed
}

Expand All @@ -1217,6 +1201,13 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("source_image_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

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 @@ -1226,6 +1217,13 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("source_snapshot_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

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
104 changes: 82 additions & 22 deletions google-beta/resource_compute_region_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform/helper/customdiff"
Expand Down Expand Up @@ -76,6 +76,11 @@ func resourceComputeRegionDisk() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"raw_key": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -118,6 +123,11 @@ func resourceComputeRegionDisk() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"raw_key": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -183,6 +193,12 @@ func resourceComputeRegionDiskCreate(d *schema.ResourceData, meta interface{}) e
config := meta.(*Config)

obj := make(map[string]interface{})
labelFingerprintProp, err := expandComputeRegionDiskLabelFingerprint(d.Get("label_fingerprint"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("label_fingerprint"); !isEmptyValue(reflect.ValueOf(labelFingerprintProp)) && (ok || !reflect.DeepEqual(v, labelFingerprintProp)) {
obj["labelFingerprint"] = labelFingerprintProp
}
descriptionProp, err := expandComputeRegionDiskDescription(d.Get("description"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -379,8 +395,12 @@ func resourceComputeRegionDiskUpdate(d *schema.ResourceData, meta interface{}) e

if d.HasChange("label_fingerprint") || d.HasChange("labels") {
obj := make(map[string]interface{})
labelFingerprintProp := d.Get("label_fingerprint")
obj["labelFingerprint"] = labelFingerprintProp
labelFingerprintProp, err := expandComputeRegionDiskLabelFingerprint(d.Get("label_fingerprint"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("label_fingerprint"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelFingerprintProp)) {
obj["labelFingerprint"] = labelFingerprintProp
}
labelsProp, err := expandComputeRegionDiskLabels(d.Get("labels"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -638,6 +658,8 @@ func flattenComputeRegionDiskDiskEncryptionKey(v interface{}) interface{} {
flattenComputeRegionDiskDiskEncryptionKeyRawKey(original["rawKey"])
transformed["sha256"] =
flattenComputeRegionDiskDiskEncryptionKeySha256(original["sha256"])
transformed["kms_key_name"] =
flattenComputeRegionDiskDiskEncryptionKeyKmsKeyName(original["kmsKeyName"])
return []interface{}{transformed}
}
func flattenComputeRegionDiskDiskEncryptionKeyRawKey(v interface{}) interface{} {
Expand All @@ -648,6 +670,10 @@ func flattenComputeRegionDiskDiskEncryptionKeySha256(v interface{}) interface{}
return v
}

func flattenComputeRegionDiskDiskEncryptionKeyKmsKeyName(v interface{}) interface{} {
return v
}

func flattenComputeRegionDiskSnapshot(v interface{}) interface{} {
if v == nil {
return v
Expand All @@ -663,6 +689,8 @@ func flattenComputeRegionDiskSourceSnapshotEncryptionKey(v interface{}) interfac
transformed := make(map[string]interface{})
transformed["raw_key"] =
flattenComputeRegionDiskSourceSnapshotEncryptionKeyRawKey(original["rawKey"])
transformed["kms_key_name"] =
flattenComputeRegionDiskSourceSnapshotEncryptionKeyKmsKeyName(original["kmsKeyName"])
transformed["sha256"] =
flattenComputeRegionDiskSourceSnapshotEncryptionKeySha256(original["sha256"])
return []interface{}{transformed}
Expand All @@ -671,6 +699,10 @@ func flattenComputeRegionDiskSourceSnapshotEncryptionKeyRawKey(v interface{}) in
return v
}

func flattenComputeRegionDiskSourceSnapshotEncryptionKeyKmsKeyName(v interface{}) interface{} {
return v
}

func flattenComputeRegionDiskSourceSnapshotEncryptionKeySha256(v interface{}) interface{} {
return v
}
Expand All @@ -679,6 +711,10 @@ func flattenComputeRegionDiskSourceSnapshotId(v interface{}) interface{} {
return v
}

func expandComputeRegionDiskLabelFingerprint(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionDiskDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -754,6 +790,13 @@ func expandComputeRegionDiskDiskEncryptionKey(v interface{}, d *schema.ResourceD
transformed["sha256"] = transformedSha256
}

transformedKmsKeyName, err := expandComputeRegionDiskDiskEncryptionKeyKmsKeyName(original["kms_key_name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedKmsKeyName); val.IsValid() && !isEmptyValue(val) {
transformed["kmsKeyName"] = transformedKmsKeyName
}

return transformed, nil
}

Expand All @@ -765,6 +808,10 @@ func expandComputeRegionDiskDiskEncryptionKeySha256(v interface{}, d *schema.Res
return v, nil
}

func expandComputeRegionDiskDiskEncryptionKeyKmsKeyName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionDiskSnapshot(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("snapshots", v.(string), "project", d, config, true)
if err != nil {
Expand All @@ -789,6 +836,13 @@ func expandComputeRegionDiskSourceSnapshotEncryptionKey(v interface{}, d *schema
transformed["rawKey"] = transformedRawKey
}

transformedKmsKeyName, err := expandComputeRegionDiskSourceSnapshotEncryptionKeyKmsKeyName(original["kms_key_name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedKmsKeyName); val.IsValid() && !isEmptyValue(val) {
transformed["kmsKeyName"] = transformedKmsKeyName
}

transformedSha256, err := expandComputeRegionDiskSourceSnapshotEncryptionKeySha256(original["sha256"], d, config)
if err != nil {
return nil, err
Expand All @@ -803,6 +857,10 @@ func expandComputeRegionDiskSourceSnapshotEncryptionKeyRawKey(v interface{}, d *
return v, nil
}

func expandComputeRegionDiskSourceSnapshotEncryptionKeyKmsKeyName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeRegionDiskSourceSnapshotEncryptionKeySha256(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -849,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 @@ -878,6 +917,13 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("disk_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

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 @@ -887,6 +933,13 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("source_image_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

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 @@ -896,6 +949,13 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("source_snapshot_encryption_key.0.raw_key")
transformed["sha256"] = original["sha256"]

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 0c58094

Please sign in to comment.