Skip to content

Commit

Permalink
add more options (#4221) (#7937)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 4, 2020
1 parent 3fd0300 commit f72176d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/4221.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
storage: added more lifecycle conditions to `google_storage_bucket` resource
```
49 changes: 45 additions & 4 deletions google/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 @@ -1029,10 +1050,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 @@ -1246,6 +1271,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 google/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1290,13 +1290,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 @@ -1368,6 +1377,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
8 changes: 8 additions & 0 deletions website/docs/r/storage_bucket.html.markdown
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

0 comments on commit f72176d

Please sign in to comment.