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 More GCS Bucket lifecycle conditions #4221

Merged
49 changes: 45 additions & 4 deletions third_party/terraform/resources/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ func resourceStorageBucket() *schema.Resource {
Optional: true,
Description: `Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.`,
},
"custom_time_before": {
Type: schema.TypeString,
Optional: true,
Description: `Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.`,
},
"days_since_custom_time": {
Type: schema.TypeInt,
Optional: true,
Description: `Number of days elapsed since the user-specified timestamp set on an object.`,
},
"days_since_noncurrent_time": {
Type: schema.TypeInt,
Optional: true,
Description: `Number of days elapsed since the noncurrent timestamp of an object. This
condition is relevant only for versioned objects.`,
},
"noncurrent_time_before": {
Type: schema.TypeString,
Optional: true,
Description: `Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.`,
},
"with_state": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -1022,10 +1043,14 @@ func flattenBucketLifecycleRuleAction(action *storage.BucketLifecycleRuleAction)

func flattenBucketLifecycleRuleCondition(condition *storage.BucketLifecycleRuleCondition) map[string]interface{} {
ruleCondition := map[string]interface{}{
"age": int(condition.Age),
"created_before": condition.CreatedBefore,
"matches_storage_class": convertStringArrToInterface(condition.MatchesStorageClass),
"num_newer_versions": int(condition.NumNewerVersions),
"age": int(condition.Age),
"created_before": condition.CreatedBefore,
"matches_storage_class": convertStringArrToInterface(condition.MatchesStorageClass),
"num_newer_versions": int(condition.NumNewerVersions),
"custom_time_before": condition.CustomTimeBefore,
"days_since_custom_time": int(condition.DaysSinceCustomTime),
"days_since_noncurrent_time": int(condition.DaysSinceNoncurrentTime),
"noncurrent_time_before": condition.NoncurrentTimeBefore,
}
if condition.IsLive == nil {
ruleCondition["with_state"] = "ANY"
Expand Down Expand Up @@ -1239,6 +1264,22 @@ func expandStorageBucketLifecycleRuleCondition(v interface{}) (*storage.BucketLi
transformed.NumNewerVersions = int64(v.(int))
}

if v, ok := condition["custom_time_before"]; ok {
transformed.CustomTimeBefore = v.(string)
}

if v, ok := condition["days_since_custom_time"]; ok {
transformed.DaysSinceCustomTime = int64(v.(int))
}

if v, ok := condition["days_since_noncurrent_time"]; ok {
transformed.DaysSinceNoncurrentTime = int64(v.(int))
}

if v, ok := condition["noncurrent_time_before"]; ok {
transformed.NoncurrentTimeBefore = v.(string)
}

return transformed, nil
}

Expand Down
22 changes: 21 additions & 1 deletion third_party/terraform/tests/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1282,13 +1282,22 @@ resource "google_storage_bucket" "bucket" {
age = 10
}
}
lifecycle_rule {
action {
type = "Delete"
}
condition {
custom_time_before = "2019-01-01"
}
}
lifecycle_rule {
action {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition {
created_before = "2019-01-01"
created_before = "2019-01-01"
days_since_custom_time = 3
}
}
lifecycle_rule {
Expand Down Expand Up @@ -1360,6 +1369,17 @@ resource "google_storage_bucket" "bucket" {
condition {
age = 10
with_state = "LIVE"
days_since_noncurrent_time = 5
}
}
lifecycle_rule {
action {
type = "Delete"
}

condition {
age = 2
noncurrent_time_before = "2019-01-01"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ The `condition` block supports the following elements, and requires at least one

* `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) Creation date of an object in RFC 3339 (e.g. `2017-06-13`) to satisfy this condition.

* `days_since_custom_time` - (Optional) Date in RFC 3339 (e.g. `2017-06-13`) when an object's Custom-Time metadata is earlier than the date specified in this condition.

* `days_since_noncurrent_time` - (Optional) Relevant only for versioned objects. Number of days elapsed since the noncurrent timestamp of an object.

* `noncurrent_time_before` - (Optional) Relevant only for versioned objects. The date in RFC 3339 (e.g. `2017-06-13`) when the object became nonconcurrent.

The `versioning` block supports:

* `enabled` - (Required) While set to `true`, versioning is fully enabled for this bucket.
Expand Down