Skip to content

Commit 2b00543

Browse files
kautikdkNickElliot
andauthored
Add send_age_if_zero field and deprecate no_age. (GoogleCloudPlatform#11098)
Co-authored-by: Nick Elliot <[email protected]>
1 parent a2799fb commit 2b00543

File tree

4 files changed

+64
-27
lines changed

4 files changed

+64
-27
lines changed

mmv1/third_party/terraform/services/storage/resource_storage_bucket.go.erb

+24-4
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func ResourceStorageBucket() *schema.Resource {
229229
},
230230
"no_age": {
231231
Type: schema.TypeBool,
232+
Deprecated: "`no_age` is deprecated and will be removed in a future major release. Use `send_age_if_zero` instead.",
232233
Optional: true,
233234
Description: `While set true, age value will be omitted.Required to set true when age is unset in the config file.`,
234235
},
@@ -262,6 +263,12 @@ func ResourceStorageBucket() *schema.Resource {
262263
Elem: &schema.Schema{Type: schema.TypeString},
263264
Description: `One or more matching name suffixes to satisfy this condition.`,
264265
},
266+
"send_age_if_zero": {
267+
Type: schema.TypeBool,
268+
Optional: true,
269+
Default: true,
270+
Description: `While set true, age value will be sent in the request even for zero value of the field. This field is only useful for setting 0 value to the age field. It can be used alone or together with age.`,
271+
},
265272
"send_days_since_noncurrent_time_if_zero": {
266273
Type: schema.TypeBool,
267274
Optional: true,
@@ -1396,13 +1403,21 @@ func flattenBucketLifecycleRuleCondition(index int, d *schema.ResourceData, cond
13961403
ruleCondition["with_state"] = "ARCHIVED"
13971404
}
13981405
}
1399-
// setting no_age value from state config since it is terraform only variable and not getting value from backend.
1406+
// Setting the lifecycle condition virtual fields from the state file if they
1407+
// are already present otherwise setting them to individual default values.
14001408
if v, ok := d.GetOk(fmt.Sprintf("lifecycle_rule.%d.condition",index)); ok{
14011409
state_condition := v.(*schema.Set).List()[0].(map[string]interface{})
14021410
ruleCondition["no_age"] = state_condition["no_age"].(bool)
14031411
ruleCondition["send_days_since_noncurrent_time_if_zero"] = state_condition["send_days_since_noncurrent_time_if_zero"].(bool)
14041412
ruleCondition["send_days_since_custom_time_if_zero"] = state_condition["send_days_since_custom_time_if_zero"].(bool)
14051413
ruleCondition["send_num_newer_versions_if_zero"] = state_condition["send_num_newer_versions_if_zero"].(bool)
1414+
ruleCondition["send_age_if_zero"] = state_condition["send_age_if_zero"].(bool)
1415+
} else {
1416+
ruleCondition["no_age"] = false
1417+
ruleCondition["send_age_if_zero"] = true
1418+
ruleCondition["send_days_since_noncurrent_time_if_zero"] = false
1419+
ruleCondition["send_days_since_custom_time_if_zero"] = false
1420+
ruleCondition["send_num_newer_versions_if_zero"] = false
14061421
}
14071422

14081423
return ruleCondition
@@ -1552,13 +1567,15 @@ func expandStorageBucketLifecycleRuleCondition(v interface{}) (*storage.BucketLi
15521567

15531568
condition := conditions[0].(map[string]interface{})
15541569
transformed := &storage.BucketLifecycleRuleCondition{}
1555-
// Setting high precedence of no_age over age when both used together.
1570+
// Setting high precedence of no_age over age and send_age_if_zero.
15561571
// Only sets age value when no_age is not present or no_age is present and has false value
15571572
if v, ok := condition["no_age"]; !ok || !(v.(bool)) {
15581573
if v, ok := condition["age"]; ok {
15591574
age := int64(v.(int))
1560-
transformed.Age = &age
1561-
transformed.ForceSendFields = append(transformed.ForceSendFields, "Age")
1575+
u, ok := condition["send_age_if_zero"]
1576+
if age > 0 || (ok && u.(bool)) {
1577+
transformed.Age = &age
1578+
}
15621579
}
15631580
}
15641581

@@ -1672,6 +1689,9 @@ func resourceGCSBucketLifecycleRuleConditionHash(v interface{}) int {
16721689
if v, ok := m["no_age"]; ok && v.(bool){
16731690
buf.WriteString(fmt.Sprintf("%t-", v.(bool)))
16741691
} else {
1692+
if v, ok := m["send_age_if_zero"]; ok {
1693+
buf.WriteString(fmt.Sprintf("%t-", v.(bool)))
1694+
}
16751695
if v, ok := m["age"]; ok {
16761696
buf.WriteString(fmt.Sprintf("%d-", v.(int)))
16771697
}

mmv1/third_party/terraform/services/storage/resource_storage_bucket_test.go.erb

+24-19
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func TestAccStorageBucket_lifecycleRulesMultiple(t *testing.T) {
461461
ResourceName: "google_storage_bucket.bucket",
462462
ImportState: true,
463463
ImportStateVerify: true,
464-
ImportStateVerifyIgnore: []string{"force_destroy"},
464+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero","lifecycle_rule.1.condition.0.send_age_if_zero","lifecycle_rule.2.condition.0.send_age_if_zero","lifecycle_rule.3.condition.0.send_age_if_zero","lifecycle_rule.4.condition.0.send_age_if_zero","lifecycle_rule.5.condition.0.send_age_if_zero","lifecycle_rule.6.condition.0.send_age_if_zero","lifecycle_rule.7.condition.0.send_age_if_zero","lifecycle_rule.8.condition.0.send_age_if_zero","lifecycle_rule.9.condition.0.send_age_if_zero"},
465465
},
466466
{
467467
Config: testAccStorageBucket_lifecycleRulesMultiple_update(bucketName),
@@ -470,7 +470,7 @@ func TestAccStorageBucket_lifecycleRulesMultiple(t *testing.T) {
470470
ResourceName: "google_storage_bucket.bucket",
471471
ImportState: true,
472472
ImportStateVerify: true,
473-
ImportStateVerifyIgnore: []string{"force_destroy"},
473+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero","lifecycle_rule.1.condition.0.send_age_if_zero","lifecycle_rule.2.condition.0.send_age_if_zero","lifecycle_rule.3.condition.0.send_age_if_zero","lifecycle_rule.4.condition.0.send_age_if_zero","lifecycle_rule.5.condition.0.send_age_if_zero","lifecycle_rule.6.condition.0.send_age_if_zero","lifecycle_rule.7.condition.0.send_age_if_zero","lifecycle_rule.8.condition.0.send_age_if_zero","lifecycle_rule.9.condition.0.send_age_if_zero"},
474474
},
475475
},
476476
})
@@ -499,7 +499,7 @@ func TestAccStorageBucket_lifecycleRuleStateLive(t *testing.T) {
499499
ResourceName: "google_storage_bucket.bucket",
500500
ImportState: true,
501501
ImportStateVerify: true,
502-
ImportStateVerifyIgnore: []string{"force_destroy"},
502+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero","lifecycle_rule.1.condition.0.send_age_if_zero"},
503503
},
504504
},
505505
})
@@ -528,7 +528,7 @@ func TestAccStorageBucket_lifecycleRuleStateArchived(t *testing.T) {
528528
ResourceName: "google_storage_bucket.bucket",
529529
ImportState: true,
530530
ImportStateVerify: true,
531-
ImportStateVerifyIgnore: []string{"force_destroy"},
531+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero"},
532532
},
533533
{
534534
Config: testAccStorageBucket_lifecycleRule_withStateArchived(bucketName),
@@ -542,7 +542,7 @@ func TestAccStorageBucket_lifecycleRuleStateArchived(t *testing.T) {
542542
ResourceName: "google_storage_bucket.bucket",
543543
ImportState: true,
544544
ImportStateVerify: true,
545-
ImportStateVerifyIgnore: []string{"force_destroy"},
545+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero"},
546546
},
547547
},
548548
})
@@ -571,7 +571,7 @@ func TestAccStorageBucket_lifecycleRuleStateAny(t *testing.T) {
571571
ResourceName: "google_storage_bucket.bucket",
572572
ImportState: true,
573573
ImportStateVerify: true,
574-
ImportStateVerifyIgnore: []string{"force_destroy"},
574+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero"},
575575
},
576576
{
577577
Config: testAccStorageBucket_lifecycleRule_withStateLive(bucketName),
@@ -585,7 +585,7 @@ func TestAccStorageBucket_lifecycleRuleStateAny(t *testing.T) {
585585
ResourceName: "google_storage_bucket.bucket",
586586
ImportState: true,
587587
ImportStateVerify: true,
588-
ImportStateVerifyIgnore: []string{"force_destroy"},
588+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero","lifecycle_rule.1.condition.0.send_age_if_zero"},
589589
},
590590
{
591591
Config: testAccStorageBucket_lifecycleRule_withStateAny(bucketName),
@@ -599,7 +599,7 @@ func TestAccStorageBucket_lifecycleRuleStateAny(t *testing.T) {
599599
ResourceName: "google_storage_bucket.bucket",
600600
ImportState: true,
601601
ImportStateVerify: true,
602-
ImportStateVerifyIgnore: []string{"force_destroy"},
602+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero"},
603603
},
604604
{
605605
Config: testAccStorageBucket_lifecycleRule_withStateArchived(bucketName),
@@ -613,7 +613,7 @@ func TestAccStorageBucket_lifecycleRuleStateAny(t *testing.T) {
613613
ResourceName: "google_storage_bucket.bucket",
614614
ImportState: true,
615615
ImportStateVerify: true,
616-
ImportStateVerifyIgnore: []string{"force_destroy"},
616+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero"},
617617
},
618618
},
619619
})
@@ -622,6 +622,7 @@ func TestAccStorageBucket_lifecycleRuleStateAny(t *testing.T) {
622622
func TestAccStorageBucket_lifecycleRulesVirtualFields(t *testing.T) {
623623
t.Parallel()
624624
var bucket storage.Bucket
625+
zero_age := int64(0)
625626
bucketName := acctest.TestBucketName(t)
626627

627628
acctest.VcrTest(t, resource.TestCase{
@@ -647,28 +648,30 @@ func TestAccStorageBucket_lifecycleRulesVirtualFields(t *testing.T) {
647648
Check: resource.ComposeTestCheckFunc(
648649
testAccCheckStorageBucketExists(
649650
t, "google_storage_bucket.bucket", bucketName, &bucket),
650-
testAccCheckStorageBucketLifecycleConditionNoAge(nil, &bucket),
651+
testAccCheckStorageBucketLifecycleConditionNoAge(nil, &bucket, 1),
652+
testAccCheckStorageBucketLifecycleConditionNoAge(&zero_age, &bucket, 2),
651653
),
652654
},
653655
{
654656
ResourceName: "google_storage_bucket.bucket",
655657
ImportState: true,
656658
ImportStateVerify: true,
657-
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.1.condition.0.no_age","lifecycle_rule.1.condition.0.send_days_since_noncurrent_time_if_zero","lifecycle_rule.2.condition.0.send_days_since_noncurrent_time_if_zero","lifecycle_rule.1.condition.0.send_days_since_custom_time_if_zero","lifecycle_rule.2.condition.0.send_days_since_custom_time_if_zero","lifecycle_rule.1.condition.0.send_num_newer_versions_if_zero","lifecycle_rule.2.condition.0.send_num_newer_versions_if_zero"},
659+
ImportStateVerifyIgnore: []string{"force_destroy", "lifecycle_rule.1.condition.0.no_age", "lifecycle_rule.1.condition.0.send_days_since_noncurrent_time_if_zero", "lifecycle_rule.2.condition.0.send_days_since_noncurrent_time_if_zero", "lifecycle_rule.1.condition.0.send_days_since_custom_time_if_zero", "lifecycle_rule.2.condition.0.send_days_since_custom_time_if_zero", "lifecycle_rule.1.condition.0.send_num_newer_versions_if_zero", "lifecycle_rule.2.condition.0.send_num_newer_versions_if_zero", "lifecycle_rule.1.condition.0.send_age_if_zero"},
658660
},
659661
{
660662
Config: testAccStorageBucket_customAttributes_withLifecycleVirtualFieldsUpdate2(bucketName),
661663
Check: resource.ComposeTestCheckFunc(
662664
testAccCheckStorageBucketExists(
663665
t, "google_storage_bucket.bucket", bucketName, &bucket),
664-
testAccCheckStorageBucketLifecycleConditionNoAge(nil, &bucket),
666+
testAccCheckStorageBucketLifecycleConditionNoAge(nil, &bucket, 1),
667+
testAccCheckStorageBucketLifecycleConditionNoAge(nil, &bucket, 2),
665668
),
666669
},
667670
{
668671
ResourceName: "google_storage_bucket.bucket",
669672
ImportState: true,
670673
ImportStateVerify: true,
671-
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.1.condition.0.no_age","lifecycle_rule.0.condition.0.send_days_since_noncurrent_time_if_zero","lifecycle_rule.0.condition.0.send_days_since_custom_time_if_zero","lifecycle_rule.0.condition.0.send_num_newer_versions_if_zero"},
674+
ImportStateVerifyIgnore: []string{"force_destroy", "lifecycle_rule.1.condition.0.no_age", "lifecycle_rule.0.condition.0.send_days_since_noncurrent_time_if_zero", "lifecycle_rule.0.condition.0.send_days_since_custom_time_if_zero", "lifecycle_rule.0.condition.0.send_num_newer_versions_if_zero", "lifecycle_rule.0.condition.0.send_age_if_zero", "lifecycle_rule.1.condition.0.send_age_if_zero", "lifecycle_rule.2.condition.0.send_age_if_zero"},
672675
},
673676
{
674677
Config: testAccStorageBucket_customAttributes_withLifecycle1(bucketName),
@@ -847,7 +850,7 @@ func TestAccStorageBucket_update(t *testing.T) {
847850
ResourceName: "google_storage_bucket.bucket",
848851
ImportState: true,
849852
ImportStateVerify: true,
850-
ImportStateVerifyIgnore: []string{"force_destroy"},
853+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero"},
851854
},
852855
{
853856
Config: testAccStorageBucket_customAttributes_withLifecycle2(bucketName),
@@ -863,7 +866,7 @@ func TestAccStorageBucket_update(t *testing.T) {
863866
ResourceName: "google_storage_bucket.bucket",
864867
ImportState: true,
865868
ImportStateVerify: true,
866-
ImportStateVerifyIgnore: []string{"force_destroy"},
869+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero","lifecycle_rule.1.condition.0.send_age_if_zero"},
867870
},
868871
{
869872
Config: testAccStorageBucket_customAttributes_withLifecycle1Update(bucketName),
@@ -879,7 +882,7 @@ func TestAccStorageBucket_update(t *testing.T) {
879882
ResourceName: "google_storage_bucket.bucket",
880883
ImportState: true,
881884
ImportStateVerify: true,
882-
ImportStateVerifyIgnore: []string{"force_destroy"},
885+
ImportStateVerifyIgnore: []string{"force_destroy","lifecycle_rule.0.condition.0.send_age_if_zero"},
883886
},
884887
{
885888
Config: testAccStorageBucket_customAttributes(bucketName),
@@ -1681,10 +1684,10 @@ func testAccCheckStorageBucketLifecycleConditionState(expected *bool, b *storage
16811684
}
16821685
}
16831686

1684-
func testAccCheckStorageBucketLifecycleConditionNoAge(expected *int64, b *storage.Bucket) resource.TestCheckFunc {
1687+
func testAccCheckStorageBucketLifecycleConditionNoAge(expected *int64, b *storage.Bucket, index int) resource.TestCheckFunc {
16851688
return func(s *terraform.State) error {
1686-
actual := b.Lifecycle.Rule[1].Condition.Age
1687-
if expected == nil && b.Lifecycle.Rule[1].Condition.Age == nil {
1689+
actual := b.Lifecycle.Rule[index].Condition.Age
1690+
if expected == nil && b.Lifecycle.Rule[index].Condition.Age == nil {
16881691
return nil
16891692
}
16901693
if expected == nil {
@@ -1978,6 +1981,7 @@ resource "google_storage_bucket" "bucket" {
19781981
condition {
19791982
age = 10
19801983
no_age = true
1984+
send_age_if_zero = false
19811985
custom_time_before = "2022-09-01"
19821986
days_since_noncurrent_time = 0
19831987
send_days_since_noncurrent_time_if_zero = false
@@ -1992,6 +1996,7 @@ resource "google_storage_bucket" "bucket" {
19921996
type = "Delete"
19931997
}
19941998
condition {
1999+
send_age_if_zero= false
19952000
custom_time_before = "2022-09-01"
19962001
send_days_since_noncurrent_time_if_zero = false
19972002
send_days_since_custom_time_if_zero = false

mmv1/third_party/terraform/website/docs/guides/version_6_upgrade.html.markdown

+10
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,13 @@ An empty value means the setting should be cleared.
126126

127127
Cloud Run does not provide a default value for liveness probe. Now removing this field
128128
will remove the liveness probe from the Cloud Run service.
129+
130+
## Resource: `google_storage_bucket`
131+
132+
### `lifecycle_rule.condition.no_age` is now removed
133+
134+
Previously `lifecycle_rule.condition.age` attirbute was being set zero value by default and `lifecycle_rule.condition.no_age` was introduced to prevent that.
135+
Now `lifecycle_rule.condition.no_age` is no longer supported and `lifecycle_rule.condition.age` won't set a zero value by default.
136+
Removed in favor of the field `lifecycle_rule.condition.send_age_if_zero` which can be used to set zero value for `lifecycle_rule.condition.age` attribute.
137+
138+
For a seamless update, if your state today uses `no_age=true`, update it to remove `no_age` and set `send_age_if_zero=false`. If you do not use `no_age=true`, you will need to add `send_age_if_zero=true` to your state to avoid any changes after updating to 6.0.0.

mmv1/third_party/terraform/website/docs/r/storage_bucket.html.markdown

+6-4
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ resource "google_storage_bucket" "auto-expire" {
6969
}
7070
```
7171

72-
## Example Usage - Life cycle settings for storage bucket objects with `no_age` enabled
73-
When creating a life cycle condition that does not also include an `age` field, a default `age` of 0 will be set. Set the `no_age` flag to `true` to prevent this and avoid any potentially unintended interactions.
72+
## Example Usage - Life cycle settings for storage bucket objects with `send_age_if_zero` disabled
73+
When creating a life cycle condition that does not also include an `age` field, a default `age` of 0 will be set. Set the `send_age_if_zero` flag to `false` to prevent this and avoid any potentially unintended interactions.
7474

7575
```hcl
7676
resource "google_storage_bucket" "no-age-enabled" {
@@ -85,7 +85,7 @@ resource "google_storage_bucket" "no-age-enabled" {
8585
}
8686
condition {
8787
days_since_noncurrent_time = 3
88-
no_age = true
88+
send_age_if_zero = false
8989
}
9090
}
9191
}
@@ -173,7 +173,7 @@ The following arguments are supported:
173173

174174
* `age` - (Optional) Minimum age of an object in days to satisfy this condition. If not supplied alongside another condition and without setting `no_age` to `true`, a default `age` of 0 will be set.
175175

176-
* `no_age` - (Optional) While set `true`, `age` value will be omitted from requests. This prevents a default age of `0` from being applied, and if you do not have an `age` value set, setting this to `true` is strongly recommended. When unset and other conditions are set to zero values, this can result in a rule that applies your action to all files in the bucket.
176+
* `no_age` - (Optional, Deprecated) While set `true`, `age` value will be omitted from requests. This prevents a default age of `0` from being applied, and if you do not have an `age` value set, setting this to `true` is strongly recommended. When unset and other conditions are set to zero values, this can result in a rule that applies your action to all files in the bucket. `no_age` is deprecated and will be removed in a future major release. Use `send_age_if_zero` instead.
177177

178178
* `created_before` - (Optional) A date in the RFC 3339 format YYYY-MM-DD. This condition is satisfied when an object is created before midnight of the specified date in UTC.
179179

@@ -193,6 +193,8 @@ The following arguments are supported:
193193

194194
* `days_since_custom_time` - (Optional) Days since the date set in the `customTime` metadata for the object. This condition is satisfied when the current date and time is at least the specified number of days after the `customTime`. Due to a current bug you are unable to set this value to `0` within Terraform. When set to `0` it will be ignored, and your state will treat it as though you supplied no `days_since_custom_time` condition.
195195

196+
* `send_age_if_zero` - (Optional, Default: true) While set true, `age` value will be sent in the request even for zero value of the field. This field is only useful and required for setting 0 value to the `age` field. It can be used alone or together with `age` attribute. **NOTE** `age` attibute with `0` value will be ommitted from the API request if `send_age_if_zero` field is having `false` value.
197+
196198
* `send_days_since_custom_time_if_zero` - (Optional) While set true, `days_since_custom_time` value will be sent in the request even for zero value of the field. This field is only useful for setting 0 value to the `days_since_custom_time` field. It can be used alone or together with `days_since_custom_time`.
197199

198200
* `days_since_noncurrent_time` - (Optional) Relevant only for versioned objects. Number of days elapsed since the noncurrent timestamp of an object. Due to a current bug you are unable to set this value to `0` within Terraform. When set to `0` it will be ignored, and your state will treat it as though you supplied no `days_since_noncurrent_time` condition.

0 commit comments

Comments
 (0)