Skip to content

Commit b39e2cd

Browse files
authored
feat: add support for lifecycle rules in simple bucket (#49)
* add support for lifecycle rules * revert main.tf * update readme * update example * dashes * dash * inline
1 parent a0782d7 commit b39e2cd

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

examples/simple_bucket/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ module "bucket" {
2020
name = "example-bucket"
2121
project_id = "example-project"
2222
location = "us-east1"
23+
24+
lifecycle_rules = [{
25+
action = {
26+
type = "Delete"
27+
}
28+
condition = {
29+
age = 365
30+
with_state = "ANY"
31+
}
32+
}]
33+
2334
iam_members = [{
2435
role = "roles/storage.viewer"
2536
member = "user:[email protected]"

modules/simple_bucket/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Functional examples are included in the
4343
| force\_destroy | When deleting a bucket, this boolean option will delete all contained objects. If false, Terraform will fail to delete buckets which contain objects. | bool | `"false"` | no |
4444
| iam\_members | The list of IAM members to grant permissions on the bucket. | object | `<list>` | no |
4545
| labels | A set of key/value label pairs to assign to the bucket. | map(string) | `"null"` | no |
46+
| lifecycle\_rules | The bucket's Lifecycle Rules configuration. | object | `<list>` | no |
4647
| location | The location of the bucket. | string | n/a | yes |
4748
| name | The name of the bucket. | string | n/a | yes |
4849
| project\_id | The ID of the project to create the bucket in. | string | n/a | yes |

modules/simple_bucket/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ resource "google_storage_bucket" "bucket" {
4141
default_kms_key_name = var.encryption.default_kms_key_name
4242
}
4343
}
44+
45+
dynamic "lifecycle_rule" {
46+
for_each = var.lifecycle_rules
47+
content {
48+
action {
49+
type = lifecycle_rule.value.action.type
50+
storage_class = lookup(lifecycle_rule.value.action, "storage_class", null)
51+
}
52+
condition {
53+
age = lookup(lifecycle_rule.value.condition, "age", null)
54+
created_before = lookup(lifecycle_rule.value.condition, "storage_class", null)
55+
with_state = lookup(lifecycle_rule.value.condition, "with_state", null)
56+
matches_storage_class = lookup(lifecycle_rule.value.condition, "matches_storage_class", null)
57+
num_newer_versions = lookup(lifecycle_rule.value.condition, "num_newer_versions", null)
58+
}
59+
}
60+
}
4461
}
4562

4663
resource "google_storage_bucket_iam_member" "members" {

modules/simple_bucket/variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,22 @@ variable "encryption" {
8585
})
8686
default = null
8787
}
88+
89+
variable "lifecycle_rules" {
90+
description = "The bucket's Lifecycle Rules configuration."
91+
type = list(object({
92+
# Object with keys:
93+
# - type - The type of the action of this Lifecycle Rule. Supported values: Delete and SetStorageClass.
94+
# - storage_class - (Required if action type is SetStorageClass) The target Storage Class of objects affected by this Lifecycle Rule.
95+
action = any
96+
97+
# Object with keys:
98+
# - age - (Optional) Minimum age of an object in days to satisfy this condition.
99+
# - created_before - (Optional) Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.
100+
# - with_state - (Optional) Match to live and/or archived objects. Supported values include: "LIVE", "ARCHIVED", "ANY".
101+
# - matches_storage_class - (Optional) Storage Class of objects to satisfy this condition. Supported values include: MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, STANDARD, DURABLE_REDUCED_AVAILABILITY.
102+
# - num_newer_versions - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.
103+
condition = any
104+
}))
105+
default = []
106+
}

0 commit comments

Comments
 (0)