diff --git a/.changelog/6237.txt b/.changelog/6237.txt new file mode 100644 index 00000000000..28997ce8e16 --- /dev/null +++ b/.changelog/6237.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +bucket: added support for `matches_prefix` and `matches_suffix`` in `condition` of a `lifecycle_rule` in `google_storage_bucket` +``` diff --git a/google/resource_storage_bucket.go b/google/resource_storage_bucket.go index 5bf2be59516..e59fa806487 100644 --- a/google/resource_storage_bucket.go +++ b/google/resource_storage_bucket.go @@ -207,6 +207,18 @@ func resourceStorageBucket() *schema.Resource { Optional: true, Description: `Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.`, }, + "matches_prefix": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: `One or more matching name prefixes to satisfy this condition.`, + }, + "matches_suffix": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: `One or more matching name suffixes to satisfy this condition.`, + }, }, }, Description: `The Lifecycle Rule's condition configuration.`, @@ -987,6 +999,8 @@ func flattenBucketLifecycleRuleCondition(condition *storage.BucketLifecycleRuleC "days_since_custom_time": int(condition.DaysSinceCustomTime), "days_since_noncurrent_time": int(condition.DaysSinceNoncurrentTime), "noncurrent_time_before": condition.NoncurrentTimeBefore, + "matches_prefix": convertStringArrToInterface(condition.MatchesPrefix), + "matches_suffix": convertStringArrToInterface(condition.MatchesSuffix), } if condition.IsLive == nil { ruleCondition["with_state"] = "ANY" @@ -1196,6 +1210,25 @@ func expandStorageBucketLifecycleRuleCondition(v interface{}) (*storage.BucketLi transformed.NoncurrentTimeBefore = v.(string) } + if v, ok := condition["matches_prefix"]; ok { + prefixes := v.([]interface{}) + transformedPrefixes := make([]string, 0, len(prefixes)) + + for _, v := range prefixes { + transformedPrefixes = append(transformedPrefixes, v.(string)) + } + transformed.MatchesPrefix = transformedPrefixes + } + if v, ok := condition["matches_suffix"]; ok { + suffixes := v.([]interface{}) + transformedSuffixes := make([]string, 0, len(suffixes)) + + for _, v := range suffixes { + transformedSuffixes = append(transformedSuffixes, v.(string)) + } + transformed.MatchesSuffix = transformedSuffixes + } + return transformed, nil } @@ -1261,6 +1294,19 @@ func resourceGCSBucketLifecycleRuleConditionHash(v interface{}) int { buf.WriteString(fmt.Sprintf("%d-", v.(int))) } + if v, ok := m["matches_prefix"]; ok { + matches_prefixes := v.([]interface{}) + for _, matches_prefix := range matches_prefixes { + buf.WriteString(fmt.Sprintf("%s-", matches_prefix)) + } + } + if v, ok := m["matches_suffix"]; ok { + matches_suffixes := v.([]interface{}) + for _, matches_suffix := range matches_suffixes { + buf.WriteString(fmt.Sprintf("%s-", matches_suffix)) + } + } + return hashcode(buf.String()) } diff --git a/google/resource_storage_bucket_test.go b/google/resource_storage_bucket_test.go index 08f50624ac7..5e4f7041514 100644 --- a/google/resource_storage_bucket_test.go +++ b/google/resource_storage_bucket_test.go @@ -1356,6 +1356,24 @@ resource "google_storage_bucket" "bucket" { with_state = "ARCHIVED" } } + lifecycle_rule { + action { + type = "Delete" + } + condition { + matches_prefix = ["test"] + age = 2 + } + } + lifecycle_rule { + action { + type = "Delete" + } + condition { + matches_suffix = ["test"] + age = 2 + } + } } `, bucketName) } @@ -1421,6 +1439,24 @@ resource "google_storage_bucket" "bucket" { with_state = "ARCHIVED" } } + lifecycle_rule { + action { + type = "Delete" + } + condition { + matches_prefix = ["test"] + age = 2 + } + } + lifecycle_rule { + action { + type = "Delete" + } + condition { + matches_suffix = ["test"] + age = 2 + } + } } `, bucketName) } diff --git a/website/docs/r/storage_bucket.html.markdown b/website/docs/r/storage_bucket.html.markdown index d6b9eefc011..6fbc8f01795 100644 --- a/website/docs/r/storage_bucket.html.markdown +++ b/website/docs/r/storage_bucket.html.markdown @@ -123,6 +123,10 @@ The following arguments are supported: * `matches_storage_class` - (Optional) [Storage Class](https://cloud.google.com/storage/docs/storage-classes) of objects to satisfy this condition. Supported values include: `STANDARD`, `MULTI_REGIONAL`, `REGIONAL`, `NEARLINE`, `COLDLINE`, `ARCHIVE`, `DURABLE_REDUCED_AVAILABILITY`. +* `matches_prefix` - (Optional) One or more matching name prefixes to satisfy this condition. + +* `matches_suffix` - (Optional) One or more matching name suffixes to satisfy this condition. + * `num_newer_versions` - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition. * `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.