Skip to content

feat(eventarc): new sample for storage event receiver #10222

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

Merged
merged 17 commits into from
Jun 28, 2023
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
1 change: 1 addition & 0 deletions eventarc/storage_handler/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
55 changes: 55 additions & 0 deletions eventarc/storage_handler/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2023 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START eventarc_storage_cloudevent_server]
import os

from cloudevents.http import from_http

from flask import Flask, request

from google.events.cloud.storage import StorageObjectData


app = Flask(__name__)
# [END eventarc_storage_cloudevent_server]


# [START eventarc_storage_cloudevent_handler]
@app.route("/", methods=["POST"])
def index():
event = from_http(request.headers, request.get_data())

# Gets the GCS bucket name from the CloudEvent data
# Example: "storage.googleapis.com/projects/_/buckets/my-bucket"
try:
storage_obj = StorageObjectData(event.data)
gcs_object = os.path.join(storage_obj.bucket, storage_obj.name)
update_time = storage_obj.updated
return (
f"Cloud Storage object changed: {gcs_object}"
+ f" updated at {update_time}",
200,
)
except ValueError as e:
return (f"Failed to parse event data: {e}", 400)


# [END eventarc_storage_cloudevent_handler]


# [START eventarc_storage_cloudevent_server]
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
# [END eventarc_storage_cloudevent_server]
60 changes: 60 additions & 0 deletions eventarc/storage_handler/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2023 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START eventarc_testing_cloudevent]
from uuid import uuid4

from cloudevents.conversion import to_binary
from cloudevents.http import CloudEvent

from google.events.cloud.storage import StorageObjectData

import pytest

import main


ce_attributes = {
"id": str(uuid4),
"type": "com.pytest.sample.event",
"source": "<my-test-source>",
"specversion": "1.0",
}


@pytest.fixture
def client():
main.app.testing = True
return main.app.test_client()


def test_endpoint(client):
storagedata = StorageObjectData(bucket="test-bucket", name="my-file.txt")
event = CloudEvent(ce_attributes, StorageObjectData.to_dict(storagedata))
headers, body = to_binary(event)

r = client.post("/", headers=headers, data=body)
assert r.status_code == 200
assert "Cloud Storage object changed: test-bucket/my-file.txt" in r.text


# [END eventarc_testing_cloudevent]


def test_invalid_data(client):
event = CloudEvent(ce_attributes, {"an_unknown_field": "some_value"})
headers, body = to_binary(event)

r = client.post("/", headers=headers, data=body)
assert r.status_code == 400
1 change: 1 addition & 0 deletions eventarc/storage_handler/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==7.0.1
4 changes: 4 additions & 0 deletions eventarc/storage_handler/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Flask==2.1.0
gunicorn==20.1.0
google-events==0.7.0
cloudevents==1.9.0