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

Added check for bucket retention policy list being empty. #3181

Merged
merged 2 commits into from
Feb 28, 2020
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
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