Skip to content

Commit 609e09f

Browse files
chore(eventarc): rename existing region, and update to use cloudevent sdk. (#10232)
* chore(eventarc): rename existing region, and update to use cloudevent sdk. * lint fix from prior merge * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent dddaea4 commit 609e09f

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

eventarc/audit-storage/main.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,40 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# [START eventarc_http_quickstart_server]
1516
# [START eventarc_audit_storage_server]
1617
import os
1718

18-
from flask import Flask, request
19+
from cloudevents.http import from_http
1920

21+
from flask import Flask, request
2022

2123
app = Flask(__name__)
2224
# [END eventarc_audit_storage_server]
25+
# [END eventarc_http_quickstart_server]
2326

2427

28+
# [START eventarc_http_quickstart_handler]
2529
# [START eventarc_audit_storage_handler]
2630
@app.route("/", methods=["POST"])
2731
def index():
28-
# Gets the GCS bucket name from the CloudEvent header
32+
# Create a CloudEvent object from the incoming request
33+
event = from_http(request.headers, request.data)
34+
# Gets the GCS bucket name from the CloudEvent
2935
# Example: "storage.googleapis.com/projects/_/buckets/my-bucket"
30-
bucket = request.headers.get("ce-subject")
36+
bucket = event.get("subject")
3137

3238
print(f"Detected change in Cloud Storage bucket: {bucket}")
3339
return (f"Detected change in Cloud Storage bucket: {bucket}", 200)
3440

3541

3642
# [END eventarc_audit_storage_handler]
43+
# [END eventarc_http_quickstart_handler]
3744

3845

46+
# [START eventarc_http_quickstart_server]
3947
# [START eventarc_audit_storage_server]
4048
if __name__ == "__main__":
4149
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
4250
# [END eventarc_audit_storage_server]
51+
# [END eventarc_http_quickstart_server]

eventarc/audit-storage/main_test.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
import copy
1514

1615
from uuid import uuid4
1716

17+
from cloudevents.conversion import to_binary
18+
from cloudevents.http import CloudEvent
19+
1820
import pytest
1921

2022
import main
2123

2224

23-
binary_headers = {
24-
"ce-id": str(uuid4),
25-
"ce-type": "com.pytest.sample.event",
26-
"ce-source": "<my-test-source>",
27-
"ce-specversion": "1.0",
25+
ce_attributes = {
26+
"id": str(uuid4),
27+
"type": "com.pytest.sample.event",
28+
"source": "<my-test-source>",
29+
"specversion": "1.0",
30+
"subject": "test-bucket",
2831
}
2932

3033

@@ -34,14 +37,11 @@ def client():
3437
return main.app.test_client()
3538

3639

37-
def test_endpoint(client, capsys):
38-
test_headers = copy.copy(binary_headers)
39-
test_headers["Ce-Subject"] = "test-subject"
40+
def test_endpoint(client):
41+
event = CloudEvent(ce_attributes, dict())
4042

41-
r = client.post("/", headers=test_headers)
42-
assert r.status_code == 200
43+
headers, body = to_binary(event)
4344

44-
out, _ = capsys.readouterr()
45-
assert (
46-
f"Detected change in Cloud Storage bucket: {test_headers['Ce-Subject']}" in out
47-
)
45+
r = client.post("/", headers=headers, data=body)
46+
assert r.status_code == 200
47+
assert "Detected change in Cloud Storage bucket: test-bucket" in r.text
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Flask==2.1.0
22
gunicorn==20.1.0
3+
cloudevents==1.9.0

0 commit comments

Comments
 (0)