From 8fdeccfc944c7108c6e3d6503acd886101b1a8a1 Mon Sep 17 00:00:00 2001 From: Lauren Huang Date: Wed, 16 Aug 2023 17:21:58 -0400 Subject: [PATCH 1/7] feat: add cloud storage subscription support --- README.md | 15 +++++- examples/bigquery/test.yaml | 20 +++++++ examples/cloud_storage/README.md | 35 ++++++++++++ examples/cloud_storage/main.tf | 43 +++++++++++++++ examples/cloud_storage/outputs.tf | 30 +++++++++++ examples/cloud_storage/test.yaml | 20 +++++++ examples/cloud_storage/variables.tf | 20 +++++++ examples/cloud_storage/versions.tf | 25 +++++++++ main.tf | 83 +++++++++++++++++++++++++++++ outputs.tf | 2 + test/setup/iam.tf | 3 +- test/setup/main.tf | 1 + test/setup/versions.tf | 4 +- variables.tf | 8 ++- versions.tf | 2 +- 15 files changed, 305 insertions(+), 6 deletions(-) create mode 100644 examples/bigquery/test.yaml create mode 100644 examples/cloud_storage/README.md create mode 100644 examples/cloud_storage/main.tf create mode 100644 examples/cloud_storage/outputs.tf create mode 100644 examples/cloud_storage/test.yaml create mode 100644 examples/cloud_storage/variables.tf create mode 100644 examples/cloud_storage/versions.tf diff --git a/README.md b/README.md index d99bfa2..58c6477 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,18 @@ module "pubsub" { drop_unknown_fields = false // optional } ] + cloud_storage_subscriptions = [ + { + name = "cloud-storage" // required + bucket = "example-bucket" // required + filename_prefix = "log_events_" // optional + filename_suffix = ".avro" // optional + max_duration = "60s" // optional + max_bytes = "10000000" // optional + output_format = "avro" // optional + write_metadata = false // optional + } + ] } ``` @@ -67,7 +79,8 @@ module "pubsub" { | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| bigquery\_subscriptions | The list of the bigquery push subscriptions. | `list(map(string))` | `[]` | no | +| bigquery\_subscriptions | The list of the Bigquery push subscriptions. | `list(map(string))` | `[]` | no | +| cloud\_storage\_subscriptions | The list of the Cloud Storage push subscriptions. | `list(map(string))` | `[]` | no | | create\_subscriptions | Specify true if you want to create subscriptions. | `bool` | `true` | no | | create\_topic | Specify true if you want to create a topic. | `bool` | `true` | no | | grant\_token\_creator | Specify true if you want to add token creator role to the default Pub/Sub SA. | `bool` | `true` | no | diff --git a/examples/bigquery/test.yaml b/examples/bigquery/test.yaml new file mode 100644 index 0000000..557a25c --- /dev/null +++ b/examples/bigquery/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2021 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. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: bigquery +spec: + skip: true diff --git a/examples/cloud_storage/README.md b/examples/cloud_storage/README.md new file mode 100644 index 0000000..fcb8be6 --- /dev/null +++ b/examples/cloud_storage/README.md @@ -0,0 +1,35 @@ +# Cloud Storage Example + +This example illustrates how to use the `pubsub` module. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| project\_id | The project ID | +| topic\_labels | The labels of the Pub/Sub topic created | +| topic\_name | The name of the Pub/Sub topic created | + + + +## Requirements + +The following sections describe the requirements which must be met in +order to invoke this example. The requirements of the +[root module][root-module-requirements] must be met. + +## Usage + +To provision this example, populate `terraform.tfvars` with the [required variables](#inputs) and run the following commands 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/cloud_storage/main.tf b/examples/cloud_storage/main.tf new file mode 100644 index 0000000..a4c9f58 --- /dev/null +++ b/examples/cloud_storage/main.tf @@ -0,0 +1,43 @@ +/** + * 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. + */ + +provider "google" { + region = "europe-west1" +} + +module "pubsub" { + source = "../../" + project_id = var.project_id + topic = "cft-tf-pubsub-topic-cloud-storage" + + cloud_storage_subscriptions = [ + { + name = "example_subscription" + bucket = "example_bucket" + }, + ] + + depends_on = [ + google_storage_bucket.test + ] + +} + +resource "google_storage_bucket" "test" { + project = var.project_id + name = "example_bucket" + location = "europe-west1" +} diff --git a/examples/cloud_storage/outputs.tf b/examples/cloud_storage/outputs.tf new file mode 100644 index 0000000..2cd492c --- /dev/null +++ b/examples/cloud_storage/outputs.tf @@ -0,0 +1,30 @@ +/** + * 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. + */ + +output "project_id" { + value = var.project_id + description = "The project ID" +} + +output "topic_name" { + value = module.pubsub.topic + description = "The name of the Pub/Sub topic created" +} + +output "topic_labels" { + value = module.pubsub.topic_labels + description = "The labels of the Pub/Sub topic created" +} diff --git a/examples/cloud_storage/test.yaml b/examples/cloud_storage/test.yaml new file mode 100644 index 0000000..f417d36 --- /dev/null +++ b/examples/cloud_storage/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2021 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. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: cloud-storage +spec: + skip: true diff --git a/examples/cloud_storage/variables.tf b/examples/cloud_storage/variables.tf new file mode 100644 index 0000000..5abd8d8 --- /dev/null +++ b/examples/cloud_storage/variables.tf @@ -0,0 +1,20 @@ +/** + * 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" { + type = string + description = "The project ID to manage the Pub/Sub resources" +} diff --git a/examples/cloud_storage/versions.tf b/examples/cloud_storage/versions.tf new file mode 100644 index 0000000..c4396b8 --- /dev/null +++ b/examples/cloud_storage/versions.tf @@ -0,0 +1,25 @@ +/** + * Copyright 2021 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_providers { + google = { + source = "hashicorp/google" + version = "~> 4.78" + } + } + required_version = ">= 0.13" +} diff --git a/main.tf b/main.tf index 3223355..2e80964 100644 --- a/main.tf +++ b/main.tf @@ -45,6 +45,13 @@ resource "google_project_iam_member" "bigquery_data_editor_binding" { member = "serviceAccount:${local.pubsub_svc_account_email}" } +resource "google_project_iam_member" "storage_admin_binding" { + count = length(var.cloud_storage_subscriptions) != 0 ? 1 : 0 + project = var.project_id + role = "roles/storage.admin" + member = "serviceAccount:${local.pubsub_svc_account_email}" +} + resource "google_project_iam_member" "token_creator_binding" { count = var.grant_token_creator ? 1 : 0 project = var.project_id @@ -337,6 +344,82 @@ resource "google_pubsub_subscription" "bigquery_subscriptions" { ] } +resource "google_pubsub_subscription" "cloud_storage_subscriptions" { + for_each = var.create_subscriptions ? { for i in var.cloud_storage_subscriptions : i.name => i } : {} + + name = each.value.name + topic = var.create_topic ? google_pubsub_topic.topic[0].name : var.topic + project = var.project_id + labels = var.subscription_labels + ack_deadline_seconds = lookup( + each.value, + "ack_deadline_seconds", + local.default_ack_deadline_seconds, + ) + message_retention_duration = lookup( + each.value, + "message_retention_duration", + null, + ) + retain_acked_messages = lookup( + each.value, + "retain_acked_messages", + null, + ) + filter = lookup( + each.value, + "filter", + null, + ) + enable_message_ordering = lookup( + each.value, + "enable_message_ordering", + null, + ) + dynamic "expiration_policy" { + // check if the 'expiration_policy' key exists, if yes, return a list containing it. + for_each = contains(keys(each.value), "expiration_policy") ? [each.value.expiration_policy] : [] + content { + ttl = expiration_policy.value + } + } + + dynamic "dead_letter_policy" { + for_each = (lookup(each.value, "dead_letter_topic", "") != "") ? [each.value.dead_letter_topic] : [] + content { + dead_letter_topic = lookup(each.value, "dead_letter_topic", "") + max_delivery_attempts = lookup(each.value, "max_delivery_attempts", "5") + } + } + + dynamic "retry_policy" { + for_each = (lookup(each.value, "maximum_backoff", "") != "") ? [each.value.maximum_backoff] : [] + content { + maximum_backoff = lookup(each.value, "maximum_backoff", "") + minimum_backoff = lookup(each.value, "minimum_backoff", "") + } + } + + cloud_storage_config { + bucket = each.value["bucket"] + filename_prefix = lookup(each.value, "filename_prefix", null) + filename_suffix = lookup(each.value, "filename_suffix", null) + max_duration = lookup(each.value, "max_duration", null) + max_bytes = lookup(each.value, "max_bytes", null) + dynamic "avro_config" { + for_each = (lookup(each.value, "output_format", "") == "avro") ? [true] : [] + content { + write_metadata = lookup(each.value, "write_metadata", null) + } + } + } + + depends_on = [ + google_pubsub_topic.topic, + google_project_iam_member.storage_admin_binding + ] +} + resource "google_pubsub_subscription_iam_member" "pull_subscription_sa_binding_subscriber" { for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i if lookup(i, "service_account", null) != null } : {} diff --git a/outputs.tf b/outputs.tf index 9ad7fa0..c10c907 100644 --- a/outputs.tf +++ b/outputs.tf @@ -39,6 +39,7 @@ output "subscription_names" { values({ for k, v in google_pubsub_subscription.push_subscriptions : k => v.name }), values({ for k, v in google_pubsub_subscription.pull_subscriptions : k => v.name }), values({ for k, v in google_pubsub_subscription.bigquery_subscriptions : k => v.name }), + values({ for k, v in google_pubsub_subscription.cloud_storage_subscriptions : k => v.name }), ) description = "The name list of Pub/Sub subscriptions" @@ -49,6 +50,7 @@ output "subscription_paths" { values({ for k, v in google_pubsub_subscription.push_subscriptions : k => v.id }), values({ for k, v in google_pubsub_subscription.pull_subscriptions : k => v.id }), values({ for k, v in google_pubsub_subscription.bigquery_subscriptions : k => v.name }), + values({ for k, v in google_pubsub_subscription.cloud_storage_subscriptions : k => v.name }), ) description = "The path list of Pub/Sub subscriptions" diff --git a/test/setup/iam.tf b/test/setup/iam.tf index a501766..5a41966 100644 --- a/test/setup/iam.tf +++ b/test/setup/iam.tf @@ -18,7 +18,8 @@ locals { int_required_roles = [ "roles/pubsub.admin", "roles/resourcemanager.projectIamAdmin", - "roles/bigquery.admin" + "roles/bigquery.admin", + "roles/storage.admin" ] } diff --git a/test/setup/main.tf b/test/setup/main.tf index b4f07ec..3bacc20 100644 --- a/test/setup/main.tf +++ b/test/setup/main.tf @@ -29,5 +29,6 @@ module "project-ci-int-pubsub" { "pubsub.googleapis.com", "serviceusage.googleapis.com", "bigquery.googleapis.com", + "storage.googleapis.com" ] } diff --git a/test/setup/versions.tf b/test/setup/versions.tf index 0177d7a..ff32990 100644 --- a/test/setup/versions.tf +++ b/test/setup/versions.tf @@ -19,11 +19,11 @@ terraform { required_providers { google = { source = "hashicorp/google" - version = "~> 4.23" + version = "~> 4.78" } google-beta = { source = "hashicorp/google-beta" - version = "~> 4.23" + version = "~> 4.78" } null = { source = "hashicorp/null" diff --git a/variables.tf b/variables.tf index 8b5e4de..c12f340 100644 --- a/variables.tf +++ b/variables.tf @@ -55,7 +55,13 @@ variable "pull_subscriptions" { variable "bigquery_subscriptions" { type = list(map(string)) - description = "The list of the bigquery push subscriptions." + description = "The list of the Bigquery push subscriptions." + default = [] +} + +variable "cloud_storage_subscriptions" { + type = list(map(string)) + description = "The list of the Cloud Storage push subscriptions." default = [] } diff --git a/versions.tf b/versions.tf index 3c87dd0..b2a18bf 100644 --- a/versions.tf +++ b/versions.tf @@ -20,7 +20,7 @@ terraform { google = { source = "hashicorp/google" - version = ">= 4.32, < 5.0" + version = ">= 4.78, < 5.0" } } From 4074731eee5643f1871253eab9eb76cda4ef4cb1 Mon Sep 17 00:00:00 2001 From: Lauren Huang Date: Mon, 23 Oct 2023 15:25:59 -0400 Subject: [PATCH 2/7] Add bucket_name variable. --- examples/cloud_storage/README.md | 3 ++- examples/cloud_storage/main.tf | 4 ++-- examples/cloud_storage/outputs.tf | 10 +++++----- examples/cloud_storage/variables.tf | 4 ++++ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/cloud_storage/README.md b/examples/cloud_storage/README.md index fcb8be6..9b45f25 100644 --- a/examples/cloud_storage/README.md +++ b/examples/cloud_storage/README.md @@ -8,13 +8,14 @@ This example illustrates how to use the `pubsub` module. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes | +| bucket\_name | The name of the GCS bucket name to which to write messages | `string` | n/a | yes | ## Outputs | Name | Description | |------|-------------| | project\_id | The project ID | -| topic\_labels | The labels of the Pub/Sub topic created | +| bucket\_name | The name of the Cloud Storage bucket created | | topic\_name | The name of the Pub/Sub topic created | diff --git a/examples/cloud_storage/main.tf b/examples/cloud_storage/main.tf index a4c9f58..f2f39cc 100644 --- a/examples/cloud_storage/main.tf +++ b/examples/cloud_storage/main.tf @@ -26,7 +26,7 @@ module "pubsub" { cloud_storage_subscriptions = [ { name = "example_subscription" - bucket = "example_bucket" + bucket = var.bucket_name }, ] @@ -38,6 +38,6 @@ module "pubsub" { resource "google_storage_bucket" "test" { project = var.project_id - name = "example_bucket" + name = var.bucket_name location = "europe-west1" } diff --git a/examples/cloud_storage/outputs.tf b/examples/cloud_storage/outputs.tf index 2cd492c..48c833d 100644 --- a/examples/cloud_storage/outputs.tf +++ b/examples/cloud_storage/outputs.tf @@ -19,12 +19,12 @@ output "project_id" { description = "The project ID" } +output "bucket_name" { + value = var.bucket_name + description = "The name of the Cloud Storage bucket created" +} + output "topic_name" { value = module.pubsub.topic description = "The name of the Pub/Sub topic created" } - -output "topic_labels" { - value = module.pubsub.topic_labels - description = "The labels of the Pub/Sub topic created" -} diff --git a/examples/cloud_storage/variables.tf b/examples/cloud_storage/variables.tf index 5abd8d8..a9c54de 100644 --- a/examples/cloud_storage/variables.tf +++ b/examples/cloud_storage/variables.tf @@ -18,3 +18,7 @@ variable "project_id" { type = string description = "The project ID to manage the Pub/Sub resources" } +variable "bucket_name" { + type = string + description = "The name of the GCS bucket name to which to write messages" +} From c3a6035a49f8bf15159403948545d08dc01e20f5 Mon Sep 17 00:00:00 2001 From: policy-librarians Date: Mon, 23 Oct 2023 20:11:19 +0000 Subject: [PATCH 3/7] changes --- examples/bigquery/test.yaml | 20 -------------------- examples/cloud_storage/README.md | 4 ++-- examples/cloud_storage/test.yaml | 20 -------------------- examples/cloud_storage/versions.tf | 25 ------------------------- test/setup/versions.tf | 4 ++-- 5 files changed, 4 insertions(+), 69 deletions(-) delete mode 100644 examples/bigquery/test.yaml delete mode 100644 examples/cloud_storage/test.yaml delete mode 100644 examples/cloud_storage/versions.tf diff --git a/examples/bigquery/test.yaml b/examples/bigquery/test.yaml deleted file mode 100644 index 557a25c..0000000 --- a/examples/bigquery/test.yaml +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2021 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. - -apiVersion: blueprints.cloud.google.com/v1alpha1 -kind: BlueprintTest -metadata: - name: bigquery -spec: - skip: true diff --git a/examples/cloud_storage/README.md b/examples/cloud_storage/README.md index 9b45f25..93b8998 100644 --- a/examples/cloud_storage/README.md +++ b/examples/cloud_storage/README.md @@ -7,15 +7,15 @@ This example illustrates how to use the `pubsub` module. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes | | bucket\_name | The name of the GCS bucket name to which to write messages | `string` | n/a | yes | +| project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes | ## Outputs | Name | Description | |------|-------------| -| project\_id | The project ID | | bucket\_name | The name of the Cloud Storage bucket created | +| project\_id | The project ID | | topic\_name | The name of the Pub/Sub topic created | diff --git a/examples/cloud_storage/test.yaml b/examples/cloud_storage/test.yaml deleted file mode 100644 index f417d36..0000000 --- a/examples/cloud_storage/test.yaml +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2021 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. - -apiVersion: blueprints.cloud.google.com/v1alpha1 -kind: BlueprintTest -metadata: - name: cloud-storage -spec: - skip: true diff --git a/examples/cloud_storage/versions.tf b/examples/cloud_storage/versions.tf deleted file mode 100644 index c4396b8..0000000 --- a/examples/cloud_storage/versions.tf +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright 2021 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_providers { - google = { - source = "hashicorp/google" - version = "~> 4.78" - } - } - required_version = ">= 0.13" -} diff --git a/test/setup/versions.tf b/test/setup/versions.tf index ff32990..f106432 100644 --- a/test/setup/versions.tf +++ b/test/setup/versions.tf @@ -19,11 +19,11 @@ terraform { required_providers { google = { source = "hashicorp/google" - version = "~> 4.78" + version = ">= 4.78" } google-beta = { source = "hashicorp/google-beta" - version = "~> 4.78" + version = ">= 4.78" } null = { source = "hashicorp/null" From 40eb34b097786245cdfac982b083e63510b04593 Mon Sep 17 00:00:00 2001 From: policy-librarians Date: Mon, 23 Oct 2023 20:43:41 +0000 Subject: [PATCH 4/7] more changes --- examples/cloud_storage/README.md | 3 ++- examples/cloud_storage/main.tf | 16 ++++++++-------- examples/cloud_storage/outputs.tf | 4 ++-- examples/cloud_storage/variables.tf | 8 ++++++++ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/examples/cloud_storage/README.md b/examples/cloud_storage/README.md index 93b8998..07ae999 100644 --- a/examples/cloud_storage/README.md +++ b/examples/cloud_storage/README.md @@ -7,8 +7,9 @@ This example illustrates how to use the `pubsub` module. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| bucket\_name | The name of the GCS bucket name to which to write messages | `string` | n/a | yes | +| bucket\_name | The name of the GCS bucket name to which to write messages | `string` | `"test_bucket"` | no | | project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes | +| random\_bucket\_suffix | Add a randomized 4-character suffix to bucket name | `bool` | `true` | no | ## Outputs diff --git a/examples/cloud_storage/main.tf b/examples/cloud_storage/main.tf index f2f39cc..c92729f 100644 --- a/examples/cloud_storage/main.tf +++ b/examples/cloud_storage/main.tf @@ -1,5 +1,5 @@ /** - * Copyright 2018 Google LLC + * Copyright 2018-2023 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,6 +14,11 @@ * limitations under the License. */ +resource "random_id" "bucket_suffix" { + count = var.random_bucket_suffix ? 1 : 0 + byte_length = 4 +} + provider "google" { region = "europe-west1" } @@ -26,18 +31,13 @@ module "pubsub" { cloud_storage_subscriptions = [ { name = "example_subscription" - bucket = var.bucket_name + bucket = google_storage_bucket.test.name }, ] - - depends_on = [ - google_storage_bucket.test - ] - } resource "google_storage_bucket" "test" { project = var.project_id - name = var.bucket_name + name = var.random_bucket_suffix ? join("-", [var.bucket_name, random_id.bucket_suffix[0].id]) : var.bucket_name location = "europe-west1" } diff --git a/examples/cloud_storage/outputs.tf b/examples/cloud_storage/outputs.tf index 48c833d..073d4cc 100644 --- a/examples/cloud_storage/outputs.tf +++ b/examples/cloud_storage/outputs.tf @@ -1,5 +1,5 @@ /** - * Copyright 2018 Google LLC + * Copyright 2018-2023 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ output "project_id" { } output "bucket_name" { - value = var.bucket_name + value = google_storage_bucket.test.name description = "The name of the Cloud Storage bucket created" } diff --git a/examples/cloud_storage/variables.tf b/examples/cloud_storage/variables.tf index a9c54de..d71e3aa 100644 --- a/examples/cloud_storage/variables.tf +++ b/examples/cloud_storage/variables.tf @@ -18,7 +18,15 @@ variable "project_id" { type = string description = "The project ID to manage the Pub/Sub resources" } + variable "bucket_name" { type = string description = "The name of the GCS bucket name to which to write messages" + default = "test_bucket" +} + +variable "random_bucket_suffix" { + type = bool + description = "Add a randomized 4-character suffix to bucket name" + default = true } From 7c7161d88495ba27b8a304feb749c1e5f9e8dc98 Mon Sep 17 00:00:00 2001 From: policy-librarians Date: Mon, 23 Oct 2023 21:08:22 +0000 Subject: [PATCH 5/7] bucket name --- examples/cloud_storage/README.md | 2 -- examples/cloud_storage/main.tf | 3 +-- examples/cloud_storage/variables.tf | 12 ------------ 3 files changed, 1 insertion(+), 16 deletions(-) diff --git a/examples/cloud_storage/README.md b/examples/cloud_storage/README.md index 07ae999..4a7d812 100644 --- a/examples/cloud_storage/README.md +++ b/examples/cloud_storage/README.md @@ -7,9 +7,7 @@ This example illustrates how to use the `pubsub` module. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| bucket\_name | The name of the GCS bucket name to which to write messages | `string` | `"test_bucket"` | no | | project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes | -| random\_bucket\_suffix | Add a randomized 4-character suffix to bucket name | `bool` | `true` | no | ## Outputs diff --git a/examples/cloud_storage/main.tf b/examples/cloud_storage/main.tf index c92729f..893df1e 100644 --- a/examples/cloud_storage/main.tf +++ b/examples/cloud_storage/main.tf @@ -15,7 +15,6 @@ */ resource "random_id" "bucket_suffix" { - count = var.random_bucket_suffix ? 1 : 0 byte_length = 4 } @@ -38,6 +37,6 @@ module "pubsub" { resource "google_storage_bucket" "test" { project = var.project_id - name = var.random_bucket_suffix ? join("-", [var.bucket_name, random_id.bucket_suffix[0].id]) : var.bucket_name + name = join("-", ["test_bucket", random_id.bucket_suffix.hex]) location = "europe-west1" } diff --git a/examples/cloud_storage/variables.tf b/examples/cloud_storage/variables.tf index d71e3aa..5abd8d8 100644 --- a/examples/cloud_storage/variables.tf +++ b/examples/cloud_storage/variables.tf @@ -18,15 +18,3 @@ variable "project_id" { type = string description = "The project ID to manage the Pub/Sub resources" } - -variable "bucket_name" { - type = string - description = "The name of the GCS bucket name to which to write messages" - default = "test_bucket" -} - -variable "random_bucket_suffix" { - type = bool - description = "Add a randomized 4-character suffix to bucket name" - default = true -} From e7a3018fe3fa3974b52a910194bb6b1e211725c2 Mon Sep 17 00:00:00 2001 From: policy-librarians Date: Mon, 23 Oct 2023 21:24:35 +0000 Subject: [PATCH 6/7] dedup name --- examples/cloud_storage/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/cloud_storage/main.tf b/examples/cloud_storage/main.tf index 893df1e..567d3ca 100644 --- a/examples/cloud_storage/main.tf +++ b/examples/cloud_storage/main.tf @@ -29,7 +29,7 @@ module "pubsub" { cloud_storage_subscriptions = [ { - name = "example_subscription" + name = "example_bucket_subscription" bucket = google_storage_bucket.test.name }, ] From 39ae4465b667ac705c486f82ddc767f35d377a59 Mon Sep 17 00:00:00 2001 From: policy-librarians Date: Mon, 23 Oct 2023 21:48:35 +0000 Subject: [PATCH 7/7] for CI --- examples/cloud_storage/main.tf | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/cloud_storage/main.tf b/examples/cloud_storage/main.tf index 567d3ca..4fec517 100644 --- a/examples/cloud_storage/main.tf +++ b/examples/cloud_storage/main.tf @@ -27,10 +27,16 @@ module "pubsub" { project_id = var.project_id topic = "cft-tf-pubsub-topic-cloud-storage" + topic_labels = { + foo_label = "foo_value" + } + cloud_storage_subscriptions = [ { name = "example_bucket_subscription" bucket = google_storage_bucket.test.name + + ack_deadline_seconds = 300 }, ] }