Skip to content

Commit b546ae2

Browse files
Gurov IlyaleahecoleTakashi Matsuo
authored
feat(datastore): Add Datastore Admin API samples (#4121)
* feat(datastore): Add Datastore Admin API samples * add test for entities import/export * add requirements into the README file * fix imports order, add Storage roles tip, add retrying into requirements * use backoff instead of retrying, add App Engine tip * del retrying from requrements-test.txt * fix imports order * add timeouts Co-authored-by: Leah E. Cole <[email protected]> Co-authored-by: Takashi Matsuo <[email protected]>
1 parent 810bcf2 commit b546ae2

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed

datastore/cloud-client/README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ This directory contains samples for Google Cloud Datastore. `Google Cloud Datast
1414

1515
.. _Google Cloud Datastore: https://cloud.google.com/datastore/docs
1616

17+
18+
19+
20+
Set environment variables:
21+
`GOOGLE_CLOUD_PROJECT` - Google Cloud project id
22+
`CLOUD_STORAGE_BUCKET` - Google Cloud Storage bucket name
23+
24+
Roles to be set in your Service Account and App Engine default service account:
25+
`Datastore Import Export Admin`, or `Cloud Datastore Owner`, or `Owner`
26+
`Storage Admin`, or `Owner`
27+
28+
1729
Setup
1830
-------------------------------------------------------------------------------
1931

datastore/cloud-client/README.rst.in

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@ product:
44
name: Google Cloud Datastore
55
short_name: Cloud Datastore
66
url: https://cloud.google.com/datastore/docs
7-
description: >
7+
description: >
88
`Google Cloud Datastore`_ is a NoSQL document database built for automatic
99
scaling, high performance, and ease of application development.
1010

11+
other_required_steps: >
12+
Set environment variables:
13+
`GOOGLE_CLOUD_PROJECT` - Google Cloud project id
14+
`CLOUD_STORAGE_BUCKET` - Google Cloud Storage bucket name
15+
16+
Roles to be set in your Service Account and App Engine default service account:
17+
`Datastore Import Export Admin`, or `Cloud Datastore Owner`, or `Owner`
18+
`Storage Admin`, or `Owner`
19+
1120
setup:
1221
- auth
1322
- install_deps

datastore/cloud-client/admin.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
# [START datastore_admin_client_create]
15+
from google.cloud.datastore_admin_v1.gapic import datastore_admin_client
16+
17+
18+
def client_create():
19+
"""Creates a new Datastore admin client."""
20+
client = datastore_admin_client.DatastoreAdminClient()
21+
22+
print("Admin client created\n")
23+
return client
24+
# [END datastore_admin_client_create]
25+
26+
27+
# [START datastore_admin_entities_export]
28+
def export_entities(project_id, output_url_prefix):
29+
"""
30+
Exports a copy of all or a subset of entities from
31+
Datastore to another storage system, such as Cloud Storage.
32+
"""
33+
# project_id = "project-id"
34+
# output_url_prefix = "gs://bucket-name"
35+
client = datastore_admin_client.DatastoreAdminClient()
36+
37+
op = client.export_entities(project_id, output_url_prefix)
38+
response = op.result(timeout=200)
39+
40+
print("Entities were exported\n")
41+
return response
42+
# [END datastore_admin_entities_export]
43+
44+
45+
# [START datastore_admin_entities_import]
46+
def import_entities(project_id, input_url):
47+
"""Imports entities into Datastore."""
48+
# project_id := "project-id"
49+
# input_url := "gs://bucket-name/overall-export-metadata-file"
50+
client = datastore_admin_client.DatastoreAdminClient()
51+
52+
op = client.import_entities(project_id, input_url)
53+
response = op.result(timeout=200)
54+
55+
print("Entities were imported\n")
56+
return response
57+
# [END datastore_admin_entities_import]
58+
59+
60+
# [START datastore_admin_index_get]
61+
def get_index(project_id, index_id):
62+
"""Gets an index."""
63+
# project_id := "my-project-id"
64+
# index_id := "my-index"
65+
client = datastore_admin_client.DatastoreAdminClient()
66+
index = client.get_index(project_id, index_id)
67+
68+
print("Got index: %v\n", index.index_id)
69+
return index
70+
# [END datastore_admin_index_get]
71+
72+
73+
# [START datastore_admin_index_list]
74+
def list_indexes(project_id):
75+
"""Lists the indexes."""
76+
# project_id := "my-project-id"
77+
client = datastore_admin_client.DatastoreAdminClient()
78+
79+
indexes = []
80+
for index in client.list_indexes(project_id):
81+
indexes.append(index)
82+
print("Got index: %v\n", index.index_id)
83+
84+
print("Got list of indexes\n")
85+
return indexes
86+
# [END datastore_admin_index_list]

datastore/cloud-client/admin_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import os
15+
16+
import backoff
17+
import pytest
18+
19+
import admin
20+
21+
22+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
BUCKET = os.environ["CLOUD_STORAGE_BUCKET"]
24+
25+
26+
class TestDatastoreAdminSnippets:
27+
def test_client_create(self):
28+
assert admin.client_create()
29+
30+
def test_get_index(self):
31+
indexes = admin.list_indexes(PROJECT)
32+
if not indexes:
33+
pytest.skip(
34+
"Skipping datastore test. At least "
35+
"one index should present in database."
36+
)
37+
38+
assert admin.get_index(PROJECT, indexes[0].index_id)
39+
40+
def test_list_index(self):
41+
assert admin.list_indexes(PROJECT)
42+
43+
@backoff.on_exception(backoff.expo, AssertionError, max_tries=3, max_time=540000)
44+
def test_export_import_entities(self):
45+
response = admin.export_entities(PROJECT, "gs://" + BUCKET)
46+
assert response
47+
48+
assert admin.import_entities(PROJECT, response.output_url)

0 commit comments

Comments
 (0)