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

add support for MatchesPrefix and MatchesSuffix conditions #12175

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
3 changes: 3 additions & 0 deletions .changelog/6237.txt
Original file line number Diff line number Diff line change
@@ -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`
```
46 changes: 46 additions & 0 deletions google/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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())
}

Expand Down
36 changes: 36 additions & 0 deletions google/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/storage_bucket.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down