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

Fix two lifecycle rules with different no_age value always generates change issue #17513

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/10137.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
storage: fixed two or more lifecycle rules with different values of `no_age` field always generates change in `google_storage_bucket` resource.
```
8 changes: 4 additions & 4 deletions google/services/storage/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1238,10 +1238,10 @@ func flattenBucketLifecycle(d *schema.ResourceData, lifecycle *storage.BucketLif

rules := make([]map[string]interface{}, 0, len(lifecycle.Rule))

for _, rule := range lifecycle.Rule {
for index, rule := range lifecycle.Rule {
rules = append(rules, map[string]interface{}{
"action": schema.NewSet(resourceGCSBucketLifecycleRuleActionHash, []interface{}{flattenBucketLifecycleRuleAction(rule.Action)}),
"condition": schema.NewSet(resourceGCSBucketLifecycleRuleConditionHash, []interface{}{flattenBucketLifecycleRuleCondition(d, rule.Condition)}),
"condition": schema.NewSet(resourceGCSBucketLifecycleRuleConditionHash, []interface{}{flattenBucketLifecycleRuleCondition(index, d, rule.Condition)}),
})
}

Expand All @@ -1255,7 +1255,7 @@ func flattenBucketLifecycleRuleAction(action *storage.BucketLifecycleRuleAction)
}
}

func flattenBucketLifecycleRuleCondition(d *schema.ResourceData, condition *storage.BucketLifecycleRuleCondition) map[string]interface{} {
func flattenBucketLifecycleRuleCondition(index int, d *schema.ResourceData, condition *storage.BucketLifecycleRuleCondition) map[string]interface{} {
ruleCondition := map[string]interface{}{
"created_before": condition.CreatedBefore,
"matches_storage_class": tpgresource.ConvertStringArrToInterface(condition.MatchesStorageClass),
Expand All @@ -1280,7 +1280,7 @@ func flattenBucketLifecycleRuleCondition(d *schema.ResourceData, condition *stor
}
}
// setting no_age value from state config since it is terraform only variable and not getting value from backend.
if v, ok := d.GetOk("lifecycle_rule.0.condition"); ok {
if v, ok := d.GetOk(fmt.Sprintf("lifecycle_rule.%d.condition", index)); ok {
state_condition := v.(*schema.Set).List()[0].(map[string]interface{})
ruleCondition["no_age"] = state_condition["no_age"].(bool)
}
Expand Down
26 changes: 22 additions & 4 deletions google/services/storage/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ func TestAccStorageBucket_lifecycleRulesNoAge(t *testing.T) {
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy", "lifecycle_rule.0.condition.0.no_age"},
ImportStateVerifyIgnore: []string{"force_destroy", "lifecycle_rule.1.condition.0.no_age"},
},
{
Config: testAccStorageBucket_customAttributes_withLifecycleNoAgeAndAge(bucketName),
Expand All @@ -523,7 +523,7 @@ func TestAccStorageBucket_lifecycleRulesNoAge(t *testing.T) {
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy", "lifecycle_rule.0.condition.0.no_age"},
ImportStateVerifyIgnore: []string{"force_destroy", "lifecycle_rule.1.condition.0.no_age"},
},
{
Config: testAccStorageBucket_customAttributes_withLifecycle1(bucketName),
Expand Down Expand Up @@ -1478,8 +1478,8 @@ func testAccCheckStorageBucketLifecycleConditionState(expected *bool, b *storage

func testAccCheckStorageBucketLifecycleConditionNoAge(expected *int64, b *storage.Bucket) resource.TestCheckFunc {
return func(s *terraform.State) error {
actual := b.Lifecycle.Rule[0].Condition.Age
if expected == nil && b.Lifecycle.Rule[0].Condition.Age == nil {
actual := b.Lifecycle.Rule[1].Condition.Age
if expected == nil && b.Lifecycle.Rule[1].Condition.Age == nil {
return nil
}
if expected == nil {
Expand Down Expand Up @@ -1689,6 +1689,15 @@ resource "google_storage_bucket" "bucket" {
name = "%s"
location = "EU"
force_destroy = "true"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
no_age = false
}
}
lifecycle_rule {
action {
type = "Delete"
Expand All @@ -1708,6 +1717,15 @@ resource "google_storage_bucket" "bucket" {
name = "%s"
location = "EU"
force_destroy = "true"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
no_age = false
}
}
lifecycle_rule {
action {
type = "Delete"
Expand Down