Skip to content

Commit 6505c5e

Browse files
Adding Eventarc-Workflows example for cgc docs (#6279) (#4529)
* Adding Eventarc-Workflows example for cgc docs * Modified eventarc-workflows to eventarc_workflows * Addressed the comments; Added a new file to be referenced from the eventarc_workflows.tf.erb file * Moved workflow.yaml to third_party/terraform/utils/test-fixtures/workflow.yaml * Added the hard-coded path * Updated eventarc_workflows.tf.erb Signed-off-by: Modular Magician <[email protected]>
1 parent 53d01b6 commit 6505c5e

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

.changelog/6279.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
```release-note:none
2+
```

google-beta/resource_cgc_snippet_generated_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,127 @@ resource "google_compute_instance" "instance_virtual_display" {
395395
`, context)
396396
}
397397

398+
func TestAccCGCSnippet_eventarcWorkflowsExample(t *testing.T) {
399+
t.Parallel()
400+
401+
context := map[string]interface{}{
402+
"random_suffix": randString(t, 10),
403+
}
404+
405+
vcrTest(t, resource.TestCase{
406+
PreCheck: func() { testAccPreCheck(t) },
407+
Providers: testAccProvidersOiCS,
408+
Steps: []resource.TestStep{
409+
{
410+
Config: testAccCGCSnippet_eventarcWorkflowsExample(context),
411+
},
412+
{
413+
ResourceName: "google_eventarc_trigger.trigger_pubsub_tf",
414+
ImportState: true,
415+
ImportStateVerify: true,
416+
},
417+
},
418+
})
419+
}
420+
421+
func testAccCGCSnippet_eventarcWorkflowsExample(context map[string]interface{}) string {
422+
return Nprintf(`
423+
# Used to retrieve project_number later
424+
data "google_project" "project" {
425+
provider = google-beta
426+
}
427+
428+
# Enable Eventarc API
429+
resource "google_project_service" "eventarc" {
430+
provider = google-beta
431+
service = "eventarc.googleapis.com"
432+
disable_on_destroy = false
433+
}
434+
435+
# Enable Pub/Sub API
436+
resource "google_project_service" "pubsub" {
437+
provider = google-beta
438+
service = "pubsub.googleapis.com"
439+
disable_on_destroy = false
440+
}
441+
442+
# Enable Workflows API
443+
resource "google_project_service" "workflows" {
444+
provider = google-beta
445+
service = "workflows.googleapis.com"
446+
disable_on_destroy = false
447+
}
448+
449+
450+
451+
# Create a service account for Eventarc trigger and Workflows
452+
resource "google_service_account" "eventarc_workflows_service_account" {
453+
provider = google-beta
454+
account_id = "eventarc-workflows-sa"
455+
display_name = "Eventarc Workflows Service Account"
456+
}
457+
458+
# Grant the logWriter role to the service account
459+
resource "google_project_iam_binding" "project_binding_eventarc" {
460+
provider = google-beta
461+
project = data.google_project.project.id
462+
role = "roles/logging.logWriter"
463+
464+
members = ["serviceAccount:${google_service_account.eventarc_workflows_service_account.email}"]
465+
466+
depends_on = [google_service_account.eventarc_workflows_service_account]
467+
}
468+
469+
# Grant the workflows.invoker role to the service account
470+
resource "google_project_iam_binding" "project_binding_workflows" {
471+
provider = google-beta
472+
project = data.google_project.project.id
473+
role = "roles/workflows.invoker"
474+
475+
members = ["serviceAccount:${google_service_account.eventarc_workflows_service_account.email}"]
476+
477+
depends_on = [google_service_account.eventarc_workflows_service_account]
478+
}
479+
480+
481+
# Define and deploy a workflow
482+
resource "google_workflows_workflow" "workflows_example" {
483+
name = "tf-test-pubsub-workflow-tf%{random_suffix}"
484+
provider = google-beta
485+
region = "us-central1"
486+
description = "A sample workflow"
487+
service_account = google_service_account.eventarc_workflows_service_account.id
488+
# Imported main workflow YAML file
489+
source_contents = templatefile("test-fixtures/workflow.yaml",{})
490+
491+
depends_on = [google_project_service.workflows,
492+
google_service_account.eventarc_workflows_service_account]
493+
}
494+
495+
496+
# Create an Eventarc trigger routing Pub/Sub events to Workflows
497+
resource "google_eventarc_trigger" "trigger_pubsub_tf" {
498+
name = "tf-test-trigger-pubsub-workflow-tf%{random_suffix}"
499+
provider = google-beta
500+
location = "us-central1"
501+
matching_criteria {
502+
attribute = "type"
503+
value = "google.cloud.pubsub.topic.v1.messagePublished"
504+
}
505+
destination {
506+
workflow = google_workflows_workflow.workflows_example.id
507+
}
508+
509+
510+
service_account = google_service_account.eventarc_workflows_service_account.id
511+
512+
depends_on = [google_project_service.pubsub, google_project_service.eventarc,
513+
google_service_account.eventarc_workflows_service_account]
514+
}
515+
516+
`, context)
517+
}
518+
398519
func TestAccCGCSnippet_sqlDatabaseInstanceSqlserverExample(t *testing.T) {
399520
skipIfVcr(t)
400521
t.Parallel()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This is a sample workflow that simply logs the incoming Pub/Sub event
2+
# Note that $$ is needed for Terraform
3+
4+
main:
5+
params: [event]
6+
steps:
7+
- log_event:
8+
call: sys.log
9+
args:
10+
text: $${event}
11+
severity: INFO
12+
- decode_pubsub_message:
13+
assign:
14+
- base64: $${base64.decode(event.data.data)}
15+
- message: $${text.decode(base64)}
16+
- return_pubsub_message:
17+
return: $${message}

0 commit comments

Comments
 (0)