Skip to content

Commit

Permalink
Fixes two lifecycle rules with different no_age value always generate…
Browse files Browse the repository at this point in the history
…s change. (#10137)
  • Loading branch information
kautikdk authored Mar 7, 2024
1 parent 5c5b415 commit 12b3ce1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
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

0 comments on commit 12b3ce1

Please sign in to comment.