Skip to content

Commit ac463f5

Browse files
authored
Schedule datastore export (#3264)
Sample app: schedule Datastore exports
1 parent 98006ca commit ac463f5

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

datastore/schedule-export/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Scheduling Datastore exports with Cloud Functions and Cloud Scheduler
2+
3+
This sample application demonstrates how to schedule exports of your Datastore entities. To deploy this sample, see:
4+
5+
[Scheduling exports](https://cloud.google.com/datastore/docs/schedule-export)

datastore/schedule-export/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import base64
2+
import json
3+
import os
4+
5+
from googleapiclient.discovery import build
6+
7+
datastore = build('datastore', 'v1')
8+
project_id = os.environ.get('GCP_PROJECT')
9+
10+
11+
def datastore_export(event, context):
12+
'''Triggers a Datastore export from a Cloud Scheduler job.
13+
14+
Args:
15+
event (dict): event[data] must contain a json object encoded in
16+
base-64. Cloud Scheduler encodes payloads in base-64 by default.
17+
Object must include a 'bucket' value and can include 'kinds'
18+
and 'namespaceIds' values.
19+
context (google.cloud.functions.Context): The Cloud Functions event
20+
metadata.
21+
'''
22+
23+
json_data = json.loads(base64.b64decode(event['data']).decode('utf-8'))
24+
bucket = json_data['bucket']
25+
entity_filter = {}
26+
27+
if 'kinds' in json_data:
28+
entity_filter['kinds'] = json_data['kinds']
29+
30+
if 'namespaceIds' in json_data:
31+
entity_filter['namespaceIds'] = json_data['namespaceIds']
32+
33+
request_body = {
34+
'outputUrlPrefix': bucket,
35+
'entityFilter': entity_filter
36+
}
37+
38+
export_request = datastore.projects().export(
39+
projectId=project_id,
40+
body=request_body
41+
)
42+
response = export_request.execute()
43+
print(response)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==5.3.2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-api-python-client>=1.7.12
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
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+
import base64
16+
17+
from unittest.mock import Mock
18+
19+
import main
20+
21+
mock_context = Mock()
22+
mock_context.event_id = '617187464135194'
23+
mock_context.timestamp = '2020-04-15T22:09:03.761Z'
24+
25+
26+
def test_datastore_export(capsys):
27+
# Test an export without an entity filter
28+
bucket = 'gs://my-bucket'
29+
json_string = '{{ "bucket": "{bucket}" }}'.format(bucket=bucket)
30+
31+
# Encode data like Cloud Scheduler
32+
data = bytes(json_string, 'utf-8')
33+
data_encoded = base64.b64encode(data)
34+
event = {"data": data_encoded}
35+
36+
# Mock the Datastore service
37+
mockDatastore = Mock()
38+
main.datastore = mockDatastore
39+
40+
# Call tested function
41+
main.datastore_export(event, mock_context)
42+
out, err = capsys.readouterr()
43+
export_args = mockDatastore.projects().export.call_args[1]
44+
req_body = export_args['body']
45+
# Assert request includes test values
46+
assert req_body['outputUrlPrefix'] == bucket
47+
48+
49+
def test_datastore_export_entity_filter(capsys):
50+
# Test an export with an entity filter
51+
bucket = 'gs://my-bucket'
52+
kinds = 'Users,Tasks'
53+
namespaceIds = 'Customer831,Customer157'
54+
json_string = '{{ "bucket": "{bucket}", "kinds": "{kinds}", "namespaceIds": "{namespaceIds}" }}'.format(
55+
bucket=bucket, kinds=kinds, namespaceIds=namespaceIds)
56+
57+
# Encode data like Cloud Scheduler
58+
data = bytes(json_string, 'utf-8')
59+
data_encoded = base64.b64encode(data)
60+
event = {"data": data_encoded}
61+
62+
# Mock the Datastore service
63+
mockDatastore = Mock()
64+
main.datastore = mockDatastore
65+
66+
# Call tested function
67+
main.datastore_export(event, mock_context)
68+
out, err = capsys.readouterr()
69+
export_args = mockDatastore.projects().export.call_args[1]
70+
req_body = export_args['body']
71+
# Assert request includes test values
72+
assert req_body['outputUrlPrefix'] == bucket
73+
assert req_body['entityFilter']['kinds'] == kinds
74+
assert req_body['entityFilter']['namespaceIds'] == namespaceIds

0 commit comments

Comments
 (0)