Skip to content

Commit

Permalink
Added check for bucket retention policy list being empty. (GoogleClou…
Browse files Browse the repository at this point in the history
…dPlatform#3181)

* Empty bucket rentention policy list should be checked.

* Address code review comments.
  • Loading branch information
xingao267 authored and Nathan Klish committed May 18, 2020
1 parent e615147 commit fd8c534
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
14 changes: 10 additions & 4 deletions third_party/terraform/resources/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

Expand Down Expand Up @@ -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{
Expand Down
24 changes: 15 additions & 9 deletions third_party/validator/storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{})
Expand Down

0 comments on commit fd8c534

Please sign in to comment.