Skip to content

Commit af4aa3a

Browse files
committed
Add cloud-run push subscription
1 parent ff63099 commit af4aa3a

File tree

10 files changed

+256
-2
lines changed

10 files changed

+256
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Simple Example
2+
3+
This example illustrates how to use the `pub` and `sub` module.
4+
5+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6+
## Inputs
7+
8+
| Name | Description | Type | Default | Required |
9+
|------|-------------|------|---------|:--------:|
10+
| project\_id | The project ID to manage the Pub/Sub resources | `string` | n/a | yes |
11+
12+
## Outputs
13+
14+
| Name | Description |
15+
|------|-------------|
16+
| project\_id | The project ID |
17+
| topic\_labels | The labels of the Pub/Sub topic created |
18+
| topic\_name | The name of the Pub/Sub topic created |
19+
20+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
21+
22+
## Requirements
23+
24+
The following sections describe the requirements which must be met in
25+
order to invoke this example. The requirements of the
26+
[root module][root-module-requirements] must be met.
27+
28+
## Usage
29+
30+
To provision this example, populate `terraform.tfvars` with the [required variables](#inputs) and run the following commands within
31+
this directory:
32+
- `terraform init` to get the plugins
33+
- `terraform plan` to see the infrastructure plan
34+
- `terraform apply` to apply the infrastructure build
35+
- `terraform destroy` to destroy the built infrastructure
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module "pub" {
18+
source = "terraform-google-modules/pubsub/google//modules/pub"
19+
version = "~> 7.0"
20+
21+
project_id = var.project_id
22+
topic = "cft-tf-pub-topic-cr-push"
23+
topic_labels = {
24+
foo_label = "foo_value"
25+
bar_label = "bar_value"
26+
}
27+
}
28+
29+
module "sub" {
30+
source = "terraform-google-modules/pubsub/google//modules/sub"
31+
version = "~> 7.0"
32+
33+
project_id = var.project_id
34+
topic = module.pub.topic
35+
36+
push_subscriptions = [
37+
{
38+
name = module.cloud-run.service_name
39+
push_endpoint = module.cloud-run.service_uri
40+
oidc_service_account_email = module.cloud-run.service_account_id.email
41+
},
42+
]
43+
}
44+
45+
module "cloud-run" {
46+
source = "GoogleCloudPlatform/cloud-run/google//modules/v2"
47+
version = "~> 0.17"
48+
project_id = var.project_id
49+
location = "us-central1"
50+
service_name = "cr-service"
51+
containers = [{ "container_name" = "", "container_image" = "gcr.io/design-center-container-repo/pubsub-cr-push:latest-1703" }]
52+
service_account_project_roles = ["roles/run.invoker"]
53+
members = ["allUsers"]
54+
cloud_run_deletion_protection = false
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "project_id" {
18+
value = var.project_id
19+
description = "The project ID"
20+
}
21+
22+
output "topic_name" {
23+
value = module.pub.topic
24+
description = "The name of the Pub/Sub topic created"
25+
}
26+
27+
output "topic_labels" {
28+
value = module.pub.topic_labels
29+
description = "The labels of the Pub/Sub topic created"
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "project_id" {
18+
type = string
19+
description = "The project ID to manage the Pub/Sub resources"
20+
}

metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ spec:
4646
location: examples/cloud_storage-separate-pub-sub
4747
- name: kms
4848
location: examples/kms
49+
- name: push_subscription-separate-pub-sub
50+
location: examples/push_subscription-separate-pub-sub
4951
- name: simple
5052
location: examples/simple
5153
- name: simple-separate-pub-sub
@@ -243,12 +245,18 @@ spec:
243245
- roles/resourcemanager.projectIamAdmin
244246
- roles/bigquery.admin
245247
- roles/storage.admin
248+
- roles/run.admin
249+
- roles/iam.serviceAccountAdmin
250+
- roles/iam.serviceAccountUser
251+
- roles/resourcemanager.projectIamAdmin
246252
services:
247253
- cloudresourcemanager.googleapis.com
248254
- pubsub.googleapis.com
249255
- serviceusage.googleapis.com
250256
- bigquery.googleapis.com
251257
- storage.googleapis.com
258+
- run.googleapis.com
259+
- iam.googleapis.com
252260
providerVersions:
253261
- source: hashicorp/google
254262
version: ">= 6.2, < 7"

modules/pub/metadata.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ spec:
4242
location: examples/cloud_storage-separate-pub-sub
4343
- name: kms
4444
location: examples/kms
45+
- name: push_subscription-separate-pub-sub
46+
location: examples/push_subscription-separate-pub-sub
4547
- name: simple
4648
location: examples/simple
4749
- name: simple-separate-pub-sub
@@ -102,12 +104,18 @@ spec:
102104
- roles/resourcemanager.projectIamAdmin
103105
- roles/bigquery.admin
104106
- roles/storage.admin
107+
- roles/run.admin
108+
- roles/iam.serviceAccountAdmin
109+
- roles/iam.serviceAccountUser
110+
- roles/resourcemanager.projectIamAdmin
105111
services:
106112
- cloudresourcemanager.googleapis.com
107113
- pubsub.googleapis.com
108114
- serviceusage.googleapis.com
109115
- bigquery.googleapis.com
110116
- storage.googleapis.com
117+
- run.googleapis.com
118+
- iam.googleapis.com
111119
providerVersions:
112120
- source: hashicorp/google
113121
version: ">= 6.2, < 7"

modules/sub/metadata.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ spec:
4242
location: examples/cloud_storage-separate-pub-sub
4343
- name: kms
4444
location: examples/kms
45+
- name: push_subscription-separate-pub-sub
46+
location: examples/push_subscription-separate-pub-sub
4547
- name: simple
4648
location: examples/simple
4749
- name: simple-separate-pub-sub
@@ -58,6 +60,12 @@ spec:
5860
description: The Pub/Sub topic name.
5961
varType: string
6062
required: true
63+
connections:
64+
- source:
65+
source: github.com/terraform-google-modules/terraform-google-pubsub//modules/pub
66+
version: ">= 7.0.0"
67+
spec:
68+
outputExpr: topic
6169
- name: create_subscriptions
6270
description: Specify true if you want to create subscriptions.
6371
varType: bool
@@ -83,6 +91,12 @@ spec:
8391
enable_message_ordering = optional(bool),
8492
}))
8593
defaultValue: []
94+
connections:
95+
- source:
96+
source: github.com/GoogleCloudPlatform/terraform-google-cloud-run//modules/v2
97+
version: ">= 0.13"
98+
spec:
99+
outputExpr: "{ \"name\": service_name, \"push_endpoint\": service_uri, \"oidc_service_account_email\": service_account_id.email }"
86100
- name: pull_subscriptions
87101
description: The list of the pull subscriptions.
88102
varType: |-
@@ -123,6 +137,12 @@ spec:
123137
minimum_backoff = optional(string)
124138
}))
125139
defaultValue: []
140+
connections:
141+
- source:
142+
source: github.com/terraform-google-modules/terraform-google-bigquery
143+
version: ">= 10.0.0"
144+
spec:
145+
outputExpr: "{ \"name\": external_table_ids[0], \"table\": external_table_ids[0]}"
126146
- name: cloud_storage_subscriptions
127147
description: The list of the Cloud Storage push subscriptions.
128148
varType: |-
@@ -149,6 +169,12 @@ spec:
149169
minimum_backoff = optional(string)
150170
}))
151171
defaultValue: []
172+
connections:
173+
- source:
174+
source: github.com/terraform-google-modules/terraform-google-cloud-storage//modules/simple_bucket
175+
version: ">= 9.0.1"
176+
spec:
177+
outputExpr: "{ \"name\": name, \"bucket\": name}"
152178
- name: subscription_labels
153179
description: A map of labels to assign to every Pub/Sub subscription.
154180
varType: map(string)
@@ -174,12 +200,18 @@ spec:
174200
- roles/resourcemanager.projectIamAdmin
175201
- roles/bigquery.admin
176202
- roles/storage.admin
203+
- roles/run.admin
204+
- roles/iam.serviceAccountAdmin
205+
- roles/iam.serviceAccountUser
206+
- roles/resourcemanager.projectIamAdmin
177207
services:
178208
- cloudresourcemanager.googleapis.com
179209
- pubsub.googleapis.com
180210
- serviceusage.googleapis.com
181211
- bigquery.googleapis.com
182212
- storage.googleapis.com
213+
- run.googleapis.com
214+
- iam.googleapis.com
183215
providerVersions:
184216
- source: hashicorp/google
185217
version: ">= 6.2, < 7"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package push_subscription_separate_pub_sub
16+
17+
import (
18+
"fmt"
19+
"testing"
20+
"time"
21+
22+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
23+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestPushSubscriptionSeparatePubSub(t *testing.T) {
28+
bpt := tft.NewTFBlueprintTest(t)
29+
30+
bpt.DefineVerify(func(assert *assert.Assertions) {
31+
bpt.DefaultVerify(assert)
32+
33+
projectId := bpt.GetStringOutput("project_id")
34+
35+
op := gcloud.Runf(t, "pubsub topics describe cft-tf-pub-topic --project=%s", projectId)
36+
assert.Equal(fmt.Sprintf("projects/%s/topics/cft-tf-pub-topic", projectId), op.Get("name").String(), "has expected name")
37+
assert.Equal("bar_value", op.Get("labels.bar_label").String(), "has expected labels")
38+
assert.Equal("foo_value", op.Get("labels.foo_label").String(), "has expected labels")
39+
40+
op = gcloud.Runf(t, "pubsub subscriptions describe cr-service --project=%s", projectId)
41+
assert.Equal(fmt.Sprintf("projects/%s/subscriptions/cr-service", projectId), op.Get("name").String(), "has expected name")
42+
assert.Equal(fmt.Sprintf("projects/%s/topics/cft-tf-pub-topic", projectId), op.Get("topic").String(), "has expected topic")
43+
// Publish a test message
44+
message := "Hello Runner!"
45+
publishCmd := fmt.Sprintf("pubsub topics publish cft-tf-pub-topic --message='%s' --project=%s", message, projectId)
46+
gcloud.Runf(t, publishCmd)
47+
48+
// Wait for a short time to allow the message to be processed. Adjust if necessary.
49+
time.Sleep(30 * time.Second)
50+
51+
// Check Cloud Run logs for the message
52+
logCmd := fmt.Sprintf("logging read \"resource.type=cloud_run_revision AND resource.labels.service_name=run-service AND textPayload:\\\"%s\\\"\" --project=%s --limit=1", message, projectId)
53+
logOp := gcloud.Runf(t, logCmd)
54+
55+
// Assert that the log entry is found
56+
assert.Contains(logOp.String(), message, "Cloud Run logs should contain the published message")
57+
})
58+
59+
bpt.Test()
60+
}

test/setup/iam.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ locals {
1919
"roles/pubsub.admin",
2020
"roles/resourcemanager.projectIamAdmin",
2121
"roles/bigquery.admin",
22-
"roles/storage.admin"
22+
"roles/storage.admin",
23+
"roles/run.admin",
24+
"roles/iam.serviceAccountAdmin",
25+
"roles/iam.serviceAccountUser",
26+
"roles/resourcemanager.projectIamAdmin"
2327
]
2428
}
2529

test/setup/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ module "project-ci-int-pubsub" {
2929
"pubsub.googleapis.com",
3030
"serviceusage.googleapis.com",
3131
"bigquery.googleapis.com",
32-
"storage.googleapis.com"
32+
"storage.googleapis.com",
33+
"run.googleapis.com",
34+
"iam.googleapis.com"
3335
]
3436
}

0 commit comments

Comments
 (0)