Skip to content

Commit

Permalink
feat: add support for lifecycle rules in simple bucket (#49)
Browse files Browse the repository at this point in the history
* add support for lifecycle rules

* revert main.tf

* update readme

* update example

* dashes

* dash

* inline
  • Loading branch information
umairidris authored Apr 22, 2020
1 parent a0782d7 commit b39e2cd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/simple_bucket/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ module "bucket" {
name = "example-bucket"
project_id = "example-project"
location = "us-east1"

lifecycle_rules = [{
action = {
type = "Delete"
}
condition = {
age = 365
with_state = "ANY"
}
}]

iam_members = [{
role = "roles/storage.viewer"
member = "user:[email protected]"
Expand Down
1 change: 1 addition & 0 deletions modules/simple_bucket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Functional examples are included in the
| 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 |
| iam\_members | The list of IAM members to grant permissions on the bucket. | object | `<list>` | no |
| labels | A set of key/value label pairs to assign to the bucket. | map(string) | `"null"` | no |
| lifecycle\_rules | The bucket's Lifecycle Rules configuration. | object | `<list>` | no |
| location | The location of the bucket. | string | n/a | yes |
| name | The name of the bucket. | string | n/a | yes |
| project\_id | The ID of the project to create the bucket in. | string | n/a | yes |
Expand Down
17 changes: 17 additions & 0 deletions modules/simple_bucket/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ resource "google_storage_bucket" "bucket" {
default_kms_key_name = var.encryption.default_kms_key_name
}
}

dynamic "lifecycle_rule" {
for_each = var.lifecycle_rules
content {
action {
type = lifecycle_rule.value.action.type
storage_class = lookup(lifecycle_rule.value.action, "storage_class", null)
}
condition {
age = lookup(lifecycle_rule.value.condition, "age", null)
created_before = lookup(lifecycle_rule.value.condition, "storage_class", null)
with_state = lookup(lifecycle_rule.value.condition, "with_state", null)
matches_storage_class = lookup(lifecycle_rule.value.condition, "matches_storage_class", null)
num_newer_versions = lookup(lifecycle_rule.value.condition, "num_newer_versions", null)
}
}
}
}

resource "google_storage_bucket_iam_member" "members" {
Expand Down
19 changes: 19 additions & 0 deletions modules/simple_bucket/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,22 @@ variable "encryption" {
})
default = null
}

variable "lifecycle_rules" {
description = "The bucket's Lifecycle Rules configuration."
type = list(object({
# Object with keys:
# - type - The type of the action of this Lifecycle Rule. Supported values: Delete and SetStorageClass.
# - storage_class - (Required if action type is SetStorageClass) The target Storage Class of objects affected by this Lifecycle Rule.
action = any

# Object with keys:
# - age - (Optional) Minimum age of an object in days to satisfy this condition.
# - created_before - (Optional) Creation date of an object in RFC 3339 (e.g. 2017-06-13) to satisfy this condition.
# - with_state - (Optional) Match to live and/or archived objects. Supported values include: "LIVE", "ARCHIVED", "ANY".
# - matches_storage_class - (Optional) Storage Class of objects to satisfy this condition. Supported values include: MULTI_REGIONAL, REGIONAL, NEARLINE, COLDLINE, STANDARD, DURABLE_REDUCED_AVAILABILITY.
# - num_newer_versions - (Optional) Relevant only for versioned objects. The number of newer versions of an object to satisfy this condition.
condition = any
}))
default = []
}

0 comments on commit b39e2cd

Please sign in to comment.