From 0c25bf2f4f736a5008d680a04cc43409993e858b Mon Sep 17 00:00:00 2001 From: Alex Co Date: Thu, 19 Aug 2021 22:11:18 +0700 Subject: [PATCH] feat: Allow creating subscriptions without creating topic (#72) BREAKING CHANGE: The `create_subscriptions` variable is now used to control whether subscriptions should be created. Set `create_subscriptions = false` *and* `create_topic = false` if you don't want the module to do anything. --- README.md | 1 + examples/simple/main.tf | 1 + examples/subscriptions_only/README.md | 36 ++++++++++++++++ examples/subscriptions_only/main.tf | 52 ++++++++++++++++++++++++ examples/subscriptions_only/outputs.tf | 30 ++++++++++++++ examples/subscriptions_only/variables.tf | 25 ++++++++++++ main.tf | 16 ++++---- variables.tf | 5 +++ 8 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 examples/subscriptions_only/README.md create mode 100644 examples/subscriptions_only/main.tf create mode 100644 examples/subscriptions_only/outputs.tf create mode 100644 examples/subscriptions_only/variables.tf diff --git a/README.md b/README.md index 11b0e3d..252d03b 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ module "pubsub" { | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| +| 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 | | message\_storage\_policy | A map of storage policies. Default - inherit from organization's Resource Location Restriction policy. | `map(any)` | `{}` | no | diff --git a/examples/simple/main.tf b/examples/simple/main.tf index aa81d56..4b9c18f 100644 --- a/examples/simple/main.tf +++ b/examples/simple/main.tf @@ -25,6 +25,7 @@ module "pubsub" { topic = var.topic_name topic_labels = var.topic_labels + pull_subscriptions = [ { name = "pull" diff --git a/examples/subscriptions_only/README.md b/examples/subscriptions_only/README.md new file mode 100644 index 0000000..14991db --- /dev/null +++ b/examples/subscriptions_only/README.md @@ -0,0 +1,36 @@ +# Simple Example - Creates Subscriptions Only + +This example illustrates how to use the `pubsub` module to create only subscriptions. + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes | +| topic\_project | The project ID of the topic | `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/subscriptions_only/main.tf b/examples/subscriptions_only/main.tf new file mode 100644 index 0000000..0a210fb --- /dev/null +++ b/examples/subscriptions_only/main.tf @@ -0,0 +1,52 @@ +/** + * 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" { + version = "~> 3.51" + region = "us-central1" +} + +resource "google_pubsub_topic" "example" { + name = "terraform-test-topic" + project = var.topic_project + +} +module "pubsub" { + source = "../../" + project_id = var.project_id + create_topic = false + create_subscriptions = true + topic = google_pubsub_topic.example.id + + + pull_subscriptions = [ + { + name = "pull" + ack_deadline_seconds = 10 + }, + ] + + push_subscriptions = [ + { + name = "push" + push_endpoint = "https://${var.project_id}.appspot.com/" + x-goog-version = "v1beta1" + ack_deadline_seconds = 20 + expiration_policy = "1209600s" // two weeks + }, + ] + +} diff --git a/examples/subscriptions_only/outputs.tf b/examples/subscriptions_only/outputs.tf new file mode 100644 index 0000000..2cd492c --- /dev/null +++ b/examples/subscriptions_only/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/subscriptions_only/variables.tf b/examples/subscriptions_only/variables.tf new file mode 100644 index 0000000..97d28ba --- /dev/null +++ b/examples/subscriptions_only/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" { + type = string + description = "The project ID to manage the Pub/Sub resources" +} + +variable "topic_project" { + type = string + description = "The project ID of the topic" +} diff --git a/main.tf b/main.tf index a04a3b5..dfcca2c 100644 --- a/main.tf +++ b/main.tf @@ -58,7 +58,7 @@ resource "google_pubsub_topic_iam_member" "pull_topic_binding" { } resource "google_pubsub_subscription_iam_member" "pull_subscription_binding" { - for_each = var.create_topic ? { for i in var.pull_subscriptions : i.name => i } : {} + for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i } : {} project = var.project_id subscription = each.value.name @@ -70,7 +70,7 @@ resource "google_pubsub_subscription_iam_member" "pull_subscription_binding" { } resource "google_pubsub_subscription_iam_member" "push_subscription_binding" { - for_each = var.create_topic ? { for i in var.push_subscriptions : i.name => i } : {} + for_each = var.create_subscriptions ? { for i in var.push_subscriptions : i.name => i } : {} project = var.project_id subscription = each.value.name @@ -97,10 +97,10 @@ resource "google_pubsub_topic" "topic" { } resource "google_pubsub_subscription" "push_subscriptions" { - for_each = var.create_topic ? { for i in var.push_subscriptions : i.name => i } : {} + for_each = var.create_subscriptions ? { for i in var.push_subscriptions : i.name => i } : {} name = each.value.name - topic = google_pubsub_topic.topic.0.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( @@ -175,10 +175,10 @@ resource "google_pubsub_subscription" "push_subscriptions" { } resource "google_pubsub_subscription" "pull_subscriptions" { - for_each = var.create_topic ? { for i in var.pull_subscriptions : i.name => i } : {} + for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i } : {} name = each.value.name - topic = google_pubsub_topic.topic.0.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( @@ -236,7 +236,7 @@ resource "google_pubsub_subscription" "pull_subscriptions" { } resource "google_pubsub_subscription_iam_member" "pull_subscription_sa_binding_subscriber" { - for_each = var.create_topic ? { for i in var.pull_subscriptions : i.name => i if lookup(i, "service_account", null) != null } : {} + for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i if lookup(i, "service_account", null) != null } : {} project = var.project_id subscription = each.value.name @@ -248,7 +248,7 @@ resource "google_pubsub_subscription_iam_member" "pull_subscription_sa_binding_s } resource "google_pubsub_subscription_iam_member" "pull_subscription_sa_binding_viewer" { - for_each = var.create_topic ? { for i in var.pull_subscriptions : i.name => i if lookup(i, "service_account", null) != null } : {} + for_each = var.create_subscriptions ? { for i in var.pull_subscriptions : i.name => i if lookup(i, "service_account", null) != null } : {} project = var.project_id subscription = each.value.name diff --git a/variables.tf b/variables.tf index 2fddccc..db8e933 100644 --- a/variables.tf +++ b/variables.tf @@ -30,6 +30,11 @@ variable "create_topic" { default = true } +variable "create_subscriptions" { + type = bool + description = "Specify true if you want to create subscriptions" + default = true +} variable "topic_labels" { type = map(string) description = "A map of labels to assign to the Pub/Sub topic"