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 #6279

Merged
merged 7 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions mmv1/products/cgc/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ overrides: !ruby/object:Overrides::ResourceOverrides
primary_resource_id: "instance_virtual_display"
vars:
instance_virtual_display: "instance-virtual-display"

# Eventarc
### Eventarc Workflows quickstart
- !ruby/object:Provider::Terraform::Examples
name: "eventarc-workflows"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name: "eventarc-workflows"
name: "eventarc_workflows"

We will want to use an underscore instead of the dash here, and we'll need to update the associated filename as well. Thanks!

primary_resource_type: "google_eventarc_trigger"
primary_resource_id: "trigger_pubsub_tf"
vars:
pubsub_workflow_tf: "pubsub-workflow-tf"
trigger_pubsub_workflow_tf: "trigger-pubsub-workflow-tf"
min_version: beta
# ignore_read_extra:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: these commented out lines can be removed i believe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thank you!

# - "port_range"
# - "target"
# - "ip_address"

# SQL
### When including separate samples for each DB type, add `skip_test: true`
### for Postgres and MySQL, but not for SQL Server
Expand Down
108 changes: 108 additions & 0 deletions mmv1/templates/terraform/examples/eventarc-workflows.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# [START eventarc_terraform_enableapis]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file can be removed in place of the new eventarc_workflows.tf.erb file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thank you Megan.

# 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
}

# [END eventarc_terraform_enableapis]

# [START eventarc_workflows_create_serviceaccount]

# 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]
}

# [END eventarc_workflows_create_serviceaccount]

# [START eventarc_workflows_deploy]
# Define and deploy a workflow
resource "google_workflows_workflow" "workflows_example" {
name = "<%= ctx[:vars]['pubsub_workflow_tf'] %>"
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("${path.module}/workflow.yaml",{})

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

# [END eventarc_workflows_deploy]

# [START eventarc_create_pubsub_trigger]
# Create an Eventarc trigger routing Pub/Sub events to Workflows
resource "google_eventarc_trigger" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['trigger_pubsub_workflow_tf'] %>"
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]
}

# [END eventarc_create_pubsub_trigger]