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 #10137

Merged
merged 1 commit into from
Mar 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -1237,10 +1237,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 @@ -1254,7 +1254,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 @@ -1279,7 +1279,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
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,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 @@ -522,7 +522,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 @@ -1477,8 +1477,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 @@ -1688,6 +1688,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 @@ -1707,6 +1716,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