From f2e6ea3e1954c8311ff326d7c512756db1dd5ae9 Mon Sep 17 00:00:00 2001 From: Umair Idris Date: Fri, 1 May 2020 15:35:30 -0400 Subject: [PATCH] fix: convert lifecycle action object to map (#52) * convert object to map * fmt * doc --- examples/multiple_buckets/main.tf | 1 - examples/simple_bucket/README.md | 19 +++++++++++++++++++ examples/simple_bucket/main.tf | 4 ++-- examples/simple_bucket/variables.tf | 25 +++++++++++++++++++++++++ examples/simple_bucket/versions.tf | 19 +++++++++++++++++++ main.tf | 2 +- modules/simple_bucket/main.tf | 14 +++++++------- variables.tf | 15 +++++++++++---- 8 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 examples/simple_bucket/README.md create mode 100644 examples/simple_bucket/variables.tf create mode 100644 examples/simple_bucket/versions.tf diff --git a/examples/multiple_buckets/main.tf b/examples/multiple_buckets/main.tf index 60214d20..9c895b56 100644 --- a/examples/multiple_buckets/main.tf +++ b/examples/multiple_buckets/main.tf @@ -36,4 +36,3 @@ module "cloud_storage" { } }] } - diff --git a/examples/simple_bucket/README.md b/examples/simple_bucket/README.md new file mode 100644 index 00000000..ad95c2f4 --- /dev/null +++ b/examples/simple_bucket/README.md @@ -0,0 +1,19 @@ +# Simple Example + +This example illustrates how to use the `simple-bucket` submodule. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|:----:|:-----:|:-----:| +| name | Name of the buckets to create. | string | n/a | yes | +| project\_id | The ID of the project in which to provision resources. | string | n/a | yes | + + + +To provision this example, run the following from within this directory: +- `terraform init` to get the plugins +- `terraform plan` to see the infrastructure plan +- `terraform apply` to apply the infrastructure build +- `terraform destroy` to destroy the built infrastructure diff --git a/examples/simple_bucket/main.tf b/examples/simple_bucket/main.tf index 5285b8ae..b01cf569 100644 --- a/examples/simple_bucket/main.tf +++ b/examples/simple_bucket/main.tf @@ -17,8 +17,8 @@ module "bucket" { source = "../../modules/simple_bucket" - name = "example-bucket" - project_id = "example-project" + name = var.name + project_id = var.project_id location = "us-east1" lifecycle_rules = [{ diff --git a/examples/simple_bucket/variables.tf b/examples/simple_bucket/variables.tf new file mode 100644 index 00000000..54b91432 --- /dev/null +++ b/examples/simple_bucket/variables.tf @@ -0,0 +1,25 @@ +/** + * Copyright 2018 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +variable "project_id" { + description = "The ID of the project in which to provision resources." + type = string +} + +variable "name" { + description = "Name of the buckets to create." + type = string +} diff --git a/examples/simple_bucket/versions.tf b/examples/simple_bucket/versions.tf new file mode 100644 index 00000000..29704272 --- /dev/null +++ b/examples/simple_bucket/versions.tf @@ -0,0 +1,19 @@ +/** + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +terraform { + required_version = ">= 0.12" +} diff --git a/main.tf b/main.tf index 3d2b49a5..8ed907c7 100644 --- a/main.tf +++ b/main.tf @@ -64,7 +64,7 @@ resource "google_storage_bucket" "buckets" { content { action { type = lifecycle_rule.value.action.type - storage_class = lifecycle_rule.value.action.storage_class + storage_class = lookup(lifecycle_rule.value.action, "storage_class", null) } condition { age = lookup(lifecycle_rule.value.condition, "age", null) diff --git a/modules/simple_bucket/main.tf b/modules/simple_bucket/main.tf index c0811817..650801bf 100644 --- a/modules/simple_bucket/main.tf +++ b/modules/simple_bucket/main.tf @@ -46,15 +46,15 @@ resource "google_storage_bucket" "bucket" { for_each = var.lifecycle_rules content { action { - type = lifecycle_rule.value.action.type - storage_class = lookup(lifecycle_rule.value.action, "storage_class", null) + 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) + 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) } } } diff --git a/variables.tf b/variables.tf index 83851d12..3a0501d2 100644 --- a/variables.tf +++ b/variables.tf @@ -129,10 +129,17 @@ variable "set_viewer_roles" { variable "lifecycle_rules" { type = set(object({ - action = object({ - type = string - storage_class = string - }) + # 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 = map(string) + + # 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) Comma delimited string for 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 = map(string) })) description = "List of lifecycle rules to configure. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket.html#lifecycle_rule except condition.matches_storage_class should be a comma delimited string."