-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
998a495
feat(eventarc): cloudevent GCS handler
muncus 892ba23
Procfile, for easier cloud run deployment
muncus 38b3e9d
lint cleanups
muncus b2ed2a1
lint errors from headers and imports
muncus 902eec8
flake8 import order fix
muncus 8b67931
Merge branch 'main' into eventarc_storage_handler
muncus d46b8d4
fix region tags
muncus c733d0d
really fix the region tag
muncus b4380ab
Merge branch 'main' into eventarc_storage_handler
muncus 7ea1a5e
Merge branch 'main' into eventarc_storage_handler
kweinmeister 51269dd
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 3777531
Merge branch 'main' into eventarc_storage_handler
muncus 289c1c3
Merge branch 'main' into eventarc_storage_handler
muncus 8877626
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 1eb3361
Merge branch 'main' into eventarc_storage_handler
muncus 9c76c73
improve error handling, tests, per @grayside.
muncus d7d1f4f
Merge branch 'main' into eventarc_storage_handler
muncus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
muncus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest==7.0.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.