Skip to content

Commit

Permalink
feat: Allow creating subscriptions without creating topic (#72)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
onimsha committed Aug 19, 2021
1 parent 30bab0c commit 0c25bf2
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
1 change: 1 addition & 0 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module "pubsub" {
topic = var.topic_name
topic_labels = var.topic_labels


pull_subscriptions = [
{
name = "pull"
Expand Down
36 changes: 36 additions & 0 deletions examples/subscriptions_only/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Simple Example - Creates Subscriptions Only

This example illustrates how to use the `pubsub` module to create only subscriptions.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## 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 |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## 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
52 changes: 52 additions & 0 deletions examples/subscriptions_only/main.tf
Original file line number Diff line number Diff line change
@@ -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
},
]

}
30 changes: 30 additions & 0 deletions examples/subscriptions_only/outputs.tf
Original file line number Diff line number Diff line change
@@ -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"
}
25 changes: 25 additions & 0 deletions examples/subscriptions_only/variables.tf
Original file line number Diff line number Diff line change
@@ -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"
}
16 changes: 8 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0c25bf2

Please sign in to comment.