Skip to content

Commit a823eeb

Browse files
Fixes LifecycleRules Conditions not being set issue. (#10717) (#18231)
[upstream:4dd153f6cb4e3f3b58973bcf5723502b67ccf472] Signed-off-by: Modular Magician <[email protected]>
1 parent 8318041 commit a823eeb

File tree

3 files changed

+107
-18
lines changed

3 files changed

+107
-18
lines changed

google/services/storage/resource_storage_bucket.go

+39
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,21 @@ func ResourceStorageBucket() *schema.Resource {
263263
Elem: &schema.Schema{Type: schema.TypeString},
264264
Description: `One or more matching name suffixes to satisfy this condition.`,
265265
},
266+
"send_days_since_noncurrent_time_if_zero": {
267+
Type: schema.TypeBool,
268+
Optional: true,
269+
Description: `While set true, days_since_noncurrent_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_noncurrent_time field. It can be used alone or together with days_since_noncurrent_time.`,
270+
},
271+
"send_days_since_custom_time_if_zero": {
272+
Type: schema.TypeBool,
273+
Optional: true,
274+
Description: `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.`,
275+
},
276+
"send_num_newer_versions_if_zero": {
277+
Type: schema.TypeBool,
278+
Optional: true,
279+
Description: `While set true, num_newer_versions 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 num_newer_versions field. It can be used alone or together with num_newer_versions.`,
280+
},
266281
},
267282
},
268283
Description: `The Lifecycle Rule's condition configuration.`,
@@ -1377,6 +1392,9 @@ func flattenBucketLifecycleRuleCondition(index int, d *schema.ResourceData, cond
13771392
if v, ok := d.GetOk(fmt.Sprintf("lifecycle_rule.%d.condition", index)); ok {
13781393
state_condition := v.(*schema.Set).List()[0].(map[string]interface{})
13791394
ruleCondition["no_age"] = state_condition["no_age"].(bool)
1395+
ruleCondition["send_days_since_noncurrent_time_if_zero"] = state_condition["send_days_since_noncurrent_time_if_zero"].(bool)
1396+
ruleCondition["send_days_since_custom_time_if_zero"] = state_condition["send_days_since_custom_time_if_zero"].(bool)
1397+
ruleCondition["send_num_newer_versions_if_zero"] = state_condition["send_num_newer_versions_if_zero"].(bool)
13801398
}
13811399

13821400
return ruleCondition
@@ -1569,6 +1587,9 @@ func expandStorageBucketLifecycleRuleCondition(v interface{}) (*storage.BucketLi
15691587

15701588
if v, ok := condition["num_newer_versions"]; ok {
15711589
transformed.NumNewerVersions = int64(v.(int))
1590+
if u, ok := condition["send_num_newer_versions_if_zero"]; ok && u.(bool) {
1591+
transformed.ForceSendFields = append(transformed.ForceSendFields, "NumNewerVersions")
1592+
}
15721593
}
15731594

15741595
if v, ok := condition["custom_time_before"]; ok {
@@ -1577,10 +1598,16 @@ func expandStorageBucketLifecycleRuleCondition(v interface{}) (*storage.BucketLi
15771598

15781599
if v, ok := condition["days_since_custom_time"]; ok {
15791600
transformed.DaysSinceCustomTime = int64(v.(int))
1601+
if u, ok := condition["send_days_since_custom_time_if_zero"]; ok && u.(bool) {
1602+
transformed.ForceSendFields = append(transformed.ForceSendFields, "DaysSinceCustomTime")
1603+
}
15801604
}
15811605

15821606
if v, ok := condition["days_since_noncurrent_time"]; ok {
15831607
transformed.DaysSinceNoncurrentTime = int64(v.(int))
1608+
if u, ok := condition["send_days_since_noncurrent_time_if_zero"]; ok && u.(bool) {
1609+
transformed.ForceSendFields = append(transformed.ForceSendFields, "DaysSinceNoncurrentTime")
1610+
}
15841611
}
15851612

15861613
if v, ok := condition["noncurrent_time_before"]; ok {
@@ -1683,6 +1710,18 @@ func resourceGCSBucketLifecycleRuleConditionHash(v interface{}) int {
16831710
buf.WriteString(fmt.Sprintf("%d-", v.(int)))
16841711
}
16851712

1713+
if v, ok := m["send_days_since_noncurrent_time_if_zero"]; ok {
1714+
buf.WriteString(fmt.Sprintf("%t-", v.(bool)))
1715+
}
1716+
1717+
if v, ok := m["send_days_since_custom_time_if_zero"]; ok {
1718+
buf.WriteString(fmt.Sprintf("%t-", v.(bool)))
1719+
}
1720+
1721+
if v, ok := m["send_num_newer_versions_if_zero"]; ok {
1722+
buf.WriteString(fmt.Sprintf("%t-", v.(bool)))
1723+
}
1724+
16861725
if v, ok := m["matches_prefix"]; ok {
16871726
matches_prefixes := v.([]interface{})
16881727
for _, matches_prefix := range matches_prefixes {

google/services/storage/resource_storage_bucket_test.go

+59-15
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ func TestAccStorageBucket_lifecycleRuleStateAny(t *testing.T) {
545545
})
546546
}
547547

548-
func TestAccStorageBucket_lifecycleRulesNoAge(t *testing.T) {
548+
func TestAccStorageBucket_lifecycleRulesVirtualFields(t *testing.T) {
549549
t.Parallel()
550550
var bucket storage.Bucket
551551
bucketName := acctest.TestBucketName(t)
@@ -569,7 +569,7 @@ func TestAccStorageBucket_lifecycleRulesNoAge(t *testing.T) {
569569
ImportStateVerifyIgnore: []string{"force_destroy"},
570570
},
571571
{
572-
Config: testAccStorageBucket_customAttributes_withLifecycleNoAge(bucketName),
572+
Config: testAccStorageBucket_customAttributes_withLifecycleVirtualFieldsUpdate1(bucketName),
573573
Check: resource.ComposeTestCheckFunc(
574574
testAccCheckStorageBucketExists(
575575
t, "google_storage_bucket.bucket", bucketName, &bucket),
@@ -580,10 +580,10 @@ func TestAccStorageBucket_lifecycleRulesNoAge(t *testing.T) {
580580
ResourceName: "google_storage_bucket.bucket",
581581
ImportState: true,
582582
ImportStateVerify: true,
583-
ImportStateVerifyIgnore: []string{"force_destroy", "lifecycle_rule.1.condition.0.no_age"},
583+
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"},
584584
},
585585
{
586-
Config: testAccStorageBucket_customAttributes_withLifecycleNoAgeAndAge(bucketName),
586+
Config: testAccStorageBucket_customAttributes_withLifecycleVirtualFieldsUpdate2(bucketName),
587587
Check: resource.ComposeTestCheckFunc(
588588
testAccCheckStorageBucketExists(
589589
t, "google_storage_bucket.bucket", bucketName, &bucket),
@@ -594,7 +594,7 @@ func TestAccStorageBucket_lifecycleRulesNoAge(t *testing.T) {
594594
ResourceName: "google_storage_bucket.bucket",
595595
ImportState: true,
596596
ImportStateVerify: true,
597-
ImportStateVerifyIgnore: []string{"force_destroy", "lifecycle_rule.1.condition.0.no_age"},
597+
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"},
598598
},
599599
{
600600
Config: testAccStorageBucket_customAttributes_withLifecycle1(bucketName),
@@ -1814,57 +1814,101 @@ resource "google_storage_bucket" "bucket" {
18141814
`, bucketName)
18151815
}
18161816

1817-
func testAccStorageBucket_customAttributes_withLifecycleNoAge(bucketName string) string {
1817+
func testAccStorageBucket_customAttributes_withLifecycleVirtualFieldsUpdate1(bucketName string) string {
18181818
return fmt.Sprintf(`
18191819
resource "google_storage_bucket" "bucket" {
18201820
name = "%s"
18211821
location = "EU"
18221822
force_destroy = "true"
18231823
lifecycle_rule {
18241824
action {
1825-
type = "Delete"
1825+
type = "Delete"
18261826
}
18271827
condition {
1828-
age = 10
1829-
no_age = false
1828+
age = 10
1829+
no_age = false
1830+
days_since_noncurrent_time = 0
1831+
send_days_since_noncurrent_time_if_zero = false
1832+
days_since_custom_time = 0
1833+
send_days_since_custom_time_if_zero = false
1834+
num_newer_versions = 0
1835+
send_num_newer_versions_if_zero = false
18301836
}
18311837
}
18321838
lifecycle_rule {
18331839
action {
18341840
type = "Delete"
18351841
}
18361842
condition {
1837-
num_newer_versions = 2
18381843
no_age = true
1844+
days_since_noncurrent_time = 0
1845+
send_days_since_noncurrent_time_if_zero = true
1846+
days_since_custom_time = 0
1847+
send_days_since_custom_time_if_zero = true
1848+
num_newer_versions = 0
1849+
send_num_newer_versions_if_zero = true
1850+
}
1851+
}
1852+
lifecycle_rule {
1853+
action {
1854+
type = "Delete"
1855+
}
1856+
condition {
1857+
send_days_since_noncurrent_time_if_zero = true
1858+
send_days_since_custom_time_if_zero = true
1859+
send_num_newer_versions_if_zero = true
18391860
}
18401861
}
18411862
}
18421863
`, bucketName)
18431864
}
18441865

1845-
func testAccStorageBucket_customAttributes_withLifecycleNoAgeAndAge(bucketName string) string {
1866+
func testAccStorageBucket_customAttributes_withLifecycleVirtualFieldsUpdate2(bucketName string) string {
18461867
return fmt.Sprintf(`
18471868
resource "google_storage_bucket" "bucket" {
18481869
name = "%s"
18491870
location = "EU"
18501871
force_destroy = "true"
18511872
lifecycle_rule {
18521873
action {
1853-
type = "Delete"
1874+
type = "Delete"
18541875
}
18551876
condition {
1856-
age = 10
1857-
no_age = false
1877+
age = 10
1878+
no_age = false
1879+
days_since_noncurrent_time = 0
1880+
send_days_since_noncurrent_time_if_zero = true
1881+
days_since_custom_time = 0
1882+
send_days_since_custom_time_if_zero = true
1883+
num_newer_versions = 0
1884+
send_num_newer_versions_if_zero = true
18581885
}
18591886
}
18601887
lifecycle_rule {
18611888
action {
18621889
type = "Delete"
18631890
}
18641891
condition {
1865-
num_newer_versions = 2
18661892
age = 10
18671893
no_age = true
1894+
custom_time_before = "2022-09-01"
1895+
days_since_noncurrent_time = 0
1896+
send_days_since_noncurrent_time_if_zero = false
1897+
days_since_custom_time = 0
1898+
send_days_since_custom_time_if_zero = false
1899+
num_newer_versions = 0
1900+
send_num_newer_versions_if_zero = false
1901+
}
1902+
}
1903+
lifecycle_rule {
1904+
action {
1905+
type = "Delete"
1906+
}
1907+
condition {
1908+
custom_time_before = "2022-09-01"
1909+
send_days_since_noncurrent_time_if_zero = false
1910+
send_days_since_custom_time_if_zero = false
1911+
send_num_newer_versions_if_zero = false
18681912
}
18691913
}
18701914
}

website/docs/r/storage_bucket.html.markdown

+9-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ The following arguments are supported:
171171

172172
<a name="nested_condition"></a>The `condition` block supports the following elements, and requires at least one to be defined. If you specify multiple conditions in a rule, an object has to match all of the conditions for the action to be taken:
173173

174-
* `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.
174+
* `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

176176
* `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.
177177

@@ -187,12 +187,18 @@ The following arguments are supported:
187187

188188
* `num_newer_versions` - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition. 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 `num_newer_versions` condition.
189189

190+
* `send_num_newer_versions_if_zero` - (Optional) While set true, `num_newer_versions` 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 `num_newer_versions` field. It can be used alone or together with `num_newer_versions`.
191+
190192
* `custom_time_before` - (Optional) A date in the RFC 3339 format YYYY-MM-DD. This condition is satisfied when the customTime metadata for the object is set to an earlier date than the date used in this lifecycle condition.
191193

192-
* `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.
194+
* `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.
195+
196+
* `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`.
193197

194198
* `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.
195199

200+
* `send_days_since_noncurrent_time_if_zero` - (Optional) While set true, `days_since_noncurrent_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_noncurrent_time` field. It can be used alone or together with `days_since_noncurrent_time`.
201+
196202
* `noncurrent_time_before` - (Optional) Relevant only for versioned objects. The date in RFC 3339 (e.g. `2017-06-13`) when the object became nonconcurrent. 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 `noncurrent_time_before` condition.
197203

198204
<a name="nested_autoclass"></a>The `autoclass` block supports:
@@ -259,7 +265,7 @@ The following arguments are supported:
259265

260266
<a name="nested_soft_delete_policy"></a>The `soft_delete_policy` block supports:
261267

262-
* `retention_duration_seconds` - (Optional, Default: 604800) The duration in seconds that soft-deleted objects in the bucket will be retained and cannot be permanently deleted. Default value is 604800. The value must be in between 604800(7 days) and 7776000(90 days). **Note**: To disable the soft delete policy on a bucket, This field must be set to 0.
268+
* `retention_duration_seconds` - (Optional, Default: 604800) The duration in seconds that soft-deleted objects in the bucket will be retained and cannot be permanently deleted. Default value is 604800. The value must be in between 604800(7 days) and 7776000(90 days). **Note**: To disable the soft delete policy on a bucket, This field must be set to 0.
263269

264270
* `effective_time` - (Computed) Server-determined value that indicates the time from which the policy, or one with a greater retention, was effective. This value is in RFC 3339 format.
265271

0 commit comments

Comments
 (0)