Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Snapshot location to compute snapshot #2461

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3896.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added storage_locations field to `google_compute_snapshot`
```
16 changes: 14 additions & 2 deletions google-beta/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ See https://cloud.google.com/compute/docs/disks/customer-managed-encryption#encr
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The service account used for the encryption request for the given KMS key.
Description: `The service account used for the encryption request for the given KMS key.
If absent, the Compute Engine Service Agent service account is used.`,
},
"raw_key": {
Expand Down Expand Up @@ -421,7 +421,7 @@ See https://cloud.google.com/compute/docs/disks/customer-managed-encryption#encr
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The service account used for the encryption request for the given KMS key.
Description: `The service account used for the encryption request for the given KMS key.
If absent, the Compute Engine Service Agent service account is used.`,
},
"raw_key": {
Expand Down Expand Up @@ -1551,6 +1551,10 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

if kmsKeyServiceAccount, ok := original["kmsKeyServiceAccount"]; ok {
transformed["kmsKeyServiceAccount"] = kmsKeyServiceAccount
}

res["diskEncryptionKey"] = transformed
}

Expand All @@ -1567,6 +1571,10 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

if kmsKeyServiceAccount, ok := original["kmsKeyServiceAccount"]; ok {
transformed["kmsKeyServiceAccount"] = kmsKeyServiceAccount
}

res["sourceImageEncryptionKey"] = transformed
}

Expand All @@ -1583,6 +1591,10 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

if kmsKeyServiceAccount, ok := original["kmsKeyServiceAccount"]; ok {
transformed["kmsKeyServiceAccount"] = kmsKeyServiceAccount
}

res["sourceSnapshotEncryptionKey"] = transformed
}

Expand Down
12 changes: 12 additions & 0 deletions google-beta/resource_compute_region_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,10 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

if kmsKeyServiceAccount, ok := original["kmsKeyServiceAccount"]; ok {
transformed["kmsKeyServiceAccount"] = kmsKeyServiceAccount
}

res["diskEncryptionKey"] = transformed
}

Expand All @@ -1096,6 +1100,10 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

if kmsKeyServiceAccount, ok := original["kmsKeyServiceAccount"]; ok {
transformed["kmsKeyServiceAccount"] = kmsKeyServiceAccount
}

res["sourceImageEncryptionKey"] = transformed
}

Expand All @@ -1112,6 +1120,10 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
transformed["kmsKeyName"] = strings.Split(kmsKeyName.(string), "/cryptoKeyVersions")[0]
}

if kmsKeyServiceAccount, ok := original["kmsKeyServiceAccount"]; ok {
transformed["kmsKeyServiceAccount"] = kmsKeyServiceAccount
}

res["sourceSnapshotEncryptionKey"] = transformed
}

Expand Down
135 changes: 134 additions & 1 deletion google-beta/resource_compute_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"log"
"reflect"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -82,9 +83,22 @@ source snapshot is protected by a customer-supplied encryption key.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_self_link": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The name of the encryption key that is stored in Google Cloud KMS.`,
},
"kms_key_service_account": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The service account used for the encryption request for the given KMS key.
If absent, the Compute Engine Service Agent service account is used.`,
},
"raw_key": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
Description: `Specifies a 256-bit customer-supplied encryption key, encoded in
RFC 4648 base64 to either encrypt or decrypt this resource.`,
Expand All @@ -109,6 +123,13 @@ key.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_service_account": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `The service account used for the encryption request for the given KMS key.
If absent, the Compute Engine Service Agent service account is used.`,
},
"raw_key": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -120,6 +141,16 @@ RFC 4648 base64 to either encrypt or decrypt this resource.`,
},
},
},
"storage_locations": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Cloud Storage bucket storage location of the snapshot (regional or multi-regional).`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"zone": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -202,6 +233,12 @@ func resourceComputeSnapshotCreate(d *schema.ResourceData, meta interface{}) err
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
storageLocationsProp, err := expandComputeSnapshotStorageLocations(d.Get("storage_locations"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("storage_locations"); !isEmptyValue(reflect.ValueOf(storageLocationsProp)) && (ok || !reflect.DeepEqual(v, storageLocationsProp)) {
obj["storageLocations"] = storageLocationsProp
}
labelsProp, err := expandComputeSnapshotLabels(d.Get("labels"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -345,6 +382,9 @@ func resourceComputeSnapshotRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("storage_bytes", flattenComputeSnapshotStorageBytes(res["storageBytes"], d, config)); err != nil {
return fmt.Errorf("Error reading Snapshot: %s", err)
}
if err := d.Set("storage_locations", flattenComputeSnapshotStorageLocations(res["storageLocations"], d, config)); err != nil {
return fmt.Errorf("Error reading Snapshot: %s", err)
}
if err := d.Set("licenses", flattenComputeSnapshotLicenses(res["licenses"], d, config)); err != nil {
return fmt.Errorf("Error reading Snapshot: %s", err)
}
Expand Down Expand Up @@ -553,6 +593,10 @@ func flattenComputeSnapshotStorageBytes(v interface{}, d *schema.ResourceData, c
return v // let terraform core handle it otherwise
}

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

func flattenComputeSnapshotLicenses(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -588,6 +632,10 @@ func flattenComputeSnapshotSnapshotEncryptionKey(v interface{}, d *schema.Resour
flattenComputeSnapshotSnapshotEncryptionKeyRawKey(original["rawKey"], d, config)
transformed["sha256"] =
flattenComputeSnapshotSnapshotEncryptionKeySha256(original["sha256"], d, config)
transformed["kms_key_self_link"] =
flattenComputeSnapshotSnapshotEncryptionKeyKmsKeySelfLink(original["kmsKeyName"], d, config)
transformed["kms_key_service_account"] =
flattenComputeSnapshotSnapshotEncryptionKeyKmsKeyServiceAccount(original["kmsKeyServiceAccount"], d, config)
return []interface{}{transformed}
}
func flattenComputeSnapshotSnapshotEncryptionKeyRawKey(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Expand All @@ -598,6 +646,14 @@ func flattenComputeSnapshotSnapshotEncryptionKeySha256(v interface{}, d *schema.
return v
}

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

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

func expandComputeSnapshotName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand All @@ -606,6 +662,10 @@ func expandComputeSnapshotDescription(v interface{}, d TerraformResourceData, co
return v, nil
}

func expandComputeSnapshotStorageLocations(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSnapshotLabels(v interface{}, d TerraformResourceData, config *Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
Expand Down Expand Up @@ -660,6 +720,20 @@ func expandComputeSnapshotSnapshotEncryptionKey(v interface{}, d TerraformResour
transformed["sha256"] = transformedSha256
}

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

transformedKmsKeyServiceAccount, err := expandComputeSnapshotSnapshotEncryptionKeyKmsKeyServiceAccount(original["kms_key_service_account"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedKmsKeyServiceAccount); val.IsValid() && !isEmptyValue(val) {
transformed["kmsKeyServiceAccount"] = transformedKmsKeyServiceAccount
}

return transformed, nil
}

Expand All @@ -671,6 +745,14 @@ func expandComputeSnapshotSnapshotEncryptionKeySha256(v interface{}, d Terraform
return v, nil
}

func expandComputeSnapshotSnapshotEncryptionKeyKmsKeySelfLink(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSnapshotSnapshotEncryptionKeyKmsKeyServiceAccount(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSnapshotSourceDiskEncryptionKey(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand All @@ -687,14 +769,65 @@ func expandComputeSnapshotSourceDiskEncryptionKey(v interface{}, d TerraformReso
transformed["rawKey"] = transformedRawKey
}

transformedKmsKeyServiceAccount, err := expandComputeSnapshotSourceDiskEncryptionKeyKmsKeyServiceAccount(original["kms_key_service_account"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedKmsKeyServiceAccount); val.IsValid() && !isEmptyValue(val) {
transformed["kmsKeyServiceAccount"] = transformedKmsKeyServiceAccount
}

return transformed, nil
}

func expandComputeSnapshotSourceDiskEncryptionKeyRawKey(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeSnapshotSourceDiskEncryptionKeyKmsKeyServiceAccount(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func resourceComputeSnapshotDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
if v, ok := res["snapshotEncryptionKey"]; ok {
original := v.(map[string]interface{})
transformed := make(map[string]interface{})
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("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]
}

if kmsKeyServiceAccount, ok := original["kmsKeyServiceAccount"]; ok {
transformed["kmsKeyServiceAccount"] = kmsKeyServiceAccount
}

res["snapshotEncryptionKey"] = transformed
}

if v, ok := res["sourceDiskEncryptionKey"]; ok {
original := v.(map[string]interface{})
transformed := make(map[string]interface{})
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("source_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]
}

if kmsKeyServiceAccount, ok := original["kmsKeyServiceAccount"]; ok {
transformed["kmsKeyServiceAccount"] = kmsKeyServiceAccount
}

res["sourceDiskEncryptionKey"] = transformed
}

d.Set("source_disk_link", ConvertSelfLinkToV1(res["sourceDisk"].(string)))
return res, nil
}
1 change: 1 addition & 0 deletions google-beta/resource_compute_snapshot_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ resource "google_compute_snapshot" "snapshot" {
labels = {
my_label = "value"
}
storage_locations = ["us-central1"]
}

data "google_compute_image" "debian" {
Expand Down
Loading