Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Eventarc-Workflows example for cgc docs #4529

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changelog/6279.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
```release-note:none
```
121 changes: 121 additions & 0 deletions google-beta/resource_cgc_snippet_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,127 @@ resource "google_compute_instance" "instance_virtual_display" {
`, context)
}

func TestAccCGCSnippet_eventarcWorkflowsExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
Steps: []resource.TestStep{
{
Config: testAccCGCSnippet_eventarcWorkflowsExample(context),
},
{
ResourceName: "google_eventarc_trigger.trigger_pubsub_tf",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCGCSnippet_eventarcWorkflowsExample(context map[string]interface{}) string {
return Nprintf(`
# Used to retrieve project_number later
data "google_project" "project" {
provider = google-beta
}

# Enable Eventarc API
resource "google_project_service" "eventarc" {
provider = google-beta
service = "eventarc.googleapis.com"
disable_on_destroy = false
}

# Enable Pub/Sub API
resource "google_project_service" "pubsub" {
provider = google-beta
service = "pubsub.googleapis.com"
disable_on_destroy = false
}

# Enable Workflows API
resource "google_project_service" "workflows" {
provider = google-beta
service = "workflows.googleapis.com"
disable_on_destroy = false
}



# Create a service account for Eventarc trigger and Workflows
resource "google_service_account" "eventarc_workflows_service_account" {
provider = google-beta
account_id = "eventarc-workflows-sa"
display_name = "Eventarc Workflows Service Account"
}

# Grant the logWriter role to the service account
resource "google_project_iam_binding" "project_binding_eventarc" {
provider = google-beta
project = data.google_project.project.id
role = "roles/logging.logWriter"

members = ["serviceAccount:${google_service_account.eventarc_workflows_service_account.email}"]

depends_on = [google_service_account.eventarc_workflows_service_account]
}

# Grant the workflows.invoker role to the service account
resource "google_project_iam_binding" "project_binding_workflows" {
provider = google-beta
project = data.google_project.project.id
role = "roles/workflows.invoker"

members = ["serviceAccount:${google_service_account.eventarc_workflows_service_account.email}"]

depends_on = [google_service_account.eventarc_workflows_service_account]
}


# Define and deploy a workflow
resource "google_workflows_workflow" "workflows_example" {
name = "tf-test-pubsub-workflow-tf%{random_suffix}"
provider = google-beta
region = "us-central1"
description = "A sample workflow"
service_account = google_service_account.eventarc_workflows_service_account.id
# Imported main workflow YAML file
source_contents = templatefile("test-fixtures/workflow.yaml",{})

depends_on = [google_project_service.workflows,
google_service_account.eventarc_workflows_service_account]
}


# Create an Eventarc trigger routing Pub/Sub events to Workflows
resource "google_eventarc_trigger" "trigger_pubsub_tf" {
name = "tf-test-trigger-pubsub-workflow-tf%{random_suffix}"
provider = google-beta
location = "us-central1"
matching_criteria {
attribute = "type"
value = "google.cloud.pubsub.topic.v1.messagePublished"
}
destination {
workflow = google_workflows_workflow.workflows_example.id
}


service_account = google_service_account.eventarc_workflows_service_account.id

depends_on = [google_project_service.pubsub, google_project_service.eventarc,
google_service_account.eventarc_workflows_service_account]
}

`, context)
}

func TestAccCGCSnippet_sqlDatabaseInstanceSqlserverExample(t *testing.T) {
skipIfVcr(t)
t.Parallel()
Expand Down
17 changes: 17 additions & 0 deletions google-beta/test-fixtures/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This is a sample workflow that simply logs the incoming Pub/Sub event
# Note that $$ is needed for Terraform

main:
params: [event]
steps:
- log_event:
call: sys.log
args:
text: $${event}
severity: INFO
- decode_pubsub_message:
assign:
- base64: $${base64.decode(event.data.data)}
- message: $${text.decode(base64)}
- return_pubsub_message:
return: $${message}