Skip to content

Commit b7c80d4

Browse files
authored
feat: init eventarc quickstarts (GoogleCloudPlatform#4710)
* feat: init eventarc quickstarts Signed-off-by: Grant Timmerman * fix: update readmes and gh owners Signed-off-by: Grant Timmerman * fix: fix gcs cal assertion Signed-off-by: Grant Timmerman * fix: readme nits Signed-off-by: Grant Timmerman * docs: update eventarc readme formatting
1 parent 4831320 commit b7c80d4

File tree

20 files changed

+233
-155
lines changed

20 files changed

+233
-155
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
/dns/**/*.py @michaelawyu @GoogleCloudPlatform/python-samples-owners
3535
/endpoints/**/*.py @GoogleCloudPlatform/python-samples-owners
3636
/error_reporting/**/*.py @GoogleCloudPlatform/python-samples-owners
37+
/eventarc/**/*.py @averikitsch @grant @GoogleCloudPlatform/python-samples-owners
3738
/firestore/**/*.py @GoogleCloudPlatform/firestore-dpe @GoogleCloudPlatform/python-samples-owners
3839
/functions/**/*.py @ace-n @grant @GoogleCloudPlatform/python-samples-owners
3940
/healthcare/**/*.py @noerog @GoogleCloudPlatform/python-samples-owners

eventarc/README.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Google Cloud Eventarc Python Samples
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=blog/README.md
7+
8+
This directory contains samples for Google Cloud Eventarc.
9+
10+
## Samples
11+
12+
| Sample | Description | Deploy |
13+
| ------------------------------- | ------------------------ | ------------- |
14+
|[Events – Pub/Sub][events_pubsub] | Event-driven service with Events for Cloud Run for Pub/Sub | - |
15+
|[Anthos Events – Pub/Sub][anthos_events_pubsub] | Event-driven service with Events for Cloud Run on Anthos for Pub/Sub | - |
16+
|[Events – GCS][events_storage] | Event-driven service with Events for Cloud Run for GCS | - |
17+
|[Anthos Events – GCS][anthos_events_storage] | Event-driven service with Events for Cloud Run on Anthos for GCS | - |
18+
19+
For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
20+
21+
## Setup
22+
23+
1. [Set up for Cloud Run development](https://cloud.google.com/run/docs/setup)
24+
25+
2. Clone this repository:
26+
27+
```
28+
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
29+
```
30+
31+
Note: Some samples in the list above are hosted in other repositories. They are noted with the symbol "➥".
32+
33+
34+
## How to run a sample locally
35+
36+
1. [Install docker locally](https://docs.docker.com/install/)
37+
38+
2. [Build the sample container](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker):
39+
40+
```
41+
export SAMPLE=
42+
cd $SAMPLE
43+
docker build --tag $SAMPLE .
44+
```
45+
46+
3. [Run containers locally](https://cloud.google.com/run/docs/testing/local)
47+
48+
With the built container:
49+
50+
```
51+
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE
52+
```
53+
54+
Overriding the built container with local code:
55+
56+
```
57+
PORT=8080 && docker run --rm \
58+
-p 8080:${PORT} -e PORT=${PORT} \
59+
-v $PWD:/app $SAMPLE
60+
```
61+
62+
Injecting your service account key for access to GCP services:
63+
64+
```
65+
# Set the name of the service account key within the container
66+
export SA_KEY_NAME=my-key-name-123
67+
68+
PORT=8080 && docker run --rm \
69+
-p 8080:${PORT} \
70+
-e PORT=${PORT} \
71+
-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \
72+
-v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \
73+
-v $PWD:/app $SAMPLE
74+
```
75+
76+
* Use the --volume (-v) flag to inject the credential file into the container
77+
(assumes you have already set your `GOOGLE_APPLICATION_CREDENTIALS`
78+
environment variable on your machine)
79+
80+
* Use the --environment (-e) flag to set the `GOOGLE_APPLICATION_CREDENTIALS`
81+
variable inside the container
82+
83+
Learn more about [testing your container image locally.][testing]
84+
85+
## Deploying a Cloud Run service
86+
87+
1. Set an environment variable with your GCP Project ID
88+
```
89+
export GOOGLE_CLOUD_PROJECT=
90+
```
91+
92+
1. Submit a build using Google Cloud Build
93+
```
94+
gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
95+
```
96+
97+
1. Deploy to Cloud Run
98+
```
99+
gcloud run deploy $SAMPLE --image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
100+
```
101+
102+
Choose a particular sample for information about triggering the service with an event.
103+
104+
See [Building containers][run_build] and [Deploying container images][run_deploy]
105+
for more information.
106+
107+
[run_docs]: https://cloud.google.com/run/docs/
108+
[run_build]: https://cloud.google.com/run/docs/building/containers
109+
[run_deploy]: https://cloud.google.com/run/docs/deploying
110+
[events_pubsub]: pubsub/README.md
111+
[anthos_events_pubsub]: pubsub/anthos.md
112+
[events_storage]: audit-storage/README.md
113+
[anthos_events_storage]: audit-storage/anthos.md
114+
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services
File renamed without changes.

eventarc/audit-storage/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Cloud Eventarc – GCS tutorial
2+
3+
This sample shows how to create a service that processes GCS using [the CloudEvents SDK](https://github.com/cloudevents/sdk-python).
4+
5+
## Setup
6+
7+
Configure environment variables:
8+
9+
```sh
10+
MY_RUN_SERVICE=gcs-service
11+
MY_RUN_CONTAINER=gcs-container
12+
MY_GCS_BUCKET=gcs-bucket # Must be globally unique.
13+
```
14+
15+
## Quickstart
16+
17+
Deploy your Cloud Run service:
18+
19+
```sh
20+
gcloud builds submit \
21+
--tag gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER
22+
gcloud run deploy $MY_RUN_SERVICE \
23+
--image gcr.io/$(gcloud config get-value project)/$MY_RUN_CONTAINER \
24+
--platform managed
25+
```
26+
27+
Create a _single region_ Cloud Storage bucket:
28+
29+
```sh
30+
gsutil mb -p $(gcloud config get-value project) \
31+
-l us-central1 \
32+
gs://"$MY_GCS_BUCKET"
33+
```
34+
35+
Create Cloud Storage trigger:
36+
37+
```sh
38+
gcloud beta eventarc triggers create my-gcs-trigger \
39+
--destination-run-service $MY_RUN_SERVICE \
40+
--matching-criteria type=google.cloud.audit.log.v1.written \
41+
--matching-criteria methodName=storage.buckets.update \
42+
--matching-criteria serviceName=storage.googleapis.com \
43+
--matching-criteria resourceName=projects/_/buckets/"$MY_GCS_BUCKET"
44+
```
45+
46+
## Test
47+
48+
Test your Cloud Run service by creating a GCS event:
49+
50+
```sh
51+
gsutil defstorageclass set STANDARD gs://$MY_GCS_BUCKET
52+
```
53+
54+
Observe the Cloud Run service printing upon receiving an event in Cloud Logging:
55+
56+
```sh
57+
gcloud logging read "resource.type=cloud_run_revision AND \
58+
resource.labels.service_name=$MY_RUN_SERVICE" \
59+
--project $(gcloud config get-value project) \
60+
--limit 30 \
61+
--format 'value(textPayload)'
62+
```
63+
64+
One of the logs you'll see shows the Run service confirming the event was received:
65+
66+
```
67+
GCS CloudEvent type: storage.googleapis.com/projects/_/buckets/gcs-bucket
68+
```
File renamed without changes.

run/events-storage/main.py renamed to eventarc/audit-storage/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def index():
4747
print(errmsg)
4848
return errmsg, 400
4949

50-
print(f"GCS CloudEvent type: {event['subject']}")
51-
return (f"GCS CloudEvent type: {event['subject']}", 200)
50+
print(f"Detected change in GCS bucket: {event['subject']}")
51+
return (f"Detected change in GCS bucket: {event['subject']}", 200)
5252
# [END run_events_gcs_handler]
5353

5454

run/events-storage/main_test.py renamed to eventarc/audit-storage/main_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_endpoint(client, capsys):
4242
assert r.status_code == 200
4343

4444
out, _ = capsys.readouterr()
45-
assert f"GCS CloudEvent type: {test_headers['Ce-Subject']}" in out
45+
assert f"Detected change in GCS bucket: {test_headers['Ce-Subject']}" in out
4646

4747

4848
def test_missing_subject(client, capsys):
File renamed without changes.
File renamed without changes.

eventarc/pubsub/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Cloud Eventarc – Pub/Sub tutorial
2+
3+
This sample shows how to create a service that processes Pub/Sub events using
4+
[the CloudEvents SDK](https://github.com/cloudevents/sdk-python).
5+
6+
## Quickstart
7+
8+
Deploy your Cloud Run service:
9+
10+
```sh
11+
gcloud builds submit \
12+
--tag gcr.io/$(gcloud config get-value project)/eventarc-pubsub
13+
gcloud run deploy eventarc-pubsub \
14+
--image gcr.io/$(gcloud config get-value project)/eventarc-pubsub \
15+
--platform managed
16+
```
17+
18+
Create a Cloud Eventarc trigger, which will also create a Pub/Sub topic:
19+
20+
```sh
21+
gcloud beta eventarc triggers create pubsub-trigger \
22+
--destination-run-service eventarc-pubsub \
23+
--matching-criteria "type=google.cloud.pubsub.topic.v1.messagePublished"
24+
```
25+
26+
## Test
27+
28+
Test your Cloud Run service by getting the created topic, and publishing a message to that topic:
29+
30+
```sh
31+
TOPIC=$(gcloud beta eventarc triggers describe pubsub-trigger \
32+
--format="value(transport.pubsub.topic)")
33+
34+
echo "Listening to events on topic: $TOPIC"
35+
36+
gcloud pubsub topics publish $TOPIC --message="Events"
37+
```
38+
39+
You may observe the Run service receiving an event in Cloud Logging:
40+
41+
```sh
42+
gcloud logging read "resource.type=cloud_run_revision AND \
43+
resource.labels.service_name=eventarc-pubsub" \
44+
--project $(gcloud config get-value project) \
45+
--limit 10 \
46+
--format 'value(textPayload)'
47+
```
File renamed without changes.
File renamed without changes.
File renamed without changes.

run/README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
1515
|[Cloud Pub/Sub][pubsub] | Handling Pub/Sub push messages | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_pubsub] |
1616
|[Cloud SQL (MySQL)][mysql] | Use MySQL with Cloud Run | - |
1717
|[Cloud SQL (Postgres)][postgres] | Use Postgres with Cloud Run | - |
18-
|[Events – Pub/Sub][events_pubsub] | Event-driven service with Events for Cloud Run for Pub/Sub | - |
19-
|[Anthos Events – Pub/Sub][anthos_events_pubsub] | Event-driven service with Events for Cloud Run on Anthos for Pub/Sub | - |
20-
|[Events – GCS][events_storage] | Event-driven service with Events for Cloud Run for GCS | - |
21-
|[Anthos Events – GCS][anthos_events_storage] | Event-driven service with Events for Cloud Run on Anthos for GCS | - |
2218

2319
For more Cloud Run samples beyond Python, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
2420

@@ -113,10 +109,6 @@ for more information.
113109
[pubsub]: pubsub/
114110
[mysql]: ../cloud-sql/mysql/sqlalchemy
115111
[postgres]: ../cloud-sql/postgres/sqlalchemy
116-
[events_pubsub]: events-pubsub/
117-
[anthos_events_pubsub]: events-pubsub/anthos.md
118-
[events_storage]: events-storage/
119-
[anthos_events_storage]: events-storage/anthos.md
120112
[run_button_helloworld]: https://deploy.cloud.run/?git_repo=https://github.com/knative/docs&dir=docs/serving/samples/hello-world/helloworld-python
121113
[run_button_pubsub]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&dir=run/pubsub
122114
[testing]: https://cloud.google.com/run/docs/testing/local#running_locally_using_docker_with_access_to_services

run/events-pubsub/README.md

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)