Skip to content

Commit

Permalink
Add example of datastore with v2 functions. (#11096)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
JU-2094 committed Jan 11, 2024
1 parent 1a1cfef commit 04e9b28
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
40 changes: 40 additions & 0 deletions functions/v2/datastore/hello-datastore/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 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 functions_cloudevent_datastore]
from cloudevents.http import CloudEvent
import functions_framework
from google.events.cloud import datastore


@functions_framework.cloud_event
def hello_datastore(cloud_event: CloudEvent) -> None:
"""Triggers by a change to a Firestore entity.
Args:
cloud_event: cloud event with information on the firestore event trigger
"""
datastore_payload = datastore.EntityEventData()
datastore_payload._pb.ParseFromString(cloud_event.data)

print(f"Function triggered by change to: {cloud_event['source']}")

print("\nOld value:")
print(datastore_payload.old_value)

print("\nNew value:")
print(datastore_payload.value)


# [END functions_cloudevent_datastore]
54 changes: 54 additions & 0 deletions functions/v2/datastore/hello-datastore/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2024 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.

from cloudevents.http import CloudEvent
from google.events.cloud import datastore

import main


def test_hello_datastore(capsys):
old_entity = datastore.EntityResult(
entity=datastore.Entity(
properties={"name": datastore.Value(string_value="Old Test Name")}),
)
new_entity = datastore.EntityResult(
entity=datastore.Entity(
properties={"name": datastore.Value(string_value="New Test Name")}),
)

datastore_payload = datastore.EntityEventData(
old_value=old_entity,
value=new_entity,
)

attributes = {
"id": "5e9f24a",
"type": "google.cloud.datastore.entity.v1.written",
"source": "/projects/test-project/databases/(default)/documents/test-kind/test-entity",
}

data = datastore_payload._pb.SerializeToString()

cloud_event = CloudEvent(attributes, data)

# Calling the function with the mocked cloud event
main.hello_datastore(cloud_event)

out, _ = capsys.readouterr()

assert "\nOld value:" in out
assert datastore_payload.old_value.__str__() in out
assert "\nNew value:" in out
assert datastore_payload.value.__str__() in out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==7.4.4
6 changes: 6 additions & 0 deletions functions/v2/datastore/hello-datastore/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
functions-framework==3.5.0
google-events==0.11.0
google-cloud-datastore==2.19.0
google-api-core==2.15.0
protobuf==4.25.2
cloudevents==1.10.1

0 comments on commit 04e9b28

Please sign in to comment.