From fd8c5343278c7e69107778ac0d45bc80856c5465 Mon Sep 17 00:00:00 2001 From: Xin Date: Fri, 28 Feb 2020 12:43:33 -0500 Subject: [PATCH] Added check for bucket retention policy list being empty. (#3181) * Empty bucket rentention policy list should be checked. * Address code review comments. --- .../resources/resource_storage_bucket.go | 14 +++++++---- third_party/validator/storage_bucket.go | 24 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/third_party/terraform/resources/resource_storage_bucket.go b/third_party/terraform/resources/resource_storage_bucket.go index 2e177870973f..008c19e46670 100644 --- a/third_party/terraform/resources/resource_storage_bucket.go +++ b/third_party/terraform/resources/resource_storage_bucket.go @@ -349,14 +349,17 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error } if v, ok := d.GetOk("retention_policy"); ok { + // Not using expandBucketRetentionPolicy() here because `is_locked` cannot be set on creation. retention_policies := v.([]interface{}) - sb.RetentionPolicy = &storage.BucketRetentionPolicy{} + if len(retention_policies) > 0 { + sb.RetentionPolicy = &storage.BucketRetentionPolicy{} - retentionPolicy := retention_policies[0].(map[string]interface{}) + retentionPolicy := retention_policies[0].(map[string]interface{}) - if v, ok := retentionPolicy["retention_period"]; ok { - sb.RetentionPolicy.RetentionPeriod = int64(v.(int)) + if v, ok := retentionPolicy["retention_period"]; ok { + sb.RetentionPolicy.RetentionPeriod = int64(v.(int)) + } } } @@ -819,6 +822,9 @@ func flattenBucketLogging(bucketLogging *storage.BucketLogging) []map[string]int func expandBucketRetentionPolicy(configured interface{}) *storage.BucketRetentionPolicy { retentionPolicies := configured.([]interface{}) + if len(retentionPolicies) == 0 { + return nil + } retentionPolicy := retentionPolicies[0].(map[string]interface{}) bucketRetentionPolicy := &storage.BucketRetentionPolicy{ diff --git a/third_party/validator/storage_bucket.go b/third_party/validator/storage_bucket.go index 08d2103f0f56..3e8a3921a69d 100644 --- a/third_party/validator/storage_bucket.go +++ b/third_party/validator/storage_bucket.go @@ -72,15 +72,7 @@ func GetStorageBucketApiObject(d TerraformResourceData, config *Config) (map[str } if v, ok := d.GetOk("retention_policy"); ok { - retention_policies := v.([]interface{}) - - sb.RetentionPolicy = &storage.BucketRetentionPolicy{} - - retentionPolicy := retention_policies[0].(map[string]interface{}) - - if v, ok := retentionPolicy["retention_period"]; ok { - sb.RetentionPolicy.RetentionPeriod = int64(v.(int)) - } + sb.RetentionPolicy = expandBucketRetentionPolicy(v.([]interface{})) } if v, ok := d.GetOk("cors"); ok { @@ -202,6 +194,20 @@ func expandBucketWebsite(v interface{}) *storage.BucketWebsite { return w } +func expandBucketRetentionPolicy(configured interface{}) *storage.BucketRetentionPolicy { + retentionPolicies := configured.([]interface{}) + if len(retentionPolicies) == 0 { + return nil + } + retentionPolicy := retentionPolicies[0].(map[string]interface{}) + + bucketRetentionPolicy := &storage.BucketRetentionPolicy{ + RetentionPeriod: int64(retentionPolicy["retention_period"].(int)), + } + + return bucketRetentionPolicy +} + func resourceGCSBucketLifecycleCreateOrUpdate(d TerraformResourceData, sb *storage.Bucket) error { if v, ok := d.GetOk("lifecycle_rule"); ok { lifecycle_rules := v.([]interface{})