Skip to content

Commit 04e9b28

Browse files
authored
Add example of datastore with v2 functions. (GoogleCloudPlatform#11096)
## Description Add example for Cloud Functions and Firestore in Datastore Mode ## Checklist - [X] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md) - [X] README is updated to include [all relevant information](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file) - [ ] **Tests** pass: `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/CODEOWNERS) with the codeowners for this sample - [ ] This sample adds a new **Product API**, and I updated the [Blunderbuss issue/PR auto-assigner](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/blunderbuss.yml) with the codeowners for this sample - [x] Please **merge** this PR for me once it is approved
1 parent 1a1cfef commit 04e9b28

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START functions_cloudevent_datastore]
16+
from cloudevents.http import CloudEvent
17+
import functions_framework
18+
from google.events.cloud import datastore
19+
20+
21+
@functions_framework.cloud_event
22+
def hello_datastore(cloud_event: CloudEvent) -> None:
23+
"""Triggers by a change to a Firestore entity.
24+
25+
Args:
26+
cloud_event: cloud event with information on the firestore event trigger
27+
"""
28+
datastore_payload = datastore.EntityEventData()
29+
datastore_payload._pb.ParseFromString(cloud_event.data)
30+
31+
print(f"Function triggered by change to: {cloud_event['source']}")
32+
33+
print("\nOld value:")
34+
print(datastore_payload.old_value)
35+
36+
print("\nNew value:")
37+
print(datastore_payload.value)
38+
39+
40+
# [END functions_cloudevent_datastore]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from cloudevents.http import CloudEvent
16+
from google.events.cloud import datastore
17+
18+
import main
19+
20+
21+
def test_hello_datastore(capsys):
22+
old_entity = datastore.EntityResult(
23+
entity=datastore.Entity(
24+
properties={"name": datastore.Value(string_value="Old Test Name")}),
25+
)
26+
new_entity = datastore.EntityResult(
27+
entity=datastore.Entity(
28+
properties={"name": datastore.Value(string_value="New Test Name")}),
29+
)
30+
31+
datastore_payload = datastore.EntityEventData(
32+
old_value=old_entity,
33+
value=new_entity,
34+
)
35+
36+
attributes = {
37+
"id": "5e9f24a",
38+
"type": "google.cloud.datastore.entity.v1.written",
39+
"source": "/projects/test-project/databases/(default)/documents/test-kind/test-entity",
40+
}
41+
42+
data = datastore_payload._pb.SerializeToString()
43+
44+
cloud_event = CloudEvent(attributes, data)
45+
46+
# Calling the function with the mocked cloud event
47+
main.hello_datastore(cloud_event)
48+
49+
out, _ = capsys.readouterr()
50+
51+
assert "\nOld value:" in out
52+
assert datastore_payload.old_value.__str__() in out
53+
assert "\nNew value:" in out
54+
assert datastore_payload.value.__str__() in out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==7.4.4
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
functions-framework==3.5.0
2+
google-events==0.11.0
3+
google-cloud-datastore==2.19.0
4+
google-api-core==2.15.0
5+
protobuf==4.25.2
6+
cloudevents==1.10.1

0 commit comments

Comments
 (0)