diff --git a/.coveragerc b/.coveragerc index 0d8e6297..ce32b322 100644 --- a/.coveragerc +++ b/.coveragerc @@ -14,25 +14,23 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Generated by synthtool. DO NOT EDIT! [run] branch = True -omit = - google/cloud/__init__.py - [report] fail_under = 100 show_missing = True +omit = + google/cloud/__init__.py + google/cloud/datastore_v1/__init__.py + google/cloud/datastore_admin_v1/__init__.py + */site-packages/*.py exclude_lines = # Re-enable the standard pragma pragma: NO COVER # Ignore debug-only repr def __repr__ - # Ignore abstract methods - raise NotImplementedError -omit = - */gapic/*.py - */proto/*.py - */core/*.py - */site-packages/*.py - google/cloud/__init__.py + # Ignore pkg_resources exceptions. + # This is added at the module level as a safeguard for if someone + # generates the code and tries to run it without pip installing. This + # makes it virtually impossible to test properly. + except pkg_resources.DistributionNotFound diff --git a/.kokoro/samples/python3.6/common.cfg b/.kokoro/samples/python3.6/common.cfg index 84174002..a65c0f39 100644 --- a/.kokoro/samples/python3.6/common.cfg +++ b/.kokoro/samples/python3.6/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.6" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py36" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-datastore/.kokoro/test-samples.sh" diff --git a/.kokoro/samples/python3.7/common.cfg b/.kokoro/samples/python3.7/common.cfg index 2122ef4c..18251bfc 100644 --- a/.kokoro/samples/python3.7/common.cfg +++ b/.kokoro/samples/python3.7/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.7" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py37" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-datastore/.kokoro/test-samples.sh" diff --git a/.kokoro/samples/python3.8/common.cfg b/.kokoro/samples/python3.8/common.cfg index c4ca39f0..77f73452 100644 --- a/.kokoro/samples/python3.8/common.cfg +++ b/.kokoro/samples/python3.8/common.cfg @@ -13,6 +13,12 @@ env_vars: { value: "py-3.8" } +# Declare build specific Cloud project. +env_vars: { + key: "BUILD_SPECIFIC_GCLOUD_PROJECT" + value: "python-docs-samples-tests-py38" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-datastore/.kokoro/test-samples.sh" diff --git a/README.rst b/README.rst index bb685f04..0b6470e6 100644 --- a/README.rst +++ b/README.rst @@ -53,11 +53,7 @@ dependencies. Supported Python Versions ^^^^^^^^^^^^^^^^^^^^^^^^^ -Python >= 3.5 - -Deprecated Python Versions -^^^^^^^^^^^^^^^^^^^^^^^^^^ -Python == 2.7. Python 2.7 support will be removed on January 1, 2020. +Python >= 3.6 Mac/Linux diff --git a/docs/admin_client.rst b/docs/admin_client.rst index 1c025ca5..1eed2696 100644 --- a/docs/admin_client.rst +++ b/docs/admin_client.rst @@ -1,6 +1,6 @@ Datastore Admin Client ====================== -.. automodule:: google.cloud.datastore_admin_v1.gapic.datastore_admin_client +.. automodule:: google.cloud.datastore_admin_v1.services.datastore_admin.client :members: :show-inheritance: diff --git a/google/cloud/datastore/_gapic.py b/google/cloud/datastore/_gapic.py index 18c2ce91..3200ea3c 100644 --- a/google/cloud/datastore/_gapic.py +++ b/google/cloud/datastore/_gapic.py @@ -19,7 +19,8 @@ from google.cloud._helpers import make_secure_channel from google.cloud._http import DEFAULT_USER_AGENT -from google.cloud.datastore_v1.gapic import datastore_client +from google.cloud.datastore_v1.services.datastore import client as datastore_client +from google.cloud.datastore_v1.services.datastore.transports import grpc def make_datastore_api(client): @@ -38,6 +39,7 @@ def make_datastore_api(client): else: channel = insecure_channel(host) + transport = grpc.DatastoreGrpcTransport(channel=channel) return datastore_client.DatastoreClient( - channel=channel, client_info=client._client_info + transport=transport, client_info=client._client_info ) diff --git a/google/cloud/datastore/_http.py b/google/cloud/datastore/_http.py index 0d100353..8f2c9c58 100644 --- a/google/cloud/datastore/_http.py +++ b/google/cloud/datastore/_http.py @@ -18,7 +18,7 @@ from google.cloud import _http as connection_module from google.cloud import exceptions -from google.cloud.datastore_v1.proto import datastore_pb2 as _datastore_pb2 +from google.cloud.datastore_v1.types import datastore as _datastore_pb2 DATASTORE_API_HOST = "datastore.googleapis.com" @@ -108,9 +108,9 @@ def _rpc(http, project, method, base_url, client_info, request_pb, response_pb_c :rtype: :class:`google.protobuf.message.Message` :returns: The RPC message parsed from the response. """ - req_data = request_pb.SerializeToString() + req_data = request_pb._pb.SerializeToString() response = _request(http, project, method, req_data, base_url, client_info) - return response_pb_cls.FromString(response) + return response_pb_cls.deserialize(response) def build_api_url(project, method, base_url): diff --git a/google/cloud/datastore/batch.py b/google/cloud/datastore/batch.py index 294c1b45..7b0b4758 100644 --- a/google/cloud/datastore/batch.py +++ b/google/cloud/datastore/batch.py @@ -22,7 +22,7 @@ """ from google.cloud.datastore import helpers -from google.cloud.datastore_v1.proto import datastore_pb2 as _datastore_pb2 +from google.cloud.datastore_v1.types import datastore as _datastore_pb2 class Batch(object): @@ -219,7 +219,7 @@ def delete(self, key): raise ValueError("Key must be from same project as batch") key_pb = key.to_protobuf() - self._add_delete_key_pb().CopyFrom(key_pb) + self._add_delete_key_pb()._pb.CopyFrom(key_pb._pb) def begin(self): """Begins a batch. @@ -242,9 +242,9 @@ def _commit(self, retry, timeout): This is called by :meth:`commit`. """ if self._id is None: - mode = _datastore_pb2.CommitRequest.NON_TRANSACTIONAL + mode = _datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL else: - mode = _datastore_pb2.CommitRequest.TRANSACTIONAL + mode = _datastore_pb2.CommitRequest.Mode.TRANSACTIONAL kwargs = {} @@ -255,8 +255,15 @@ def _commit(self, retry, timeout): kwargs["timeout"] = timeout commit_response_pb = self._client._datastore_api.commit( - self.project, mode, self._mutations, transaction=self._id, **kwargs + request={ + "project_id": self.project, + "mode": mode, + "transaction": self._id, + "mutations": self._mutations, + }, + **kwargs, ) + _, updated_keys = _parse_commit_response(commit_response_pb) # If the back-end returns without error, we are guaranteed that # ``commit`` will return keys that match (length and @@ -337,11 +344,11 @@ def _assign_entity_to_pb(entity_pb, entity): :param entity: The entity being updated within the batch / transaction. """ bare_entity_pb = helpers.entity_to_protobuf(entity) - bare_entity_pb.key.CopyFrom(bare_entity_pb.key) - entity_pb.CopyFrom(bare_entity_pb) + bare_entity_pb._pb.key.CopyFrom(bare_entity_pb._pb.key) + entity_pb._pb.CopyFrom(bare_entity_pb._pb) -def _parse_commit_response(commit_response_pb): +def _parse_commit_response(commit_response): """Extract response data from a commit response. :type commit_response_pb: :class:`.datastore_pb2.CommitResponse` @@ -352,6 +359,7 @@ def _parse_commit_response(commit_response_pb): :class:`.entity_pb2.Key` for each incomplete key that was completed in the commit. """ + commit_response_pb = commit_response._pb mut_results = commit_response_pb.mutation_results index_updates = commit_response_pb.index_updates completed_keys = [ diff --git a/google/cloud/datastore/client.py b/google/cloud/datastore/client.py index 86e513a8..e06b8e60 100644 --- a/google/cloud/datastore/client.py +++ b/google/cloud/datastore/client.py @@ -185,7 +185,12 @@ def _extended_lookup( while loop_num < _MAX_LOOPS: # loop against possible deferred. loop_num += 1 lookup_response = datastore_api.lookup( - project, key_pbs, read_options=read_options, **kwargs + request={ + "project_id": project, + "keys": key_pbs, + "read_options": read_options, + }, + **kwargs, ) # Accumulate the new results. @@ -535,7 +540,7 @@ def get_multi( helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred ] - return [helpers.entity_from_protobuf(entity_pb) for entity_pb in entity_pbs] + return [helpers.entity_from_protobuf(entity_pb._pb) for entity_pb in entity_pbs] def put(self, entity, retry=None, timeout=None): """Save an entity in the Cloud Datastore. @@ -702,7 +707,8 @@ def allocate_ids(self, incomplete_key, num_ids, retry=None, timeout=None): kwargs = _make_retry_timeout_kwargs(retry, timeout) response_pb = self._datastore_api.allocate_ids( - incomplete_key.project, incomplete_key_pbs, **kwargs + request={"project_id": incomplete_key.project, "keys": incomplete_key_pbs}, + **kwargs, ) allocated_ids = [ allocated_key_pb.path[-1].id for allocated_key_pb in response_pb.keys @@ -871,8 +877,9 @@ def reserve_ids_sequential(self, complete_key, num_ids, retry=None, timeout=None key_pbs.append(key.to_protobuf()) kwargs = _make_retry_timeout_kwargs(retry, timeout) - self._datastore_api.reserve_ids(complete_key.project, key_pbs, **kwargs) - + self._datastore_api.reserve_ids( + request={"project_id": complete_key.project, "keys": key_pbs}, **kwargs + ) return None def reserve_ids(self, complete_key, num_ids, retry=None, timeout=None): @@ -921,6 +928,8 @@ def reserve_ids_multi(self, complete_keys, retry=None, timeout=None): kwargs = _make_retry_timeout_kwargs(retry, timeout) key_pbs = [key.to_protobuf() for key in complete_keys] - self._datastore_api.reserve_ids(complete_keys[0].project, key_pbs, **kwargs) + self._datastore_api.reserve_ids( + request={"project_id": complete_keys[0].project, "keys": key_pbs}, **kwargs + ) return None diff --git a/google/cloud/datastore/helpers.py b/google/cloud/datastore/helpers.py index db6f150e..f8b32f38 100644 --- a/google/cloud/datastore/helpers.py +++ b/google/cloud/datastore/helpers.py @@ -25,9 +25,8 @@ import six from google.cloud._helpers import _datetime_to_pb_timestamp -from google.cloud._helpers import _pb_timestamp_to_datetime -from google.cloud.datastore_v1.proto import datastore_pb2 -from google.cloud.datastore_v1.proto import entity_pb2 +from google.cloud.datastore_v1.types import datastore as datastore_pb2 +from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.key import Key @@ -86,7 +85,14 @@ def _new_value_pb(entity_pb, name): :rtype: :class:`.entity_pb2.Value` :returns: The new ``Value`` protobuf that was added to the entity. """ - return entity_pb.properties.get_or_create(name) + properties = entity_pb.properties + try: + properties = properties._pb + except AttributeError: + # TODO(microgenerator): shouldn't need this. the issue is that + # we have wrapped and non-wrapped protos coming here. + pass + return properties.get_or_create(name) def _property_tuples(entity_pb): @@ -114,15 +120,24 @@ def entity_from_protobuf(pb): :rtype: :class:`google.cloud.datastore.entity.Entity` :returns: The entity derived from the protobuf. """ + + if not getattr(pb, "_pb", False): + # Coerce raw pb type into proto-plus pythonic type. + proto_pb = entity_pb2.Entity(pb) + pb = pb + else: + proto_pb = pb + pb = pb._pb + key = None - if pb.HasField("key"): # Message field (Key) - key = key_from_protobuf(pb.key) + if "key" in proto_pb: # Message field (Key) + key = key_from_protobuf(proto_pb.key) entity_props = {} entity_meanings = {} exclude_from_indexes = [] - for prop_name, value_pb in _property_tuples(pb): + for prop_name, value_pb in _property_tuples(proto_pb): value = _get_value_from_value_pb(value_pb) entity_props[prop_name] = value @@ -211,7 +226,7 @@ def entity_to_protobuf(entity): entity_pb = entity_pb2.Entity() if entity.key is not None: key_pb = entity.key.to_protobuf() - entity_pb.key.CopyFrom(key_pb) + entity_pb._pb.key.CopyFrom(key_pb._pb) for name, value in entity.items(): value_is_list = isinstance(value, list) @@ -256,7 +271,7 @@ def get_read_options(eventual, transaction_id): if transaction_id is None: if eventual: return datastore_pb2.ReadOptions( - read_consistency=datastore_pb2.ReadOptions.EVENTUAL + read_consistency=datastore_pb2.ReadOptions.ReadConsistency.EVENTUAL ) else: return datastore_pb2.ReadOptions() @@ -368,7 +383,7 @@ def _pb_attr_value(val): return name + "_value", value -def _get_value_from_value_pb(value_pb): +def _get_value_from_value_pb(value): """Given a protobuf for a Value, get the correct value. The Cloud Datastore Protobuf API returns a Property Protobuf which @@ -386,40 +401,44 @@ def _get_value_from_value_pb(value_pb): :raises: :class:`ValueError ` if no value type has been set. """ - value_type = value_pb.WhichOneof("value_type") + if not getattr(value, "_pb", False): + # Coerce raw pb type into proto-plus pythonic type. + value = entity_pb2.Value(value) + + value_type = value._pb.WhichOneof("value_type") if value_type == "timestamp_value": - result = _pb_timestamp_to_datetime(value_pb.timestamp_value) + result = value.timestamp_value elif value_type == "key_value": - result = key_from_protobuf(value_pb.key_value) + result = key_from_protobuf(value.key_value) elif value_type == "boolean_value": - result = value_pb.boolean_value + result = value.boolean_value elif value_type == "double_value": - result = value_pb.double_value + result = value.double_value elif value_type == "integer_value": - result = value_pb.integer_value + result = value.integer_value elif value_type == "string_value": - result = value_pb.string_value + result = value.string_value elif value_type == "blob_value": - result = value_pb.blob_value + result = value.blob_value elif value_type == "entity_value": - result = entity_from_protobuf(value_pb.entity_value) + result = entity_from_protobuf(value.entity_value) elif value_type == "array_value": result = [ - _get_value_from_value_pb(value) for value in value_pb.array_value.values + _get_value_from_value_pb(value) for value in value._pb.array_value.values ] elif value_type == "geo_point_value": result = GeoPoint( - value_pb.geo_point_value.latitude, value_pb.geo_point_value.longitude + value.geo_point_value.latitude, value.geo_point_value.longitude, ) elif value_type == "null_value": @@ -450,15 +469,15 @@ def _set_protobuf_value(value_pb, val): """ attr, val = _pb_attr_value(val) if attr == "key_value": - value_pb.key_value.CopyFrom(val) + value_pb.key_value.CopyFrom(val._pb) elif attr == "timestamp_value": value_pb.timestamp_value.CopyFrom(val) elif attr == "entity_value": entity_pb = entity_to_protobuf(val) - value_pb.entity_value.CopyFrom(entity_pb) + value_pb.entity_value.CopyFrom(entity_pb._pb) elif attr == "array_value": if len(val) == 0: - array_value = entity_pb2.ArrayValue(values=[]) + array_value = entity_pb2.ArrayValue(values=[])._pb value_pb.array_value.CopyFrom(array_value) else: l_pb = value_pb.array_value.values diff --git a/google/cloud/datastore/key.py b/google/cloud/datastore/key.py index c988eebd..d03359bc 100644 --- a/google/cloud/datastore/key.py +++ b/google/cloud/datastore/key.py @@ -18,7 +18,7 @@ import copy import six -from google.cloud.datastore_v1.proto import entity_pb2 as _entity_pb2 +from google.cloud.datastore_v1.types import entity as _entity_pb2 from google.cloud._helpers import _to_bytes from google.cloud.datastore import _app_engine_key_pb2 @@ -289,14 +289,14 @@ def to_protobuf(self): key.partition_id.namespace_id = self.namespace for item in self.path: - element = key.path.add() + element = key.PathElement() if "kind" in item: element.kind = item["kind"] if "id" in item: element.id = item["id"] if "name" in item: element.name = item["name"] - + key.path.append(element) return key def to_legacy_urlsafe(self, location_prefix=None): diff --git a/google/cloud/datastore/query.py b/google/cloud/datastore/query.py index 7a4bedeb..2f455b6f 100644 --- a/google/cloud/datastore/query.py +++ b/google/cloud/datastore/query.py @@ -19,19 +19,19 @@ from google.api_core import page_iterator from google.cloud._helpers import _ensure_tuple_or_list -from google.cloud.datastore_v1.proto import entity_pb2 -from google.cloud.datastore_v1.proto import query_pb2 +from google.cloud.datastore_v1.types import entity as entity_pb2 +from google.cloud.datastore_v1.types import query as query_pb2 from google.cloud.datastore import helpers from google.cloud.datastore.key import Key -_NOT_FINISHED = query_pb2.QueryResultBatch.NOT_FINISHED -_NO_MORE_RESULTS = query_pb2.QueryResultBatch.NO_MORE_RESULTS +_NOT_FINISHED = query_pb2.QueryResultBatch.MoreResultsType.NOT_FINISHED +_NO_MORE_RESULTS = query_pb2.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS _FINISHED = ( _NO_MORE_RESULTS, - query_pb2.QueryResultBatch.MORE_RESULTS_AFTER_LIMIT, - query_pb2.QueryResultBatch.MORE_RESULTS_AFTER_CURSOR, + query_pb2.QueryResultBatch.MoreResultsType.MORE_RESULTS_AFTER_LIMIT, + query_pb2.QueryResultBatch.MoreResultsType.MORE_RESULTS_AFTER_CURSOR, ) @@ -81,11 +81,11 @@ class Query(object): """ OPERATORS = { - "<=": query_pb2.PropertyFilter.LESS_THAN_OR_EQUAL, - ">=": query_pb2.PropertyFilter.GREATER_THAN_OR_EQUAL, - "<": query_pb2.PropertyFilter.LESS_THAN, - ">": query_pb2.PropertyFilter.GREATER_THAN, - "=": query_pb2.PropertyFilter.EQUAL, + "<=": query_pb2.PropertyFilter.Operator.LESS_THAN_OR_EQUAL, + ">=": query_pb2.PropertyFilter.Operator.GREATER_THAN_OR_EQUAL, + "<": query_pb2.PropertyFilter.Operator.LESS_THAN, + ">": query_pb2.PropertyFilter.Operator.GREATER_THAN, + "=": query_pb2.PropertyFilter.Operator.EQUAL, } """Mapping of operator strings and their protobuf equivalents.""" @@ -506,7 +506,7 @@ def _build_protobuf(self): pb.end_cursor = base64.urlsafe_b64decode(end_cursor) if self.max_results is not None: - pb.limit.value = self.max_results - self.num_results + pb.limit = self.max_results - self.num_results if start_cursor is None and self._offset is not None: # NOTE: We don't need to add an offset to the request protobuf @@ -576,7 +576,13 @@ def _next_page(self): kwargs["timeout"] = self._timeout response_pb = self.client._datastore_api.run_query( - self._query.project, partition_id, read_options, query=query_pb, **kwargs + request={ + "project_id": self._query.project, + "partition_id": partition_id, + "read_options": read_options, + "query": query_pb, + }, + **kwargs, ) while ( @@ -590,13 +596,14 @@ def _next_page(self): query_pb.start_cursor = response_pb.batch.skipped_cursor query_pb.offset -= response_pb.batch.skipped_results response_pb = self.client._datastore_api.run_query( - self._query.project, - partition_id, - read_options, - query=query_pb, - **kwargs + request={ + "project_id": self._query.project, + "partition_id": partition_id, + "read_options": read_options, + "query": query_pb, + }, + **kwargs, ) - entity_pbs = self._process_query_results(response_pb) return page_iterator.Page(self, entity_pbs, self.item_to_value) @@ -615,53 +622,61 @@ def _pb_from_query(query): pb = query_pb2.Query() for projection_name in query.projection: - pb.projection.add().property.name = projection_name + projection = query_pb2.Projection() + projection.property.name = projection_name + pb.projection.append(projection) if query.kind: - pb.kind.add().name = query.kind + kind = query_pb2.KindExpression() + kind.name = query.kind + pb.kind.append(kind) composite_filter = pb.filter.composite_filter - composite_filter.op = query_pb2.CompositeFilter.AND + composite_filter.op = query_pb2.CompositeFilter.Operator.AND if query.ancestor: ancestor_pb = query.ancestor.to_protobuf() # Filter on __key__ HAS_ANCESTOR == ancestor. - ancestor_filter = composite_filter.filters.add().property_filter + ancestor_filter = composite_filter.filters._pb.add().property_filter ancestor_filter.property.name = "__key__" - ancestor_filter.op = query_pb2.PropertyFilter.HAS_ANCESTOR - ancestor_filter.value.key_value.CopyFrom(ancestor_pb) + ancestor_filter.op = query_pb2.PropertyFilter.Operator.HAS_ANCESTOR + ancestor_filter.value.key_value.CopyFrom(ancestor_pb._pb) for property_name, operator, value in query.filters: pb_op_enum = query.OPERATORS.get(operator) # Add the specific filter - property_filter = composite_filter.filters.add().property_filter + property_filter = composite_filter.filters._pb.add().property_filter property_filter.property.name = property_name property_filter.op = pb_op_enum # Set the value to filter on based on the type. if property_name == "__key__": key_pb = value.to_protobuf() - property_filter.value.key_value.CopyFrom(key_pb) + property_filter.value.key_value.CopyFrom(key_pb._pb) else: helpers._set_protobuf_value(property_filter.value, value) if not composite_filter.filters: - pb.ClearField("filter") + pb._pb.ClearField("filter") for prop in query.order: - property_order = pb.order.add() + property_order = query_pb2.PropertyOrder() if prop.startswith("-"): property_order.property.name = prop[1:] - property_order.direction = property_order.DESCENDING + property_order.direction = property_order.Direction.DESCENDING else: property_order.property.name = prop - property_order.direction = property_order.ASCENDING + property_order.direction = property_order.Direction.ASCENDING + + pb.order.append(property_order) for distinct_on_name in query.distinct_on: - pb.distinct_on.add().name = distinct_on_name + ref = query_pb2.PropertyReference() + ref.name = distinct_on_name + pb.distinct_on.append(ref) return pb diff --git a/google/cloud/datastore/transaction.py b/google/cloud/datastore/transaction.py index 705cc059..a1eabed5 100644 --- a/google/cloud/datastore/transaction.py +++ b/google/cloud/datastore/transaction.py @@ -233,7 +233,7 @@ def begin(self, retry=None, timeout=None): try: response_pb = self._client._datastore_api.begin_transaction( - self.project, **kwargs + request={"project_id": self.project}, **kwargs ) self._id = response_pb.transaction except: # noqa: E722 do not use bare except, specify exception instead @@ -262,7 +262,9 @@ def rollback(self, retry=None, timeout=None): try: # No need to use the response it contains nothing. - self._client._datastore_api.rollback(self.project, self._id, **kwargs) + self._client._datastore_api.rollback( + request={"project_id": self.project, "transaction": self._id}, **kwargs + ) finally: super(Transaction, self).rollback() # Clear our own ID in case this gets accidentally reused. @@ -311,7 +313,7 @@ def put(self, entity): :raises: :class:`RuntimeError` if the transaction is marked ReadOnly """ - if self._options.HasField("read_only"): + if "read_only" in self._options: raise RuntimeError("Transaction is read only") else: super(Transaction, self).put(entity) diff --git a/google/cloud/datastore_admin_v1/__init__.py b/google/cloud/datastore_admin_v1/__init__.py index a588c3e0..89cac8e1 100644 --- a/google/cloud/datastore_admin_v1/__init__.py +++ b/google/cloud/datastore_admin_v1/__init__.py @@ -1,45 +1,51 @@ # -*- coding: utf-8 -*- -# + # Copyright 2020 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 # -# https://www.apache.org/licenses/LICENSE-2.0 +# 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 __future__ import absolute_import -import sys -import warnings - -from google.cloud.datastore_admin_v1 import types -from google.cloud.datastore_admin_v1.gapic import datastore_admin_client -from google.cloud.datastore_admin_v1.gapic import enums - - -if sys.version_info[:2] == (2, 7): - message = ( - "A future version of this library will drop support for Python 2.7. " - "More details about Python 2 support for Google Cloud Client Libraries " - "can be found at https://cloud.google.com/python/docs/python2-sunset/" - ) - warnings.warn(message, DeprecationWarning) - - -class DatastoreAdminClient(datastore_admin_client.DatastoreAdminClient): - __doc__ = datastore_admin_client.DatastoreAdminClient.__doc__ - enums = enums +from .services.datastore_admin import DatastoreAdminClient +from .types.datastore_admin import CommonMetadata +from .types.datastore_admin import EntityFilter +from .types.datastore_admin import ExportEntitiesMetadata +from .types.datastore_admin import ExportEntitiesRequest +from .types.datastore_admin import ExportEntitiesResponse +from .types.datastore_admin import GetIndexRequest +from .types.datastore_admin import ImportEntitiesMetadata +from .types.datastore_admin import ImportEntitiesRequest +from .types.datastore_admin import IndexOperationMetadata +from .types.datastore_admin import ListIndexesRequest +from .types.datastore_admin import ListIndexesResponse +from .types.datastore_admin import OperationType +from .types.datastore_admin import Progress +from .types.index import Index __all__ = ( - "enums", - "types", + "CommonMetadata", + "EntityFilter", + "ExportEntitiesMetadata", + "ExportEntitiesRequest", + "ExportEntitiesResponse", + "GetIndexRequest", + "ImportEntitiesMetadata", + "ImportEntitiesRequest", + "Index", + "IndexOperationMetadata", + "ListIndexesRequest", + "ListIndexesResponse", + "OperationType", + "Progress", "DatastoreAdminClient", ) diff --git a/google/cloud/datastore_admin_v1/gapic/__init__.py b/google/cloud/datastore_admin_v1/gapic/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/google/cloud/datastore_admin_v1/gapic/datastore_admin_client.py b/google/cloud/datastore_admin_v1/gapic/datastore_admin_client.py deleted file mode 100644 index ffb2b030..00000000 --- a/google/cloud/datastore_admin_v1/gapic/datastore_admin_client.py +++ /dev/null @@ -1,667 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2020 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 -# -# https://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. - -"""Accesses the google.datastore.admin.v1 DatastoreAdmin API.""" - -import functools -import os -import warnings - -from google.oauth2 import service_account -import google.api_core.client_options -import google.api_core.gapic_v1.client_info -import google.api_core.gapic_v1.config -import google.api_core.gapic_v1.method -import google.api_core.gapic_v1.routing_header -import google.api_core.grpc_helpers -import google.api_core.operation -import google.api_core.operations_v1 -import google.api_core.page_iterator -import grpc - -from google.cloud.datastore_admin_v1.gapic import datastore_admin_client_config -from google.cloud.datastore_admin_v1.gapic import enums -from google.cloud.datastore_admin_v1.gapic.transports import ( - datastore_admin_grpc_transport, -) -from google.cloud.datastore_admin_v1.proto import datastore_admin_pb2 -from google.cloud.datastore_admin_v1.proto import datastore_admin_pb2_grpc -from google.cloud.datastore_admin_v1.proto import index_pb2 -from google.longrunning import operations_pb2 -from google.protobuf import empty_pb2 - -# To avoid importing datastore into admin (which would result in a -# circular dependency), We exec to get the version via a dict. -dir_path = os.path.abspath(os.path.dirname(__file__)) -version = {} -with open(os.path.join(dir_path, "../../datastore/version.py")) as fp: - exec(fp.read(), version) - -_GAPIC_LIBRARY_VERSION = version["__version__"] - - -class DatastoreAdminClient(object): - """ - Google Cloud Datastore Admin API - - - The Datastore Admin API provides several admin services for Cloud Datastore. - - ## Concepts - - Project, namespace, kind, and entity as defined in the Google Cloud Datastore - API. - - Operation: An Operation represents work being performed in the background. - - EntityFilter: Allows specifying a subset of entities in a project. This is - specified as a combination of kinds and namespaces (either or both of which - may be all). - - ## Services - - # Export/Import - - The Export/Import service provides the ability to copy all or a subset of - entities to/from Google Cloud Storage. - - Exported data may be imported into Cloud Datastore for any Google Cloud - Platform project. It is not restricted to the export source project. It is - possible to export from one project and then import into another. - - Exported data can also be loaded into Google BigQuery for analysis. - - Exports and imports are performed asynchronously. An Operation resource is - created for each export/import. The state (including any errors encountered) - of the export/import may be queried via the Operation resource. - - # Index - - The index service manages Cloud Datastore composite indexes. - - Index creation and deletion are performed asynchronously. - An Operation resource is created for each such asynchronous operation. - The state of the operation (including any errors encountered) - may be queried via the Operation resource. - - # Operation - - The Operations collection provides a record of actions performed for the - specified project (including any operations in progress). Operations are not - created directly but through calls on other collections or resources. - - An operation that is not yet done may be cancelled. The request to cancel is - asynchronous and the operation may continue to run for some time after the - request to cancel is made. - - An operation that is done may be deleted so that it is no longer listed as - part of the Operation collection. - - ListOperations returns all pending operations, but not completed operations. - - Operations are created by service DatastoreAdmin, - but are accessed via service google.longrunning.Operations. - """ - - SERVICE_ADDRESS = "datastore.googleapis.com:443" - """The default address of the service.""" - - # The name of the interface for this client. This is the key used to - # find the method configuration in the client_config dictionary. - _INTERFACE_NAME = "google.datastore.admin.v1.DatastoreAdmin" - - @classmethod - def from_service_account_file(cls, filename, *args, **kwargs): - """Creates an instance of this client using the provided credentials - file. - - Args: - filename (str): The path to the service account private key json - file. - args: Additional arguments to pass to the constructor. - kwargs: Additional arguments to pass to the constructor. - - Returns: - DatastoreAdminClient: The constructed client. - """ - credentials = service_account.Credentials.from_service_account_file(filename) - kwargs["credentials"] = credentials - return cls(*args, **kwargs) - - from_service_account_json = from_service_account_file - - def __init__( - self, - transport=None, - channel=None, - credentials=None, - client_config=None, - client_info=None, - client_options=None, - ): - """Constructor. - - Args: - transport (Union[~.DatastoreAdminGrpcTransport, - Callable[[~.Credentials, type], ~.DatastoreAdminGrpcTransport]): A transport - instance, responsible for actually making the API calls. - The default transport uses the gRPC protocol. - This argument may also be a callable which returns a - transport instance. Callables will be sent the credentials - as the first argument and the default transport class as - the second argument. - channel (grpc.Channel): DEPRECATED. A ``Channel`` instance - through which to make calls. This argument is mutually exclusive - with ``credentials``; providing both will raise an exception. - credentials (google.auth.credentials.Credentials): The - authorization credentials to attach to requests. These - credentials identify this application to the service. If none - are specified, the client will attempt to ascertain the - credentials from the environment. - This argument is mutually exclusive with providing a - transport instance to ``transport``; doing so will raise - an exception. - client_config (dict): DEPRECATED. A dictionary of call options for - each method. If not specified, the default configuration is used. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing - your own client library. - client_options (Union[dict, google.api_core.client_options.ClientOptions]): - Client options used to set user options on the client. API Endpoint - should be set through client_options. - """ - # Raise deprecation warnings for things we want to go away. - if client_config is not None: - warnings.warn( - "The `client_config` argument is deprecated.", - PendingDeprecationWarning, - stacklevel=2, - ) - else: - client_config = datastore_admin_client_config.config - - if channel: - warnings.warn( - "The `channel` argument is deprecated; use " "`transport` instead.", - PendingDeprecationWarning, - stacklevel=2, - ) - - api_endpoint = self.SERVICE_ADDRESS - if client_options: - if type(client_options) == dict: - client_options = google.api_core.client_options.from_dict( - client_options - ) - if client_options.api_endpoint: - api_endpoint = client_options.api_endpoint - - # Instantiate the transport. - # The transport is responsible for handling serialization and - # deserialization and actually sending data to the service. - if transport: - if callable(transport): - self.transport = transport( - credentials=credentials, - default_class=datastore_admin_grpc_transport.DatastoreAdminGrpcTransport, - address=api_endpoint, - ) - else: - if credentials: - raise ValueError( - "Received both a transport instance and " - "credentials; these are mutually exclusive." - ) - self.transport = transport - else: - self.transport = datastore_admin_grpc_transport.DatastoreAdminGrpcTransport( - address=api_endpoint, channel=channel, credentials=credentials, - ) - - if client_info is None: - client_info = google.api_core.gapic_v1.client_info.ClientInfo( - gapic_version=_GAPIC_LIBRARY_VERSION, - ) - else: - client_info.gapic_version = _GAPIC_LIBRARY_VERSION - self._client_info = client_info - - # Parse out the default settings for retry and timeout for each RPC - # from the client configuration. - # (Ordinarily, these are the defaults specified in the `*_config.py` - # file next to this one.) - self._method_configs = google.api_core.gapic_v1.config.parse_method_configs( - client_config["interfaces"][self._INTERFACE_NAME], - ) - - # Save a dictionary of cached API call functions. - # These are the actual callables which invoke the proper - # transport methods, wrapped with `wrap_method` to add retry, - # timeout, and the like. - self._inner_api_calls = {} - - # Service calls - def export_entities( - self, - project_id, - output_url_prefix, - labels=None, - entity_filter=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Exports a copy of all or a subset of entities from Google Cloud Datastore - to another storage system, such as Google Cloud Storage. Recent updates to - entities may not be reflected in the export. The export occurs in the - background and its progress can be monitored and managed via the - Operation resource that is created. The output of an export may only be - used once the associated operation is done. If an export operation is - cancelled before completion it may leave partial data behind in Google - Cloud Storage. - - Example: - >>> from google.cloud import datastore_admin_v1 - >>> - >>> client = datastore_admin_v1.DatastoreAdminClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> # TODO: Initialize `output_url_prefix`: - >>> output_url_prefix = '' - >>> - >>> response = client.export_entities(project_id, output_url_prefix) - >>> - >>> def callback(operation_future): - ... # Handle result. - ... result = operation_future.result() - >>> - >>> response.add_done_callback(callback) - >>> - >>> # Handle metadata. - >>> metadata = response.metadata() - - Args: - project_id (str): Required. Project ID against which to make the request. - output_url_prefix (str): Required. Location for the export metadata and data files. - - The full resource URL of the external storage location. Currently, only - Google Cloud Storage is supported. So output_url_prefix should be of the - form: ``gs://BUCKET_NAME[/NAMESPACE_PATH]``, where ``BUCKET_NAME`` is - the name of the Cloud Storage bucket and ``NAMESPACE_PATH`` is an - optional Cloud Storage namespace path (this is not a Cloud Datastore - namespace). For more information about Cloud Storage namespace paths, - see `Object name - considerations `__. - - The resulting files will be nested deeper than the specified URL prefix. - The final output URL will be provided in the - ``google.datastore.admin.v1.ExportEntitiesResponse.output_url`` field. - That value should be used for subsequent ImportEntities operations. - - By nesting the data files deeper, the same Cloud Storage bucket can be - used in multiple ExportEntities operations without conflict. - labels (dict[str -> str]): Client-assigned labels. - entity_filter (Union[dict, ~google.cloud.datastore_admin_v1.types.EntityFilter]): Description of what data from the project is included in the export. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_admin_v1.types.EntityFilter` - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_admin_v1.types._OperationFuture` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "export_entities" not in self._inner_api_calls: - self._inner_api_calls[ - "export_entities" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.export_entities, - default_retry=self._method_configs["ExportEntities"].retry, - default_timeout=self._method_configs["ExportEntities"].timeout, - client_info=self._client_info, - ) - - request = datastore_admin_pb2.ExportEntitiesRequest( - project_id=project_id, - output_url_prefix=output_url_prefix, - labels=labels, - entity_filter=entity_filter, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - operation = self._inner_api_calls["export_entities"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - return google.api_core.operation.from_gapic( - operation, - self.transport._operations_client, - datastore_admin_pb2.ExportEntitiesResponse, - metadata_type=datastore_admin_pb2.ExportEntitiesMetadata, - ) - - def import_entities( - self, - project_id, - input_url, - labels=None, - entity_filter=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Imports entities into Google Cloud Datastore. Existing entities with the - same key are overwritten. The import occurs in the background and its - progress can be monitored and managed via the Operation resource that is - created. If an ImportEntities operation is cancelled, it is possible - that a subset of the data has already been imported to Cloud Datastore. - - Example: - >>> from google.cloud import datastore_admin_v1 - >>> - >>> client = datastore_admin_v1.DatastoreAdminClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> # TODO: Initialize `input_url`: - >>> input_url = '' - >>> - >>> response = client.import_entities(project_id, input_url) - >>> - >>> def callback(operation_future): - ... # Handle result. - ... result = operation_future.result() - >>> - >>> response.add_done_callback(callback) - >>> - >>> # Handle metadata. - >>> metadata = response.metadata() - - Args: - project_id (str): Required. Project ID against which to make the request. - input_url (str): Required. The full resource URL of the external storage location. - Currently, only Google Cloud Storage is supported. So input_url should - be of the form: - ``gs://BUCKET_NAME[/NAMESPACE_PATH]/OVERALL_EXPORT_METADATA_FILE``, - where ``BUCKET_NAME`` is the name of the Cloud Storage bucket, - ``NAMESPACE_PATH`` is an optional Cloud Storage namespace path (this is - not a Cloud Datastore namespace), and ``OVERALL_EXPORT_METADATA_FILE`` - is the metadata file written by the ExportEntities operation. For more - information about Cloud Storage namespace paths, see `Object name - considerations `__. - - For more information, see - ``google.datastore.admin.v1.ExportEntitiesResponse.output_url``. - labels (dict[str -> str]): Client-assigned labels. - entity_filter (Union[dict, ~google.cloud.datastore_admin_v1.types.EntityFilter]): Optionally specify which kinds/namespaces are to be imported. If - provided, the list must be a subset of the EntityFilter used in creating - the export, otherwise a FAILED_PRECONDITION error will be returned. If - no filter is specified then all entities from the export are imported. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_admin_v1.types.EntityFilter` - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_admin_v1.types._OperationFuture` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "import_entities" not in self._inner_api_calls: - self._inner_api_calls[ - "import_entities" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.import_entities, - default_retry=self._method_configs["ImportEntities"].retry, - default_timeout=self._method_configs["ImportEntities"].timeout, - client_info=self._client_info, - ) - - request = datastore_admin_pb2.ImportEntitiesRequest( - project_id=project_id, - input_url=input_url, - labels=labels, - entity_filter=entity_filter, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - operation = self._inner_api_calls["import_entities"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - return google.api_core.operation.from_gapic( - operation, - self.transport._operations_client, - empty_pb2.Empty, - metadata_type=datastore_admin_pb2.ImportEntitiesMetadata, - ) - - def get_index( - self, - project_id=None, - index_id=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Gets an index. - - Example: - >>> from google.cloud import datastore_admin_v1 - >>> - >>> client = datastore_admin_v1.DatastoreAdminClient() - >>> - >>> response = client.get_index() - - Args: - project_id (str): Project ID against which to make the request. - index_id (str): The resource ID of the index to get. - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_admin_v1.types.Index` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "get_index" not in self._inner_api_calls: - self._inner_api_calls[ - "get_index" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.get_index, - default_retry=self._method_configs["GetIndex"].retry, - default_timeout=self._method_configs["GetIndex"].timeout, - client_info=self._client_info, - ) - - request = datastore_admin_pb2.GetIndexRequest( - project_id=project_id, index_id=index_id, - ) - return self._inner_api_calls["get_index"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - - def list_indexes( - self, - project_id=None, - filter_=None, - page_size=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Lists the indexes that match the specified filters. Datastore uses an - eventually consistent query to fetch the list of indexes and may - occasionally return stale results. - - Example: - >>> from google.cloud import datastore_admin_v1 - >>> - >>> client = datastore_admin_v1.DatastoreAdminClient() - >>> - >>> # Iterate over all results - >>> for element in client.list_indexes(): - ... # process element - ... pass - >>> - >>> - >>> # Alternatively: - >>> - >>> # Iterate over results one page at a time - >>> for page in client.list_indexes().pages: - ... for element in page: - ... # process element - ... pass - - Args: - project_id (str): Project ID against which to make the request. - filter_ (str) - page_size (int): The maximum number of resources contained in the - underlying API response. If page streaming is performed per- - resource, this parameter does not affect the return value. If page - streaming is performed per-page, this determines the maximum number - of resources in a page. - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.api_core.page_iterator.PageIterator` instance. - An iterable of :class:`~google.cloud.datastore_admin_v1.types.Index` instances. - You can also iterate over the pages of the response - using its `pages` property. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "list_indexes" not in self._inner_api_calls: - self._inner_api_calls[ - "list_indexes" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.list_indexes, - default_retry=self._method_configs["ListIndexes"].retry, - default_timeout=self._method_configs["ListIndexes"].timeout, - client_info=self._client_info, - ) - - request = datastore_admin_pb2.ListIndexesRequest( - project_id=project_id, filter=filter_, page_size=page_size, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - iterator = google.api_core.page_iterator.GRPCIterator( - client=None, - method=functools.partial( - self._inner_api_calls["list_indexes"], - retry=retry, - timeout=timeout, - metadata=metadata, - ), - request=request, - items_field="indexes", - request_token_field="page_token", - response_token_field="next_page_token", - ) - return iterator diff --git a/google/cloud/datastore_admin_v1/gapic/datastore_admin_client_config.py b/google/cloud/datastore_admin_v1/gapic/datastore_admin_client_config.py deleted file mode 100644 index dbbe2b85..00000000 --- a/google/cloud/datastore_admin_v1/gapic/datastore_admin_client_config.py +++ /dev/null @@ -1,43 +0,0 @@ -config = { - "interfaces": { - "google.datastore.admin.v1.DatastoreAdmin": { - "retry_codes": { - "idempotent": ["DEADLINE_EXCEEDED", "UNAVAILABLE"], - "non_idempotent": [], - }, - "retry_params": { - "default": { - "initial_retry_delay_millis": 100, - "retry_delay_multiplier": 1.3, - "max_retry_delay_millis": 60000, - "initial_rpc_timeout_millis": 20000, - "rpc_timeout_multiplier": 1.0, - "max_rpc_timeout_millis": 20000, - "total_timeout_millis": 600000, - } - }, - "methods": { - "ExportEntities": { - "timeout_millis": 60000, - "retry_codes_name": "non_idempotent", - "retry_params_name": "default", - }, - "ImportEntities": { - "timeout_millis": 60000, - "retry_codes_name": "non_idempotent", - "retry_params_name": "default", - }, - "GetIndex": { - "timeout_millis": 60000, - "retry_codes_name": "idempotent", - "retry_params_name": "default", - }, - "ListIndexes": { - "timeout_millis": 60000, - "retry_codes_name": "idempotent", - "retry_params_name": "default", - }, - }, - } - } -} diff --git a/google/cloud/datastore_admin_v1/gapic/enums.py b/google/cloud/datastore_admin_v1/gapic/enums.py deleted file mode 100644 index 77c303fc..00000000 --- a/google/cloud/datastore_admin_v1/gapic/enums.py +++ /dev/null @@ -1,130 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2020 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 -# -# https://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. - -"""Wrappers for protocol buffer enum types.""" - -import enum - - -class OperationType(enum.IntEnum): - """ - Operation types. - - Attributes: - OPERATION_TYPE_UNSPECIFIED (int): Unspecified. - EXPORT_ENTITIES (int): ExportEntities. - IMPORT_ENTITIES (int): ImportEntities. - CREATE_INDEX (int): CreateIndex. - DELETE_INDEX (int): DeleteIndex. - """ - - OPERATION_TYPE_UNSPECIFIED = 0 - EXPORT_ENTITIES = 1 - IMPORT_ENTITIES = 2 - CREATE_INDEX = 3 - DELETE_INDEX = 4 - - -class CommonMetadata(object): - class State(enum.IntEnum): - """ - The various possible states for an ongoing Operation. - - Attributes: - STATE_UNSPECIFIED (int): Unspecified. - INITIALIZING (int): Request is being prepared for processing. - PROCESSING (int): Request is actively being processed. - CANCELLING (int): Request is in the process of being cancelled after user called - google.longrunning.Operations.CancelOperation on the operation. - FINALIZING (int): Request has been processed and is in its finalization stage. - SUCCESSFUL (int): Request has completed successfully. - FAILED (int): Request has finished being processed, but encountered an error. - CANCELLED (int): Request has finished being cancelled after user called - google.longrunning.Operations.CancelOperation. - """ - - STATE_UNSPECIFIED = 0 - INITIALIZING = 1 - PROCESSING = 2 - CANCELLING = 3 - FINALIZING = 4 - SUCCESSFUL = 5 - FAILED = 6 - CANCELLED = 7 - - -class Index(object): - class AncestorMode(enum.IntEnum): - """ - For an ordered index, specifies whether each of the entity's ancestors - will be included. - - Attributes: - ANCESTOR_MODE_UNSPECIFIED (int): The ancestor mode is unspecified. - NONE (int): Do not include the entity's ancestors in the index. - ALL_ANCESTORS (int): Include all the entity's ancestors in the index. - """ - - ANCESTOR_MODE_UNSPECIFIED = 0 - NONE = 1 - ALL_ANCESTORS = 2 - - class Direction(enum.IntEnum): - """ - The direction determines how a property is indexed. - - Attributes: - DIRECTION_UNSPECIFIED (int): The direction is unspecified. - ASCENDING (int): The property's values are indexed so as to support sequencing in - ascending order and also query by <, >, <=, >=, and =. - DESCENDING (int): The property's values are indexed so as to support sequencing in - descending order and also query by <, >, <=, >=, and =. - """ - - DIRECTION_UNSPECIFIED = 0 - ASCENDING = 1 - DESCENDING = 2 - - class State(enum.IntEnum): - """ - The possible set of states of an index. - - Attributes: - STATE_UNSPECIFIED (int): The state is unspecified. - CREATING (int): The index is being created, and cannot be used by queries. - There is an active long-running operation for the index. - The index is updated when writing an entity. - Some index data may exist. - READY (int): The index is ready to be used. - The index is updated when writing an entity. - The index is fully populated from all stored entities it applies to. - DELETING (int): The index is being deleted, and cannot be used by queries. - There is an active long-running operation for the index. - The index is not updated when writing an entity. - Some index data may exist. - ERROR (int): The index was being created or deleted, but something went wrong. - The index cannot by used by queries. - There is no active long-running operation for the index, - and the most recently finished long-running operation failed. - The index is not updated when writing an entity. - Some index data may exist. - """ - - STATE_UNSPECIFIED = 0 - CREATING = 1 - READY = 2 - DELETING = 3 - ERROR = 4 diff --git a/google/cloud/datastore_admin_v1/gapic/transports/__init__.py b/google/cloud/datastore_admin_v1/gapic/transports/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/google/cloud/datastore_admin_v1/gapic/transports/datastore_admin_grpc_transport.py b/google/cloud/datastore_admin_v1/gapic/transports/datastore_admin_grpc_transport.py deleted file mode 100644 index 11fd92af..00000000 --- a/google/cloud/datastore_admin_v1/gapic/transports/datastore_admin_grpc_transport.py +++ /dev/null @@ -1,186 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2020 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 -# -# https://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. - - -import google.api_core.grpc_helpers -import google.api_core.operations_v1 - -from google.cloud.datastore_admin_v1.proto import datastore_admin_pb2_grpc - - -class DatastoreAdminGrpcTransport(object): - """gRPC transport class providing stubs for - google.datastore.admin.v1 DatastoreAdmin API. - - The transport provides access to the raw gRPC stubs, - which can be used to take advantage of advanced - features of gRPC. - """ - - # The scopes needed to make gRPC calls to all of the methods defined - # in this service. - _OAUTH_SCOPES = ( - "https://www.googleapis.com/auth/cloud-platform", - "https://www.googleapis.com/auth/datastore", - ) - - def __init__( - self, channel=None, credentials=None, address="datastore.googleapis.com:443" - ): - """Instantiate the transport class. - - Args: - channel (grpc.Channel): A ``Channel`` instance through - which to make calls. This argument is mutually exclusive - with ``credentials``; providing both will raise an exception. - credentials (google.auth.credentials.Credentials): The - authorization credentials to attach to requests. These - credentials identify this application to the service. If none - are specified, the client will attempt to ascertain the - credentials from the environment. - address (str): The address where the service is hosted. - """ - # If both `channel` and `credentials` are specified, raise an - # exception (channels come with credentials baked in already). - if channel is not None and credentials is not None: - raise ValueError( - "The `channel` and `credentials` arguments are mutually " "exclusive.", - ) - - # Create the channel. - if channel is None: - channel = self.create_channel( - address=address, - credentials=credentials, - options={ - "grpc.max_send_message_length": -1, - "grpc.max_receive_message_length": -1, - }.items(), - ) - - self._channel = channel - - # gRPC uses objects called "stubs" that are bound to the - # channel and provide a basic method for each RPC. - self._stubs = { - "datastore_admin_stub": datastore_admin_pb2_grpc.DatastoreAdminStub( - channel - ), - } - - # Because this API includes a method that returns a - # long-running operation (proto: google.longrunning.Operation), - # instantiate an LRO client. - self._operations_client = google.api_core.operations_v1.OperationsClient( - channel - ) - - @classmethod - def create_channel( - cls, address="datastore.googleapis.com:443", credentials=None, **kwargs - ): - """Create and return a gRPC channel object. - - Args: - address (str): The host for the channel to use. - credentials (~.Credentials): The - authorization credentials to attach to requests. These - credentials identify this application to the service. If - none are specified, the client will attempt to ascertain - the credentials from the environment. - kwargs (dict): Keyword arguments, which are passed to the - channel creation. - - Returns: - grpc.Channel: A gRPC channel object. - """ - return google.api_core.grpc_helpers.create_channel( - address, credentials=credentials, scopes=cls._OAUTH_SCOPES, **kwargs - ) - - @property - def channel(self): - """The gRPC channel used by the transport. - - Returns: - grpc.Channel: A gRPC channel object. - """ - return self._channel - - @property - def export_entities(self): - """Return the gRPC stub for :meth:`DatastoreAdminClient.export_entities`. - - Exports a copy of all or a subset of entities from Google Cloud Datastore - to another storage system, such as Google Cloud Storage. Recent updates to - entities may not be reflected in the export. The export occurs in the - background and its progress can be monitored and managed via the - Operation resource that is created. The output of an export may only be - used once the associated operation is done. If an export operation is - cancelled before completion it may leave partial data behind in Google - Cloud Storage. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_admin_stub"].ExportEntities - - @property - def import_entities(self): - """Return the gRPC stub for :meth:`DatastoreAdminClient.import_entities`. - - Imports entities into Google Cloud Datastore. Existing entities with the - same key are overwritten. The import occurs in the background and its - progress can be monitored and managed via the Operation resource that is - created. If an ImportEntities operation is cancelled, it is possible - that a subset of the data has already been imported to Cloud Datastore. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_admin_stub"].ImportEntities - - @property - def get_index(self): - """Return the gRPC stub for :meth:`DatastoreAdminClient.get_index`. - - Gets an index. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_admin_stub"].GetIndex - - @property - def list_indexes(self): - """Return the gRPC stub for :meth:`DatastoreAdminClient.list_indexes`. - - Lists the indexes that match the specified filters. Datastore uses an - eventually consistent query to fetch the list of indexes and may - occasionally return stale results. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_admin_stub"].ListIndexes diff --git a/google/cloud/datastore_admin_v1/proto/__init__.py b/google/cloud/datastore_admin_v1/proto/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/google/cloud/datastore_admin_v1/proto/datastore_admin_pb2.py b/google/cloud/datastore_admin_v1/proto/datastore_admin_pb2.py deleted file mode 100644 index f16463bb..00000000 --- a/google/cloud/datastore_admin_v1/proto/datastore_admin_pb2.py +++ /dev/null @@ -1,1847 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: google/cloud/datastore_admin_v1/proto/datastore_admin.proto -"""Generated protocol buffer code.""" -from google.protobuf.internal import enum_type_wrapper -from google.protobuf import descriptor as _descriptor -from google.protobuf import message as _message -from google.protobuf import reflection as _reflection -from google.protobuf import symbol_database as _symbol_database - -# @@protoc_insertion_point(imports) - -_sym_db = _symbol_database.Default() - - -from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2 -from google.api import client_pb2 as google_dot_api_dot_client__pb2 -from google.api import field_behavior_pb2 as google_dot_api_dot_field__behavior__pb2 -from google.cloud.datastore_admin_v1.proto import ( - index_pb2 as google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_index__pb2, -) -from google.longrunning import ( - operations_pb2 as google_dot_longrunning_dot_operations__pb2, -) -from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__pb2 - - -DESCRIPTOR = _descriptor.FileDescriptor( - name="google/cloud/datastore_admin_v1/proto/datastore_admin.proto", - package="google.datastore.admin.v1", - syntax="proto3", - serialized_options=b"\n\035com.google.datastore.admin.v1B\023DatastoreAdminProtoP\001Z>google.golang.org/genproto/googleapis/datastore/admin/v1;admin\252\002\037Google.Cloud.Datastore.Admin.V1\352\002#Google::Cloud::Datastore::Admin::V1", - create_key=_descriptor._internal_create_key, - serialized_pb=b'\n;google/cloud/datastore_admin_v1/proto/datastore_admin.proto\x12\x19google.datastore.admin.v1\x1a\x1cgoogle/api/annotations.proto\x1a\x17google/api/client.proto\x1a\x1fgoogle/api/field_behavior.proto\x1a\x31google/cloud/datastore_admin_v1/proto/index.proto\x1a#google/longrunning/operations.proto\x1a\x1fgoogle/protobuf/timestamp.proto"\xf4\x03\n\x0e\x43ommonMetadata\x12.\n\nstart_time\x18\x01 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12,\n\x08\x65nd_time\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12@\n\x0eoperation_type\x18\x03 \x01(\x0e\x32(.google.datastore.admin.v1.OperationType\x12\x45\n\x06labels\x18\x04 \x03(\x0b\x32\x35.google.datastore.admin.v1.CommonMetadata.LabelsEntry\x12>\n\x05state\x18\x05 \x01(\x0e\x32/.google.datastore.admin.v1.CommonMetadata.State\x1a-\n\x0bLabelsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01"\x8b\x01\n\x05State\x12\x15\n\x11STATE_UNSPECIFIED\x10\x00\x12\x10\n\x0cINITIALIZING\x10\x01\x12\x0e\n\nPROCESSING\x10\x02\x12\x0e\n\nCANCELLING\x10\x03\x12\x0e\n\nFINALIZING\x10\x04\x12\x0e\n\nSUCCESSFUL\x10\x05\x12\n\n\x06\x46\x41ILED\x10\x06\x12\r\n\tCANCELLED\x10\x07":\n\x08Progress\x12\x16\n\x0ework_completed\x18\x01 \x01(\x03\x12\x16\n\x0ework_estimated\x18\x02 \x01(\x03"\x8d\x02\n\x15\x45xportEntitiesRequest\x12\x17\n\nproject_id\x18\x01 \x01(\tB\x03\xe0\x41\x02\x12L\n\x06labels\x18\x02 \x03(\x0b\x32<.google.datastore.admin.v1.ExportEntitiesRequest.LabelsEntry\x12>\n\rentity_filter\x18\x03 \x01(\x0b\x32\'.google.datastore.admin.v1.EntityFilter\x12\x1e\n\x11output_url_prefix\x18\x04 \x01(\tB\x03\xe0\x41\x02\x1a-\n\x0bLabelsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01"\x85\x02\n\x15ImportEntitiesRequest\x12\x17\n\nproject_id\x18\x01 \x01(\tB\x03\xe0\x41\x02\x12L\n\x06labels\x18\x02 \x03(\x0b\x32<.google.datastore.admin.v1.ImportEntitiesRequest.LabelsEntry\x12\x16\n\tinput_url\x18\x03 \x01(\tB\x03\xe0\x41\x02\x12>\n\rentity_filter\x18\x04 \x01(\x0b\x32\'.google.datastore.admin.v1.EntityFilter\x1a-\n\x0bLabelsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t:\x02\x38\x01",\n\x16\x45xportEntitiesResponse\x12\x12\n\noutput_url\x18\x01 \x01(\t"\xab\x02\n\x16\x45xportEntitiesMetadata\x12\x39\n\x06\x63ommon\x18\x01 \x01(\x0b\x32).google.datastore.admin.v1.CommonMetadata\x12>\n\x11progress_entities\x18\x02 \x01(\x0b\x32#.google.datastore.admin.v1.Progress\x12;\n\x0eprogress_bytes\x18\x03 \x01(\x0b\x32#.google.datastore.admin.v1.Progress\x12>\n\rentity_filter\x18\x04 \x01(\x0b\x32\'.google.datastore.admin.v1.EntityFilter\x12\x19\n\x11output_url_prefix\x18\x05 \x01(\t"\xa3\x02\n\x16ImportEntitiesMetadata\x12\x39\n\x06\x63ommon\x18\x01 \x01(\x0b\x32).google.datastore.admin.v1.CommonMetadata\x12>\n\x11progress_entities\x18\x02 \x01(\x0b\x32#.google.datastore.admin.v1.Progress\x12;\n\x0eprogress_bytes\x18\x03 \x01(\x0b\x32#.google.datastore.admin.v1.Progress\x12>\n\rentity_filter\x18\x04 \x01(\x0b\x32\'.google.datastore.admin.v1.EntityFilter\x12\x11\n\tinput_url\x18\x05 \x01(\t"4\n\x0c\x45ntityFilter\x12\r\n\x05kinds\x18\x01 \x03(\t\x12\x15\n\rnamespace_ids\x18\x02 \x03(\t"7\n\x0fGetIndexRequest\x12\x12\n\nproject_id\x18\x01 \x01(\t\x12\x10\n\x08index_id\x18\x03 \x01(\t"_\n\x12ListIndexesRequest\x12\x12\n\nproject_id\x18\x01 \x01(\t\x12\x0e\n\x06\x66ilter\x18\x03 \x01(\t\x12\x11\n\tpage_size\x18\x04 \x01(\x05\x12\x12\n\npage_token\x18\x05 \x01(\t"a\n\x13ListIndexesResponse\x12\x31\n\x07indexes\x18\x01 \x03(\x0b\x32 .google.datastore.admin.v1.Index\x12\x17\n\x0fnext_page_token\x18\x02 \x01(\t"\xa5\x01\n\x16IndexOperationMetadata\x12\x39\n\x06\x63ommon\x18\x01 \x01(\x0b\x32).google.datastore.admin.v1.CommonMetadata\x12>\n\x11progress_entities\x18\x02 \x01(\x0b\x32#.google.datastore.admin.v1.Progress\x12\x10\n\x08index_id\x18\x03 \x01(\t*}\n\rOperationType\x12\x1e\n\x1aOPERATION_TYPE_UNSPECIFIED\x10\x00\x12\x13\n\x0f\x45XPORT_ENTITIES\x10\x01\x12\x13\n\x0fIMPORT_ENTITIES\x10\x02\x12\x10\n\x0c\x43REATE_INDEX\x10\x03\x12\x10\n\x0c\x44\x45LETE_INDEX\x10\x04\x32\x9c\x07\n\x0e\x44\x61tastoreAdmin\x12\xf6\x01\n\x0e\x45xportEntities\x12\x30.google.datastore.admin.v1.ExportEntitiesRequest\x1a\x1d.google.longrunning.Operation"\x92\x01\x82\xd3\xe4\x93\x02%" /v1/projects/{project_id}:export:\x01*\xda\x41\x31project_id,labels,entity_filter,output_url_prefix\xca\x41\x30\n\x16\x45xportEntitiesResponse\x12\x16\x45xportEntitiesMetadata\x12\xed\x01\n\x0eImportEntities\x12\x30.google.datastore.admin.v1.ImportEntitiesRequest\x1a\x1d.google.longrunning.Operation"\x89\x01\x82\xd3\xe4\x93\x02%" /v1/projects/{project_id}:import:\x01*\xda\x41)project_id,labels,input_url,entity_filter\xca\x41/\n\x15google.protobuf.Empty\x12\x16ImportEntitiesMetadata\x12\x8e\x01\n\x08GetIndex\x12*.google.datastore.admin.v1.GetIndexRequest\x1a .google.datastore.admin.v1.Index"4\x82\xd3\xe4\x93\x02.\x12,/v1/projects/{project_id}/indexes/{index_id}\x12\x97\x01\n\x0bListIndexes\x12-.google.datastore.admin.v1.ListIndexesRequest\x1a..google.datastore.admin.v1.ListIndexesResponse")\x82\xd3\xe4\x93\x02#\x12!/v1/projects/{project_id}/indexes\x1av\xca\x41\x18\x64\x61tastore.googleapis.com\xd2\x41Xhttps://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/datastoreB\xbe\x01\n\x1d\x63om.google.datastore.admin.v1B\x13\x44\x61tastoreAdminProtoP\x01Z>google.golang.org/genproto/googleapis/datastore/admin/v1;admin\xaa\x02\x1fGoogle.Cloud.Datastore.Admin.V1\xea\x02#Google::Cloud::Datastore::Admin::V1b\x06proto3', - dependencies=[ - google_dot_api_dot_annotations__pb2.DESCRIPTOR, - google_dot_api_dot_client__pb2.DESCRIPTOR, - google_dot_api_dot_field__behavior__pb2.DESCRIPTOR, - google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_index__pb2.DESCRIPTOR, - google_dot_longrunning_dot_operations__pb2.DESCRIPTOR, - google_dot_protobuf_dot_timestamp__pb2.DESCRIPTOR, - ], -) - -_OPERATIONTYPE = _descriptor.EnumDescriptor( - name="OperationType", - full_name="google.datastore.admin.v1.OperationType", - filename=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - values=[ - _descriptor.EnumValueDescriptor( - name="OPERATION_TYPE_UNSPECIFIED", - index=0, - number=0, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="EXPORT_ENTITIES", - index=1, - number=1, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="IMPORT_ENTITIES", - index=2, - number=2, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="CREATE_INDEX", - index=3, - number=3, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="DELETE_INDEX", - index=4, - number=4, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - ], - containing_type=None, - serialized_options=None, - serialized_start=2515, - serialized_end=2640, -) -_sym_db.RegisterEnumDescriptor(_OPERATIONTYPE) - -OperationType = enum_type_wrapper.EnumTypeWrapper(_OPERATIONTYPE) -OPERATION_TYPE_UNSPECIFIED = 0 -EXPORT_ENTITIES = 1 -IMPORT_ENTITIES = 2 -CREATE_INDEX = 3 -DELETE_INDEX = 4 - - -_COMMONMETADATA_STATE = _descriptor.EnumDescriptor( - name="State", - full_name="google.datastore.admin.v1.CommonMetadata.State", - filename=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - values=[ - _descriptor.EnumValueDescriptor( - name="STATE_UNSPECIFIED", - index=0, - number=0, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="INITIALIZING", - index=1, - number=1, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="PROCESSING", - index=2, - number=2, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="CANCELLING", - index=3, - number=3, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="FINALIZING", - index=4, - number=4, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="SUCCESSFUL", - index=5, - number=5, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="FAILED", - index=6, - number=6, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="CANCELLED", - index=7, - number=7, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - ], - containing_type=None, - serialized_options=None, - serialized_start=661, - serialized_end=800, -) -_sym_db.RegisterEnumDescriptor(_COMMONMETADATA_STATE) - - -_COMMONMETADATA_LABELSENTRY = _descriptor.Descriptor( - name="LabelsEntry", - full_name="google.datastore.admin.v1.CommonMetadata.LabelsEntry", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="key", - full_name="google.datastore.admin.v1.CommonMetadata.LabelsEntry.key", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="value", - full_name="google.datastore.admin.v1.CommonMetadata.LabelsEntry.value", - index=1, - number=2, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=b"8\001", - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=613, - serialized_end=658, -) - -_COMMONMETADATA = _descriptor.Descriptor( - name="CommonMetadata", - full_name="google.datastore.admin.v1.CommonMetadata", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="start_time", - full_name="google.datastore.admin.v1.CommonMetadata.start_time", - index=0, - number=1, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="end_time", - full_name="google.datastore.admin.v1.CommonMetadata.end_time", - index=1, - number=2, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="operation_type", - full_name="google.datastore.admin.v1.CommonMetadata.operation_type", - index=2, - number=3, - type=14, - cpp_type=8, - label=1, - has_default_value=False, - default_value=0, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="labels", - full_name="google.datastore.admin.v1.CommonMetadata.labels", - index=3, - number=4, - type=11, - cpp_type=10, - label=3, - has_default_value=False, - default_value=[], - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="state", - full_name="google.datastore.admin.v1.CommonMetadata.state", - index=4, - number=5, - type=14, - cpp_type=8, - label=1, - has_default_value=False, - default_value=0, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[_COMMONMETADATA_LABELSENTRY,], - enum_types=[_COMMONMETADATA_STATE,], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=300, - serialized_end=800, -) - - -_PROGRESS = _descriptor.Descriptor( - name="Progress", - full_name="google.datastore.admin.v1.Progress", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="work_completed", - full_name="google.datastore.admin.v1.Progress.work_completed", - index=0, - number=1, - type=3, - cpp_type=2, - label=1, - has_default_value=False, - default_value=0, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="work_estimated", - full_name="google.datastore.admin.v1.Progress.work_estimated", - index=1, - number=2, - type=3, - cpp_type=2, - label=1, - has_default_value=False, - default_value=0, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=802, - serialized_end=860, -) - - -_EXPORTENTITIESREQUEST_LABELSENTRY = _descriptor.Descriptor( - name="LabelsEntry", - full_name="google.datastore.admin.v1.ExportEntitiesRequest.LabelsEntry", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="key", - full_name="google.datastore.admin.v1.ExportEntitiesRequest.LabelsEntry.key", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="value", - full_name="google.datastore.admin.v1.ExportEntitiesRequest.LabelsEntry.value", - index=1, - number=2, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=b"8\001", - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=613, - serialized_end=658, -) - -_EXPORTENTITIESREQUEST = _descriptor.Descriptor( - name="ExportEntitiesRequest", - full_name="google.datastore.admin.v1.ExportEntitiesRequest", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="project_id", - full_name="google.datastore.admin.v1.ExportEntitiesRequest.project_id", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="labels", - full_name="google.datastore.admin.v1.ExportEntitiesRequest.labels", - index=1, - number=2, - type=11, - cpp_type=10, - label=3, - has_default_value=False, - default_value=[], - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="entity_filter", - full_name="google.datastore.admin.v1.ExportEntitiesRequest.entity_filter", - index=2, - number=3, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="output_url_prefix", - full_name="google.datastore.admin.v1.ExportEntitiesRequest.output_url_prefix", - index=3, - number=4, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[_EXPORTENTITIESREQUEST_LABELSENTRY,], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=863, - serialized_end=1132, -) - - -_IMPORTENTITIESREQUEST_LABELSENTRY = _descriptor.Descriptor( - name="LabelsEntry", - full_name="google.datastore.admin.v1.ImportEntitiesRequest.LabelsEntry", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="key", - full_name="google.datastore.admin.v1.ImportEntitiesRequest.LabelsEntry.key", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="value", - full_name="google.datastore.admin.v1.ImportEntitiesRequest.LabelsEntry.value", - index=1, - number=2, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=b"8\001", - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=613, - serialized_end=658, -) - -_IMPORTENTITIESREQUEST = _descriptor.Descriptor( - name="ImportEntitiesRequest", - full_name="google.datastore.admin.v1.ImportEntitiesRequest", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="project_id", - full_name="google.datastore.admin.v1.ImportEntitiesRequest.project_id", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="labels", - full_name="google.datastore.admin.v1.ImportEntitiesRequest.labels", - index=1, - number=2, - type=11, - cpp_type=10, - label=3, - has_default_value=False, - default_value=[], - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="input_url", - full_name="google.datastore.admin.v1.ImportEntitiesRequest.input_url", - index=2, - number=3, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="entity_filter", - full_name="google.datastore.admin.v1.ImportEntitiesRequest.entity_filter", - index=3, - number=4, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[_IMPORTENTITIESREQUEST_LABELSENTRY,], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=1135, - serialized_end=1396, -) - - -_EXPORTENTITIESRESPONSE = _descriptor.Descriptor( - name="ExportEntitiesResponse", - full_name="google.datastore.admin.v1.ExportEntitiesResponse", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="output_url", - full_name="google.datastore.admin.v1.ExportEntitiesResponse.output_url", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=1398, - serialized_end=1442, -) - - -_EXPORTENTITIESMETADATA = _descriptor.Descriptor( - name="ExportEntitiesMetadata", - full_name="google.datastore.admin.v1.ExportEntitiesMetadata", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="common", - full_name="google.datastore.admin.v1.ExportEntitiesMetadata.common", - index=0, - number=1, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="progress_entities", - full_name="google.datastore.admin.v1.ExportEntitiesMetadata.progress_entities", - index=1, - number=2, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="progress_bytes", - full_name="google.datastore.admin.v1.ExportEntitiesMetadata.progress_bytes", - index=2, - number=3, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="entity_filter", - full_name="google.datastore.admin.v1.ExportEntitiesMetadata.entity_filter", - index=3, - number=4, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="output_url_prefix", - full_name="google.datastore.admin.v1.ExportEntitiesMetadata.output_url_prefix", - index=4, - number=5, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=1445, - serialized_end=1744, -) - - -_IMPORTENTITIESMETADATA = _descriptor.Descriptor( - name="ImportEntitiesMetadata", - full_name="google.datastore.admin.v1.ImportEntitiesMetadata", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="common", - full_name="google.datastore.admin.v1.ImportEntitiesMetadata.common", - index=0, - number=1, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="progress_entities", - full_name="google.datastore.admin.v1.ImportEntitiesMetadata.progress_entities", - index=1, - number=2, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="progress_bytes", - full_name="google.datastore.admin.v1.ImportEntitiesMetadata.progress_bytes", - index=2, - number=3, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="entity_filter", - full_name="google.datastore.admin.v1.ImportEntitiesMetadata.entity_filter", - index=3, - number=4, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="input_url", - full_name="google.datastore.admin.v1.ImportEntitiesMetadata.input_url", - index=4, - number=5, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=1747, - serialized_end=2038, -) - - -_ENTITYFILTER = _descriptor.Descriptor( - name="EntityFilter", - full_name="google.datastore.admin.v1.EntityFilter", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="kinds", - full_name="google.datastore.admin.v1.EntityFilter.kinds", - index=0, - number=1, - type=9, - cpp_type=9, - label=3, - has_default_value=False, - default_value=[], - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="namespace_ids", - full_name="google.datastore.admin.v1.EntityFilter.namespace_ids", - index=1, - number=2, - type=9, - cpp_type=9, - label=3, - has_default_value=False, - default_value=[], - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=2040, - serialized_end=2092, -) - - -_GETINDEXREQUEST = _descriptor.Descriptor( - name="GetIndexRequest", - full_name="google.datastore.admin.v1.GetIndexRequest", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="project_id", - full_name="google.datastore.admin.v1.GetIndexRequest.project_id", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="index_id", - full_name="google.datastore.admin.v1.GetIndexRequest.index_id", - index=1, - number=3, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=2094, - serialized_end=2149, -) - - -_LISTINDEXESREQUEST = _descriptor.Descriptor( - name="ListIndexesRequest", - full_name="google.datastore.admin.v1.ListIndexesRequest", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="project_id", - full_name="google.datastore.admin.v1.ListIndexesRequest.project_id", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="filter", - full_name="google.datastore.admin.v1.ListIndexesRequest.filter", - index=1, - number=3, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="page_size", - full_name="google.datastore.admin.v1.ListIndexesRequest.page_size", - index=2, - number=4, - type=5, - cpp_type=1, - label=1, - has_default_value=False, - default_value=0, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="page_token", - full_name="google.datastore.admin.v1.ListIndexesRequest.page_token", - index=3, - number=5, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=2151, - serialized_end=2246, -) - - -_LISTINDEXESRESPONSE = _descriptor.Descriptor( - name="ListIndexesResponse", - full_name="google.datastore.admin.v1.ListIndexesResponse", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="indexes", - full_name="google.datastore.admin.v1.ListIndexesResponse.indexes", - index=0, - number=1, - type=11, - cpp_type=10, - label=3, - has_default_value=False, - default_value=[], - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="next_page_token", - full_name="google.datastore.admin.v1.ListIndexesResponse.next_page_token", - index=1, - number=2, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=2248, - serialized_end=2345, -) - - -_INDEXOPERATIONMETADATA = _descriptor.Descriptor( - name="IndexOperationMetadata", - full_name="google.datastore.admin.v1.IndexOperationMetadata", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="common", - full_name="google.datastore.admin.v1.IndexOperationMetadata.common", - index=0, - number=1, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="progress_entities", - full_name="google.datastore.admin.v1.IndexOperationMetadata.progress_entities", - index=1, - number=2, - type=11, - cpp_type=10, - label=1, - has_default_value=False, - default_value=None, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="index_id", - full_name="google.datastore.admin.v1.IndexOperationMetadata.index_id", - index=2, - number=3, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=2348, - serialized_end=2513, -) - -_COMMONMETADATA_LABELSENTRY.containing_type = _COMMONMETADATA -_COMMONMETADATA.fields_by_name[ - "start_time" -].message_type = google_dot_protobuf_dot_timestamp__pb2._TIMESTAMP -_COMMONMETADATA.fields_by_name[ - "end_time" -].message_type = google_dot_protobuf_dot_timestamp__pb2._TIMESTAMP -_COMMONMETADATA.fields_by_name["operation_type"].enum_type = _OPERATIONTYPE -_COMMONMETADATA.fields_by_name["labels"].message_type = _COMMONMETADATA_LABELSENTRY -_COMMONMETADATA.fields_by_name["state"].enum_type = _COMMONMETADATA_STATE -_COMMONMETADATA_STATE.containing_type = _COMMONMETADATA -_EXPORTENTITIESREQUEST_LABELSENTRY.containing_type = _EXPORTENTITIESREQUEST -_EXPORTENTITIESREQUEST.fields_by_name[ - "labels" -].message_type = _EXPORTENTITIESREQUEST_LABELSENTRY -_EXPORTENTITIESREQUEST.fields_by_name["entity_filter"].message_type = _ENTITYFILTER -_IMPORTENTITIESREQUEST_LABELSENTRY.containing_type = _IMPORTENTITIESREQUEST -_IMPORTENTITIESREQUEST.fields_by_name[ - "labels" -].message_type = _IMPORTENTITIESREQUEST_LABELSENTRY -_IMPORTENTITIESREQUEST.fields_by_name["entity_filter"].message_type = _ENTITYFILTER -_EXPORTENTITIESMETADATA.fields_by_name["common"].message_type = _COMMONMETADATA -_EXPORTENTITIESMETADATA.fields_by_name["progress_entities"].message_type = _PROGRESS -_EXPORTENTITIESMETADATA.fields_by_name["progress_bytes"].message_type = _PROGRESS -_EXPORTENTITIESMETADATA.fields_by_name["entity_filter"].message_type = _ENTITYFILTER -_IMPORTENTITIESMETADATA.fields_by_name["common"].message_type = _COMMONMETADATA -_IMPORTENTITIESMETADATA.fields_by_name["progress_entities"].message_type = _PROGRESS -_IMPORTENTITIESMETADATA.fields_by_name["progress_bytes"].message_type = _PROGRESS -_IMPORTENTITIESMETADATA.fields_by_name["entity_filter"].message_type = _ENTITYFILTER -_LISTINDEXESRESPONSE.fields_by_name[ - "indexes" -].message_type = ( - google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_index__pb2._INDEX -) -_INDEXOPERATIONMETADATA.fields_by_name["common"].message_type = _COMMONMETADATA -_INDEXOPERATIONMETADATA.fields_by_name["progress_entities"].message_type = _PROGRESS -DESCRIPTOR.message_types_by_name["CommonMetadata"] = _COMMONMETADATA -DESCRIPTOR.message_types_by_name["Progress"] = _PROGRESS -DESCRIPTOR.message_types_by_name["ExportEntitiesRequest"] = _EXPORTENTITIESREQUEST -DESCRIPTOR.message_types_by_name["ImportEntitiesRequest"] = _IMPORTENTITIESREQUEST -DESCRIPTOR.message_types_by_name["ExportEntitiesResponse"] = _EXPORTENTITIESRESPONSE -DESCRIPTOR.message_types_by_name["ExportEntitiesMetadata"] = _EXPORTENTITIESMETADATA -DESCRIPTOR.message_types_by_name["ImportEntitiesMetadata"] = _IMPORTENTITIESMETADATA -DESCRIPTOR.message_types_by_name["EntityFilter"] = _ENTITYFILTER -DESCRIPTOR.message_types_by_name["GetIndexRequest"] = _GETINDEXREQUEST -DESCRIPTOR.message_types_by_name["ListIndexesRequest"] = _LISTINDEXESREQUEST -DESCRIPTOR.message_types_by_name["ListIndexesResponse"] = _LISTINDEXESRESPONSE -DESCRIPTOR.message_types_by_name["IndexOperationMetadata"] = _INDEXOPERATIONMETADATA -DESCRIPTOR.enum_types_by_name["OperationType"] = _OPERATIONTYPE -_sym_db.RegisterFileDescriptor(DESCRIPTOR) - -CommonMetadata = _reflection.GeneratedProtocolMessageType( - "CommonMetadata", - (_message.Message,), - { - "LabelsEntry": _reflection.GeneratedProtocolMessageType( - "LabelsEntry", - (_message.Message,), - { - "DESCRIPTOR": _COMMONMETADATA_LABELSENTRY, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2" - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.CommonMetadata.LabelsEntry) - }, - ), - "DESCRIPTOR": _COMMONMETADATA, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """Metadata common to all Datastore Admin operations. - - Attributes: - start_time: - The time that work began on the operation. - end_time: - The time the operation ended, either successfully or - otherwise. - operation_type: - The type of the operation. Can be used as a filter in - ListOperationsRequest. - labels: - The client-assigned labels which were provided when the - operation was created. May also include additional labels. - state: - The current state of the Operation. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.CommonMetadata) - }, -) -_sym_db.RegisterMessage(CommonMetadata) -_sym_db.RegisterMessage(CommonMetadata.LabelsEntry) - -Progress = _reflection.GeneratedProtocolMessageType( - "Progress", - (_message.Message,), - { - "DESCRIPTOR": _PROGRESS, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """Measures the progress of a particular metric. - - Attributes: - work_completed: - The amount of work that has been completed. Note that this may - be greater than work_estimated. - work_estimated: - An estimate of how much work needs to be performed. May be - zero if the work estimate is unavailable. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.Progress) - }, -) -_sym_db.RegisterMessage(Progress) - -ExportEntitiesRequest = _reflection.GeneratedProtocolMessageType( - "ExportEntitiesRequest", - (_message.Message,), - { - "LabelsEntry": _reflection.GeneratedProtocolMessageType( - "LabelsEntry", - (_message.Message,), - { - "DESCRIPTOR": _EXPORTENTITIESREQUEST_LABELSENTRY, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2" - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ExportEntitiesRequest.LabelsEntry) - }, - ), - "DESCRIPTOR": _EXPORTENTITIESREQUEST, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """The request for [google.datastore.admin.v1.DatastoreAdmin.ExportEntiti - es][google.datastore.admin.v1.DatastoreAdmin.ExportEntities]. - - Attributes: - project_id: - Required. Project ID against which to make the request. - labels: - Client-assigned labels. - entity_filter: - Description of what data from the project is included in the - export. - output_url_prefix: - Required. Location for the export metadata and data files. - The full resource URL of the external storage location. - Currently, only Google Cloud Storage is supported. So - output_url_prefix should be of the form: - ``gs://BUCKET_NAME[/NAMESPACE_PATH]``, where ``BUCKET_NAME`` - is the name of the Cloud Storage bucket and ``NAMESPACE_PATH`` - is an optional Cloud Storage namespace path (this is not a - Cloud Datastore namespace). For more information about Cloud - Storage namespace paths, see `Object name considerations - `__. The resulting files will be nested deeper - than the specified URL prefix. The final output URL will be - provided in the [google.datastore.admin.v1.ExportEntitiesRespo - nse.output_url][google.datastore.admin.v1.ExportEntitiesRespon - se.output_url] field. That value should be used for subsequent - ImportEntities operations. By nesting the data files deeper, - the same Cloud Storage bucket can be used in multiple - ExportEntities operations without conflict. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ExportEntitiesRequest) - }, -) -_sym_db.RegisterMessage(ExportEntitiesRequest) -_sym_db.RegisterMessage(ExportEntitiesRequest.LabelsEntry) - -ImportEntitiesRequest = _reflection.GeneratedProtocolMessageType( - "ImportEntitiesRequest", - (_message.Message,), - { - "LabelsEntry": _reflection.GeneratedProtocolMessageType( - "LabelsEntry", - (_message.Message,), - { - "DESCRIPTOR": _IMPORTENTITIESREQUEST_LABELSENTRY, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2" - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ImportEntitiesRequest.LabelsEntry) - }, - ), - "DESCRIPTOR": _IMPORTENTITIESREQUEST, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """The request for [google.datastore.admin.v1.DatastoreAdmin.ImportEntiti - es][google.datastore.admin.v1.DatastoreAdmin.ImportEntities]. - - Attributes: - project_id: - Required. Project ID against which to make the request. - labels: - Client-assigned labels. - input_url: - Required. The full resource URL of the external storage - location. Currently, only Google Cloud Storage is supported. - So input_url should be of the form: ``gs://BUCKET_NAME[/NAMESP - ACE_PATH]/OVERALL_EXPORT_METADATA_FILE``, where - ``BUCKET_NAME`` is the name of the Cloud Storage bucket, - ``NAMESPACE_PATH`` is an optional Cloud Storage namespace path - (this is not a Cloud Datastore namespace), and - ``OVERALL_EXPORT_METADATA_FILE`` is the metadata file written - by the ExportEntities operation. For more information about - Cloud Storage namespace paths, see `Object name considerations - `__. For more information, see [google.datasto - re.admin.v1.ExportEntitiesResponse.output_url][google.datastor - e.admin.v1.ExportEntitiesResponse.output_url]. - entity_filter: - Optionally specify which kinds/namespaces are to be imported. - If provided, the list must be a subset of the EntityFilter - used in creating the export, otherwise a FAILED_PRECONDITION - error will be returned. If no filter is specified then all - entities from the export are imported. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ImportEntitiesRequest) - }, -) -_sym_db.RegisterMessage(ImportEntitiesRequest) -_sym_db.RegisterMessage(ImportEntitiesRequest.LabelsEntry) - -ExportEntitiesResponse = _reflection.GeneratedProtocolMessageType( - "ExportEntitiesResponse", - (_message.Message,), - { - "DESCRIPTOR": _EXPORTENTITIESRESPONSE, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """The response for [google.datastore.admin.v1.DatastoreAdmin.ExportEntit - ies][google.datastore.admin.v1.DatastoreAdmin.ExportEntities]. - - Attributes: - output_url: - Location of the output metadata file. This can be used to - begin an import into Cloud Datastore (this project or another - project). See [google.datastore.admin.v1.ImportEntitiesRequest - .input_url][google.datastore.admin.v1.ImportEntitiesRequest.in - put_url]. Only present if the operation completed - successfully. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ExportEntitiesResponse) - }, -) -_sym_db.RegisterMessage(ExportEntitiesResponse) - -ExportEntitiesMetadata = _reflection.GeneratedProtocolMessageType( - "ExportEntitiesMetadata", - (_message.Message,), - { - "DESCRIPTOR": _EXPORTENTITIESMETADATA, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """Metadata for ExportEntities operations. - - Attributes: - common: - Metadata common to all Datastore Admin operations. - progress_entities: - An estimate of the number of entities processed. - progress_bytes: - An estimate of the number of bytes processed. - entity_filter: - Description of which entities are being exported. - output_url_prefix: - Location for the export metadata and data files. This will be - the same value as the [google.datastore.admin.v1.ExportEntitie - sRequest.output_url_prefix][google.datastore.admin.v1.ExportEn - titiesRequest.output_url_prefix] field. The final output - location is provided in [google.datastore.admin.v1.ExportEntit - iesResponse.output_url][google.datastore.admin.v1.ExportEntiti - esResponse.output_url]. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ExportEntitiesMetadata) - }, -) -_sym_db.RegisterMessage(ExportEntitiesMetadata) - -ImportEntitiesMetadata = _reflection.GeneratedProtocolMessageType( - "ImportEntitiesMetadata", - (_message.Message,), - { - "DESCRIPTOR": _IMPORTENTITIESMETADATA, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """Metadata for ImportEntities operations. - - Attributes: - common: - Metadata common to all Datastore Admin operations. - progress_entities: - An estimate of the number of entities processed. - progress_bytes: - An estimate of the number of bytes processed. - entity_filter: - Description of which entities are being imported. - input_url: - The location of the import metadata file. This will be the - same value as the [google.datastore.admin.v1.ExportEntitiesRes - ponse.output_url][google.datastore.admin.v1.ExportEntitiesResp - onse.output_url] field. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ImportEntitiesMetadata) - }, -) -_sym_db.RegisterMessage(ImportEntitiesMetadata) - -EntityFilter = _reflection.GeneratedProtocolMessageType( - "EntityFilter", - (_message.Message,), - { - "DESCRIPTOR": _ENTITYFILTER, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """Identifies a subset of entities in a project. This is specified as - combinations of kinds and namespaces (either or both of which may be - all, as described in the following examples). Example usage: Entire - project: kinds=[], namespace_ids=[] Kinds Foo and Bar in all - namespaces: kinds=[‘Foo’, ‘Bar’], namespace_ids=[] Kinds Foo and Bar - only in the default namespace: kinds=[‘Foo’, ‘Bar’], - namespace_ids=[’’] Kinds Foo and Bar in both the default and Baz - namespaces: kinds=[‘Foo’, ‘Bar’], namespace_ids=[’‘, ’Baz’] The - entire Baz namespace: kinds=[], namespace_ids=[‘Baz’] - - Attributes: - kinds: - If empty, then this represents all kinds. - namespace_ids: - An empty list represents all namespaces. This is the preferred - usage for projects that don’t use namespaces. An empty string - element represents the default namespace. This should be used - if the project has data in non-default namespaces, but doesn’t - want to include them. Each namespace in this list must be - unique. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.EntityFilter) - }, -) -_sym_db.RegisterMessage(EntityFilter) - -GetIndexRequest = _reflection.GeneratedProtocolMessageType( - "GetIndexRequest", - (_message.Message,), - { - "DESCRIPTOR": _GETINDEXREQUEST, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """The request for [google.datastore.admin.v1.DatastoreAdmin.GetIndex][go - ogle.datastore.admin.v1.DatastoreAdmin.GetIndex]. - - Attributes: - project_id: - Project ID against which to make the request. - index_id: - The resource ID of the index to get. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.GetIndexRequest) - }, -) -_sym_db.RegisterMessage(GetIndexRequest) - -ListIndexesRequest = _reflection.GeneratedProtocolMessageType( - "ListIndexesRequest", - (_message.Message,), - { - "DESCRIPTOR": _LISTINDEXESREQUEST, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """The request for [google.datastore.admin.v1.DatastoreAdmin.ListIndexes] - [google.datastore.admin.v1.DatastoreAdmin.ListIndexes]. - - Attributes: - project_id: - Project ID against which to make the request. - page_size: - The maximum number of items to return. If zero, then all - results will be returned. - page_token: - The next_page_token value returned from a previous List - request, if any. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ListIndexesRequest) - }, -) -_sym_db.RegisterMessage(ListIndexesRequest) - -ListIndexesResponse = _reflection.GeneratedProtocolMessageType( - "ListIndexesResponse", - (_message.Message,), - { - "DESCRIPTOR": _LISTINDEXESRESPONSE, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """The response for [google.datastore.admin.v1.DatastoreAdmin.ListIndexes - ][google.datastore.admin.v1.DatastoreAdmin.ListIndexes]. - - Attributes: - indexes: - The indexes. - next_page_token: - The standard List next-page token. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.ListIndexesResponse) - }, -) -_sym_db.RegisterMessage(ListIndexesResponse) - -IndexOperationMetadata = _reflection.GeneratedProtocolMessageType( - "IndexOperationMetadata", - (_message.Message,), - { - "DESCRIPTOR": _INDEXOPERATIONMETADATA, - "__module__": "google.cloud.datastore_admin_v1.proto.datastore_admin_pb2", - "__doc__": """Metadata for Index operations. - - Attributes: - common: - Metadata common to all Datastore Admin operations. - progress_entities: - An estimate of the number of entities processed. - index_id: - The index resource ID that this operation is acting on. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.IndexOperationMetadata) - }, -) -_sym_db.RegisterMessage(IndexOperationMetadata) - - -DESCRIPTOR._options = None -_COMMONMETADATA_LABELSENTRY._options = None -_EXPORTENTITIESREQUEST_LABELSENTRY._options = None -_EXPORTENTITIESREQUEST.fields_by_name["project_id"]._options = None -_EXPORTENTITIESREQUEST.fields_by_name["output_url_prefix"]._options = None -_IMPORTENTITIESREQUEST_LABELSENTRY._options = None -_IMPORTENTITIESREQUEST.fields_by_name["project_id"]._options = None -_IMPORTENTITIESREQUEST.fields_by_name["input_url"]._options = None - -_DATASTOREADMIN = _descriptor.ServiceDescriptor( - name="DatastoreAdmin", - full_name="google.datastore.admin.v1.DatastoreAdmin", - file=DESCRIPTOR, - index=0, - serialized_options=b"\312A\030datastore.googleapis.com\322AXhttps://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/datastore", - create_key=_descriptor._internal_create_key, - serialized_start=2643, - serialized_end=3567, - methods=[ - _descriptor.MethodDescriptor( - name="ExportEntities", - full_name="google.datastore.admin.v1.DatastoreAdmin.ExportEntities", - index=0, - containing_service=None, - input_type=_EXPORTENTITIESREQUEST, - output_type=google_dot_longrunning_dot_operations__pb2._OPERATION, - serialized_options=b'\202\323\344\223\002%" /v1/projects/{project_id}:export:\001*\332A1project_id,labels,entity_filter,output_url_prefix\312A0\n\026ExportEntitiesResponse\022\026ExportEntitiesMetadata', - create_key=_descriptor._internal_create_key, - ), - _descriptor.MethodDescriptor( - name="ImportEntities", - full_name="google.datastore.admin.v1.DatastoreAdmin.ImportEntities", - index=1, - containing_service=None, - input_type=_IMPORTENTITIESREQUEST, - output_type=google_dot_longrunning_dot_operations__pb2._OPERATION, - serialized_options=b'\202\323\344\223\002%" /v1/projects/{project_id}:import:\001*\332A)project_id,labels,input_url,entity_filter\312A/\n\025google.protobuf.Empty\022\026ImportEntitiesMetadata', - create_key=_descriptor._internal_create_key, - ), - _descriptor.MethodDescriptor( - name="GetIndex", - full_name="google.datastore.admin.v1.DatastoreAdmin.GetIndex", - index=2, - containing_service=None, - input_type=_GETINDEXREQUEST, - output_type=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_index__pb2._INDEX, - serialized_options=b"\202\323\344\223\002.\022,/v1/projects/{project_id}/indexes/{index_id}", - create_key=_descriptor._internal_create_key, - ), - _descriptor.MethodDescriptor( - name="ListIndexes", - full_name="google.datastore.admin.v1.DatastoreAdmin.ListIndexes", - index=3, - containing_service=None, - input_type=_LISTINDEXESREQUEST, - output_type=_LISTINDEXESRESPONSE, - serialized_options=b"\202\323\344\223\002#\022!/v1/projects/{project_id}/indexes", - create_key=_descriptor._internal_create_key, - ), - ], -) -_sym_db.RegisterServiceDescriptor(_DATASTOREADMIN) - -DESCRIPTOR.services_by_name["DatastoreAdmin"] = _DATASTOREADMIN - -# @@protoc_insertion_point(module_scope) diff --git a/google/cloud/datastore_admin_v1/proto/datastore_admin_pb2_grpc.py b/google/cloud/datastore_admin_v1/proto/datastore_admin_pb2_grpc.py deleted file mode 100644 index 177889e1..00000000 --- a/google/cloud/datastore_admin_v1/proto/datastore_admin_pb2_grpc.py +++ /dev/null @@ -1,414 +0,0 @@ -# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! -"""Client and server classes corresponding to protobuf-defined services.""" -import grpc - -from google.cloud.datastore_admin_v1.proto import ( - datastore_admin_pb2 as google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2, -) -from google.cloud.datastore_admin_v1.proto import ( - index_pb2 as google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_index__pb2, -) -from google.longrunning import ( - operations_pb2 as google_dot_longrunning_dot_operations__pb2, -) - - -class DatastoreAdminStub(object): - """Google Cloud Datastore Admin API - - - The Datastore Admin API provides several admin services for Cloud Datastore. - - ----------------------------------------------------------------------------- - ## Concepts - - Project, namespace, kind, and entity as defined in the Google Cloud Datastore - API. - - Operation: An Operation represents work being performed in the background. - - EntityFilter: Allows specifying a subset of entities in a project. This is - specified as a combination of kinds and namespaces (either or both of which - may be all). - - ----------------------------------------------------------------------------- - ## Services - - # Export/Import - - The Export/Import service provides the ability to copy all or a subset of - entities to/from Google Cloud Storage. - - Exported data may be imported into Cloud Datastore for any Google Cloud - Platform project. It is not restricted to the export source project. It is - possible to export from one project and then import into another. - - Exported data can also be loaded into Google BigQuery for analysis. - - Exports and imports are performed asynchronously. An Operation resource is - created for each export/import. The state (including any errors encountered) - of the export/import may be queried via the Operation resource. - - # Index - - The index service manages Cloud Datastore composite indexes. - - Index creation and deletion are performed asynchronously. - An Operation resource is created for each such asynchronous operation. - The state of the operation (including any errors encountered) - may be queried via the Operation resource. - - # Operation - - The Operations collection provides a record of actions performed for the - specified project (including any operations in progress). Operations are not - created directly but through calls on other collections or resources. - - An operation that is not yet done may be cancelled. The request to cancel is - asynchronous and the operation may continue to run for some time after the - request to cancel is made. - - An operation that is done may be deleted so that it is no longer listed as - part of the Operation collection. - - ListOperations returns all pending operations, but not completed operations. - - Operations are created by service DatastoreAdmin, - but are accessed via service google.longrunning.Operations. - """ - - def __init__(self, channel): - """Constructor. - - Args: - channel: A grpc.Channel. - """ - self.ExportEntities = channel.unary_unary( - "/google.datastore.admin.v1.DatastoreAdmin/ExportEntities", - request_serializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ExportEntitiesRequest.SerializeToString, - response_deserializer=google_dot_longrunning_dot_operations__pb2.Operation.FromString, - ) - self.ImportEntities = channel.unary_unary( - "/google.datastore.admin.v1.DatastoreAdmin/ImportEntities", - request_serializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ImportEntitiesRequest.SerializeToString, - response_deserializer=google_dot_longrunning_dot_operations__pb2.Operation.FromString, - ) - self.GetIndex = channel.unary_unary( - "/google.datastore.admin.v1.DatastoreAdmin/GetIndex", - request_serializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.GetIndexRequest.SerializeToString, - response_deserializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_index__pb2.Index.FromString, - ) - self.ListIndexes = channel.unary_unary( - "/google.datastore.admin.v1.DatastoreAdmin/ListIndexes", - request_serializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ListIndexesRequest.SerializeToString, - response_deserializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ListIndexesResponse.FromString, - ) - - -class DatastoreAdminServicer(object): - """Google Cloud Datastore Admin API - - - The Datastore Admin API provides several admin services for Cloud Datastore. - - ----------------------------------------------------------------------------- - ## Concepts - - Project, namespace, kind, and entity as defined in the Google Cloud Datastore - API. - - Operation: An Operation represents work being performed in the background. - - EntityFilter: Allows specifying a subset of entities in a project. This is - specified as a combination of kinds and namespaces (either or both of which - may be all). - - ----------------------------------------------------------------------------- - ## Services - - # Export/Import - - The Export/Import service provides the ability to copy all or a subset of - entities to/from Google Cloud Storage. - - Exported data may be imported into Cloud Datastore for any Google Cloud - Platform project. It is not restricted to the export source project. It is - possible to export from one project and then import into another. - - Exported data can also be loaded into Google BigQuery for analysis. - - Exports and imports are performed asynchronously. An Operation resource is - created for each export/import. The state (including any errors encountered) - of the export/import may be queried via the Operation resource. - - # Index - - The index service manages Cloud Datastore composite indexes. - - Index creation and deletion are performed asynchronously. - An Operation resource is created for each such asynchronous operation. - The state of the operation (including any errors encountered) - may be queried via the Operation resource. - - # Operation - - The Operations collection provides a record of actions performed for the - specified project (including any operations in progress). Operations are not - created directly but through calls on other collections or resources. - - An operation that is not yet done may be cancelled. The request to cancel is - asynchronous and the operation may continue to run for some time after the - request to cancel is made. - - An operation that is done may be deleted so that it is no longer listed as - part of the Operation collection. - - ListOperations returns all pending operations, but not completed operations. - - Operations are created by service DatastoreAdmin, - but are accessed via service google.longrunning.Operations. - """ - - def ExportEntities(self, request, context): - """Exports a copy of all or a subset of entities from Google Cloud Datastore - to another storage system, such as Google Cloud Storage. Recent updates to - entities may not be reflected in the export. The export occurs in the - background and its progress can be monitored and managed via the - Operation resource that is created. The output of an export may only be - used once the associated operation is done. If an export operation is - cancelled before completion it may leave partial data behind in Google - Cloud Storage. - """ - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details("Method not implemented!") - raise NotImplementedError("Method not implemented!") - - def ImportEntities(self, request, context): - """Imports entities into Google Cloud Datastore. Existing entities with the - same key are overwritten. The import occurs in the background and its - progress can be monitored and managed via the Operation resource that is - created. If an ImportEntities operation is cancelled, it is possible - that a subset of the data has already been imported to Cloud Datastore. - """ - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details("Method not implemented!") - raise NotImplementedError("Method not implemented!") - - def GetIndex(self, request, context): - """Gets an index. - """ - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details("Method not implemented!") - raise NotImplementedError("Method not implemented!") - - def ListIndexes(self, request, context): - """Lists the indexes that match the specified filters. Datastore uses an - eventually consistent query to fetch the list of indexes and may - occasionally return stale results. - """ - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details("Method not implemented!") - raise NotImplementedError("Method not implemented!") - - -def add_DatastoreAdminServicer_to_server(servicer, server): - rpc_method_handlers = { - "ExportEntities": grpc.unary_unary_rpc_method_handler( - servicer.ExportEntities, - request_deserializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ExportEntitiesRequest.FromString, - response_serializer=google_dot_longrunning_dot_operations__pb2.Operation.SerializeToString, - ), - "ImportEntities": grpc.unary_unary_rpc_method_handler( - servicer.ImportEntities, - request_deserializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ImportEntitiesRequest.FromString, - response_serializer=google_dot_longrunning_dot_operations__pb2.Operation.SerializeToString, - ), - "GetIndex": grpc.unary_unary_rpc_method_handler( - servicer.GetIndex, - request_deserializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.GetIndexRequest.FromString, - response_serializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_index__pb2.Index.SerializeToString, - ), - "ListIndexes": grpc.unary_unary_rpc_method_handler( - servicer.ListIndexes, - request_deserializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ListIndexesRequest.FromString, - response_serializer=google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ListIndexesResponse.SerializeToString, - ), - } - generic_handler = grpc.method_handlers_generic_handler( - "google.datastore.admin.v1.DatastoreAdmin", rpc_method_handlers - ) - server.add_generic_rpc_handlers((generic_handler,)) - - -# This class is part of an EXPERIMENTAL API. -class DatastoreAdmin(object): - """Google Cloud Datastore Admin API - - - The Datastore Admin API provides several admin services for Cloud Datastore. - - ----------------------------------------------------------------------------- - ## Concepts - - Project, namespace, kind, and entity as defined in the Google Cloud Datastore - API. - - Operation: An Operation represents work being performed in the background. - - EntityFilter: Allows specifying a subset of entities in a project. This is - specified as a combination of kinds and namespaces (either or both of which - may be all). - - ----------------------------------------------------------------------------- - ## Services - - # Export/Import - - The Export/Import service provides the ability to copy all or a subset of - entities to/from Google Cloud Storage. - - Exported data may be imported into Cloud Datastore for any Google Cloud - Platform project. It is not restricted to the export source project. It is - possible to export from one project and then import into another. - - Exported data can also be loaded into Google BigQuery for analysis. - - Exports and imports are performed asynchronously. An Operation resource is - created for each export/import. The state (including any errors encountered) - of the export/import may be queried via the Operation resource. - - # Index - - The index service manages Cloud Datastore composite indexes. - - Index creation and deletion are performed asynchronously. - An Operation resource is created for each such asynchronous operation. - The state of the operation (including any errors encountered) - may be queried via the Operation resource. - - # Operation - - The Operations collection provides a record of actions performed for the - specified project (including any operations in progress). Operations are not - created directly but through calls on other collections or resources. - - An operation that is not yet done may be cancelled. The request to cancel is - asynchronous and the operation may continue to run for some time after the - request to cancel is made. - - An operation that is done may be deleted so that it is no longer listed as - part of the Operation collection. - - ListOperations returns all pending operations, but not completed operations. - - Operations are created by service DatastoreAdmin, - but are accessed via service google.longrunning.Operations. - """ - - @staticmethod - def ExportEntities( - request, - target, - options=(), - channel_credentials=None, - call_credentials=None, - compression=None, - wait_for_ready=None, - timeout=None, - metadata=None, - ): - return grpc.experimental.unary_unary( - request, - target, - "/google.datastore.admin.v1.DatastoreAdmin/ExportEntities", - google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ExportEntitiesRequest.SerializeToString, - google_dot_longrunning_dot_operations__pb2.Operation.FromString, - options, - channel_credentials, - call_credentials, - compression, - wait_for_ready, - timeout, - metadata, - ) - - @staticmethod - def ImportEntities( - request, - target, - options=(), - channel_credentials=None, - call_credentials=None, - compression=None, - wait_for_ready=None, - timeout=None, - metadata=None, - ): - return grpc.experimental.unary_unary( - request, - target, - "/google.datastore.admin.v1.DatastoreAdmin/ImportEntities", - google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ImportEntitiesRequest.SerializeToString, - google_dot_longrunning_dot_operations__pb2.Operation.FromString, - options, - channel_credentials, - call_credentials, - compression, - wait_for_ready, - timeout, - metadata, - ) - - @staticmethod - def GetIndex( - request, - target, - options=(), - channel_credentials=None, - call_credentials=None, - compression=None, - wait_for_ready=None, - timeout=None, - metadata=None, - ): - return grpc.experimental.unary_unary( - request, - target, - "/google.datastore.admin.v1.DatastoreAdmin/GetIndex", - google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.GetIndexRequest.SerializeToString, - google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_index__pb2.Index.FromString, - options, - channel_credentials, - call_credentials, - compression, - wait_for_ready, - timeout, - metadata, - ) - - @staticmethod - def ListIndexes( - request, - target, - options=(), - channel_credentials=None, - call_credentials=None, - compression=None, - wait_for_ready=None, - timeout=None, - metadata=None, - ): - return grpc.experimental.unary_unary( - request, - target, - "/google.datastore.admin.v1.DatastoreAdmin/ListIndexes", - google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ListIndexesRequest.SerializeToString, - google_dot_cloud_dot_datastore__admin__v1_dot_proto_dot_datastore__admin__pb2.ListIndexesResponse.FromString, - options, - channel_credentials, - call_credentials, - compression, - wait_for_ready, - timeout, - metadata, - ) diff --git a/google/cloud/datastore_admin_v1/proto/index_pb2.py b/google/cloud/datastore_admin_v1/proto/index_pb2.py deleted file mode 100644 index c1ccb034..00000000 --- a/google/cloud/datastore_admin_v1/proto/index_pb2.py +++ /dev/null @@ -1,430 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: google/cloud/datastore_admin_v1/proto/index.proto -"""Generated protocol buffer code.""" -from google.protobuf import descriptor as _descriptor -from google.protobuf import message as _message -from google.protobuf import reflection as _reflection -from google.protobuf import symbol_database as _symbol_database - -# @@protoc_insertion_point(imports) - -_sym_db = _symbol_database.Default() - - -from google.api import field_behavior_pb2 as google_dot_api_dot_field__behavior__pb2 -from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2 - - -DESCRIPTOR = _descriptor.FileDescriptor( - name="google/cloud/datastore_admin_v1/proto/index.proto", - package="google.datastore.admin.v1", - syntax="proto3", - serialized_options=b"\n\035com.google.datastore.admin.v1B\nIndexProtoP\001Z>google.golang.org/genproto/googleapis/datastore/admin/v1;admin\252\002\037Google.Cloud.Datastore.Admin.V1\352\002#Google::Cloud::Datastore::Admin::V1", - create_key=_descriptor._internal_create_key, - serialized_pb=b'\n1google/cloud/datastore_admin_v1/proto/index.proto\x12\x19google.datastore.admin.v1\x1a\x1fgoogle/api/field_behavior.proto\x1a\x1cgoogle/api/annotations.proto"\xe6\x04\n\x05Index\x12\x17\n\nproject_id\x18\x01 \x01(\tB\x03\xe0\x41\x03\x12\x15\n\x08index_id\x18\x03 \x01(\tB\x03\xe0\x41\x03\x12\x11\n\x04kind\x18\x04 \x01(\tB\x03\xe0\x41\x02\x12\x44\n\x08\x61ncestor\x18\x05 \x01(\x0e\x32-.google.datastore.admin.v1.Index.AncestorModeB\x03\xe0\x41\x02\x12I\n\nproperties\x18\x06 \x03(\x0b\x32\x30.google.datastore.admin.v1.Index.IndexedPropertyB\x03\xe0\x41\x02\x12:\n\x05state\x18\x07 \x01(\x0e\x32&.google.datastore.admin.v1.Index.StateB\x03\xe0\x41\x03\x1ah\n\x0fIndexedProperty\x12\x11\n\x04name\x18\x01 \x01(\tB\x03\xe0\x41\x02\x12\x42\n\tdirection\x18\x02 \x01(\x0e\x32*.google.datastore.admin.v1.Index.DirectionB\x03\xe0\x41\x02"J\n\x0c\x41ncestorMode\x12\x1d\n\x19\x41NCESTOR_MODE_UNSPECIFIED\x10\x00\x12\x08\n\x04NONE\x10\x01\x12\x11\n\rALL_ANCESTORS\x10\x02"E\n\tDirection\x12\x19\n\x15\x44IRECTION_UNSPECIFIED\x10\x00\x12\r\n\tASCENDING\x10\x01\x12\x0e\n\nDESCENDING\x10\x02"P\n\x05State\x12\x15\n\x11STATE_UNSPECIFIED\x10\x00\x12\x0c\n\x08\x43REATING\x10\x01\x12\t\n\x05READY\x10\x02\x12\x0c\n\x08\x44\x45LETING\x10\x03\x12\t\n\x05\x45RROR\x10\x04\x42\xb5\x01\n\x1d\x63om.google.datastore.admin.v1B\nIndexProtoP\x01Z>google.golang.org/genproto/googleapis/datastore/admin/v1;admin\xaa\x02\x1fGoogle.Cloud.Datastore.Admin.V1\xea\x02#Google::Cloud::Datastore::Admin::V1b\x06proto3', - dependencies=[ - google_dot_api_dot_field__behavior__pb2.DESCRIPTOR, - google_dot_api_dot_annotations__pb2.DESCRIPTOR, - ], -) - - -_INDEX_ANCESTORMODE = _descriptor.EnumDescriptor( - name="AncestorMode", - full_name="google.datastore.admin.v1.Index.AncestorMode", - filename=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - values=[ - _descriptor.EnumValueDescriptor( - name="ANCESTOR_MODE_UNSPECIFIED", - index=0, - number=0, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="NONE", - index=1, - number=1, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="ALL_ANCESTORS", - index=2, - number=2, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - ], - containing_type=None, - serialized_options=None, - serialized_start=531, - serialized_end=605, -) -_sym_db.RegisterEnumDescriptor(_INDEX_ANCESTORMODE) - -_INDEX_DIRECTION = _descriptor.EnumDescriptor( - name="Direction", - full_name="google.datastore.admin.v1.Index.Direction", - filename=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - values=[ - _descriptor.EnumValueDescriptor( - name="DIRECTION_UNSPECIFIED", - index=0, - number=0, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="ASCENDING", - index=1, - number=1, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="DESCENDING", - index=2, - number=2, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - ], - containing_type=None, - serialized_options=None, - serialized_start=607, - serialized_end=676, -) -_sym_db.RegisterEnumDescriptor(_INDEX_DIRECTION) - -_INDEX_STATE = _descriptor.EnumDescriptor( - name="State", - full_name="google.datastore.admin.v1.Index.State", - filename=None, - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - values=[ - _descriptor.EnumValueDescriptor( - name="STATE_UNSPECIFIED", - index=0, - number=0, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="CREATING", - index=1, - number=1, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="READY", - index=2, - number=2, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="DELETING", - index=3, - number=3, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - _descriptor.EnumValueDescriptor( - name="ERROR", - index=4, - number=4, - serialized_options=None, - type=None, - create_key=_descriptor._internal_create_key, - ), - ], - containing_type=None, - serialized_options=None, - serialized_start=678, - serialized_end=758, -) -_sym_db.RegisterEnumDescriptor(_INDEX_STATE) - - -_INDEX_INDEXEDPROPERTY = _descriptor.Descriptor( - name="IndexedProperty", - full_name="google.datastore.admin.v1.Index.IndexedProperty", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="name", - full_name="google.datastore.admin.v1.Index.IndexedProperty.name", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="direction", - full_name="google.datastore.admin.v1.Index.IndexedProperty.direction", - index=1, - number=2, - type=14, - cpp_type=8, - label=1, - has_default_value=False, - default_value=0, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[], - enum_types=[], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=425, - serialized_end=529, -) - -_INDEX = _descriptor.Descriptor( - name="Index", - full_name="google.datastore.admin.v1.Index", - filename=None, - file=DESCRIPTOR, - containing_type=None, - create_key=_descriptor._internal_create_key, - fields=[ - _descriptor.FieldDescriptor( - name="project_id", - full_name="google.datastore.admin.v1.Index.project_id", - index=0, - number=1, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\003", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="index_id", - full_name="google.datastore.admin.v1.Index.index_id", - index=1, - number=3, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\003", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="kind", - full_name="google.datastore.admin.v1.Index.kind", - index=2, - number=4, - type=9, - cpp_type=9, - label=1, - has_default_value=False, - default_value=b"".decode("utf-8"), - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="ancestor", - full_name="google.datastore.admin.v1.Index.ancestor", - index=3, - number=5, - type=14, - cpp_type=8, - label=1, - has_default_value=False, - default_value=0, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="properties", - full_name="google.datastore.admin.v1.Index.properties", - index=4, - number=6, - type=11, - cpp_type=10, - label=3, - has_default_value=False, - default_value=[], - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\002", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - _descriptor.FieldDescriptor( - name="state", - full_name="google.datastore.admin.v1.Index.state", - index=5, - number=7, - type=14, - cpp_type=8, - label=1, - has_default_value=False, - default_value=0, - message_type=None, - enum_type=None, - containing_type=None, - is_extension=False, - extension_scope=None, - serialized_options=b"\340A\003", - file=DESCRIPTOR, - create_key=_descriptor._internal_create_key, - ), - ], - extensions=[], - nested_types=[_INDEX_INDEXEDPROPERTY,], - enum_types=[_INDEX_ANCESTORMODE, _INDEX_DIRECTION, _INDEX_STATE,], - serialized_options=None, - is_extendable=False, - syntax="proto3", - extension_ranges=[], - oneofs=[], - serialized_start=144, - serialized_end=758, -) - -_INDEX_INDEXEDPROPERTY.fields_by_name["direction"].enum_type = _INDEX_DIRECTION -_INDEX_INDEXEDPROPERTY.containing_type = _INDEX -_INDEX.fields_by_name["ancestor"].enum_type = _INDEX_ANCESTORMODE -_INDEX.fields_by_name["properties"].message_type = _INDEX_INDEXEDPROPERTY -_INDEX.fields_by_name["state"].enum_type = _INDEX_STATE -_INDEX_ANCESTORMODE.containing_type = _INDEX -_INDEX_DIRECTION.containing_type = _INDEX -_INDEX_STATE.containing_type = _INDEX -DESCRIPTOR.message_types_by_name["Index"] = _INDEX -_sym_db.RegisterFileDescriptor(DESCRIPTOR) - -Index = _reflection.GeneratedProtocolMessageType( - "Index", - (_message.Message,), - { - "IndexedProperty": _reflection.GeneratedProtocolMessageType( - "IndexedProperty", - (_message.Message,), - { - "DESCRIPTOR": _INDEX_INDEXEDPROPERTY, - "__module__": "google.cloud.datastore_admin_v1.proto.index_pb2", - "__doc__": """A property of an index. - - Attributes: - name: - Required. The property name to index. - direction: - Required. The indexed property’s direction. Must not be - DIRECTION_UNSPECIFIED. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.Index.IndexedProperty) - }, - ), - "DESCRIPTOR": _INDEX, - "__module__": "google.cloud.datastore_admin_v1.proto.index_pb2", - "__doc__": """A minimal index definition. - - Attributes: - project_id: - Output only. Project ID. - index_id: - Output only. The resource ID of the index. - kind: - Required. The entity kind to which this index applies. - ancestor: - Required. The index’s ancestor mode. Must not be - ANCESTOR_MODE_UNSPECIFIED. - properties: - Required. An ordered sequence of property names and their - index attributes. - state: - Output only. The state of the index. - """, - # @@protoc_insertion_point(class_scope:google.datastore.admin.v1.Index) - }, -) -_sym_db.RegisterMessage(Index) -_sym_db.RegisterMessage(Index.IndexedProperty) - - -DESCRIPTOR._options = None -_INDEX_INDEXEDPROPERTY.fields_by_name["name"]._options = None -_INDEX_INDEXEDPROPERTY.fields_by_name["direction"]._options = None -_INDEX.fields_by_name["project_id"]._options = None -_INDEX.fields_by_name["index_id"]._options = None -_INDEX.fields_by_name["kind"]._options = None -_INDEX.fields_by_name["ancestor"]._options = None -_INDEX.fields_by_name["properties"]._options = None -_INDEX.fields_by_name["state"]._options = None -# @@protoc_insertion_point(module_scope) diff --git a/google/cloud/datastore_admin_v1/proto/index_pb2_grpc.py b/google/cloud/datastore_admin_v1/proto/index_pb2_grpc.py deleted file mode 100644 index 8a939394..00000000 --- a/google/cloud/datastore_admin_v1/proto/index_pb2_grpc.py +++ /dev/null @@ -1,3 +0,0 @@ -# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! -"""Client and server classes corresponding to protobuf-defined services.""" -import grpc diff --git a/google/cloud/datastore_admin_v1/py.typed b/google/cloud/datastore_admin_v1/py.typed new file mode 100644 index 00000000..dc48a544 --- /dev/null +++ b/google/cloud/datastore_admin_v1/py.typed @@ -0,0 +1,2 @@ +# Marker file for PEP 561. +# The google-cloud-datastore-admin package uses inline types. diff --git a/google/cloud/datastore_admin_v1/services/__init__.py b/google/cloud/datastore_admin_v1/services/__init__.py new file mode 100644 index 00000000..42ffdf2b --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/__init__.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# diff --git a/google/cloud/datastore_admin_v1/services/datastore_admin/__init__.py b/google/cloud/datastore_admin_v1/services/datastore_admin/__init__.py new file mode 100644 index 00000000..a004406b --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/datastore_admin/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 .client import DatastoreAdminClient +from .async_client import DatastoreAdminAsyncClient + +__all__ = ( + "DatastoreAdminClient", + "DatastoreAdminAsyncClient", +) diff --git a/google/cloud/datastore_admin_v1/services/datastore_admin/async_client.py b/google/cloud/datastore_admin_v1/services/datastore_admin/async_client.py new file mode 100644 index 00000000..fd9589b6 --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/datastore_admin/async_client.py @@ -0,0 +1,564 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 collections import OrderedDict +import functools +import re +from typing import Dict, Sequence, Tuple, Type, Union +import pkg_resources + +import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import exceptions # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google.api_core import retry as retries # type: ignore +from google.auth import credentials # type: ignore +from google.oauth2 import service_account # type: ignore + +from google.api_core import operation +from google.api_core import operation_async +from google.cloud.datastore_admin_v1.services.datastore_admin import pagers +from google.cloud.datastore_admin_v1.types import datastore_admin +from google.cloud.datastore_admin_v1.types import index +from google.protobuf import empty_pb2 as empty # type: ignore + +from .transports.base import DatastoreAdminTransport, DEFAULT_CLIENT_INFO +from .transports.grpc_asyncio import DatastoreAdminGrpcAsyncIOTransport +from .client import DatastoreAdminClient + + +class DatastoreAdminAsyncClient: + """Google Cloud Datastore Admin API + The Datastore Admin API provides several admin services for + Cloud Datastore. + ----------------------------------------------------------------------------- + ## Concepts + + Project, namespace, kind, and entity as defined in the Google + Cloud Datastore API. + + Operation: An Operation represents work being performed in the + background. + EntityFilter: Allows specifying a subset of entities in a + project. This is specified as a combination of kinds and + namespaces (either or both of which may be all). + + ----------------------------------------------------------------------------- + ## Services + + # Export/Import + + The Export/Import service provides the ability to copy all or a + subset of entities to/from Google Cloud Storage. + + Exported data may be imported into Cloud Datastore for any + Google Cloud Platform project. It is not restricted to the + export source project. It is possible to export from one project + and then import into another. + Exported data can also be loaded into Google BigQuery for + analysis. + Exports and imports are performed asynchronously. An Operation + resource is created for each export/import. The state (including + any errors encountered) of the export/import may be queried via + the Operation resource. + # Index + + The index service manages Cloud Datastore composite indexes. + Index creation and deletion are performed asynchronously. An + Operation resource is created for each such asynchronous + operation. The state of the operation (including any errors + encountered) may be queried via the Operation resource. + + # Operation + + The Operations collection provides a record of actions performed + for the specified project (including any operations in + progress). Operations are not created directly but through calls + on other collections or resources. + An operation that is not yet done may be cancelled. The request + to cancel is asynchronous and the operation may continue to run + for some time after the request to cancel is made. + + An operation that is done may be deleted so that it is no longer + listed as part of the Operation collection. + + ListOperations returns all pending operations, but not completed + operations. + Operations are created by service DatastoreAdmin, + but are accessed via service google.longrunning.Operations. + """ + + _client: DatastoreAdminClient + + DEFAULT_ENDPOINT = DatastoreAdminClient.DEFAULT_ENDPOINT + DEFAULT_MTLS_ENDPOINT = DatastoreAdminClient.DEFAULT_MTLS_ENDPOINT + + from_service_account_file = DatastoreAdminClient.from_service_account_file + from_service_account_json = from_service_account_file + + get_transport_class = functools.partial( + type(DatastoreAdminClient).get_transport_class, type(DatastoreAdminClient) + ) + + def __init__( + self, + *, + credentials: credentials.Credentials = None, + transport: Union[str, DatastoreAdminTransport] = "grpc_asyncio", + client_options: ClientOptions = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiate the datastore admin client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Union[str, ~.DatastoreAdminTransport]): The + transport to use. If set to None, a transport is chosen + automatically. + client_options (ClientOptions): Custom options for the client. It + won't take effect if a ``transport`` instance is provided. + (1) The ``api_endpoint`` property can be used to override the + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT + environment variable can also be used to override the endpoint: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + """ + + self._client = DatastoreAdminClient( + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, + ) + + async def export_entities( + self, + request: datastore_admin.ExportEntitiesRequest = None, + *, + project_id: str = None, + labels: Sequence[datastore_admin.ExportEntitiesRequest.LabelsEntry] = None, + entity_filter: datastore_admin.EntityFilter = None, + output_url_prefix: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation_async.AsyncOperation: + r"""Exports a copy of all or a subset of entities from + Google Cloud Datastore to another storage system, such + as Google Cloud Storage. Recent updates to entities may + not be reflected in the export. The export occurs in the + background and its progress can be monitored and managed + via the Operation resource that is created. The output + of an export may only be used once the associated + operation is done. If an export operation is cancelled + before completion it may leave partial data behind in + Google Cloud Storage. + + Args: + request (:class:`~.datastore_admin.ExportEntitiesRequest`): + The request object. The request for + [google.datastore.admin.v1.DatastoreAdmin.ExportEntities][google.datastore.admin.v1.DatastoreAdmin.ExportEntities]. + project_id (:class:`str`): + Required. Project ID against which to + make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + labels (:class:`Sequence[~.datastore_admin.ExportEntitiesRequest.LabelsEntry]`): + Client-assigned labels. + This corresponds to the ``labels`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + entity_filter (:class:`~.datastore_admin.EntityFilter`): + Description of what data from the + project is included in the export. + This corresponds to the ``entity_filter`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + output_url_prefix (:class:`str`): + Required. Location for the export metadata and data + files. + + The full resource URL of the external storage location. + Currently, only Google Cloud Storage is supported. So + output_url_prefix should be of the form: + ``gs://BUCKET_NAME[/NAMESPACE_PATH]``, where + ``BUCKET_NAME`` is the name of the Cloud Storage bucket + and ``NAMESPACE_PATH`` is an optional Cloud Storage + namespace path (this is not a Cloud Datastore + namespace). For more information about Cloud Storage + namespace paths, see `Object name + considerations `__. + + The resulting files will be nested deeper than the + specified URL prefix. The final output URL will be + provided in the + [google.datastore.admin.v1.ExportEntitiesResponse.output_url][google.datastore.admin.v1.ExportEntitiesResponse.output_url] + field. That value should be used for subsequent + ImportEntities operations. + + By nesting the data files deeper, the same Cloud Storage + bucket can be used in multiple ExportEntities operations + without conflict. + This corresponds to the ``output_url_prefix`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be + :class:``~.datastore_admin.ExportEntitiesResponse``: The + response for + [google.datastore.admin.v1.DatastoreAdmin.ExportEntities][google.datastore.admin.v1.DatastoreAdmin.ExportEntities]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + if request is not None and any( + [project_id, labels, entity_filter, output_url_prefix] + ): + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + request = datastore_admin.ExportEntitiesRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if labels is not None: + request.labels = labels + if entity_filter is not None: + request.entity_filter = entity_filter + if output_url_prefix is not None: + request.output_url_prefix = output_url_prefix + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.export_entities, + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + datastore_admin.ExportEntitiesResponse, + metadata_type=datastore_admin.ExportEntitiesMetadata, + ) + + # Done; return the response. + return response + + async def import_entities( + self, + request: datastore_admin.ImportEntitiesRequest = None, + *, + project_id: str = None, + labels: Sequence[datastore_admin.ImportEntitiesRequest.LabelsEntry] = None, + input_url: str = None, + entity_filter: datastore_admin.EntityFilter = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation_async.AsyncOperation: + r"""Imports entities into Google Cloud Datastore. + Existing entities with the same key are overwritten. The + import occurs in the background and its progress can be + monitored and managed via the Operation resource that is + created. If an ImportEntities operation is cancelled, it + is possible that a subset of the data has already been + imported to Cloud Datastore. + + Args: + request (:class:`~.datastore_admin.ImportEntitiesRequest`): + The request object. The request for + [google.datastore.admin.v1.DatastoreAdmin.ImportEntities][google.datastore.admin.v1.DatastoreAdmin.ImportEntities]. + project_id (:class:`str`): + Required. Project ID against which to + make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + labels (:class:`Sequence[~.datastore_admin.ImportEntitiesRequest.LabelsEntry]`): + Client-assigned labels. + This corresponds to the ``labels`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + input_url (:class:`str`): + Required. The full resource URL of the external storage + location. Currently, only Google Cloud Storage is + supported. So input_url should be of the form: + ``gs://BUCKET_NAME[/NAMESPACE_PATH]/OVERALL_EXPORT_METADATA_FILE``, + where ``BUCKET_NAME`` is the name of the Cloud Storage + bucket, ``NAMESPACE_PATH`` is an optional Cloud Storage + namespace path (this is not a Cloud Datastore + namespace), and ``OVERALL_EXPORT_METADATA_FILE`` is the + metadata file written by the ExportEntities operation. + For more information about Cloud Storage namespace + paths, see `Object name + considerations `__. + + For more information, see + [google.datastore.admin.v1.ExportEntitiesResponse.output_url][google.datastore.admin.v1.ExportEntitiesResponse.output_url]. + This corresponds to the ``input_url`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + entity_filter (:class:`~.datastore_admin.EntityFilter`): + Optionally specify which kinds/namespaces are to be + imported. If provided, the list must be a subset of the + EntityFilter used in creating the export, otherwise a + FAILED_PRECONDITION error will be returned. If no filter + is specified then all entities from the export are + imported. + This corresponds to the ``entity_filter`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operation_async.AsyncOperation: + An object representing a long-running operation. + + The result type for the operation will be + :class:``~.empty.Empty``: A generic empty message that + you can re-use to avoid defining duplicated empty + messages in your APIs. A typical example is to use it as + the request or the response type of an API method. For + instance: + + :: + + service Foo { + rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty); + } + + The JSON representation for ``Empty`` is empty JSON + object ``{}``. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + if request is not None and any([project_id, labels, input_url, entity_filter]): + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + request = datastore_admin.ImportEntitiesRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if labels is not None: + request.labels = labels + if input_url is not None: + request.input_url = input_url + if entity_filter is not None: + request.entity_filter = entity_filter + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.import_entities, + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Wrap the response in an operation future. + response = operation_async.from_gapic( + response, + self._client._transport.operations_client, + empty.Empty, + metadata_type=datastore_admin.ImportEntitiesMetadata, + ) + + # Done; return the response. + return response + + async def get_index( + self, + request: datastore_admin.GetIndexRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> index.Index: + r"""Gets an index. + + Args: + request (:class:`~.datastore_admin.GetIndexRequest`): + The request object. The request for + [google.datastore.admin.v1.DatastoreAdmin.GetIndex][google.datastore.admin.v1.DatastoreAdmin.GetIndex]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.index.Index: + A minimal index definition. + """ + # Create or coerce a protobuf request object. + + request = datastore_admin.GetIndexRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.get_index, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.ServiceUnavailable, exceptions.DeadlineExceeded, + ), + ), + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def list_indexes( + self, + request: datastore_admin.ListIndexesRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> pagers.ListIndexesAsyncPager: + r"""Lists the indexes that match the specified filters. + Datastore uses an eventually consistent query to fetch + the list of indexes and may occasionally return stale + results. + + Args: + request (:class:`~.datastore_admin.ListIndexesRequest`): + The request object. The request for + [google.datastore.admin.v1.DatastoreAdmin.ListIndexes][google.datastore.admin.v1.DatastoreAdmin.ListIndexes]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.pagers.ListIndexesAsyncPager: + The response for + [google.datastore.admin.v1.DatastoreAdmin.ListIndexes][google.datastore.admin.v1.DatastoreAdmin.ListIndexes]. + + Iterating over this object will yield results and + resolve additional pages automatically. + + """ + # Create or coerce a protobuf request object. + + request = datastore_admin.ListIndexesRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.list_indexes, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.ServiceUnavailable, exceptions.DeadlineExceeded, + ), + ), + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # This method is paged; wrap the response in a pager, which provides + # an `__aiter__` convenience method. + response = pagers.ListIndexesAsyncPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + + # Done; return the response. + return response + + +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution( + "google-cloud-datastore-admin", + ).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + +__all__ = ("DatastoreAdminAsyncClient",) diff --git a/google/cloud/datastore_admin_v1/services/datastore_admin/client.py b/google/cloud/datastore_admin_v1/services/datastore_admin/client.py new file mode 100644 index 00000000..0ebed21e --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/datastore_admin/client.py @@ -0,0 +1,700 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 collections import OrderedDict +from distutils import util +import os +import re +from typing import Callable, Dict, Sequence, Tuple, Type, Union +import pkg_resources + +import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import exceptions # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google.api_core import retry as retries # type: ignore +from google.auth import credentials # type: ignore +from google.auth.transport import mtls # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth.exceptions import MutualTLSChannelError # type: ignore +from google.oauth2 import service_account # type: ignore + +from google.api_core import operation +from google.api_core import operation_async +from google.cloud.datastore_admin_v1.services.datastore_admin import pagers +from google.cloud.datastore_admin_v1.types import datastore_admin +from google.cloud.datastore_admin_v1.types import index +from google.protobuf import empty_pb2 as empty # type: ignore + +from .transports.base import DatastoreAdminTransport, DEFAULT_CLIENT_INFO +from .transports.grpc import DatastoreAdminGrpcTransport +from .transports.grpc_asyncio import DatastoreAdminGrpcAsyncIOTransport + + +class DatastoreAdminClientMeta(type): + """Metaclass for the DatastoreAdmin client. + + This provides class-level methods for building and retrieving + support objects (e.g. transport) without polluting the client instance + objects. + """ + + _transport_registry = ( + OrderedDict() + ) # type: Dict[str, Type[DatastoreAdminTransport]] + _transport_registry["grpc"] = DatastoreAdminGrpcTransport + _transport_registry["grpc_asyncio"] = DatastoreAdminGrpcAsyncIOTransport + + def get_transport_class(cls, label: str = None,) -> Type[DatastoreAdminTransport]: + """Return an appropriate transport class. + + Args: + label: The name of the desired transport. If none is + provided, then the first transport in the registry is used. + + Returns: + The transport class to use. + """ + # If a specific transport is requested, return that one. + if label: + return cls._transport_registry[label] + + # No transport is requested; return the default (that is, the first one + # in the dictionary). + return next(iter(cls._transport_registry.values())) + + +class DatastoreAdminClient(metaclass=DatastoreAdminClientMeta): + """Google Cloud Datastore Admin API + The Datastore Admin API provides several admin services for + Cloud Datastore. + ----------------------------------------------------------------------------- + ## Concepts + + Project, namespace, kind, and entity as defined in the Google + Cloud Datastore API. + + Operation: An Operation represents work being performed in the + background. + EntityFilter: Allows specifying a subset of entities in a + project. This is specified as a combination of kinds and + namespaces (either or both of which may be all). + + ## Services + + # Export/Import + + The Export/Import service provides the ability to copy all or a + subset of entities to/from Google Cloud Storage. + + Exported data may be imported into Cloud Datastore for any + Google Cloud Platform project. It is not restricted to the + export source project. It is possible to export from one project + and then import into another. + Exported data can also be loaded into Google BigQuery for + analysis. + Exports and imports are performed asynchronously. An Operation + resource is created for each export/import. The state (including + any errors encountered) of the export/import may be queried via + the Operation resource. + # Index + + The index service manages Cloud Datastore composite indexes. + Index creation and deletion are performed asynchronously. An + Operation resource is created for each such asynchronous + operation. The state of the operation (including any errors + encountered) may be queried via the Operation resource. + + # Operation + + The Operations collection provides a record of actions performed + for the specified project (including any operations in + progress). Operations are not created directly but through calls + on other collections or resources. + An operation that is not yet done may be cancelled. The request + to cancel is asynchronous and the operation may continue to run + for some time after the request to cancel is made. + + An operation that is done may be deleted so that it is no longer + listed as part of the Operation collection. + + ListOperations returns all pending operations, but not completed + operations. + Operations are created by service DatastoreAdmin, + but are accessed via service google.longrunning.Operations. + """ + + @staticmethod + def _get_default_mtls_endpoint(api_endpoint): + """Convert api endpoint to mTLS endpoint. + Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to + "*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively. + Args: + api_endpoint (Optional[str]): the api endpoint to convert. + Returns: + str: converted mTLS api endpoint. + """ + if not api_endpoint: + return api_endpoint + + mtls_endpoint_re = re.compile( + r"(?P[^.]+)(?P\.mtls)?(?P\.sandbox)?(?P\.googleapis\.com)?" + ) + + m = mtls_endpoint_re.match(api_endpoint) + name, mtls, sandbox, googledomain = m.groups() + if mtls or not googledomain: + return api_endpoint + + if sandbox: + return api_endpoint.replace( + "sandbox.googleapis.com", "mtls.sandbox.googleapis.com" + ) + + return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com") + + DEFAULT_ENDPOINT = "datastore.googleapis.com" + DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint.__func__( # type: ignore + DEFAULT_ENDPOINT + ) + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + {@api.name}: The constructed client. + """ + credentials = service_account.Credentials.from_service_account_file(filename) + kwargs["credentials"] = credentials + return cls(*args, **kwargs) + + from_service_account_json = from_service_account_file + + def __init__( + self, + *, + credentials: credentials.Credentials = None, + transport: Union[str, DatastoreAdminTransport] = None, + client_options: ClientOptions = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiate the datastore admin client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Union[str, ~.DatastoreAdminTransport]): The + transport to use. If set to None, a transport is chosen + automatically. + client_options (ClientOptions): Custom options for the client. It + won't take effect if a ``transport`` instance is provided. + (1) The ``api_endpoint`` property can be used to override the + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT + environment variable can also be used to override the endpoint: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + """ + if isinstance(client_options, dict): + client_options = ClientOptions.from_dict(client_options) + if client_options is None: + client_options = ClientOptions.ClientOptions() + + # Create SSL credentials for mutual TLS if needed. + use_client_cert = bool( + util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) + ) + + ssl_credentials = None + is_mtls = False + if use_client_cert: + if client_options.client_cert_source: + import grpc # type: ignore + + cert, key = client_options.client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + is_mtls = True + else: + creds = SslCredentials() + is_mtls = creds.is_mtls + ssl_credentials = creds.ssl_credentials if is_mtls else None + + # Figure out which api endpoint to use. + if client_options.api_endpoint is not None: + api_endpoint = client_options.api_endpoint + else: + use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") + if use_mtls_env == "never": + api_endpoint = self.DEFAULT_ENDPOINT + elif use_mtls_env == "always": + api_endpoint = self.DEFAULT_MTLS_ENDPOINT + elif use_mtls_env == "auto": + api_endpoint = ( + self.DEFAULT_MTLS_ENDPOINT if is_mtls else self.DEFAULT_ENDPOINT + ) + else: + raise MutualTLSChannelError( + "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always" + ) + + # Save or instantiate the transport. + # Ordinarily, we provide the transport, but allowing a custom transport + # instance provides an extensibility point for unusual situations. + if isinstance(transport, DatastoreAdminTransport): + # transport is a DatastoreAdminTransport instance. + if credentials or client_options.credentials_file: + raise ValueError( + "When providing a transport instance, " + "provide its credentials directly." + ) + if client_options.scopes: + raise ValueError( + "When providing a transport instance, " + "provide its scopes directly." + ) + self._transport = transport + else: + Transport = type(self).get_transport_class(transport) + self._transport = Transport( + credentials=credentials, + credentials_file=client_options.credentials_file, + host=api_endpoint, + scopes=client_options.scopes, + ssl_channel_credentials=ssl_credentials, + quota_project_id=client_options.quota_project_id, + client_info=client_info, + ) + + def export_entities( + self, + request: datastore_admin.ExportEntitiesRequest = None, + *, + project_id: str = None, + labels: Sequence[datastore_admin.ExportEntitiesRequest.LabelsEntry] = None, + entity_filter: datastore_admin.EntityFilter = None, + output_url_prefix: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation.Operation: + r"""Exports a copy of all or a subset of entities from + Google Cloud Datastore to another storage system, such + as Google Cloud Storage. Recent updates to entities may + not be reflected in the export. The export occurs in the + background and its progress can be monitored and managed + via the Operation resource that is created. The output + of an export may only be used once the associated + operation is done. If an export operation is cancelled + before completion it may leave partial data behind in + Google Cloud Storage. + + Args: + request (:class:`~.datastore_admin.ExportEntitiesRequest`): + The request object. The request for + [google.datastore.admin.v1.DatastoreAdmin.ExportEntities][google.datastore.admin.v1.DatastoreAdmin.ExportEntities]. + project_id (:class:`str`): + Required. Project ID against which to + make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + labels (:class:`Sequence[~.datastore_admin.ExportEntitiesRequest.LabelsEntry]`): + Client-assigned labels. + This corresponds to the ``labels`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + entity_filter (:class:`~.datastore_admin.EntityFilter`): + Description of what data from the + project is included in the export. + This corresponds to the ``entity_filter`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + output_url_prefix (:class:`str`): + Required. Location for the export metadata and data + files. + + The full resource URL of the external storage location. + Currently, only Google Cloud Storage is supported. So + output_url_prefix should be of the form: + ``gs://BUCKET_NAME[/NAMESPACE_PATH]``, where + ``BUCKET_NAME`` is the name of the Cloud Storage bucket + and ``NAMESPACE_PATH`` is an optional Cloud Storage + namespace path (this is not a Cloud Datastore + namespace). For more information about Cloud Storage + namespace paths, see `Object name + considerations `__. + + The resulting files will be nested deeper than the + specified URL prefix. The final output URL will be + provided in the + [google.datastore.admin.v1.ExportEntitiesResponse.output_url][google.datastore.admin.v1.ExportEntitiesResponse.output_url] + field. That value should be used for subsequent + ImportEntities operations. + + By nesting the data files deeper, the same Cloud Storage + bucket can be used in multiple ExportEntities operations + without conflict. + This corresponds to the ``output_url_prefix`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operation.Operation: + An object representing a long-running operation. + + The result type for the operation will be + :class:``~.datastore_admin.ExportEntitiesResponse``: The + response for + [google.datastore.admin.v1.DatastoreAdmin.ExportEntities][google.datastore.admin.v1.DatastoreAdmin.ExportEntities]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any( + [project_id, labels, entity_filter, output_url_prefix] + ) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a datastore_admin.ExportEntitiesRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore_admin.ExportEntitiesRequest): + request = datastore_admin.ExportEntitiesRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if labels is not None: + request.labels = labels + if entity_filter is not None: + request.entity_filter = entity_filter + if output_url_prefix is not None: + request.output_url_prefix = output_url_prefix + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.export_entities] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Wrap the response in an operation future. + response = operation.from_gapic( + response, + self._transport.operations_client, + datastore_admin.ExportEntitiesResponse, + metadata_type=datastore_admin.ExportEntitiesMetadata, + ) + + # Done; return the response. + return response + + def import_entities( + self, + request: datastore_admin.ImportEntitiesRequest = None, + *, + project_id: str = None, + labels: Sequence[datastore_admin.ImportEntitiesRequest.LabelsEntry] = None, + input_url: str = None, + entity_filter: datastore_admin.EntityFilter = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operation.Operation: + r"""Imports entities into Google Cloud Datastore. + Existing entities with the same key are overwritten. The + import occurs in the background and its progress can be + monitored and managed via the Operation resource that is + created. If an ImportEntities operation is cancelled, it + is possible that a subset of the data has already been + imported to Cloud Datastore. + + Args: + request (:class:`~.datastore_admin.ImportEntitiesRequest`): + The request object. The request for + [google.datastore.admin.v1.DatastoreAdmin.ImportEntities][google.datastore.admin.v1.DatastoreAdmin.ImportEntities]. + project_id (:class:`str`): + Required. Project ID against which to + make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + labels (:class:`Sequence[~.datastore_admin.ImportEntitiesRequest.LabelsEntry]`): + Client-assigned labels. + This corresponds to the ``labels`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + input_url (:class:`str`): + Required. The full resource URL of the external storage + location. Currently, only Google Cloud Storage is + supported. So input_url should be of the form: + ``gs://BUCKET_NAME[/NAMESPACE_PATH]/OVERALL_EXPORT_METADATA_FILE``, + where ``BUCKET_NAME`` is the name of the Cloud Storage + bucket, ``NAMESPACE_PATH`` is an optional Cloud Storage + namespace path (this is not a Cloud Datastore + namespace), and ``OVERALL_EXPORT_METADATA_FILE`` is the + metadata file written by the ExportEntities operation. + For more information about Cloud Storage namespace + paths, see `Object name + considerations `__. + + For more information, see + [google.datastore.admin.v1.ExportEntitiesResponse.output_url][google.datastore.admin.v1.ExportEntitiesResponse.output_url]. + This corresponds to the ``input_url`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + entity_filter (:class:`~.datastore_admin.EntityFilter`): + Optionally specify which kinds/namespaces are to be + imported. If provided, the list must be a subset of the + EntityFilter used in creating the export, otherwise a + FAILED_PRECONDITION error will be returned. If no filter + is specified then all entities from the export are + imported. + This corresponds to the ``entity_filter`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.operation.Operation: + An object representing a long-running operation. + + The result type for the operation will be + :class:``~.empty.Empty``: A generic empty message that + you can re-use to avoid defining duplicated empty + messages in your APIs. A typical example is to use it as + the request or the response type of an API method. For + instance: + + :: + + service Foo { + rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty); + } + + The JSON representation for ``Empty`` is empty JSON + object ``{}``. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([project_id, labels, input_url, entity_filter]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a datastore_admin.ImportEntitiesRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore_admin.ImportEntitiesRequest): + request = datastore_admin.ImportEntitiesRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if labels is not None: + request.labels = labels + if input_url is not None: + request.input_url = input_url + if entity_filter is not None: + request.entity_filter = entity_filter + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.import_entities] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Wrap the response in an operation future. + response = operation.from_gapic( + response, + self._transport.operations_client, + empty.Empty, + metadata_type=datastore_admin.ImportEntitiesMetadata, + ) + + # Done; return the response. + return response + + def get_index( + self, + request: datastore_admin.GetIndexRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> index.Index: + r"""Gets an index. + + Args: + request (:class:`~.datastore_admin.GetIndexRequest`): + The request object. The request for + [google.datastore.admin.v1.DatastoreAdmin.GetIndex][google.datastore.admin.v1.DatastoreAdmin.GetIndex]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.index.Index: + A minimal index definition. + """ + # Create or coerce a protobuf request object. + + # Minor optimization to avoid making a copy if the user passes + # in a datastore_admin.GetIndexRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore_admin.GetIndexRequest): + request = datastore_admin.GetIndexRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.get_index] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def list_indexes( + self, + request: datastore_admin.ListIndexesRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> pagers.ListIndexesPager: + r"""Lists the indexes that match the specified filters. + Datastore uses an eventually consistent query to fetch + the list of indexes and may occasionally return stale + results. + + Args: + request (:class:`~.datastore_admin.ListIndexesRequest`): + The request object. The request for + [google.datastore.admin.v1.DatastoreAdmin.ListIndexes][google.datastore.admin.v1.DatastoreAdmin.ListIndexes]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.pagers.ListIndexesPager: + The response for + [google.datastore.admin.v1.DatastoreAdmin.ListIndexes][google.datastore.admin.v1.DatastoreAdmin.ListIndexes]. + + Iterating over this object will yield results and + resolve additional pages automatically. + + """ + # Create or coerce a protobuf request object. + + # Minor optimization to avoid making a copy if the user passes + # in a datastore_admin.ListIndexesRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore_admin.ListIndexesRequest): + request = datastore_admin.ListIndexesRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.list_indexes] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListIndexesPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + + # Done; return the response. + return response + + +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution( + "google-cloud-datastore-admin", + ).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + +__all__ = ("DatastoreAdminClient",) diff --git a/google/cloud/datastore_admin_v1/services/datastore_admin/pagers.py b/google/cloud/datastore_admin_v1/services/datastore_admin/pagers.py new file mode 100644 index 00000000..7c176fce --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/datastore_admin/pagers.py @@ -0,0 +1,149 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 typing import Any, AsyncIterable, Awaitable, Callable, Iterable, Sequence, Tuple + +from google.cloud.datastore_admin_v1.types import datastore_admin +from google.cloud.datastore_admin_v1.types import index + + +class ListIndexesPager: + """A pager for iterating through ``list_indexes`` requests. + + This class thinly wraps an initial + :class:`~.datastore_admin.ListIndexesResponse` object, and + provides an ``__iter__`` method to iterate through its + ``indexes`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListIndexes`` requests and continue to iterate + through the ``indexes`` field on the + corresponding responses. + + All the usual :class:`~.datastore_admin.ListIndexesResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., datastore_admin.ListIndexesResponse], + request: datastore_admin.ListIndexesRequest, + response: datastore_admin.ListIndexesResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (:class:`~.datastore_admin.ListIndexesRequest`): + The initial request object. + response (:class:`~.datastore_admin.ListIndexesResponse`): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = datastore_admin.ListIndexesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[datastore_admin.ListIndexesResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[index.Index]: + for page in self.pages: + yield from page.indexes + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListIndexesAsyncPager: + """A pager for iterating through ``list_indexes`` requests. + + This class thinly wraps an initial + :class:`~.datastore_admin.ListIndexesResponse` object, and + provides an ``__aiter__`` method to iterate through its + ``indexes`` field. + + If there are more pages, the ``__aiter__`` method will make additional + ``ListIndexes`` requests and continue to iterate + through the ``indexes`` field on the + corresponding responses. + + All the usual :class:`~.datastore_admin.ListIndexesResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., Awaitable[datastore_admin.ListIndexesResponse]], + request: datastore_admin.ListIndexesRequest, + response: datastore_admin.ListIndexesResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (:class:`~.datastore_admin.ListIndexesRequest`): + The initial request object. + response (:class:`~.datastore_admin.ListIndexesResponse`): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = datastore_admin.ListIndexesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + async def pages(self) -> AsyncIterable[datastore_admin.ListIndexesResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = await self._method(self._request, metadata=self._metadata) + yield self._response + + def __aiter__(self) -> AsyncIterable[index.Index]: + async def async_generator(): + async for page in self.pages: + for response in page.indexes: + yield response + + return async_generator() + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/google/cloud/datastore_admin_v1/services/datastore_admin/transports/__init__.py b/google/cloud/datastore_admin_v1/services/datastore_admin/transports/__init__.py new file mode 100644 index 00000000..41b72bc3 --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/datastore_admin/transports/__init__.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 collections import OrderedDict +from typing import Dict, Type + +from .base import DatastoreAdminTransport +from .grpc import DatastoreAdminGrpcTransport +from .grpc_asyncio import DatastoreAdminGrpcAsyncIOTransport + + +# Compile a registry of transports. +_transport_registry = OrderedDict() # type: Dict[str, Type[DatastoreAdminTransport]] +_transport_registry["grpc"] = DatastoreAdminGrpcTransport +_transport_registry["grpc_asyncio"] = DatastoreAdminGrpcAsyncIOTransport + + +__all__ = ( + "DatastoreAdminTransport", + "DatastoreAdminGrpcTransport", + "DatastoreAdminGrpcAsyncIOTransport", +) diff --git a/google/cloud/datastore_admin_v1/services/datastore_admin/transports/base.py b/google/cloud/datastore_admin_v1/services/datastore_admin/transports/base.py new file mode 100644 index 00000000..6049d546 --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/datastore_admin/transports/base.py @@ -0,0 +1,194 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import abc +import typing +import pkg_resources + +from google import auth # type: ignore +from google.api_core import exceptions # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google.api_core import retry as retries # type: ignore +from google.api_core import operations_v1 # type: ignore +from google.auth import credentials # type: ignore + +from google.cloud.datastore_admin_v1.types import datastore_admin +from google.cloud.datastore_admin_v1.types import index +from google.longrunning import operations_pb2 as operations # type: ignore + + +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution( + "google-cloud-datastore-admin", + ).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + +class DatastoreAdminTransport(abc.ABC): + """Abstract transport class for DatastoreAdmin.""" + + AUTH_SCOPES = ( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ) + + def __init__( + self, + *, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: typing.Optional[str] = None, + scopes: typing.Optional[typing.Sequence[str]] = AUTH_SCOPES, + quota_project_id: typing.Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + **kwargs, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scope (Optional[Sequence[str]]): A list of scopes. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + """ + # Save the hostname. Default to port 443 (HTTPS) if none is specified. + if ":" not in host: + host += ":443" + self._host = host + + # If no credentials are provided, then determine the appropriate + # defaults. + if credentials and credentials_file: + raise exceptions.DuplicateCredentialArgs( + "'credentials_file' and 'credentials' are mutually exclusive" + ) + + if credentials_file is not None: + credentials, _ = auth.load_credentials_from_file( + credentials_file, scopes=scopes, quota_project_id=quota_project_id + ) + + elif credentials is None: + credentials, _ = auth.default( + scopes=scopes, quota_project_id=quota_project_id + ) + + # Save the credentials. + self._credentials = credentials + + # Lifted into its own function so it can be stubbed out during tests. + self._prep_wrapped_messages(client_info) + + def _prep_wrapped_messages(self, client_info): + # Precompute the wrapped methods. + self._wrapped_methods = { + self.export_entities: gapic_v1.method.wrap_method( + self.export_entities, default_timeout=60.0, client_info=client_info, + ), + self.import_entities: gapic_v1.method.wrap_method( + self.import_entities, default_timeout=60.0, client_info=client_info, + ), + self.get_index: gapic_v1.method.wrap_method( + self.get_index, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.ServiceUnavailable, exceptions.DeadlineExceeded, + ), + ), + default_timeout=60.0, + client_info=client_info, + ), + self.list_indexes: gapic_v1.method.wrap_method( + self.list_indexes, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.ServiceUnavailable, exceptions.DeadlineExceeded, + ), + ), + default_timeout=60.0, + client_info=client_info, + ), + } + + @property + def operations_client(self) -> operations_v1.OperationsClient: + """Return the client designed to process long-running operations.""" + raise NotImplementedError() + + @property + def export_entities( + self, + ) -> typing.Callable[ + [datastore_admin.ExportEntitiesRequest], + typing.Union[operations.Operation, typing.Awaitable[operations.Operation]], + ]: + raise NotImplementedError() + + @property + def import_entities( + self, + ) -> typing.Callable[ + [datastore_admin.ImportEntitiesRequest], + typing.Union[operations.Operation, typing.Awaitable[operations.Operation]], + ]: + raise NotImplementedError() + + @property + def get_index( + self, + ) -> typing.Callable[ + [datastore_admin.GetIndexRequest], + typing.Union[index.Index, typing.Awaitable[index.Index]], + ]: + raise NotImplementedError() + + @property + def list_indexes( + self, + ) -> typing.Callable[ + [datastore_admin.ListIndexesRequest], + typing.Union[ + datastore_admin.ListIndexesResponse, + typing.Awaitable[datastore_admin.ListIndexesResponse], + ], + ]: + raise NotImplementedError() + + +__all__ = ("DatastoreAdminTransport",) diff --git a/google/cloud/datastore_admin_v1/services/datastore_admin/transports/grpc.py b/google/cloud/datastore_admin_v1/services/datastore_admin/transports/grpc.py new file mode 100644 index 00000000..b478b75a --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/datastore_admin/transports/grpc.py @@ -0,0 +1,431 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import warnings +from typing import Callable, Dict, Optional, Sequence, Tuple + +from google.api_core import grpc_helpers # type: ignore +from google.api_core import operations_v1 # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google import auth # type: ignore +from google.auth import credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore + +import grpc # type: ignore + +from google.cloud.datastore_admin_v1.types import datastore_admin +from google.cloud.datastore_admin_v1.types import index +from google.longrunning import operations_pb2 as operations # type: ignore + +from .base import DatastoreAdminTransport, DEFAULT_CLIENT_INFO + + +class DatastoreAdminGrpcTransport(DatastoreAdminTransport): + """gRPC backend transport for DatastoreAdmin. + + Google Cloud Datastore Admin API + The Datastore Admin API provides several admin services for + Cloud Datastore. + ----------------------------------------------------------------------------- + ## Concepts + + Project, namespace, kind, and entity as defined in the Google + Cloud Datastore API. + + Operation: An Operation represents work being performed in the + background. + EntityFilter: Allows specifying a subset of entities in a + project. This is specified as a combination of kinds and + namespaces (either or both of which may be all). + + ----------------------------------------------------------------------------- + ## Services + + # Export/Import + + The Export/Import service provides the ability to copy all or a + subset of entities to/from Google Cloud Storage. + + Exported data may be imported into Cloud Datastore for any + Google Cloud Platform project. It is not restricted to the + export source project. It is possible to export from one project + and then import into another. + Exported data can also be loaded into Google BigQuery for + analysis. + Exports and imports are performed asynchronously. An Operation + resource is created for each export/import. The state (including + any errors encountered) of the export/import may be queried via + the Operation resource. + # Index + + The index service manages Cloud Datastore composite indexes. + Index creation and deletion are performed asynchronously. An + Operation resource is created for each such asynchronous + operation. The state of the operation (including any errors + encountered) may be queried via the Operation resource. + + # Operation + + The Operations collection provides a record of actions performed + for the specified project (including any operations in + progress). Operations are not created directly but through calls + on other collections or resources. + An operation that is not yet done may be cancelled. The request + to cancel is asynchronous and the operation may continue to run + for some time after the request to cancel is made. + + An operation that is done may be deleted so that it is no longer + listed as part of the Operation collection. + + ListOperations returns all pending operations, but not completed + operations. + Operations are created by service DatastoreAdmin, + but are accessed via service google.longrunning.Operations. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _stubs: Dict[str, Callable] + + def __init__( + self, + *, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: str = None, + scopes: Sequence[str] = None, + channel: grpc.Channel = None, + api_mtls_endpoint: str = None, + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if ``channel`` is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if ``channel`` is provided. + channel (Optional[grpc.Channel]): A ``Channel`` instance through + which to make calls. + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create + a mutual TLS channel with client SSL credentials from + ``client_cert_source`` or applicatin default SSL credentials. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + if channel: + # Sanity check: Ensure that channel and credentials are not both + # provided. + credentials = False + + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + + host = ( + api_mtls_endpoint + if ":" in api_mtls_endpoint + else api_mtls_endpoint + ":443" + ) + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + ssl_credentials = SslCredentials().ssl_credentials + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) + + self._stubs = {} # type: Dict[str, Callable] + + # Run the base constructor. + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, + ) + + @classmethod + def create_channel( + cls, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: str = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> grpc.Channel: + """Create and return a gRPC channel object. + Args: + address (Optionsl[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + grpc.Channel: A gRPC channel object. + + Raises: + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + scopes = scopes or cls.AUTH_SCOPES + return grpc_helpers.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + **kwargs, + ) + + @property + def grpc_channel(self) -> grpc.Channel: + """Create the channel designed to connect to this service. + + This property caches on the instance; repeated calls return + the same channel. + """ + # Return the channel from cache. + return self._grpc_channel + + @property + def operations_client(self) -> operations_v1.OperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Sanity check: Only create a new client if we do not already have one. + if "operations_client" not in self.__dict__: + self.__dict__["operations_client"] = operations_v1.OperationsClient( + self.grpc_channel + ) + + # Return the client from cache. + return self.__dict__["operations_client"] + + @property + def export_entities( + self, + ) -> Callable[[datastore_admin.ExportEntitiesRequest], operations.Operation]: + r"""Return a callable for the export entities method over gRPC. + + Exports a copy of all or a subset of entities from + Google Cloud Datastore to another storage system, such + as Google Cloud Storage. Recent updates to entities may + not be reflected in the export. The export occurs in the + background and its progress can be monitored and managed + via the Operation resource that is created. The output + of an export may only be used once the associated + operation is done. If an export operation is cancelled + before completion it may leave partial data behind in + Google Cloud Storage. + + Returns: + Callable[[~.ExportEntitiesRequest], + ~.Operation]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "export_entities" not in self._stubs: + self._stubs["export_entities"] = self.grpc_channel.unary_unary( + "/google.datastore.admin.v1.DatastoreAdmin/ExportEntities", + request_serializer=datastore_admin.ExportEntitiesRequest.serialize, + response_deserializer=operations.Operation.FromString, + ) + return self._stubs["export_entities"] + + @property + def import_entities( + self, + ) -> Callable[[datastore_admin.ImportEntitiesRequest], operations.Operation]: + r"""Return a callable for the import entities method over gRPC. + + Imports entities into Google Cloud Datastore. + Existing entities with the same key are overwritten. The + import occurs in the background and its progress can be + monitored and managed via the Operation resource that is + created. If an ImportEntities operation is cancelled, it + is possible that a subset of the data has already been + imported to Cloud Datastore. + + Returns: + Callable[[~.ImportEntitiesRequest], + ~.Operation]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "import_entities" not in self._stubs: + self._stubs["import_entities"] = self.grpc_channel.unary_unary( + "/google.datastore.admin.v1.DatastoreAdmin/ImportEntities", + request_serializer=datastore_admin.ImportEntitiesRequest.serialize, + response_deserializer=operations.Operation.FromString, + ) + return self._stubs["import_entities"] + + @property + def get_index(self) -> Callable[[datastore_admin.GetIndexRequest], index.Index]: + r"""Return a callable for the get index method over gRPC. + + Gets an index. + + Returns: + Callable[[~.GetIndexRequest], + ~.Index]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "get_index" not in self._stubs: + self._stubs["get_index"] = self.grpc_channel.unary_unary( + "/google.datastore.admin.v1.DatastoreAdmin/GetIndex", + request_serializer=datastore_admin.GetIndexRequest.serialize, + response_deserializer=index.Index.deserialize, + ) + return self._stubs["get_index"] + + @property + def list_indexes( + self, + ) -> Callable[ + [datastore_admin.ListIndexesRequest], datastore_admin.ListIndexesResponse + ]: + r"""Return a callable for the list indexes method over gRPC. + + Lists the indexes that match the specified filters. + Datastore uses an eventually consistent query to fetch + the list of indexes and may occasionally return stale + results. + + Returns: + Callable[[~.ListIndexesRequest], + ~.ListIndexesResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "list_indexes" not in self._stubs: + self._stubs["list_indexes"] = self.grpc_channel.unary_unary( + "/google.datastore.admin.v1.DatastoreAdmin/ListIndexes", + request_serializer=datastore_admin.ListIndexesRequest.serialize, + response_deserializer=datastore_admin.ListIndexesResponse.deserialize, + ) + return self._stubs["list_indexes"] + + +__all__ = ("DatastoreAdminGrpcTransport",) diff --git a/google/cloud/datastore_admin_v1/services/datastore_admin/transports/grpc_asyncio.py b/google/cloud/datastore_admin_v1/services/datastore_admin/transports/grpc_asyncio.py new file mode 100644 index 00000000..f80c7da9 --- /dev/null +++ b/google/cloud/datastore_admin_v1/services/datastore_admin/transports/grpc_asyncio.py @@ -0,0 +1,438 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import warnings +from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple + +from google.api_core import gapic_v1 # type: ignore +from google.api_core import grpc_helpers_async # type: ignore +from google.api_core import operations_v1 # type: ignore +from google import auth # type: ignore +from google.auth import credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore + +import grpc # type: ignore +from grpc.experimental import aio # type: ignore + +from google.cloud.datastore_admin_v1.types import datastore_admin +from google.cloud.datastore_admin_v1.types import index +from google.longrunning import operations_pb2 as operations # type: ignore + +from .base import DatastoreAdminTransport, DEFAULT_CLIENT_INFO +from .grpc import DatastoreAdminGrpcTransport + + +class DatastoreAdminGrpcAsyncIOTransport(DatastoreAdminTransport): + """gRPC AsyncIO backend transport for DatastoreAdmin. + + Google Cloud Datastore Admin API + The Datastore Admin API provides several admin services for + Cloud Datastore. + ----------------------------------------------------------------------------- + ## Concepts + + Project, namespace, kind, and entity as defined in the Google + Cloud Datastore API. + + Operation: An Operation represents work being performed in the + background. + EntityFilter: Allows specifying a subset of entities in a + project. This is specified as a combination of kinds and + namespaces (either or both of which may be all). + + ----------------------------------------------------------------------------- + ## Services + + # Export/Import + + The Export/Import service provides the ability to copy all or a + subset of entities to/from Google Cloud Storage. + + Exported data may be imported into Cloud Datastore for any + Google Cloud Platform project. It is not restricted to the + export source project. It is possible to export from one project + and then import into another. + Exported data can also be loaded into Google BigQuery for + analysis. + Exports and imports are performed asynchronously. An Operation + resource is created for each export/import. The state (including + any errors encountered) of the export/import may be queried via + the Operation resource. + # Index + + The index service manages Cloud Datastore composite indexes. + Index creation and deletion are performed asynchronously. An + Operation resource is created for each such asynchronous + operation. The state of the operation (including any errors + encountered) may be queried via the Operation resource. + + # Operation + + The Operations collection provides a record of actions performed + for the specified project (including any operations in + progress). Operations are not created directly but through calls + on other collections or resources. + An operation that is not yet done may be cancelled. The request + to cancel is asynchronous and the operation may continue to run + for some time after the request to cancel is made. + + An operation that is done may be deleted so that it is no longer + listed as part of the Operation collection. + + ListOperations returns all pending operations, but not completed + operations. + Operations are created by service DatastoreAdmin, + but are accessed via service google.longrunning.Operations. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _grpc_channel: aio.Channel + _stubs: Dict[str, Callable] = {} + + @classmethod + def create_channel( + cls, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> aio.Channel: + """Create and return a gRPC AsyncIO channel object. + Args: + address (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + aio.Channel: A gRPC AsyncIO channel object. + """ + scopes = scopes or cls.AUTH_SCOPES + return grpc_helpers_async.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + **kwargs, + ) + + def __init__( + self, + *, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: aio.Channel = None, + api_mtls_endpoint: str = None, + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, + quota_project_id=None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if ``channel`` is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + channel (Optional[aio.Channel]): A ``Channel`` instance through + which to make calls. + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create + a mutual TLS channel with client SSL credentials from + ``client_cert_source`` or applicatin default SSL credentials. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + if channel: + # Sanity check: Ensure that channel and credentials are not both + # provided. + credentials = False + + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + + host = ( + api_mtls_endpoint + if ":" in api_mtls_endpoint + else api_mtls_endpoint + ":443" + ) + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + ssl_credentials = SslCredentials().ssl_credentials + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) + + # Run the base constructor. + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, + ) + + self._stubs = {} + + @property + def grpc_channel(self) -> aio.Channel: + """Create the channel designed to connect to this service. + + This property caches on the instance; repeated calls return + the same channel. + """ + # Return the channel from cache. + return self._grpc_channel + + @property + def operations_client(self) -> operations_v1.OperationsAsyncClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Sanity check: Only create a new client if we do not already have one. + if "operations_client" not in self.__dict__: + self.__dict__["operations_client"] = operations_v1.OperationsAsyncClient( + self.grpc_channel + ) + + # Return the client from cache. + return self.__dict__["operations_client"] + + @property + def export_entities( + self, + ) -> Callable[ + [datastore_admin.ExportEntitiesRequest], Awaitable[operations.Operation] + ]: + r"""Return a callable for the export entities method over gRPC. + + Exports a copy of all or a subset of entities from + Google Cloud Datastore to another storage system, such + as Google Cloud Storage. Recent updates to entities may + not be reflected in the export. The export occurs in the + background and its progress can be monitored and managed + via the Operation resource that is created. The output + of an export may only be used once the associated + operation is done. If an export operation is cancelled + before completion it may leave partial data behind in + Google Cloud Storage. + + Returns: + Callable[[~.ExportEntitiesRequest], + Awaitable[~.Operation]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "export_entities" not in self._stubs: + self._stubs["export_entities"] = self.grpc_channel.unary_unary( + "/google.datastore.admin.v1.DatastoreAdmin/ExportEntities", + request_serializer=datastore_admin.ExportEntitiesRequest.serialize, + response_deserializer=operations.Operation.FromString, + ) + return self._stubs["export_entities"] + + @property + def import_entities( + self, + ) -> Callable[ + [datastore_admin.ImportEntitiesRequest], Awaitable[operations.Operation] + ]: + r"""Return a callable for the import entities method over gRPC. + + Imports entities into Google Cloud Datastore. + Existing entities with the same key are overwritten. The + import occurs in the background and its progress can be + monitored and managed via the Operation resource that is + created. If an ImportEntities operation is cancelled, it + is possible that a subset of the data has already been + imported to Cloud Datastore. + + Returns: + Callable[[~.ImportEntitiesRequest], + Awaitable[~.Operation]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "import_entities" not in self._stubs: + self._stubs["import_entities"] = self.grpc_channel.unary_unary( + "/google.datastore.admin.v1.DatastoreAdmin/ImportEntities", + request_serializer=datastore_admin.ImportEntitiesRequest.serialize, + response_deserializer=operations.Operation.FromString, + ) + return self._stubs["import_entities"] + + @property + def get_index( + self, + ) -> Callable[[datastore_admin.GetIndexRequest], Awaitable[index.Index]]: + r"""Return a callable for the get index method over gRPC. + + Gets an index. + + Returns: + Callable[[~.GetIndexRequest], + Awaitable[~.Index]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "get_index" not in self._stubs: + self._stubs["get_index"] = self.grpc_channel.unary_unary( + "/google.datastore.admin.v1.DatastoreAdmin/GetIndex", + request_serializer=datastore_admin.GetIndexRequest.serialize, + response_deserializer=index.Index.deserialize, + ) + return self._stubs["get_index"] + + @property + def list_indexes( + self, + ) -> Callable[ + [datastore_admin.ListIndexesRequest], + Awaitable[datastore_admin.ListIndexesResponse], + ]: + r"""Return a callable for the list indexes method over gRPC. + + Lists the indexes that match the specified filters. + Datastore uses an eventually consistent query to fetch + the list of indexes and may occasionally return stale + results. + + Returns: + Callable[[~.ListIndexesRequest], + Awaitable[~.ListIndexesResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "list_indexes" not in self._stubs: + self._stubs["list_indexes"] = self.grpc_channel.unary_unary( + "/google.datastore.admin.v1.DatastoreAdmin/ListIndexes", + request_serializer=datastore_admin.ListIndexesRequest.serialize, + response_deserializer=datastore_admin.ListIndexesResponse.deserialize, + ) + return self._stubs["list_indexes"] + + +__all__ = ("DatastoreAdminGrpcAsyncIOTransport",) diff --git a/google/cloud/datastore_admin_v1/types.py b/google/cloud/datastore_admin_v1/types.py deleted file mode 100644 index 17ae2d27..00000000 --- a/google/cloud/datastore_admin_v1/types.py +++ /dev/null @@ -1,56 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2020 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 -# -# https://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 __future__ import absolute_import -import sys - -from google.api_core.protobuf_helpers import get_messages - -from google.cloud.datastore_admin_v1.proto import datastore_admin_pb2 -from google.cloud.datastore_admin_v1.proto import index_pb2 -from google.longrunning import operations_pb2 -from google.protobuf import any_pb2 -from google.protobuf import timestamp_pb2 -from google.rpc import status_pb2 - - -_shared_modules = [ - operations_pb2, - any_pb2, - timestamp_pb2, - status_pb2, -] - -_local_modules = [ - datastore_admin_pb2, - index_pb2, -] - -names = [] - -for module in _shared_modules: # pragma: NO COVER - for name, message in get_messages(module).items(): - setattr(sys.modules[__name__], name, message) - names.append(name) -for module in _local_modules: - for name, message in get_messages(module).items(): - message.__module__ = "google.cloud.datastore_admin_v1.types" - setattr(sys.modules[__name__], name, message) - names.append(name) - - -__all__ = tuple(sorted(names)) diff --git a/google/cloud/datastore_admin_v1/types/__init__.py b/google/cloud/datastore_admin_v1/types/__init__.py new file mode 100644 index 00000000..b3bf63d8 --- /dev/null +++ b/google/cloud/datastore_admin_v1/types/__init__.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 .index import Index +from .datastore_admin import ( + CommonMetadata, + Progress, + ExportEntitiesRequest, + ImportEntitiesRequest, + ExportEntitiesResponse, + ExportEntitiesMetadata, + ImportEntitiesMetadata, + EntityFilter, + GetIndexRequest, + ListIndexesRequest, + ListIndexesResponse, + IndexOperationMetadata, +) + + +__all__ = ( + "Index", + "CommonMetadata", + "Progress", + "ExportEntitiesRequest", + "ImportEntitiesRequest", + "ExportEntitiesResponse", + "ExportEntitiesMetadata", + "ImportEntitiesMetadata", + "EntityFilter", + "GetIndexRequest", + "ListIndexesRequest", + "ListIndexesResponse", + "IndexOperationMetadata", +) diff --git a/google/cloud/datastore_admin_v1/types/datastore_admin.py b/google/cloud/datastore_admin_v1/types/datastore_admin.py new file mode 100644 index 00000000..8f60bfe1 --- /dev/null +++ b/google/cloud/datastore_admin_v1/types/datastore_admin.py @@ -0,0 +1,408 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import proto # type: ignore + + +from google.cloud.datastore_admin_v1.types import index +from google.protobuf import timestamp_pb2 as timestamp # type: ignore + + +__protobuf__ = proto.module( + package="google.datastore.admin.v1", + manifest={ + "OperationType", + "CommonMetadata", + "Progress", + "ExportEntitiesRequest", + "ImportEntitiesRequest", + "ExportEntitiesResponse", + "ExportEntitiesMetadata", + "ImportEntitiesMetadata", + "EntityFilter", + "GetIndexRequest", + "ListIndexesRequest", + "ListIndexesResponse", + "IndexOperationMetadata", + }, +) + + +class OperationType(proto.Enum): + r"""Operation types.""" + OPERATION_TYPE_UNSPECIFIED = 0 + EXPORT_ENTITIES = 1 + IMPORT_ENTITIES = 2 + CREATE_INDEX = 3 + DELETE_INDEX = 4 + + +class CommonMetadata(proto.Message): + r"""Metadata common to all Datastore Admin operations. + + Attributes: + start_time (~.timestamp.Timestamp): + The time that work began on the operation. + end_time (~.timestamp.Timestamp): + The time the operation ended, either + successfully or otherwise. + operation_type (~.datastore_admin.OperationType): + The type of the operation. Can be used as a + filter in ListOperationsRequest. + labels (Sequence[~.datastore_admin.CommonMetadata.LabelsEntry]): + The client-assigned labels which were + provided when the operation was created. May + also include additional labels. + state (~.datastore_admin.CommonMetadata.State): + The current state of the Operation. + """ + + class State(proto.Enum): + r"""The various possible states for an ongoing Operation.""" + STATE_UNSPECIFIED = 0 + INITIALIZING = 1 + PROCESSING = 2 + CANCELLING = 3 + FINALIZING = 4 + SUCCESSFUL = 5 + FAILED = 6 + CANCELLED = 7 + + start_time = proto.Field(proto.MESSAGE, number=1, message=timestamp.Timestamp,) + + end_time = proto.Field(proto.MESSAGE, number=2, message=timestamp.Timestamp,) + + operation_type = proto.Field(proto.ENUM, number=3, enum="OperationType",) + + labels = proto.MapField(proto.STRING, proto.STRING, number=4) + + state = proto.Field(proto.ENUM, number=5, enum=State,) + + +class Progress(proto.Message): + r"""Measures the progress of a particular metric. + + Attributes: + work_completed (int): + The amount of work that has been completed. Note that this + may be greater than work_estimated. + work_estimated (int): + An estimate of how much work needs to be + performed. May be zero if the work estimate is + unavailable. + """ + + work_completed = proto.Field(proto.INT64, number=1) + + work_estimated = proto.Field(proto.INT64, number=2) + + +class ExportEntitiesRequest(proto.Message): + r"""The request for + [google.datastore.admin.v1.DatastoreAdmin.ExportEntities][google.datastore.admin.v1.DatastoreAdmin.ExportEntities]. + + Attributes: + project_id (str): + Required. Project ID against which to make + the request. + labels (Sequence[~.datastore_admin.ExportEntitiesRequest.LabelsEntry]): + Client-assigned labels. + entity_filter (~.datastore_admin.EntityFilter): + Description of what data from the project is + included in the export. + output_url_prefix (str): + Required. Location for the export metadata and data files. + + The full resource URL of the external storage location. + Currently, only Google Cloud Storage is supported. So + output_url_prefix should be of the form: + ``gs://BUCKET_NAME[/NAMESPACE_PATH]``, where ``BUCKET_NAME`` + is the name of the Cloud Storage bucket and + ``NAMESPACE_PATH`` is an optional Cloud Storage namespace + path (this is not a Cloud Datastore namespace). For more + information about Cloud Storage namespace paths, see `Object + name + considerations `__. + + The resulting files will be nested deeper than the specified + URL prefix. The final output URL will be provided in the + [google.datastore.admin.v1.ExportEntitiesResponse.output_url][google.datastore.admin.v1.ExportEntitiesResponse.output_url] + field. That value should be used for subsequent + ImportEntities operations. + + By nesting the data files deeper, the same Cloud Storage + bucket can be used in multiple ExportEntities operations + without conflict. + """ + + project_id = proto.Field(proto.STRING, number=1) + + labels = proto.MapField(proto.STRING, proto.STRING, number=2) + + entity_filter = proto.Field(proto.MESSAGE, number=3, message="EntityFilter",) + + output_url_prefix = proto.Field(proto.STRING, number=4) + + +class ImportEntitiesRequest(proto.Message): + r"""The request for + [google.datastore.admin.v1.DatastoreAdmin.ImportEntities][google.datastore.admin.v1.DatastoreAdmin.ImportEntities]. + + Attributes: + project_id (str): + Required. Project ID against which to make + the request. + labels (Sequence[~.datastore_admin.ImportEntitiesRequest.LabelsEntry]): + Client-assigned labels. + input_url (str): + Required. The full resource URL of the external storage + location. Currently, only Google Cloud Storage is supported. + So input_url should be of the form: + ``gs://BUCKET_NAME[/NAMESPACE_PATH]/OVERALL_EXPORT_METADATA_FILE``, + where ``BUCKET_NAME`` is the name of the Cloud Storage + bucket, ``NAMESPACE_PATH`` is an optional Cloud Storage + namespace path (this is not a Cloud Datastore namespace), + and ``OVERALL_EXPORT_METADATA_FILE`` is the metadata file + written by the ExportEntities operation. For more + information about Cloud Storage namespace paths, see `Object + name + considerations `__. + + For more information, see + [google.datastore.admin.v1.ExportEntitiesResponse.output_url][google.datastore.admin.v1.ExportEntitiesResponse.output_url]. + entity_filter (~.datastore_admin.EntityFilter): + Optionally specify which kinds/namespaces are to be + imported. If provided, the list must be a subset of the + EntityFilter used in creating the export, otherwise a + FAILED_PRECONDITION error will be returned. If no filter is + specified then all entities from the export are imported. + """ + + project_id = proto.Field(proto.STRING, number=1) + + labels = proto.MapField(proto.STRING, proto.STRING, number=2) + + input_url = proto.Field(proto.STRING, number=3) + + entity_filter = proto.Field(proto.MESSAGE, number=4, message="EntityFilter",) + + +class ExportEntitiesResponse(proto.Message): + r"""The response for + [google.datastore.admin.v1.DatastoreAdmin.ExportEntities][google.datastore.admin.v1.DatastoreAdmin.ExportEntities]. + + Attributes: + output_url (str): + Location of the output metadata file. This can be used to + begin an import into Cloud Datastore (this project or + another project). See + [google.datastore.admin.v1.ImportEntitiesRequest.input_url][google.datastore.admin.v1.ImportEntitiesRequest.input_url]. + Only present if the operation completed successfully. + """ + + output_url = proto.Field(proto.STRING, number=1) + + +class ExportEntitiesMetadata(proto.Message): + r"""Metadata for ExportEntities operations. + + Attributes: + common (~.datastore_admin.CommonMetadata): + Metadata common to all Datastore Admin + operations. + progress_entities (~.datastore_admin.Progress): + An estimate of the number of entities + processed. + progress_bytes (~.datastore_admin.Progress): + An estimate of the number of bytes processed. + entity_filter (~.datastore_admin.EntityFilter): + Description of which entities are being + exported. + output_url_prefix (str): + Location for the export metadata and data files. This will + be the same value as the + [google.datastore.admin.v1.ExportEntitiesRequest.output_url_prefix][google.datastore.admin.v1.ExportEntitiesRequest.output_url_prefix] + field. The final output location is provided in + [google.datastore.admin.v1.ExportEntitiesResponse.output_url][google.datastore.admin.v1.ExportEntitiesResponse.output_url]. + """ + + common = proto.Field(proto.MESSAGE, number=1, message=CommonMetadata,) + + progress_entities = proto.Field(proto.MESSAGE, number=2, message=Progress,) + + progress_bytes = proto.Field(proto.MESSAGE, number=3, message=Progress,) + + entity_filter = proto.Field(proto.MESSAGE, number=4, message="EntityFilter",) + + output_url_prefix = proto.Field(proto.STRING, number=5) + + +class ImportEntitiesMetadata(proto.Message): + r"""Metadata for ImportEntities operations. + + Attributes: + common (~.datastore_admin.CommonMetadata): + Metadata common to all Datastore Admin + operations. + progress_entities (~.datastore_admin.Progress): + An estimate of the number of entities + processed. + progress_bytes (~.datastore_admin.Progress): + An estimate of the number of bytes processed. + entity_filter (~.datastore_admin.EntityFilter): + Description of which entities are being + imported. + input_url (str): + The location of the import metadata file. This will be the + same value as the + [google.datastore.admin.v1.ExportEntitiesResponse.output_url][google.datastore.admin.v1.ExportEntitiesResponse.output_url] + field. + """ + + common = proto.Field(proto.MESSAGE, number=1, message=CommonMetadata,) + + progress_entities = proto.Field(proto.MESSAGE, number=2, message=Progress,) + + progress_bytes = proto.Field(proto.MESSAGE, number=3, message=Progress,) + + entity_filter = proto.Field(proto.MESSAGE, number=4, message="EntityFilter",) + + input_url = proto.Field(proto.STRING, number=5) + + +class EntityFilter(proto.Message): + r"""Identifies a subset of entities in a project. This is specified as + combinations of kinds and namespaces (either or both of which may be + all, as described in the following examples). Example usage: + + Entire project: kinds=[], namespace_ids=[] + + Kinds Foo and Bar in all namespaces: kinds=['Foo', 'Bar'], + namespace_ids=[] + + Kinds Foo and Bar only in the default namespace: kinds=['Foo', + 'Bar'], namespace_ids=[''] + + Kinds Foo and Bar in both the default and Baz namespaces: + kinds=['Foo', 'Bar'], namespace_ids=['', 'Baz'] + + The entire Baz namespace: kinds=[], namespace_ids=['Baz'] + + Attributes: + kinds (Sequence[str]): + If empty, then this represents all kinds. + namespace_ids (Sequence[str]): + An empty list represents all namespaces. This + is the preferred usage for projects that don't + use namespaces. + An empty string element represents the default + namespace. This should be used if the project + has data in non-default namespaces, but doesn't + want to include them. + Each namespace in this list must be unique. + """ + + kinds = proto.RepeatedField(proto.STRING, number=1) + + namespace_ids = proto.RepeatedField(proto.STRING, number=2) + + +class GetIndexRequest(proto.Message): + r"""The request for + [google.datastore.admin.v1.DatastoreAdmin.GetIndex][google.datastore.admin.v1.DatastoreAdmin.GetIndex]. + + Attributes: + project_id (str): + Project ID against which to make the request. + index_id (str): + The resource ID of the index to get. + """ + + project_id = proto.Field(proto.STRING, number=1) + + index_id = proto.Field(proto.STRING, number=3) + + +class ListIndexesRequest(proto.Message): + r"""The request for + [google.datastore.admin.v1.DatastoreAdmin.ListIndexes][google.datastore.admin.v1.DatastoreAdmin.ListIndexes]. + + Attributes: + project_id (str): + Project ID against which to make the request. + filter (str): + + page_size (int): + The maximum number of items to return. If + zero, then all results will be returned. + page_token (str): + The next_page_token value returned from a previous List + request, if any. + """ + + project_id = proto.Field(proto.STRING, number=1) + + filter = proto.Field(proto.STRING, number=3) + + page_size = proto.Field(proto.INT32, number=4) + + page_token = proto.Field(proto.STRING, number=5) + + +class ListIndexesResponse(proto.Message): + r"""The response for + [google.datastore.admin.v1.DatastoreAdmin.ListIndexes][google.datastore.admin.v1.DatastoreAdmin.ListIndexes]. + + Attributes: + indexes (Sequence[~.index.Index]): + The indexes. + next_page_token (str): + The standard List next-page token. + """ + + @property + def raw_page(self): + return self + + indexes = proto.RepeatedField(proto.MESSAGE, number=1, message=index.Index,) + + next_page_token = proto.Field(proto.STRING, number=2) + + +class IndexOperationMetadata(proto.Message): + r"""Metadata for Index operations. + + Attributes: + common (~.datastore_admin.CommonMetadata): + Metadata common to all Datastore Admin + operations. + progress_entities (~.datastore_admin.Progress): + An estimate of the number of entities + processed. + index_id (str): + The index resource ID that this operation is + acting on. + """ + + common = proto.Field(proto.MESSAGE, number=1, message=CommonMetadata,) + + progress_entities = proto.Field(proto.MESSAGE, number=2, message=Progress,) + + index_id = proto.Field(proto.STRING, number=3) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/datastore_admin_v1/types/index.py b/google/cloud/datastore_admin_v1/types/index.py new file mode 100644 index 00000000..e11a27a5 --- /dev/null +++ b/google/cloud/datastore_admin_v1/types/index.py @@ -0,0 +1,95 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import proto # type: ignore + + +__protobuf__ = proto.module(package="google.datastore.admin.v1", manifest={"Index",},) + + +class Index(proto.Message): + r"""A minimal index definition. + + Attributes: + project_id (str): + Output only. Project ID. + index_id (str): + Output only. The resource ID of the index. + kind (str): + Required. The entity kind to which this index + applies. + ancestor (~.index.Index.AncestorMode): + Required. The index's ancestor mode. Must not be + ANCESTOR_MODE_UNSPECIFIED. + properties (Sequence[~.index.Index.IndexedProperty]): + Required. An ordered sequence of property + names and their index attributes. + state (~.index.Index.State): + Output only. The state of the index. + """ + + class AncestorMode(proto.Enum): + r"""For an ordered index, specifies whether each of the entity's + ancestors will be included. + """ + ANCESTOR_MODE_UNSPECIFIED = 0 + NONE = 1 + ALL_ANCESTORS = 2 + + class Direction(proto.Enum): + r"""The direction determines how a property is indexed.""" + DIRECTION_UNSPECIFIED = 0 + ASCENDING = 1 + DESCENDING = 2 + + class State(proto.Enum): + r"""The possible set of states of an index.""" + STATE_UNSPECIFIED = 0 + CREATING = 1 + READY = 2 + DELETING = 3 + ERROR = 4 + + class IndexedProperty(proto.Message): + r"""A property of an index. + + Attributes: + name (str): + Required. The property name to index. + direction (~.index.Index.Direction): + Required. The indexed property's direction. Must not be + DIRECTION_UNSPECIFIED. + """ + + name = proto.Field(proto.STRING, number=1) + + direction = proto.Field(proto.ENUM, number=2, enum="Index.Direction",) + + project_id = proto.Field(proto.STRING, number=1) + + index_id = proto.Field(proto.STRING, number=3) + + kind = proto.Field(proto.STRING, number=4) + + ancestor = proto.Field(proto.ENUM, number=5, enum=AncestorMode,) + + properties = proto.RepeatedField(proto.MESSAGE, number=6, message=IndexedProperty,) + + state = proto.Field(proto.ENUM, number=7, enum=State,) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/datastore_v1/__init__.py b/google/cloud/datastore_v1/__init__.py index 0308f10c..a4b5de76 100644 --- a/google/cloud/datastore_v1/__init__.py +++ b/google/cloud/datastore_v1/__init__.py @@ -1,27 +1,93 @@ -# Copyright 2018 Google LLC +# -*- coding: utf-8 -*- + +# Copyright 2020 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 # -# https://www.apache.org/licenses/LICENSE-2.0 +# 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 __future__ import absolute_import - -from google.cloud.datastore_v1 import types -from google.cloud.datastore_v1.gapic import datastore_client -from google.cloud.datastore_v1.gapic import enums - - -class DatastoreClient(datastore_client.DatastoreClient): - __doc__ = datastore_client.DatastoreClient.__doc__ - enums = enums +from .services.datastore import DatastoreClient +from .types.datastore import AllocateIdsRequest +from .types.datastore import AllocateIdsResponse +from .types.datastore import BeginTransactionRequest +from .types.datastore import BeginTransactionResponse +from .types.datastore import CommitRequest +from .types.datastore import CommitResponse +from .types.datastore import LookupRequest +from .types.datastore import LookupResponse +from .types.datastore import Mutation +from .types.datastore import MutationResult +from .types.datastore import ReadOptions +from .types.datastore import ReserveIdsRequest +from .types.datastore import ReserveIdsResponse +from .types.datastore import RollbackRequest +from .types.datastore import RollbackResponse +from .types.datastore import RunQueryRequest +from .types.datastore import RunQueryResponse +from .types.datastore import TransactionOptions +from .types.entity import ArrayValue +from .types.entity import Entity +from .types.entity import Key +from .types.entity import PartitionId +from .types.entity import Value +from .types.query import CompositeFilter +from .types.query import EntityResult +from .types.query import Filter +from .types.query import GqlQuery +from .types.query import GqlQueryParameter +from .types.query import KindExpression +from .types.query import Projection +from .types.query import PropertyFilter +from .types.query import PropertyOrder +from .types.query import PropertyReference +from .types.query import Query +from .types.query import QueryResultBatch -__all__ = ("enums", "types", "DatastoreClient") +__all__ = ( + "AllocateIdsRequest", + "AllocateIdsResponse", + "ArrayValue", + "BeginTransactionRequest", + "BeginTransactionResponse", + "CommitRequest", + "CommitResponse", + "CompositeFilter", + "Entity", + "EntityResult", + "Filter", + "GqlQuery", + "GqlQueryParameter", + "Key", + "KindExpression", + "LookupRequest", + "LookupResponse", + "Mutation", + "MutationResult", + "PartitionId", + "Projection", + "PropertyFilter", + "PropertyOrder", + "PropertyReference", + "Query", + "QueryResultBatch", + "ReadOptions", + "ReserveIdsRequest", + "ReserveIdsResponse", + "RollbackRequest", + "RollbackResponse", + "RunQueryRequest", + "RunQueryResponse", + "TransactionOptions", + "Value", + "DatastoreClient", +) diff --git a/google/cloud/datastore_v1/gapic/__init__.py b/google/cloud/datastore_v1/gapic/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/google/cloud/datastore_v1/gapic/datastore_client.py b/google/cloud/datastore_v1/gapic/datastore_client.py deleted file mode 100644 index ac61c12b..00000000 --- a/google/cloud/datastore_v1/gapic/datastore_client.py +++ /dev/null @@ -1,805 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2020 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 -# -# https://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. - -"""Accesses the google.datastore.v1 Datastore API.""" - -import pkg_resources -import warnings - -from google.oauth2 import service_account -import google.api_core.client_options -import google.api_core.gapic_v1.client_info -import google.api_core.gapic_v1.config -import google.api_core.gapic_v1.method -import google.api_core.gapic_v1.routing_header -import google.api_core.grpc_helpers -import google.api_core.protobuf_helpers -import grpc - -from google.cloud.datastore_v1.gapic import datastore_client_config -from google.cloud.datastore_v1.gapic import enums -from google.cloud.datastore_v1.gapic.transports import datastore_grpc_transport -from google.cloud.datastore_v1.proto import datastore_pb2 -from google.cloud.datastore_v1.proto import datastore_pb2_grpc -from google.cloud.datastore_v1.proto import entity_pb2 -from google.cloud.datastore_v1.proto import query_pb2 -from google.cloud.datastore.version import __version__ - - -_GAPIC_LIBRARY_VERSION = __version__ - - -class DatastoreClient(object): - """ - Each RPC normalizes the partition IDs of the keys in its input entities, - and always returns entities with keys with normalized partition IDs. - This applies to all keys and entities, including those in values, except keys - with both an empty path and an empty or unset partition ID. Normalization of - input keys sets the project ID (if not already set) to the project ID from - the request. - """ - - SERVICE_ADDRESS = "datastore.googleapis.com:443" - """The default address of the service.""" - - # The name of the interface for this client. This is the key used to - # find the method configuration in the client_config dictionary. - _INTERFACE_NAME = "google.datastore.v1.Datastore" - - @classmethod - def from_service_account_file(cls, filename, *args, **kwargs): - """Creates an instance of this client using the provided credentials - file. - - Args: - filename (str): The path to the service account private key json - file. - args: Additional arguments to pass to the constructor. - kwargs: Additional arguments to pass to the constructor. - - Returns: - DatastoreClient: The constructed client. - """ - credentials = service_account.Credentials.from_service_account_file(filename) - kwargs["credentials"] = credentials - return cls(*args, **kwargs) - - from_service_account_json = from_service_account_file - - def __init__( - self, - transport=None, - channel=None, - credentials=None, - client_config=None, - client_info=None, - client_options=None, - ): - """Constructor. - - Args: - transport (Union[~.DatastoreGrpcTransport, - Callable[[~.Credentials, type], ~.DatastoreGrpcTransport]): A transport - instance, responsible for actually making the API calls. - The default transport uses the gRPC protocol. - This argument may also be a callable which returns a - transport instance. Callables will be sent the credentials - as the first argument and the default transport class as - the second argument. - channel (grpc.Channel): DEPRECATED. A ``Channel`` instance - through which to make calls. This argument is mutually exclusive - with ``credentials``; providing both will raise an exception. - credentials (google.auth.credentials.Credentials): The - authorization credentials to attach to requests. These - credentials identify this application to the service. If none - are specified, the client will attempt to ascertain the - credentials from the environment. - This argument is mutually exclusive with providing a - transport instance to ``transport``; doing so will raise - an exception. - client_config (dict): DEPRECATED. A dictionary of call options for - each method. If not specified, the default configuration is used. - client_info (google.api_core.gapic_v1.client_info.ClientInfo): - The client info used to send a user-agent string along with - API requests. If ``None``, then default info will be used. - Generally, you only need to set this if you're developing - your own client library. - client_options (Union[dict, google.api_core.client_options.ClientOptions]): - Client options used to set user options on the client. API Endpoint - should be set through client_options. - """ - # Raise deprecation warnings for things we want to go away. - if client_config is not None: - warnings.warn( - "The `client_config` argument is deprecated.", - PendingDeprecationWarning, - stacklevel=2, - ) - else: - client_config = datastore_client_config.config - - if channel: - warnings.warn( - "The `channel` argument is deprecated; use " "`transport` instead.", - PendingDeprecationWarning, - stacklevel=2, - ) - - api_endpoint = self.SERVICE_ADDRESS - if client_options: - if type(client_options) == dict: - client_options = google.api_core.client_options.from_dict( - client_options - ) - if client_options.api_endpoint: - api_endpoint = client_options.api_endpoint - - # Instantiate the transport. - # The transport is responsible for handling serialization and - # deserialization and actually sending data to the service. - if transport: - if callable(transport): - self.transport = transport( - credentials=credentials, - default_class=datastore_grpc_transport.DatastoreGrpcTransport, - address=api_endpoint, - ) - else: - if credentials: - raise ValueError( - "Received both a transport instance and " - "credentials; these are mutually exclusive." - ) - self.transport = transport - else: - self.transport = datastore_grpc_transport.DatastoreGrpcTransport( - address=api_endpoint, channel=channel, credentials=credentials, - ) - - if client_info is None: - client_info = google.api_core.gapic_v1.client_info.ClientInfo( - gapic_version=_GAPIC_LIBRARY_VERSION, - ) - else: - client_info.gapic_version = _GAPIC_LIBRARY_VERSION - self._client_info = client_info - - # Parse out the default settings for retry and timeout for each RPC - # from the client configuration. - # (Ordinarily, these are the defaults specified in the `*_config.py` - # file next to this one.) - self._method_configs = google.api_core.gapic_v1.config.parse_method_configs( - client_config["interfaces"][self._INTERFACE_NAME], - ) - - # Save a dictionary of cached API call functions. - # These are the actual callables which invoke the proper - # transport methods, wrapped with `wrap_method` to add retry, - # timeout, and the like. - self._inner_api_calls = {} - - # Service calls - def lookup( - self, - project_id, - keys, - read_options=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Looks up entities by key. - - Example: - >>> from google.cloud import datastore_v1 - >>> - >>> client = datastore_v1.DatastoreClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> # TODO: Initialize `keys`: - >>> keys = [] - >>> - >>> response = client.lookup(project_id, keys) - - Args: - project_id (str): Required. The ID of the project against which to make the request. - keys (list[Union[dict, ~google.cloud.datastore_v1.types.Key]]): Required. Keys of entities to look up. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.Key` - read_options (Union[dict, ~google.cloud.datastore_v1.types.ReadOptions]): The options for this lookup request. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.ReadOptions` - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_v1.types.LookupResponse` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "lookup" not in self._inner_api_calls: - self._inner_api_calls[ - "lookup" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.lookup, - default_retry=self._method_configs["Lookup"].retry, - default_timeout=self._method_configs["Lookup"].timeout, - client_info=self._client_info, - ) - - request = datastore_pb2.LookupRequest( - project_id=project_id, keys=keys, read_options=read_options, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - return self._inner_api_calls["lookup"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - - def run_query( - self, - project_id, - partition_id=None, - read_options=None, - query=None, - gql_query=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Queries for entities. - - Example: - >>> from google.cloud import datastore_v1 - >>> - >>> client = datastore_v1.DatastoreClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> response = client.run_query(project_id) - - Args: - project_id (str): Required. The ID of the project against which to make the request. - partition_id (Union[dict, ~google.cloud.datastore_v1.types.PartitionId]): Entities are partitioned into subsets, identified by a partition ID. - Queries are scoped to a single partition. - This partition ID is normalized with the standard default context - partition ID. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.PartitionId` - read_options (Union[dict, ~google.cloud.datastore_v1.types.ReadOptions]): The options for this query. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.ReadOptions` - query (Union[dict, ~google.cloud.datastore_v1.types.Query]): The query to run. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.Query` - gql_query (Union[dict, ~google.cloud.datastore_v1.types.GqlQuery]): The GQL query to run. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.GqlQuery` - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_v1.types.RunQueryResponse` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "run_query" not in self._inner_api_calls: - self._inner_api_calls[ - "run_query" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.run_query, - default_retry=self._method_configs["RunQuery"].retry, - default_timeout=self._method_configs["RunQuery"].timeout, - client_info=self._client_info, - ) - - # Sanity check: We have some fields which are mutually exclusive; - # raise ValueError if more than one is sent. - google.api_core.protobuf_helpers.check_oneof( - query=query, gql_query=gql_query, - ) - - request = datastore_pb2.RunQueryRequest( - project_id=project_id, - partition_id=partition_id, - read_options=read_options, - query=query, - gql_query=gql_query, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - return self._inner_api_calls["run_query"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - - def reserve_ids( - self, - project_id, - keys, - database_id=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Prevents the supplied keys' IDs from being auto-allocated by Cloud - Datastore. - - Example: - >>> from google.cloud import datastore_v1 - >>> - >>> client = datastore_v1.DatastoreClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> # TODO: Initialize `keys`: - >>> keys = [] - >>> - >>> response = client.reserve_ids(project_id, keys) - - Args: - project_id (str): Required. The ID of the project against which to make the request. - keys (list[Union[dict, ~google.cloud.datastore_v1.types.Key]]): Required. A list of keys with complete key paths whose numeric IDs should not be - auto-allocated. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.Key` - database_id (str): If not empty, the ID of the database against which to make the request. - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_v1.types.ReserveIdsResponse` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "reserve_ids" not in self._inner_api_calls: - self._inner_api_calls[ - "reserve_ids" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.reserve_ids, - default_retry=self._method_configs["ReserveIds"].retry, - default_timeout=self._method_configs["ReserveIds"].timeout, - client_info=self._client_info, - ) - - request = datastore_pb2.ReserveIdsRequest( - project_id=project_id, keys=keys, database_id=database_id, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - return self._inner_api_calls["reserve_ids"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - - def begin_transaction( - self, - project_id, - transaction_options=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Begins a new transaction. - - Example: - >>> from google.cloud import datastore_v1 - >>> - >>> client = datastore_v1.DatastoreClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> response = client.begin_transaction(project_id) - - Args: - project_id (str): Required. The ID of the project against which to make the request. - transaction_options (Union[dict, ~google.cloud.datastore_v1.types.TransactionOptions]): Options for a new transaction. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.TransactionOptions` - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_v1.types.BeginTransactionResponse` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "begin_transaction" not in self._inner_api_calls: - self._inner_api_calls[ - "begin_transaction" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.begin_transaction, - default_retry=self._method_configs["BeginTransaction"].retry, - default_timeout=self._method_configs["BeginTransaction"].timeout, - client_info=self._client_info, - ) - - request = datastore_pb2.BeginTransactionRequest( - project_id=project_id, transaction_options=transaction_options, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - return self._inner_api_calls["begin_transaction"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - - def commit( - self, - project_id, - mode=None, - mutations=None, - transaction=None, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Commits a transaction, optionally creating, deleting or modifying some - entities. - - Example: - >>> from google.cloud import datastore_v1 - >>> - >>> client = datastore_v1.DatastoreClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> response = client.commit(project_id) - - Args: - project_id (str): Required. The ID of the project against which to make the request. - mode (~google.cloud.datastore_v1.types.Mode): The type of commit to perform. Defaults to ``TRANSACTIONAL``. - transaction (bytes): The identifier of the transaction associated with the commit. A - transaction identifier is returned by a call to - ``Datastore.BeginTransaction``. - mutations (list[Union[dict, ~google.cloud.datastore_v1.types.Mutation]]): The mutations to perform. - - When mode is ``TRANSACTIONAL``, mutations affecting a single entity are - applied in order. The following sequences of mutations affecting a - single entity are not permitted in a single ``Commit`` request: - - - ``insert`` followed by ``insert`` - - ``update`` followed by ``insert`` - - ``upsert`` followed by ``insert`` - - ``delete`` followed by ``update`` - - When mode is ``NON_TRANSACTIONAL``, no two mutations may affect a single - entity. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.Mutation` - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_v1.types.CommitResponse` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "commit" not in self._inner_api_calls: - self._inner_api_calls[ - "commit" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.commit, - default_retry=self._method_configs["Commit"].retry, - default_timeout=self._method_configs["Commit"].timeout, - client_info=self._client_info, - ) - - # Sanity check: We have some fields which are mutually exclusive; - # raise ValueError if more than one is sent. - google.api_core.protobuf_helpers.check_oneof(transaction=transaction,) - - request = datastore_pb2.CommitRequest( - project_id=project_id, - mode=mode, - transaction=transaction, - mutations=mutations, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - return self._inner_api_calls["commit"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - - def rollback( - self, - project_id, - transaction, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Rolls back a transaction. - - Example: - >>> from google.cloud import datastore_v1 - >>> - >>> client = datastore_v1.DatastoreClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> # TODO: Initialize `transaction`: - >>> transaction = b'' - >>> - >>> response = client.rollback(project_id, transaction) - - Args: - project_id (str): Required. The ID of the project against which to make the request. - transaction (bytes): Required. The transaction identifier, returned by a call to - ``Datastore.BeginTransaction``. - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_v1.types.RollbackResponse` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "rollback" not in self._inner_api_calls: - self._inner_api_calls[ - "rollback" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.rollback, - default_retry=self._method_configs["Rollback"].retry, - default_timeout=self._method_configs["Rollback"].timeout, - client_info=self._client_info, - ) - - request = datastore_pb2.RollbackRequest( - project_id=project_id, transaction=transaction, - ) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - return self._inner_api_calls["rollback"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) - - def allocate_ids( - self, - project_id, - keys, - retry=google.api_core.gapic_v1.method.DEFAULT, - timeout=google.api_core.gapic_v1.method.DEFAULT, - metadata=None, - ): - """ - Allocates IDs for the given keys, which is useful for referencing an entity - before it is inserted. - - Example: - >>> from google.cloud import datastore_v1 - >>> - >>> client = datastore_v1.DatastoreClient() - >>> - >>> # TODO: Initialize `project_id`: - >>> project_id = '' - >>> - >>> # TODO: Initialize `keys`: - >>> keys = [] - >>> - >>> response = client.allocate_ids(project_id, keys) - - Args: - project_id (str): Required. The ID of the project against which to make the request. - keys (list[Union[dict, ~google.cloud.datastore_v1.types.Key]]): Required. A list of keys with incomplete key paths for which to allocate IDs. - No key may be reserved/read-only. - - If a dict is provided, it must be of the same form as the protobuf - message :class:`~google.cloud.datastore_v1.types.Key` - retry (Optional[google.api_core.retry.Retry]): A retry object used - to retry requests. If ``None`` is specified, requests will - be retried using a default configuration. - timeout (Optional[float]): The amount of time, in seconds, to wait - for the request to complete. Note that if ``retry`` is - specified, the timeout applies to each individual attempt. - metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata - that is provided to the method. - - Returns: - A :class:`~google.cloud.datastore_v1.types.AllocateIdsResponse` instance. - - Raises: - google.api_core.exceptions.GoogleAPICallError: If the request - failed for any reason. - google.api_core.exceptions.RetryError: If the request failed due - to a retryable error and retry attempts failed. - ValueError: If the parameters are invalid. - """ - # Wrap the transport method to add retry and timeout logic. - if "allocate_ids" not in self._inner_api_calls: - self._inner_api_calls[ - "allocate_ids" - ] = google.api_core.gapic_v1.method.wrap_method( - self.transport.allocate_ids, - default_retry=self._method_configs["AllocateIds"].retry, - default_timeout=self._method_configs["AllocateIds"].timeout, - client_info=self._client_info, - ) - - request = datastore_pb2.AllocateIdsRequest(project_id=project_id, keys=keys,) - if metadata is None: - metadata = [] - metadata = list(metadata) - try: - routing_header = [("project_id", project_id)] - except AttributeError: - pass - else: - routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( - routing_header - ) - metadata.append(routing_metadata) - - return self._inner_api_calls["allocate_ids"]( - request, retry=retry, timeout=timeout, metadata=metadata - ) diff --git a/google/cloud/datastore_v1/gapic/datastore_client_config.py b/google/cloud/datastore_v1/gapic/datastore_client_config.py deleted file mode 100644 index b1f7991e..00000000 --- a/google/cloud/datastore_v1/gapic/datastore_client_config.py +++ /dev/null @@ -1,77 +0,0 @@ -config = { - "interfaces": { - "google.datastore.v1.Datastore": { - "retry_codes": { - "retry_policy_1_codes": ["UNAVAILABLE", "DEADLINE_EXCEEDED"], - "no_retry_codes": [], - "no_retry_1_codes": [], - }, - "retry_params": { - "retry_policy_1_params": { - "initial_retry_delay_millis": 100, - "retry_delay_multiplier": 1.3, - "max_retry_delay_millis": 60000, - "initial_rpc_timeout_millis": 60000, - "rpc_timeout_multiplier": 1.0, - "max_rpc_timeout_millis": 60000, - "total_timeout_millis": 60000, - }, - "no_retry_params": { - "initial_retry_delay_millis": 0, - "retry_delay_multiplier": 0.0, - "max_retry_delay_millis": 0, - "initial_rpc_timeout_millis": 0, - "rpc_timeout_multiplier": 1.0, - "max_rpc_timeout_millis": 0, - "total_timeout_millis": 0, - }, - "no_retry_1_params": { - "initial_retry_delay_millis": 0, - "retry_delay_multiplier": 0.0, - "max_retry_delay_millis": 0, - "initial_rpc_timeout_millis": 60000, - "rpc_timeout_multiplier": 1.0, - "max_rpc_timeout_millis": 60000, - "total_timeout_millis": 60000, - }, - }, - "methods": { - "Lookup": { - "timeout_millis": 60000, - "retry_codes_name": "retry_policy_1_codes", - "retry_params_name": "retry_policy_1_params", - }, - "RunQuery": { - "timeout_millis": 60000, - "retry_codes_name": "retry_policy_1_codes", - "retry_params_name": "retry_policy_1_params", - }, - "ReserveIds": { - "timeout_millis": 60000, - "retry_codes_name": "retry_policy_1_codes", - "retry_params_name": "retry_policy_1_params", - }, - "BeginTransaction": { - "timeout_millis": 60000, - "retry_codes_name": "no_retry_1_codes", - "retry_params_name": "no_retry_1_params", - }, - "Commit": { - "timeout_millis": 60000, - "retry_codes_name": "no_retry_1_codes", - "retry_params_name": "no_retry_1_params", - }, - "Rollback": { - "timeout_millis": 60000, - "retry_codes_name": "no_retry_1_codes", - "retry_params_name": "no_retry_1_params", - }, - "AllocateIds": { - "timeout_millis": 60000, - "retry_codes_name": "no_retry_1_codes", - "retry_params_name": "no_retry_1_params", - }, - }, - } - } -} diff --git a/google/cloud/datastore_v1/gapic/enums.py b/google/cloud/datastore_v1/gapic/enums.py deleted file mode 100644 index f84538a3..00000000 --- a/google/cloud/datastore_v1/gapic/enums.py +++ /dev/null @@ -1,165 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2020 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 -# -# https://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. - -"""Wrappers for protocol buffer enum types.""" - -import enum - - -class NullValue(enum.IntEnum): - """ - ``NullValue`` is a singleton enumeration to represent the null value - for the ``Value`` type union. - - The JSON representation for ``NullValue`` is JSON ``null``. - - Attributes: - NULL_VALUE (int): Null value. - """ - - NULL_VALUE = 0 - - -class CommitRequest(object): - class Mode(enum.IntEnum): - """ - The modes available for commits. - - Attributes: - MODE_UNSPECIFIED (int): Unspecified. This value must not be used. - TRANSACTIONAL (int): Transactional: The mutations are either all applied, or none are - applied. Learn about transactions - `here `__. - NON_TRANSACTIONAL (int): Non-transactional: The mutations may not apply as all or none. - """ - - MODE_UNSPECIFIED = 0 - TRANSACTIONAL = 1 - NON_TRANSACTIONAL = 2 - - -class CompositeFilter(object): - class Operator(enum.IntEnum): - """ - A composite filter operator. - - Attributes: - OPERATOR_UNSPECIFIED (int): Unspecified. This value must not be used. - AND (int): The results are required to satisfy each of the combined filters. - """ - - OPERATOR_UNSPECIFIED = 0 - AND = 1 - - -class EntityResult(object): - class ResultType(enum.IntEnum): - """ - Specifies what data the 'entity' field contains. A ``ResultType`` is - either implied (for example, in ``LookupResponse.missing`` from - ``datastore.proto``, it is always ``KEY_ONLY``) or specified by context - (for example, in message ``QueryResultBatch``, field - ``entity_result_type`` specifies a ``ResultType`` for all the values in - field ``entity_results``). - - Attributes: - RESULT_TYPE_UNSPECIFIED (int): Unspecified. This value is never used. - FULL (int): The key and properties. - PROJECTION (int): A projected subset of properties. The entity may have no key. - KEY_ONLY (int): Only the key. - """ - - RESULT_TYPE_UNSPECIFIED = 0 - FULL = 1 - PROJECTION = 2 - KEY_ONLY = 3 - - -class PropertyFilter(object): - class Operator(enum.IntEnum): - """ - A property filter operator. - - Attributes: - OPERATOR_UNSPECIFIED (int): Unspecified. This value must not be used. - LESS_THAN (int): Less than. - LESS_THAN_OR_EQUAL (int): Less than or equal. - GREATER_THAN (int): Greater than. - GREATER_THAN_OR_EQUAL (int): Greater than or equal. - EQUAL (int): Equal. - HAS_ANCESTOR (int): Has ancestor. - """ - - OPERATOR_UNSPECIFIED = 0 - LESS_THAN = 1 - LESS_THAN_OR_EQUAL = 2 - GREATER_THAN = 3 - GREATER_THAN_OR_EQUAL = 4 - EQUAL = 5 - HAS_ANCESTOR = 11 - - -class PropertyOrder(object): - class Direction(enum.IntEnum): - """ - The sort direction. - - Attributes: - DIRECTION_UNSPECIFIED (int): Unspecified. This value must not be used. - ASCENDING (int): Ascending. - DESCENDING (int): Descending. - """ - - DIRECTION_UNSPECIFIED = 0 - ASCENDING = 1 - DESCENDING = 2 - - -class QueryResultBatch(object): - class MoreResultsType(enum.IntEnum): - """ - The possible values for the ``more_results`` field. - - Attributes: - MORE_RESULTS_TYPE_UNSPECIFIED (int): Unspecified. This value is never used. - NOT_FINISHED (int): There may be additional batches to fetch from this query. - MORE_RESULTS_AFTER_LIMIT (int): The query is finished, but there may be more results after the limit. - MORE_RESULTS_AFTER_CURSOR (int): The query is finished, but there may be more results after the end - cursor. - NO_MORE_RESULTS (int): The query is finished, and there are no more results. - """ - - MORE_RESULTS_TYPE_UNSPECIFIED = 0 - NOT_FINISHED = 1 - MORE_RESULTS_AFTER_LIMIT = 2 - MORE_RESULTS_AFTER_CURSOR = 4 - NO_MORE_RESULTS = 3 - - -class ReadOptions(object): - class ReadConsistency(enum.IntEnum): - """ - The possible values for read consistencies. - - Attributes: - READ_CONSISTENCY_UNSPECIFIED (int): Unspecified. This value must not be used. - STRONG (int): Strong consistency. - EVENTUAL (int): Eventual consistency. - """ - - READ_CONSISTENCY_UNSPECIFIED = 0 - STRONG = 1 - EVENTUAL = 2 diff --git a/google/cloud/datastore_v1/gapic/transports/__init__.py b/google/cloud/datastore_v1/gapic/transports/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/google/cloud/datastore_v1/gapic/transports/datastore_grpc_transport.py b/google/cloud/datastore_v1/gapic/transports/datastore_grpc_transport.py deleted file mode 100644 index 74552d8a..00000000 --- a/google/cloud/datastore_v1/gapic/transports/datastore_grpc_transport.py +++ /dev/null @@ -1,205 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2020 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 -# -# https://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. - - -import google.api_core.grpc_helpers - -from google.cloud.datastore_v1.proto import datastore_pb2_grpc - - -class DatastoreGrpcTransport(object): - """gRPC transport class providing stubs for - google.datastore.v1 Datastore API. - - The transport provides access to the raw gRPC stubs, - which can be used to take advantage of advanced - features of gRPC. - """ - - # The scopes needed to make gRPC calls to all of the methods defined - # in this service. - _OAUTH_SCOPES = ( - "https://www.googleapis.com/auth/cloud-platform", - "https://www.googleapis.com/auth/datastore", - ) - - def __init__( - self, channel=None, credentials=None, address="datastore.googleapis.com:443" - ): - """Instantiate the transport class. - - Args: - channel (grpc.Channel): A ``Channel`` instance through - which to make calls. This argument is mutually exclusive - with ``credentials``; providing both will raise an exception. - credentials (google.auth.credentials.Credentials): The - authorization credentials to attach to requests. These - credentials identify this application to the service. If none - are specified, the client will attempt to ascertain the - credentials from the environment. - address (str): The address where the service is hosted. - """ - # If both `channel` and `credentials` are specified, raise an - # exception (channels come with credentials baked in already). - if channel is not None and credentials is not None: - raise ValueError( - "The `channel` and `credentials` arguments are mutually " "exclusive.", - ) - - # Create the channel. - if channel is None: - channel = self.create_channel( - address=address, - credentials=credentials, - options={ - "grpc.max_send_message_length": -1, - "grpc.max_receive_message_length": -1, - }.items(), - ) - - self._channel = channel - - # gRPC uses objects called "stubs" that are bound to the - # channel and provide a basic method for each RPC. - self._stubs = { - "datastore_stub": datastore_pb2_grpc.DatastoreStub(channel), - } - - @classmethod - def create_channel( - cls, address="datastore.googleapis.com:443", credentials=None, **kwargs - ): - """Create and return a gRPC channel object. - - Args: - address (str): The host for the channel to use. - credentials (~.Credentials): The - authorization credentials to attach to requests. These - credentials identify this application to the service. If - none are specified, the client will attempt to ascertain - the credentials from the environment. - kwargs (dict): Keyword arguments, which are passed to the - channel creation. - - Returns: - grpc.Channel: A gRPC channel object. - """ - return google.api_core.grpc_helpers.create_channel( - address, credentials=credentials, scopes=cls._OAUTH_SCOPES, **kwargs - ) - - @property - def channel(self): - """The gRPC channel used by the transport. - - Returns: - grpc.Channel: A gRPC channel object. - """ - return self._channel - - @property - def lookup(self): - """Return the gRPC stub for :meth:`DatastoreClient.lookup`. - - Looks up entities by key. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_stub"].Lookup - - @property - def run_query(self): - """Return the gRPC stub for :meth:`DatastoreClient.run_query`. - - Queries for entities. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_stub"].RunQuery - - @property - def reserve_ids(self): - """Return the gRPC stub for :meth:`DatastoreClient.reserve_ids`. - - Prevents the supplied keys' IDs from being auto-allocated by Cloud - Datastore. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_stub"].ReserveIds - - @property - def begin_transaction(self): - """Return the gRPC stub for :meth:`DatastoreClient.begin_transaction`. - - Begins a new transaction. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_stub"].BeginTransaction - - @property - def commit(self): - """Return the gRPC stub for :meth:`DatastoreClient.commit`. - - Commits a transaction, optionally creating, deleting or modifying some - entities. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_stub"].Commit - - @property - def rollback(self): - """Return the gRPC stub for :meth:`DatastoreClient.rollback`. - - Rolls back a transaction. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_stub"].Rollback - - @property - def allocate_ids(self): - """Return the gRPC stub for :meth:`DatastoreClient.allocate_ids`. - - Allocates IDs for the given keys, which is useful for referencing an entity - before it is inserted. - - Returns: - Callable: A callable which accepts the appropriate - deserialized request object and returns a - deserialized response object. - """ - return self._stubs["datastore_stub"].AllocateIds diff --git a/google/cloud/datastore_v1/proto/__init__.py b/google/cloud/datastore_v1/proto/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/google/cloud/datastore_v1/proto/datastore_admin.proto b/google/cloud/datastore_v1/proto/datastore_admin.proto deleted file mode 100644 index c730de79..00000000 --- a/google/cloud/datastore_v1/proto/datastore_admin.proto +++ /dev/null @@ -1,329 +0,0 @@ -// Copyright 2018 Google Inc. -// -// 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. - -syntax = "proto3"; - -package google.datastore.admin.v1beta1; - -import "google/api/annotations.proto"; -import "google/longrunning/operations.proto"; -import "google/protobuf/timestamp.proto"; - -option csharp_namespace = "Google.Cloud.Datastore.Admin.V1Beta1"; -option go_package = "google.golang.org/genproto/googleapis/datastore/admin/v1beta1;admin"; -option java_multiple_files = true; -option java_outer_classname = "DatastoreAdminProto"; -option java_package = "com.google.datastore.admin.v1beta1"; - - -// Google Cloud Datastore Admin API -// -// The Datastore Admin API provides several admin services for Cloud Datastore. -// -// ----------------------------------------------------------------------------- -// ## Concepts -// -// Project, namespace, kind, and entity as defined in the Google Cloud Datastore -// API. -// -// Operation: An Operation represents work being performed in the background. -// -// EntityFilter: Allows specifying a subset of entities in a project. This is -// specified as a combination of kinds and namespaces (either or both of which -// may be all). -// -// ----------------------------------------------------------------------------- -// ## Services -// -// # Export/Import -// -// The Export/Import service provides the ability to copy all or a subset of -// entities to/from Google Cloud Storage. -// -// Exported data may be imported into Cloud Datastore for any Google Cloud -// Platform project. It is not restricted to the export source project. It is -// possible to export from one project and then import into another. -// -// Exported data can also be loaded into Google BigQuery for analysis. -// -// Exports and imports are performed asynchronously. An Operation resource is -// created for each export/import. The state (including any errors encountered) -// of the export/import may be queried via the Operation resource. -// -// # Operation -// -// The Operations collection provides a record of actions performed for the -// specified project (including any operations in progress). Operations are not -// created directly but through calls on other collections or resources. -// -// An operation that is not yet done may be cancelled. The request to cancel is -// asynchronous and the operation may continue to run for some time after the -// request to cancel is made. -// -// An operation that is done may be deleted so that it is no longer listed as -// part of the Operation collection. -// -// ListOperations returns all pending operations, but not completed operations. -// -// Operations are created by service DatastoreAdmin, -// but are accessed via service google.longrunning.Operations. -service DatastoreAdmin { - // Exports a copy of all or a subset of entities from Google Cloud Datastore - // to another storage system, such as Google Cloud Storage. Recent updates to - // entities may not be reflected in the export. The export occurs in the - // background and its progress can be monitored and managed via the - // Operation resource that is created. The output of an export may only be - // used once the associated operation is done. If an export operation is - // cancelled before completion it may leave partial data behind in Google - // Cloud Storage. - rpc ExportEntities(ExportEntitiesRequest) returns (google.longrunning.Operation) { - option (google.api.http) = { - post: "/v1beta1/projects/{project_id}:export" - body: "*" - }; - } - - // Imports entities into Google Cloud Datastore. Existing entities with the - // same key are overwritten. The import occurs in the background and its - // progress can be monitored and managed via the Operation resource that is - // created. If an ImportEntities operation is cancelled, it is possible - // that a subset of the data has already been imported to Cloud Datastore. - rpc ImportEntities(ImportEntitiesRequest) returns (google.longrunning.Operation) { - option (google.api.http) = { - post: "/v1beta1/projects/{project_id}:import" - body: "*" - }; - } -} - -// Metadata common to all Datastore Admin operations. -message CommonMetadata { - // The various possible states for an ongoing Operation. - enum State { - // Unspecified. - STATE_UNSPECIFIED = 0; - - // Request is being prepared for processing. - INITIALIZING = 1; - - // Request is actively being processed. - PROCESSING = 2; - - // Request is in the process of being cancelled after user called - // google.longrunning.Operations.CancelOperation on the operation. - CANCELLING = 3; - - // Request has been processed and is in its finalization stage. - FINALIZING = 4; - - // Request has completed successfully. - SUCCESSFUL = 5; - - // Request has finished being processed, but encountered an error. - FAILED = 6; - - // Request has finished being cancelled after user called - // google.longrunning.Operations.CancelOperation. - CANCELLED = 7; - } - - // The time that work began on the operation. - google.protobuf.Timestamp start_time = 1; - - // The time the operation ended, either successfully or otherwise. - google.protobuf.Timestamp end_time = 2; - - // The type of the operation. Can be used as a filter in - // ListOperationsRequest. - OperationType operation_type = 3; - - // The client-assigned labels which were provided when the operation was - // created. May also include additional labels. - map labels = 4; - - // The current state of the Operation. - State state = 5; -} - -// Measures the progress of a particular metric. -message Progress { - // The amount of work that has been completed. Note that this may be greater - // than work_estimated. - int64 work_completed = 1; - - // An estimate of how much work needs to be performed. May be zero if the - // work estimate is unavailable. - int64 work_estimated = 2; -} - -// The request for -// [google.datastore.admin.v1beta1.DatastoreAdmin.ExportEntities][google.datastore.admin.v1beta1.DatastoreAdmin.ExportEntities]. -message ExportEntitiesRequest { - // Project ID against which to make the request. - string project_id = 1; - - // Client-assigned labels. - map labels = 2; - - // Description of what data from the project is included in the export. - EntityFilter entity_filter = 3; - - // Location for the export metadata and data files. - // - // The full resource URL of the external storage location. Currently, only - // Google Cloud Storage is supported. So output_url_prefix should be of the - // form: `gs://BUCKET_NAME[/NAMESPACE_PATH]`, where `BUCKET_NAME` is the - // name of the Cloud Storage bucket and `NAMESPACE_PATH` is an optional Cloud - // Storage namespace path (this is not a Cloud Datastore namespace). For more - // information about Cloud Storage namespace paths, see - // [Object name - // considerations](https://cloud.google.com/storage/docs/naming#object-considerations). - // - // The resulting files will be nested deeper than the specified URL prefix. - // The final output URL will be provided in the - // [google.datastore.admin.v1beta1.ExportEntitiesResponse.output_url][google.datastore.admin.v1beta1.ExportEntitiesResponse.output_url] - // field. That value should be used for subsequent ImportEntities operations. - // - // By nesting the data files deeper, the same Cloud Storage bucket can be used - // in multiple ExportEntities operations without conflict. - string output_url_prefix = 4; -} - -// The request for -// [google.datastore.admin.v1beta1.DatastoreAdmin.ImportEntities][google.datastore.admin.v1beta1.DatastoreAdmin.ImportEntities]. -message ImportEntitiesRequest { - // Project ID against which to make the request. - string project_id = 1; - - // Client-assigned labels. - map labels = 2; - - // The full resource URL of the external storage location. Currently, only - // Google Cloud Storage is supported. So input_url should be of the form: - // `gs://BUCKET_NAME[/NAMESPACE_PATH]/OVERALL_EXPORT_METADATA_FILE`, where - // `BUCKET_NAME` is the name of the Cloud Storage bucket, `NAMESPACE_PATH` is - // an optional Cloud Storage namespace path (this is not a Cloud Datastore - // namespace), and `OVERALL_EXPORT_METADATA_FILE` is the metadata file written - // by the ExportEntities operation. For more information about Cloud Storage - // namespace paths, see - // [Object name - // considerations](https://cloud.google.com/storage/docs/naming#object-considerations). - // - // For more information, see - // [google.datastore.admin.v1beta1.ExportEntitiesResponse.output_url][google.datastore.admin.v1beta1.ExportEntitiesResponse.output_url]. - string input_url = 3; - - // Optionally specify which kinds/namespaces are to be imported. If provided, - // the list must be a subset of the EntityFilter used in creating the export, - // otherwise a FAILED_PRECONDITION error will be returned. If no filter is - // specified then all entities from the export are imported. - EntityFilter entity_filter = 4; -} - -// The response for -// [google.datastore.admin.v1beta1.DatastoreAdmin.ExportEntities][google.datastore.admin.v1beta1.DatastoreAdmin.ExportEntities]. -message ExportEntitiesResponse { - // Location of the output metadata file. This can be used to begin an import - // into Cloud Datastore (this project or another project). See - // [google.datastore.admin.v1beta1.ImportEntitiesRequest.input_url][google.datastore.admin.v1beta1.ImportEntitiesRequest.input_url]. - // Only present if the operation completed successfully. - string output_url = 1; -} - -// Metadata for ExportEntities operations. -message ExportEntitiesMetadata { - // Metadata common to all Datastore Admin operations. - CommonMetadata common = 1; - - // An estimate of the number of entities processed. - Progress progress_entities = 2; - - // An estimate of the number of bytes processed. - Progress progress_bytes = 3; - - // Description of which entities are being exported. - EntityFilter entity_filter = 4; - - // Location for the export metadata and data files. This will be the same - // value as the - // [google.datastore.admin.v1beta1.ExportEntitiesRequest.output_url_prefix][google.datastore.admin.v1beta1.ExportEntitiesRequest.output_url_prefix] - // field. The final output location is provided in - // [google.datastore.admin.v1beta1.ExportEntitiesResponse.output_url][google.datastore.admin.v1beta1.ExportEntitiesResponse.output_url]. - string output_url_prefix = 5; -} - -// Metadata for ImportEntities operations. -message ImportEntitiesMetadata { - // Metadata common to all Datastore Admin operations. - CommonMetadata common = 1; - - // An estimate of the number of entities processed. - Progress progress_entities = 2; - - // An estimate of the number of bytes processed. - Progress progress_bytes = 3; - - // Description of which entities are being imported. - EntityFilter entity_filter = 4; - - // The location of the import metadata file. This will be the same value as - // the [google.datastore.admin.v1beta1.ExportEntitiesResponse.output_url][google.datastore.admin.v1beta1.ExportEntitiesResponse.output_url] - // field. - string input_url = 5; -} - -// Identifies a subset of entities in a project. This is specified as -// combinations of kinds and namespaces (either or both of which may be all, as -// described in the following examples). -// Example usage: -// -// Entire project: -// kinds=[], namespace_ids=[] -// -// Kinds Foo and Bar in all namespaces: -// kinds=['Foo', 'Bar'], namespace_ids=[] -// -// Kinds Foo and Bar only in the default namespace: -// kinds=['Foo', 'Bar'], namespace_ids=[''] -// -// Kinds Foo and Bar in both the default and Baz namespaces: -// kinds=['Foo', 'Bar'], namespace_ids=['', 'Baz'] -// -// The entire Baz namespace: -// kinds=[], namespace_ids=['Baz'] -message EntityFilter { - // If empty, then this represents all kinds. - repeated string kinds = 1; - - // An empty list represents all namespaces. This is the preferred - // usage for projects that don't use namespaces. - // - // An empty string element represents the default namespace. This should be - // used if the project has data in non-default namespaces, but doesn't want to - // include them. - // Each namespace in this list must be unique. - repeated string namespace_ids = 2; -} - -// Operation types. -enum OperationType { - // Unspecified. - OPERATION_TYPE_UNSPECIFIED = 0; - - // ExportEntities. - EXPORT_ENTITIES = 1; - - // ImportEntities. - IMPORT_ENTITIES = 2; -} diff --git a/google/cloud/datastore_v1/proto/datastore_pb2.py b/google/cloud/datastore_v1/proto/datastore_pb2.py deleted file mode 100644 index cf7a3cfd..00000000 --- a/google/cloud/datastore_v1/proto/datastore_pb2.py +++ /dev/null @@ -1,2159 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: google/cloud/datastore_v1/proto/datastore.proto -"""Generated protocol buffer code.""" -from google.protobuf import descriptor as _descriptor -from google.protobuf import message as _message -from google.protobuf import reflection as _reflection -from google.protobuf import symbol_database as _symbol_database - -# @@protoc_insertion_point(imports) - -_sym_db = _symbol_database.Default() - - -from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2 -from google.api import client_pb2 as google_dot_api_dot_client__pb2 -from google.api import field_behavior_pb2 as google_dot_api_dot_field__behavior__pb2 -from google.cloud.datastore_v1.proto import ( - entity_pb2 as google_dot_cloud_dot_datastore__v1_dot_proto_dot_entity__pb2, -) -from google.cloud.datastore_v1.proto import ( - query_pb2 as google_dot_cloud_dot_datastore__v1_dot_proto_dot_query__pb2, -) - - -DESCRIPTOR = _descriptor.FileDescriptor( - name="google/cloud/datastore_v1/proto/datastore.proto", - package="google.datastore.v1", - syntax="proto3", - serialized_options=b"\n\027com.google.datastore.v1B\016DatastoreProtoP\001Z, <=, >=, and =. - ASCENDING = 1; - - // The property's values are indexed so as to support sequencing in - // descending order and also query by <, >, <=, >=, and =. - DESCENDING = 2; - } - - // The possible set of states of an index. - enum State { - // The state is unspecified. - STATE_UNSPECIFIED = 0; - - // The index is being created, and cannot be used by queries. - // There is an active long-running operation for the index. - // The index is updated when writing an entity. - // Some index data may exist. - CREATING = 1; - - // The index is ready to be used. - // The index is updated when writing an entity. - // The index is fully populated from all stored entities it applies to. - READY = 2; - - // The index is being deleted, and cannot be used by queries. - // There is an active long-running operation for the index. - // The index is not updated when writing an entity. - // Some index data may exist. - DELETING = 3; - - // The index was being created or deleted, but something went wrong. - // The index cannot by used by queries. - // There is no active long-running operation for the index, - // and the most recently finished long-running operation failed. - // The index is not updated when writing an entity. - // Some index data may exist. - ERROR = 4; - } - - // Project ID. - // Output only. - string project_id = 1; - - // The resource ID of the index. - // Output only. - string index_id = 3; - - // The entity kind to which this index applies. - // Required. - string kind = 4; - - // The index's ancestor mode. Must not be ANCESTOR_MODE_UNSPECIFIED. - // Required. - AncestorMode ancestor = 5; - - // An ordered sequence of property names and their index attributes. - // Required. - repeated IndexedProperty properties = 6; - - // The state of the index. - // Output only. - State state = 7; -} diff --git a/google/cloud/datastore_v1/proto/query_pb2.py b/google/cloud/datastore_v1/proto/query_pb2.py deleted file mode 100644 index e3bd1141..00000000 --- a/google/cloud/datastore_v1/proto/query_pb2.py +++ /dev/null @@ -1,1728 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: google/cloud/datastore_v1/proto/query.proto -"""Generated protocol buffer code.""" -from google.protobuf import descriptor as _descriptor -from google.protobuf import message as _message -from google.protobuf import reflection as _reflection -from google.protobuf import symbol_database as _symbol_database - -# @@protoc_insertion_point(imports) - -_sym_db = _symbol_database.Default() - - -from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2 -from google.cloud.datastore_v1.proto import ( - entity_pb2 as google_dot_cloud_dot_datastore__v1_dot_proto_dot_entity__pb2, -) -from google.protobuf import wrappers_pb2 as google_dot_protobuf_dot_wrappers__pb2 -from google.type import latlng_pb2 as google_dot_type_dot_latlng__pb2 - - -DESCRIPTOR = _descriptor.FileDescriptor( - name="google/cloud/datastore_v1/proto/query.proto", - package="google.datastore.v1", - syntax="proto3", - serialized_options=b"\n\027com.google.datastore.v1B\nQueryProtoP\001Z\n\x0fproperty_filter\x18\x02 \x01(\x0b\x32#.google.datastore.v1.PropertyFilterH\x00\x42\r\n\x0b\x66ilter_type"\xa9\x01\n\x0f\x43ompositeFilter\x12\x39\n\x02op\x18\x01 \x01(\x0e\x32-.google.datastore.v1.CompositeFilter.Operator\x12,\n\x07\x66ilters\x18\x02 \x03(\x0b\x32\x1b.google.datastore.v1.Filter"-\n\x08Operator\x12\x18\n\x14OPERATOR_UNSPECIFIED\x10\x00\x12\x07\n\x03\x41ND\x10\x01"\xc7\x02\n\x0ePropertyFilter\x12\x38\n\x08property\x18\x01 \x01(\x0b\x32&.google.datastore.v1.PropertyReference\x12\x38\n\x02op\x18\x02 \x01(\x0e\x32,.google.datastore.v1.PropertyFilter.Operator\x12)\n\x05value\x18\x03 \x01(\x0b\x32\x1a.google.datastore.v1.Value"\x95\x01\n\x08Operator\x12\x18\n\x14OPERATOR_UNSPECIFIED\x10\x00\x12\r\n\tLESS_THAN\x10\x01\x12\x16\n\x12LESS_THAN_OR_EQUAL\x10\x02\x12\x10\n\x0cGREATER_THAN\x10\x03\x12\x19\n\x15GREATER_THAN_OR_EQUAL\x10\x04\x12\t\n\x05\x45QUAL\x10\x05\x12\x10\n\x0cHAS_ANCESTOR\x10\x0b"\xa5\x02\n\x08GqlQuery\x12\x14\n\x0cquery_string\x18\x01 \x01(\t\x12\x16\n\x0e\x61llow_literals\x18\x02 \x01(\x08\x12H\n\x0enamed_bindings\x18\x05 \x03(\x0b\x32\x30.google.datastore.v1.GqlQuery.NamedBindingsEntry\x12\x43\n\x13positional_bindings\x18\x04 \x03(\x0b\x32&.google.datastore.v1.GqlQueryParameter\x1a\\\n\x12NamedBindingsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\x35\n\x05value\x18\x02 \x01(\x0b\x32&.google.datastore.v1.GqlQueryParameter:\x02\x38\x01"d\n\x11GqlQueryParameter\x12+\n\x05value\x18\x02 \x01(\x0b\x32\x1a.google.datastore.v1.ValueH\x00\x12\x10\n\x06\x63ursor\x18\x03 \x01(\x0cH\x00\x42\x10\n\x0eparameter_type"\xde\x03\n\x10QueryResultBatch\x12\x17\n\x0fskipped_results\x18\x06 \x01(\x05\x12\x16\n\x0eskipped_cursor\x18\x03 \x01(\x0c\x12H\n\x12\x65ntity_result_type\x18\x01 \x01(\x0e\x32,.google.datastore.v1.EntityResult.ResultType\x12\x39\n\x0e\x65ntity_results\x18\x02 \x03(\x0b\x32!.google.datastore.v1.EntityResult\x12\x12\n\nend_cursor\x18\x04 \x01(\x0c\x12K\n\x0cmore_results\x18\x05 \x01(\x0e\x32\x35.google.datastore.v1.QueryResultBatch.MoreResultsType\x12\x18\n\x10snapshot_version\x18\x07 \x01(\x03"\x98\x01\n\x0fMoreResultsType\x12!\n\x1dMORE_RESULTS_TYPE_UNSPECIFIED\x10\x00\x12\x10\n\x0cNOT_FINISHED\x10\x01\x12\x1c\n\x18MORE_RESULTS_AFTER_LIMIT\x10\x02\x12\x1d\n\x19MORE_RESULTS_AFTER_CURSOR\x10\x04\x12\x13\n\x0fNO_MORE_RESULTS\x10\x03\x42\xbc\x01\n\x17\x63om.google.datastore.v1B\nQueryProtoP\x01Z`__. - end_cursor: - An ending point for the query results. Query cursors are - returned in query result batches and `can only be used to - limit the same query `__. - offset: - The number of results to skip. Applies before limit, but after - all other constraints. Optional. Must be >= 0 if specified. - limit: - The maximum number of results to return. Applies after all - other constraints. Optional. Unspecified is interpreted as no - limit. Must be >= 0 if specified. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.Query) - }, -) -_sym_db.RegisterMessage(Query) - -KindExpression = _reflection.GeneratedProtocolMessageType( - "KindExpression", - (_message.Message,), - { - "DESCRIPTOR": _KINDEXPRESSION, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A representation of a kind. - - Attributes: - name: - The name of the kind. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.KindExpression) - }, -) -_sym_db.RegisterMessage(KindExpression) - -PropertyReference = _reflection.GeneratedProtocolMessageType( - "PropertyReference", - (_message.Message,), - { - "DESCRIPTOR": _PROPERTYREFERENCE, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A reference to a property relative to the kind expressions. - - Attributes: - name: - The name of the property. If name includes “.”s, it may be - interpreted as a property name path. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.PropertyReference) - }, -) -_sym_db.RegisterMessage(PropertyReference) - -Projection = _reflection.GeneratedProtocolMessageType( - "Projection", - (_message.Message,), - { - "DESCRIPTOR": _PROJECTION, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A representation of a property in a projection. - - Attributes: - property: - The property to project. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.Projection) - }, -) -_sym_db.RegisterMessage(Projection) - -PropertyOrder = _reflection.GeneratedProtocolMessageType( - "PropertyOrder", - (_message.Message,), - { - "DESCRIPTOR": _PROPERTYORDER, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """The desired order for a specific property. - - Attributes: - property: - The property to order by. - direction: - The direction to order by. Defaults to ``ASCENDING``. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.PropertyOrder) - }, -) -_sym_db.RegisterMessage(PropertyOrder) - -Filter = _reflection.GeneratedProtocolMessageType( - "Filter", - (_message.Message,), - { - "DESCRIPTOR": _FILTER, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A holder for any type of filter. - - Attributes: - filter_type: - The type of filter. - composite_filter: - A composite filter. - property_filter: - A filter on a property. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.Filter) - }, -) -_sym_db.RegisterMessage(Filter) - -CompositeFilter = _reflection.GeneratedProtocolMessageType( - "CompositeFilter", - (_message.Message,), - { - "DESCRIPTOR": _COMPOSITEFILTER, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A filter that merges multiple other filters using the given operator. - - Attributes: - op: - The operator for combining multiple filters. - filters: - The list of filters to combine. Must contain at least one - filter. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.CompositeFilter) - }, -) -_sym_db.RegisterMessage(CompositeFilter) - -PropertyFilter = _reflection.GeneratedProtocolMessageType( - "PropertyFilter", - (_message.Message,), - { - "DESCRIPTOR": _PROPERTYFILTER, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A filter on a specific property. - - Attributes: - property: - The property to filter by. - op: - The operator to filter by. - value: - The value to compare the property to. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.PropertyFilter) - }, -) -_sym_db.RegisterMessage(PropertyFilter) - -GqlQuery = _reflection.GeneratedProtocolMessageType( - "GqlQuery", - (_message.Message,), - { - "NamedBindingsEntry": _reflection.GeneratedProtocolMessageType( - "NamedBindingsEntry", - (_message.Message,), - { - "DESCRIPTOR": _GQLQUERY_NAMEDBINDINGSENTRY, - "__module__": "google.cloud.datastore_v1.proto.query_pb2" - # @@protoc_insertion_point(class_scope:google.datastore.v1.GqlQuery.NamedBindingsEntry) - }, - ), - "DESCRIPTOR": _GQLQUERY, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A `GQL query - `__. - - Attributes: - query_string: - A string of the format described `here `__. - allow_literals: - When false, the query string must not contain any literals and - instead must bind all values. For example, ``SELECT * FROM - Kind WHERE a = 'string literal'`` is not allowed, while - ``SELECT * FROM Kind WHERE a = @value`` is. - named_bindings: - For each non-reserved named binding site in the query string, - there must be a named parameter with that name, but not - necessarily the inverse. Key must match regex ``[A-Za- - z_$][A-Za-z_$0-9]*``, must not match regex ``__.*__``, and - must not be ``""``. - positional_bindings: - Numbered binding site @1 references the first numbered - parameter, effectively using 1-based indexing, rather than the - usual 0. For each binding site numbered i in - ``query_string``, there must be an i-th numbered parameter. - The inverse must also be true. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.GqlQuery) - }, -) -_sym_db.RegisterMessage(GqlQuery) -_sym_db.RegisterMessage(GqlQuery.NamedBindingsEntry) - -GqlQueryParameter = _reflection.GeneratedProtocolMessageType( - "GqlQueryParameter", - (_message.Message,), - { - "DESCRIPTOR": _GQLQUERYPARAMETER, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A binding parameter for a GQL query. - - Attributes: - parameter_type: - The type of parameter. - value: - A value parameter. - cursor: - A query cursor. Query cursors are returned in query result - batches. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.GqlQueryParameter) - }, -) -_sym_db.RegisterMessage(GqlQueryParameter) - -QueryResultBatch = _reflection.GeneratedProtocolMessageType( - "QueryResultBatch", - (_message.Message,), - { - "DESCRIPTOR": _QUERYRESULTBATCH, - "__module__": "google.cloud.datastore_v1.proto.query_pb2", - "__doc__": """A batch of results produced by a query. - - Attributes: - skipped_results: - The number of results skipped, typically because of an offset. - skipped_cursor: - A cursor that points to the position after the last skipped - result. Will be set when ``skipped_results`` != 0. - entity_result_type: - The result type for every entity in ``entity_results``. - entity_results: - The results for this batch. - end_cursor: - A cursor that points to the position after the last result in - the batch. - more_results: - The state of the query after the current batch. - snapshot_version: - The version number of the snapshot this batch was returned - from. This applies to the range of results from the query’s - ``start_cursor`` (or the beginning of the query if no cursor - was given) to this batch’s ``end_cursor`` (not the query’s - ``end_cursor``). In a single transaction, subsequent query - result batches for the same query can have a greater snapshot - version number. Each batch’s snapshot version is valid for all - preceding batches. The value will be zero for eventually - consistent queries. - """, - # @@protoc_insertion_point(class_scope:google.datastore.v1.QueryResultBatch) - }, -) -_sym_db.RegisterMessage(QueryResultBatch) - - -DESCRIPTOR._options = None -_GQLQUERY_NAMEDBINDINGSENTRY._options = None -# @@protoc_insertion_point(module_scope) diff --git a/google/cloud/datastore_v1/proto/query_pb2_grpc.py b/google/cloud/datastore_v1/proto/query_pb2_grpc.py deleted file mode 100644 index 8a939394..00000000 --- a/google/cloud/datastore_v1/proto/query_pb2_grpc.py +++ /dev/null @@ -1,3 +0,0 @@ -# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! -"""Client and server classes corresponding to protobuf-defined services.""" -import grpc diff --git a/google/cloud/datastore_v1/py.typed b/google/cloud/datastore_v1/py.typed new file mode 100644 index 00000000..e82a9319 --- /dev/null +++ b/google/cloud/datastore_v1/py.typed @@ -0,0 +1,2 @@ +# Marker file for PEP 561. +# The google-cloud-datastore package uses inline types. diff --git a/google/cloud/datastore_v1/services/__init__.py b/google/cloud/datastore_v1/services/__init__.py new file mode 100644 index 00000000..42ffdf2b --- /dev/null +++ b/google/cloud/datastore_v1/services/__init__.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# diff --git a/google/cloud/datastore_v1/services/datastore/__init__.py b/google/cloud/datastore_v1/services/datastore/__init__.py new file mode 100644 index 00000000..a8a82886 --- /dev/null +++ b/google/cloud/datastore_v1/services/datastore/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 .client import DatastoreClient +from .async_client import DatastoreAsyncClient + +__all__ = ( + "DatastoreClient", + "DatastoreAsyncClient", +) diff --git a/google/cloud/datastore_v1/services/datastore/async_client.py b/google/cloud/datastore_v1/services/datastore/async_client.py new file mode 100644 index 00000000..cc1760e0 --- /dev/null +++ b/google/cloud/datastore_v1/services/datastore/async_client.py @@ -0,0 +1,671 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 collections import OrderedDict +import functools +import re +from typing import Dict, Sequence, Tuple, Type, Union +import pkg_resources + +import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import exceptions # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google.api_core import retry as retries # type: ignore +from google.auth import credentials # type: ignore +from google.oauth2 import service_account # type: ignore + +from google.cloud.datastore_v1.types import datastore +from google.cloud.datastore_v1.types import entity +from google.cloud.datastore_v1.types import query + +from .transports.base import DatastoreTransport, DEFAULT_CLIENT_INFO +from .transports.grpc_asyncio import DatastoreGrpcAsyncIOTransport +from .client import DatastoreClient + + +class DatastoreAsyncClient: + """Each RPC normalizes the partition IDs of the keys in its + input entities, and always returns entities with keys with + normalized partition IDs. This applies to all keys and entities, + including those in values, except keys with both an empty path + and an empty or unset partition ID. Normalization of input keys + sets the project ID (if not already set) to the project ID from + the request. + """ + + _client: DatastoreClient + + DEFAULT_ENDPOINT = DatastoreClient.DEFAULT_ENDPOINT + DEFAULT_MTLS_ENDPOINT = DatastoreClient.DEFAULT_MTLS_ENDPOINT + + from_service_account_file = DatastoreClient.from_service_account_file + from_service_account_json = from_service_account_file + + get_transport_class = functools.partial( + type(DatastoreClient).get_transport_class, type(DatastoreClient) + ) + + def __init__( + self, + *, + credentials: credentials.Credentials = None, + transport: Union[str, DatastoreTransport] = "grpc_asyncio", + client_options: ClientOptions = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiate the datastore client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Union[str, ~.DatastoreTransport]): The + transport to use. If set to None, a transport is chosen + automatically. + client_options (ClientOptions): Custom options for the client. It + won't take effect if a ``transport`` instance is provided. + (1) The ``api_endpoint`` property can be used to override the + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT + environment variable can also be used to override the endpoint: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + """ + + self._client = DatastoreClient( + credentials=credentials, + transport=transport, + client_options=client_options, + client_info=client_info, + ) + + async def lookup( + self, + request: datastore.LookupRequest = None, + *, + project_id: str = None, + read_options: datastore.ReadOptions = None, + keys: Sequence[entity.Key] = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.LookupResponse: + r"""Looks up entities by key. + + Args: + request (:class:`~.datastore.LookupRequest`): + The request object. The request for + [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + read_options (:class:`~.datastore.ReadOptions`): + The options for this lookup request. + This corresponds to the ``read_options`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + keys (:class:`Sequence[~.entity.Key]`): + Required. Keys of entities to look + up. + This corresponds to the ``keys`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.LookupResponse: + The response for + [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + if request is not None and any([project_id, read_options, keys]): + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + request = datastore.LookupRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if read_options is not None: + request.read_options = read_options + if keys is not None: + request.keys = keys + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.lookup, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def run_query( + self, + request: datastore.RunQueryRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.RunQueryResponse: + r"""Queries for entities. + + Args: + request (:class:`~.datastore.RunQueryRequest`): + The request object. The request for + [Datastore.RunQuery][google.datastore.v1.Datastore.RunQuery]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.RunQueryResponse: + The response for + [Datastore.RunQuery][google.datastore.v1.Datastore.RunQuery]. + + """ + # Create or coerce a protobuf request object. + + request = datastore.RunQueryRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.run_query, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def begin_transaction( + self, + request: datastore.BeginTransactionRequest = None, + *, + project_id: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.BeginTransactionResponse: + r"""Begins a new transaction. + + Args: + request (:class:`~.datastore.BeginTransactionRequest`): + The request object. The request for + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.BeginTransactionResponse: + The response for + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + if request is not None and any([project_id]): + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + request = datastore.BeginTransactionRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.begin_transaction, + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def commit( + self, + request: datastore.CommitRequest = None, + *, + project_id: str = None, + mode: datastore.CommitRequest.Mode = None, + transaction: bytes = None, + mutations: Sequence[datastore.Mutation] = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.CommitResponse: + r"""Commits a transaction, optionally creating, deleting + or modifying some entities. + + Args: + request (:class:`~.datastore.CommitRequest`): + The request object. The request for + [Datastore.Commit][google.datastore.v1.Datastore.Commit]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + mode (:class:`~.datastore.CommitRequest.Mode`): + The type of commit to perform. Defaults to + ``TRANSACTIONAL``. + This corresponds to the ``mode`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + transaction (:class:`bytes`): + The identifier of the transaction associated with the + commit. A transaction identifier is returned by a call + to + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + This corresponds to the ``transaction`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + mutations (:class:`Sequence[~.datastore.Mutation]`): + The mutations to perform. + + When mode is ``TRANSACTIONAL``, mutations affecting a + single entity are applied in order. The following + sequences of mutations affecting a single entity are not + permitted in a single ``Commit`` request: + + - ``insert`` followed by ``insert`` + - ``update`` followed by ``insert`` + - ``upsert`` followed by ``insert`` + - ``delete`` followed by ``update`` + + When mode is ``NON_TRANSACTIONAL``, no two mutations may + affect a single entity. + This corresponds to the ``mutations`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.CommitResponse: + The response for + [Datastore.Commit][google.datastore.v1.Datastore.Commit]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + if request is not None and any([project_id, mode, transaction, mutations]): + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + request = datastore.CommitRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if mode is not None: + request.mode = mode + if transaction is not None: + request.transaction = transaction + if mutations is not None: + request.mutations = mutations + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.commit, + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def rollback( + self, + request: datastore.RollbackRequest = None, + *, + project_id: str = None, + transaction: bytes = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.RollbackResponse: + r"""Rolls back a transaction. + + Args: + request (:class:`~.datastore.RollbackRequest`): + The request object. The request for + [Datastore.Rollback][google.datastore.v1.Datastore.Rollback]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + transaction (:class:`bytes`): + Required. The transaction identifier, returned by a call + to + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + This corresponds to the ``transaction`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.RollbackResponse: + The response for + [Datastore.Rollback][google.datastore.v1.Datastore.Rollback]. + (an empty message). + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + if request is not None and any([project_id, transaction]): + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + request = datastore.RollbackRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if transaction is not None: + request.transaction = transaction + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.rollback, + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def allocate_ids( + self, + request: datastore.AllocateIdsRequest = None, + *, + project_id: str = None, + keys: Sequence[entity.Key] = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.AllocateIdsResponse: + r"""Allocates IDs for the given keys, which is useful for + referencing an entity before it is inserted. + + Args: + request (:class:`~.datastore.AllocateIdsRequest`): + The request object. The request for + [Datastore.AllocateIds][google.datastore.v1.Datastore.AllocateIds]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + keys (:class:`Sequence[~.entity.Key]`): + Required. A list of keys with + incomplete key paths for which to + allocate IDs. No key may be + reserved/read-only. + This corresponds to the ``keys`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.AllocateIdsResponse: + The response for + [Datastore.AllocateIds][google.datastore.v1.Datastore.AllocateIds]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + if request is not None and any([project_id, keys]): + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + request = datastore.AllocateIdsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if keys is not None: + request.keys = keys + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.allocate_ids, + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + async def reserve_ids( + self, + request: datastore.ReserveIdsRequest = None, + *, + project_id: str = None, + keys: Sequence[entity.Key] = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.ReserveIdsResponse: + r"""Prevents the supplied keys' IDs from being auto- + llocated by Cloud Datastore. + + Args: + request (:class:`~.datastore.ReserveIdsRequest`): + The request object. The request for + [Datastore.ReserveIds][google.datastore.v1.Datastore.ReserveIds]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + keys (:class:`Sequence[~.entity.Key]`): + Required. A list of keys with + complete key paths whose numeric IDs + should not be auto-allocated. + This corresponds to the ``keys`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.ReserveIdsResponse: + The response for + [Datastore.ReserveIds][google.datastore.v1.Datastore.ReserveIds]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + if request is not None and any([project_id, keys]): + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + request = datastore.ReserveIdsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if keys is not None: + request.keys = keys + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = gapic_v1.method_async.wrap_method( + self._client._transport.reserve_ids, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=60.0, + client_info=DEFAULT_CLIENT_INFO, + ) + + # Send the request. + response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution("google-cloud-datastore",).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + +__all__ = ("DatastoreAsyncClient",) diff --git a/google/cloud/datastore_v1/services/datastore/client.py b/google/cloud/datastore_v1/services/datastore/client.py new file mode 100644 index 00000000..5271a96a --- /dev/null +++ b/google/cloud/datastore_v1/services/datastore/client.py @@ -0,0 +1,806 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 collections import OrderedDict +from distutils import util +import os +import re +from typing import Callable, Dict, Sequence, Tuple, Type, Union +import pkg_resources + +import google.api_core.client_options as ClientOptions # type: ignore +from google.api_core import exceptions # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google.api_core import retry as retries # type: ignore +from google.auth import credentials # type: ignore +from google.auth.transport import mtls # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth.exceptions import MutualTLSChannelError # type: ignore +from google.oauth2 import service_account # type: ignore + +from google.cloud.datastore_v1.types import datastore +from google.cloud.datastore_v1.types import entity +from google.cloud.datastore_v1.types import query + +from .transports.base import DatastoreTransport, DEFAULT_CLIENT_INFO +from .transports.grpc import DatastoreGrpcTransport +from .transports.grpc_asyncio import DatastoreGrpcAsyncIOTransport + + +class DatastoreClientMeta(type): + """Metaclass for the Datastore client. + + This provides class-level methods for building and retrieving + support objects (e.g. transport) without polluting the client instance + objects. + """ + + _transport_registry = OrderedDict() # type: Dict[str, Type[DatastoreTransport]] + _transport_registry["grpc"] = DatastoreGrpcTransport + _transport_registry["grpc_asyncio"] = DatastoreGrpcAsyncIOTransport + + def get_transport_class(cls, label: str = None,) -> Type[DatastoreTransport]: + """Return an appropriate transport class. + + Args: + label: The name of the desired transport. If none is + provided, then the first transport in the registry is used. + + Returns: + The transport class to use. + """ + # If a specific transport is requested, return that one. + if label: + return cls._transport_registry[label] + + # No transport is requested; return the default (that is, the first one + # in the dictionary). + return next(iter(cls._transport_registry.values())) + + +class DatastoreClient(metaclass=DatastoreClientMeta): + """Each RPC normalizes the partition IDs of the keys in its + input entities, and always returns entities with keys with + normalized partition IDs. This applies to all keys and entities, + including those in values, except keys with both an empty path + and an empty or unset partition ID. Normalization of input keys + sets the project ID (if not already set) to the project ID from + the request. + """ + + @staticmethod + def _get_default_mtls_endpoint(api_endpoint): + """Convert api endpoint to mTLS endpoint. + Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to + "*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively. + Args: + api_endpoint (Optional[str]): the api endpoint to convert. + Returns: + str: converted mTLS api endpoint. + """ + if not api_endpoint: + return api_endpoint + + mtls_endpoint_re = re.compile( + r"(?P[^.]+)(?P\.mtls)?(?P\.sandbox)?(?P\.googleapis\.com)?" + ) + + m = mtls_endpoint_re.match(api_endpoint) + name, mtls, sandbox, googledomain = m.groups() + if mtls or not googledomain: + return api_endpoint + + if sandbox: + return api_endpoint.replace( + "sandbox.googleapis.com", "mtls.sandbox.googleapis.com" + ) + + return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com") + + DEFAULT_ENDPOINT = "datastore.googleapis.com" + DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint.__func__( # type: ignore + DEFAULT_ENDPOINT + ) + + @classmethod + def from_service_account_file(cls, filename: str, *args, **kwargs): + """Creates an instance of this client using the provided credentials + file. + + Args: + filename (str): The path to the service account private key json + file. + args: Additional arguments to pass to the constructor. + kwargs: Additional arguments to pass to the constructor. + + Returns: + {@api.name}: The constructed client. + """ + credentials = service_account.Credentials.from_service_account_file(filename) + kwargs["credentials"] = credentials + return cls(*args, **kwargs) + + from_service_account_json = from_service_account_file + + def __init__( + self, + *, + credentials: credentials.Credentials = None, + transport: Union[str, DatastoreTransport] = None, + client_options: ClientOptions = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiate the datastore client. + + Args: + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + transport (Union[str, ~.DatastoreTransport]): The + transport to use. If set to None, a transport is chosen + automatically. + client_options (ClientOptions): Custom options for the client. It + won't take effect if a ``transport`` instance is provided. + (1) The ``api_endpoint`` property can be used to override the + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT + environment variable can also be used to override the endpoint: + "always" (always use the default mTLS endpoint), "never" (always + use the default regular endpoint) and "auto" (auto switch to the + default mTLS endpoint if client certificate is present, this is + the default value). However, the ``api_endpoint`` property takes + precedence if provided. + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable + is "true", then the ``client_cert_source`` property can be used + to provide client certificate for mutual TLS transport. If + not provided, the default SSL client certificate will be used if + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not + set, no client certificate will be used. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + """ + if isinstance(client_options, dict): + client_options = ClientOptions.from_dict(client_options) + if client_options is None: + client_options = ClientOptions.ClientOptions() + + # Create SSL credentials for mutual TLS if needed. + use_client_cert = bool( + util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")) + ) + + ssl_credentials = None + is_mtls = False + if use_client_cert: + if client_options.client_cert_source: + import grpc # type: ignore + + cert, key = client_options.client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + is_mtls = True + else: + creds = SslCredentials() + is_mtls = creds.is_mtls + ssl_credentials = creds.ssl_credentials if is_mtls else None + + # Figure out which api endpoint to use. + if client_options.api_endpoint is not None: + api_endpoint = client_options.api_endpoint + else: + use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto") + if use_mtls_env == "never": + api_endpoint = self.DEFAULT_ENDPOINT + elif use_mtls_env == "always": + api_endpoint = self.DEFAULT_MTLS_ENDPOINT + elif use_mtls_env == "auto": + api_endpoint = ( + self.DEFAULT_MTLS_ENDPOINT if is_mtls else self.DEFAULT_ENDPOINT + ) + else: + raise MutualTLSChannelError( + "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always" + ) + + # Save or instantiate the transport. + # Ordinarily, we provide the transport, but allowing a custom transport + # instance provides an extensibility point for unusual situations. + if isinstance(transport, DatastoreTransport): + # transport is a DatastoreTransport instance. + if credentials or client_options.credentials_file: + raise ValueError( + "When providing a transport instance, " + "provide its credentials directly." + ) + if client_options.scopes: + raise ValueError( + "When providing a transport instance, " + "provide its scopes directly." + ) + self._transport = transport + else: + Transport = type(self).get_transport_class(transport) + self._transport = Transport( + credentials=credentials, + credentials_file=client_options.credentials_file, + host=api_endpoint, + scopes=client_options.scopes, + ssl_channel_credentials=ssl_credentials, + quota_project_id=client_options.quota_project_id, + client_info=client_info, + ) + + def lookup( + self, + request: datastore.LookupRequest = None, + *, + project_id: str = None, + read_options: datastore.ReadOptions = None, + keys: Sequence[entity.Key] = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.LookupResponse: + r"""Looks up entities by key. + + Args: + request (:class:`~.datastore.LookupRequest`): + The request object. The request for + [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + read_options (:class:`~.datastore.ReadOptions`): + The options for this lookup request. + This corresponds to the ``read_options`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + keys (:class:`Sequence[~.entity.Key]`): + Required. Keys of entities to look + up. + This corresponds to the ``keys`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.LookupResponse: + The response for + [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([project_id, read_options, keys]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a datastore.LookupRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore.LookupRequest): + request = datastore.LookupRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if read_options is not None: + request.read_options = read_options + if keys is not None: + request.keys = keys + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.lookup] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def run_query( + self, + request: datastore.RunQueryRequest = None, + *, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.RunQueryResponse: + r"""Queries for entities. + + Args: + request (:class:`~.datastore.RunQueryRequest`): + The request object. The request for + [Datastore.RunQuery][google.datastore.v1.Datastore.RunQuery]. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.RunQueryResponse: + The response for + [Datastore.RunQuery][google.datastore.v1.Datastore.RunQuery]. + + """ + # Create or coerce a protobuf request object. + + # Minor optimization to avoid making a copy if the user passes + # in a datastore.RunQueryRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore.RunQueryRequest): + request = datastore.RunQueryRequest(request) + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.run_query] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def begin_transaction( + self, + request: datastore.BeginTransactionRequest = None, + *, + project_id: str = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.BeginTransactionResponse: + r"""Begins a new transaction. + + Args: + request (:class:`~.datastore.BeginTransactionRequest`): + The request object. The request for + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.BeginTransactionResponse: + The response for + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([project_id]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a datastore.BeginTransactionRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore.BeginTransactionRequest): + request = datastore.BeginTransactionRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.begin_transaction] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def commit( + self, + request: datastore.CommitRequest = None, + *, + project_id: str = None, + mode: datastore.CommitRequest.Mode = None, + transaction: bytes = None, + mutations: Sequence[datastore.Mutation] = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.CommitResponse: + r"""Commits a transaction, optionally creating, deleting + or modifying some entities. + + Args: + request (:class:`~.datastore.CommitRequest`): + The request object. The request for + [Datastore.Commit][google.datastore.v1.Datastore.Commit]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + mode (:class:`~.datastore.CommitRequest.Mode`): + The type of commit to perform. Defaults to + ``TRANSACTIONAL``. + This corresponds to the ``mode`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + transaction (:class:`bytes`): + The identifier of the transaction associated with the + commit. A transaction identifier is returned by a call + to + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + This corresponds to the ``transaction`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + mutations (:class:`Sequence[~.datastore.Mutation]`): + The mutations to perform. + + When mode is ``TRANSACTIONAL``, mutations affecting a + single entity are applied in order. The following + sequences of mutations affecting a single entity are not + permitted in a single ``Commit`` request: + + - ``insert`` followed by ``insert`` + - ``update`` followed by ``insert`` + - ``upsert`` followed by ``insert`` + - ``delete`` followed by ``update`` + + When mode is ``NON_TRANSACTIONAL``, no two mutations may + affect a single entity. + This corresponds to the ``mutations`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.CommitResponse: + The response for + [Datastore.Commit][google.datastore.v1.Datastore.Commit]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([project_id, mode, transaction, mutations]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a datastore.CommitRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore.CommitRequest): + request = datastore.CommitRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if mode is not None: + request.mode = mode + if transaction is not None: + request.transaction = transaction + if mutations is not None: + request.mutations = mutations + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.commit] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def rollback( + self, + request: datastore.RollbackRequest = None, + *, + project_id: str = None, + transaction: bytes = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.RollbackResponse: + r"""Rolls back a transaction. + + Args: + request (:class:`~.datastore.RollbackRequest`): + The request object. The request for + [Datastore.Rollback][google.datastore.v1.Datastore.Rollback]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + transaction (:class:`bytes`): + Required. The transaction identifier, returned by a call + to + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + This corresponds to the ``transaction`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.RollbackResponse: + The response for + [Datastore.Rollback][google.datastore.v1.Datastore.Rollback]. + (an empty message). + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([project_id, transaction]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a datastore.RollbackRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore.RollbackRequest): + request = datastore.RollbackRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if transaction is not None: + request.transaction = transaction + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.rollback] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def allocate_ids( + self, + request: datastore.AllocateIdsRequest = None, + *, + project_id: str = None, + keys: Sequence[entity.Key] = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.AllocateIdsResponse: + r"""Allocates IDs for the given keys, which is useful for + referencing an entity before it is inserted. + + Args: + request (:class:`~.datastore.AllocateIdsRequest`): + The request object. The request for + [Datastore.AllocateIds][google.datastore.v1.Datastore.AllocateIds]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + keys (:class:`Sequence[~.entity.Key]`): + Required. A list of keys with + incomplete key paths for which to + allocate IDs. No key may be + reserved/read-only. + This corresponds to the ``keys`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.AllocateIdsResponse: + The response for + [Datastore.AllocateIds][google.datastore.v1.Datastore.AllocateIds]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([project_id, keys]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a datastore.AllocateIdsRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore.AllocateIdsRequest): + request = datastore.AllocateIdsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if keys is not None: + request.keys = keys + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.allocate_ids] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + def reserve_ids( + self, + request: datastore.ReserveIdsRequest = None, + *, + project_id: str = None, + keys: Sequence[entity.Key] = None, + retry: retries.Retry = gapic_v1.method.DEFAULT, + timeout: float = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> datastore.ReserveIdsResponse: + r"""Prevents the supplied keys' IDs from being auto- + llocated by Cloud Datastore. + + Args: + request (:class:`~.datastore.ReserveIdsRequest`): + The request object. The request for + [Datastore.ReserveIds][google.datastore.v1.Datastore.ReserveIds]. + project_id (:class:`str`): + Required. The ID of the project + against which to make the request. + This corresponds to the ``project_id`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + keys (:class:`Sequence[~.entity.Key]`): + Required. A list of keys with + complete key paths whose numeric IDs + should not be auto-allocated. + This corresponds to the ``keys`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + + retry (google.api_core.retry.Retry): Designation of what errors, if any, + should be retried. + timeout (float): The timeout for this request. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + + Returns: + ~.datastore.ReserveIdsResponse: + The response for + [Datastore.ReserveIds][google.datastore.v1.Datastore.ReserveIds]. + + """ + # Create or coerce a protobuf request object. + # Sanity check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = any([project_id, keys]) + if request is not None and has_flattened_params: + raise ValueError( + "If the `request` argument is set, then none of " + "the individual field arguments should be set." + ) + + # Minor optimization to avoid making a copy if the user passes + # in a datastore.ReserveIdsRequest. + # There's no risk of modifying the input as we've already verified + # there are no flattened fields. + if not isinstance(request, datastore.ReserveIdsRequest): + request = datastore.ReserveIdsRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + + if project_id is not None: + request.project_id = project_id + if keys is not None: + request.keys = keys + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[self._transport.reserve_ids] + + # Send the request. + response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + + # Done; return the response. + return response + + +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution("google-cloud-datastore",).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + +__all__ = ("DatastoreClient",) diff --git a/google/cloud/datastore_v1/services/datastore/transports/__init__.py b/google/cloud/datastore_v1/services/datastore/transports/__init__.py new file mode 100644 index 00000000..2d0659d9 --- /dev/null +++ b/google/cloud/datastore_v1/services/datastore/transports/__init__.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 collections import OrderedDict +from typing import Dict, Type + +from .base import DatastoreTransport +from .grpc import DatastoreGrpcTransport +from .grpc_asyncio import DatastoreGrpcAsyncIOTransport + + +# Compile a registry of transports. +_transport_registry = OrderedDict() # type: Dict[str, Type[DatastoreTransport]] +_transport_registry["grpc"] = DatastoreGrpcTransport +_transport_registry["grpc_asyncio"] = DatastoreGrpcAsyncIOTransport + + +__all__ = ( + "DatastoreTransport", + "DatastoreGrpcTransport", + "DatastoreGrpcAsyncIOTransport", +) diff --git a/google/cloud/datastore_v1/services/datastore/transports/base.py b/google/cloud/datastore_v1/services/datastore/transports/base.py new file mode 100644 index 00000000..ad00b33f --- /dev/null +++ b/google/cloud/datastore_v1/services/datastore/transports/base.py @@ -0,0 +1,243 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import abc +import typing +import pkg_resources + +from google import auth # type: ignore +from google.api_core import exceptions # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google.api_core import retry as retries # type: ignore +from google.auth import credentials # type: ignore + +from google.cloud.datastore_v1.types import datastore + + +try: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=pkg_resources.get_distribution("google-cloud-datastore",).version, + ) +except pkg_resources.DistributionNotFound: + DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo() + + +class DatastoreTransport(abc.ABC): + """Abstract transport class for Datastore.""" + + AUTH_SCOPES = ( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ) + + def __init__( + self, + *, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: typing.Optional[str] = None, + scopes: typing.Optional[typing.Sequence[str]] = AUTH_SCOPES, + quota_project_id: typing.Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + **kwargs, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scope (Optional[Sequence[str]]): A list of scopes. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + """ + # Save the hostname. Default to port 443 (HTTPS) if none is specified. + if ":" not in host: + host += ":443" + self._host = host + + # If no credentials are provided, then determine the appropriate + # defaults. + if credentials and credentials_file: + raise exceptions.DuplicateCredentialArgs( + "'credentials_file' and 'credentials' are mutually exclusive" + ) + + if credentials_file is not None: + credentials, _ = auth.load_credentials_from_file( + credentials_file, scopes=scopes, quota_project_id=quota_project_id + ) + + elif credentials is None: + credentials, _ = auth.default( + scopes=scopes, quota_project_id=quota_project_id + ) + + # Save the credentials. + self._credentials = credentials + + # Lifted into its own function so it can be stubbed out during tests. + self._prep_wrapped_messages(client_info) + + def _prep_wrapped_messages(self, client_info): + # Precompute the wrapped methods. + self._wrapped_methods = { + self.lookup: gapic_v1.method.wrap_method( + self.lookup, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=60.0, + client_info=client_info, + ), + self.run_query: gapic_v1.method.wrap_method( + self.run_query, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=60.0, + client_info=client_info, + ), + self.begin_transaction: gapic_v1.method.wrap_method( + self.begin_transaction, default_timeout=60.0, client_info=client_info, + ), + self.commit: gapic_v1.method.wrap_method( + self.commit, default_timeout=60.0, client_info=client_info, + ), + self.rollback: gapic_v1.method.wrap_method( + self.rollback, default_timeout=60.0, client_info=client_info, + ), + self.allocate_ids: gapic_v1.method.wrap_method( + self.allocate_ids, default_timeout=60.0, client_info=client_info, + ), + self.reserve_ids: gapic_v1.method.wrap_method( + self.reserve_ids, + default_retry=retries.Retry( + initial=0.1, + maximum=60.0, + multiplier=1.3, + predicate=retries.if_exception_type( + exceptions.DeadlineExceeded, exceptions.ServiceUnavailable, + ), + ), + default_timeout=60.0, + client_info=client_info, + ), + } + + @property + def lookup( + self, + ) -> typing.Callable[ + [datastore.LookupRequest], + typing.Union[ + datastore.LookupResponse, typing.Awaitable[datastore.LookupResponse] + ], + ]: + raise NotImplementedError() + + @property + def run_query( + self, + ) -> typing.Callable[ + [datastore.RunQueryRequest], + typing.Union[ + datastore.RunQueryResponse, typing.Awaitable[datastore.RunQueryResponse] + ], + ]: + raise NotImplementedError() + + @property + def begin_transaction( + self, + ) -> typing.Callable[ + [datastore.BeginTransactionRequest], + typing.Union[ + datastore.BeginTransactionResponse, + typing.Awaitable[datastore.BeginTransactionResponse], + ], + ]: + raise NotImplementedError() + + @property + def commit( + self, + ) -> typing.Callable[ + [datastore.CommitRequest], + typing.Union[ + datastore.CommitResponse, typing.Awaitable[datastore.CommitResponse] + ], + ]: + raise NotImplementedError() + + @property + def rollback( + self, + ) -> typing.Callable[ + [datastore.RollbackRequest], + typing.Union[ + datastore.RollbackResponse, typing.Awaitable[datastore.RollbackResponse] + ], + ]: + raise NotImplementedError() + + @property + def allocate_ids( + self, + ) -> typing.Callable[ + [datastore.AllocateIdsRequest], + typing.Union[ + datastore.AllocateIdsResponse, + typing.Awaitable[datastore.AllocateIdsResponse], + ], + ]: + raise NotImplementedError() + + @property + def reserve_ids( + self, + ) -> typing.Callable[ + [datastore.ReserveIdsRequest], + typing.Union[ + datastore.ReserveIdsResponse, typing.Awaitable[datastore.ReserveIdsResponse] + ], + ]: + raise NotImplementedError() + + +__all__ = ("DatastoreTransport",) diff --git a/google/cloud/datastore_v1/services/datastore/transports/grpc.py b/google/cloud/datastore_v1/services/datastore/transports/grpc.py new file mode 100644 index 00000000..f8f18768 --- /dev/null +++ b/google/cloud/datastore_v1/services/datastore/transports/grpc.py @@ -0,0 +1,422 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import warnings +from typing import Callable, Dict, Optional, Sequence, Tuple + +from google.api_core import grpc_helpers # type: ignore +from google.api_core import gapic_v1 # type: ignore +from google import auth # type: ignore +from google.auth import credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore + +import grpc # type: ignore + +from google.cloud.datastore_v1.types import datastore + +from .base import DatastoreTransport, DEFAULT_CLIENT_INFO + + +class DatastoreGrpcTransport(DatastoreTransport): + """gRPC backend transport for Datastore. + + Each RPC normalizes the partition IDs of the keys in its + input entities, and always returns entities with keys with + normalized partition IDs. This applies to all keys and entities, + including those in values, except keys with both an empty path + and an empty or unset partition ID. Normalization of input keys + sets the project ID (if not already set) to the project ID from + the request. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _stubs: Dict[str, Callable] + + def __init__( + self, + *, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: str = None, + scopes: Sequence[str] = None, + channel: grpc.Channel = None, + api_mtls_endpoint: str = None, + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if ``channel`` is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if ``channel`` is provided. + channel (Optional[grpc.Channel]): A ``Channel`` instance through + which to make calls. + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create + a mutual TLS channel with client SSL credentials from + ``client_cert_source`` or applicatin default SSL credentials. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + + Raises: + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + if channel: + # Sanity check: Ensure that channel and credentials are not both + # provided. + credentials = False + + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + + host = ( + api_mtls_endpoint + if ":" in api_mtls_endpoint + else api_mtls_endpoint + ":443" + ) + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + ssl_credentials = SslCredentials().ssl_credentials + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) + + self._stubs = {} # type: Dict[str, Callable] + + # Run the base constructor. + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, + ) + + @classmethod + def create_channel( + cls, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: str = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> grpc.Channel: + """Create and return a gRPC channel object. + Args: + address (Optionsl[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is mutually exclusive with credentials. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + grpc.Channel: A gRPC channel object. + + Raises: + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + scopes = scopes or cls.AUTH_SCOPES + return grpc_helpers.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + **kwargs, + ) + + @property + def grpc_channel(self) -> grpc.Channel: + """Create the channel designed to connect to this service. + + This property caches on the instance; repeated calls return + the same channel. + """ + # Return the channel from cache. + return self._grpc_channel + + @property + def lookup(self) -> Callable[[datastore.LookupRequest], datastore.LookupResponse]: + r"""Return a callable for the lookup method over gRPC. + + Looks up entities by key. + + Returns: + Callable[[~.LookupRequest], + ~.LookupResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "lookup" not in self._stubs: + self._stubs["lookup"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/Lookup", + request_serializer=datastore.LookupRequest.serialize, + response_deserializer=datastore.LookupResponse.deserialize, + ) + return self._stubs["lookup"] + + @property + def run_query( + self, + ) -> Callable[[datastore.RunQueryRequest], datastore.RunQueryResponse]: + r"""Return a callable for the run query method over gRPC. + + Queries for entities. + + Returns: + Callable[[~.RunQueryRequest], + ~.RunQueryResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "run_query" not in self._stubs: + self._stubs["run_query"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/RunQuery", + request_serializer=datastore.RunQueryRequest.serialize, + response_deserializer=datastore.RunQueryResponse.deserialize, + ) + return self._stubs["run_query"] + + @property + def begin_transaction( + self, + ) -> Callable[ + [datastore.BeginTransactionRequest], datastore.BeginTransactionResponse + ]: + r"""Return a callable for the begin transaction method over gRPC. + + Begins a new transaction. + + Returns: + Callable[[~.BeginTransactionRequest], + ~.BeginTransactionResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "begin_transaction" not in self._stubs: + self._stubs["begin_transaction"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/BeginTransaction", + request_serializer=datastore.BeginTransactionRequest.serialize, + response_deserializer=datastore.BeginTransactionResponse.deserialize, + ) + return self._stubs["begin_transaction"] + + @property + def commit(self) -> Callable[[datastore.CommitRequest], datastore.CommitResponse]: + r"""Return a callable for the commit method over gRPC. + + Commits a transaction, optionally creating, deleting + or modifying some entities. + + Returns: + Callable[[~.CommitRequest], + ~.CommitResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "commit" not in self._stubs: + self._stubs["commit"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/Commit", + request_serializer=datastore.CommitRequest.serialize, + response_deserializer=datastore.CommitResponse.deserialize, + ) + return self._stubs["commit"] + + @property + def rollback( + self, + ) -> Callable[[datastore.RollbackRequest], datastore.RollbackResponse]: + r"""Return a callable for the rollback method over gRPC. + + Rolls back a transaction. + + Returns: + Callable[[~.RollbackRequest], + ~.RollbackResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "rollback" not in self._stubs: + self._stubs["rollback"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/Rollback", + request_serializer=datastore.RollbackRequest.serialize, + response_deserializer=datastore.RollbackResponse.deserialize, + ) + return self._stubs["rollback"] + + @property + def allocate_ids( + self, + ) -> Callable[[datastore.AllocateIdsRequest], datastore.AllocateIdsResponse]: + r"""Return a callable for the allocate ids method over gRPC. + + Allocates IDs for the given keys, which is useful for + referencing an entity before it is inserted. + + Returns: + Callable[[~.AllocateIdsRequest], + ~.AllocateIdsResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "allocate_ids" not in self._stubs: + self._stubs["allocate_ids"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/AllocateIds", + request_serializer=datastore.AllocateIdsRequest.serialize, + response_deserializer=datastore.AllocateIdsResponse.deserialize, + ) + return self._stubs["allocate_ids"] + + @property + def reserve_ids( + self, + ) -> Callable[[datastore.ReserveIdsRequest], datastore.ReserveIdsResponse]: + r"""Return a callable for the reserve ids method over gRPC. + + Prevents the supplied keys' IDs from being auto- + llocated by Cloud Datastore. + + Returns: + Callable[[~.ReserveIdsRequest], + ~.ReserveIdsResponse]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "reserve_ids" not in self._stubs: + self._stubs["reserve_ids"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/ReserveIds", + request_serializer=datastore.ReserveIdsRequest.serialize, + response_deserializer=datastore.ReserveIdsResponse.deserialize, + ) + return self._stubs["reserve_ids"] + + +__all__ = ("DatastoreGrpcTransport",) diff --git a/google/cloud/datastore_v1/services/datastore/transports/grpc_asyncio.py b/google/cloud/datastore_v1/services/datastore/transports/grpc_asyncio.py new file mode 100644 index 00000000..a9c5611f --- /dev/null +++ b/google/cloud/datastore_v1/services/datastore/transports/grpc_asyncio.py @@ -0,0 +1,431 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import warnings +from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple + +from google.api_core import gapic_v1 # type: ignore +from google.api_core import grpc_helpers_async # type: ignore +from google import auth # type: ignore +from google.auth import credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore + +import grpc # type: ignore +from grpc.experimental import aio # type: ignore + +from google.cloud.datastore_v1.types import datastore + +from .base import DatastoreTransport, DEFAULT_CLIENT_INFO +from .grpc import DatastoreGrpcTransport + + +class DatastoreGrpcAsyncIOTransport(DatastoreTransport): + """gRPC AsyncIO backend transport for Datastore. + + Each RPC normalizes the partition IDs of the keys in its + input entities, and always returns entities with keys with + normalized partition IDs. This applies to all keys and entities, + including those in values, except keys with both an empty path + and an empty or unset partition ID. Normalization of input keys + sets the project ID (if not already set) to the project ID from + the request. + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends protocol buffers over the wire using gRPC (which is built on + top of HTTP/2); the ``grpcio`` package must be installed. + """ + + _grpc_channel: aio.Channel + _stubs: Dict[str, Callable] = {} + + @classmethod + def create_channel( + cls, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + quota_project_id: Optional[str] = None, + **kwargs, + ) -> aio.Channel: + """Create and return a gRPC AsyncIO channel object. + Args: + address (Optional[str]): The host for the channel to use. + credentials (Optional[~.Credentials]): The + authorization credentials to attach to requests. These + credentials identify this application to the service. If + none are specified, the client will attempt to ascertain + the credentials from the environment. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + kwargs (Optional[dict]): Keyword arguments, which are passed to the + channel creation. + Returns: + aio.Channel: A gRPC AsyncIO channel object. + """ + scopes = scopes or cls.AUTH_SCOPES + return grpc_helpers_async.create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes, + quota_project_id=quota_project_id, + **kwargs, + ) + + def __init__( + self, + *, + host: str = "datastore.googleapis.com", + credentials: credentials.Credentials = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + channel: aio.Channel = None, + api_mtls_endpoint: str = None, + client_cert_source: Callable[[], Tuple[bytes, bytes]] = None, + ssl_channel_credentials: grpc.ChannelCredentials = None, + quota_project_id=None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + This argument is ignored if ``channel`` is provided. + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional[Sequence[str]]): A optional list of scopes needed for this + service. These are only used when credentials are not specified and + are passed to :func:`google.auth.default`. + channel (Optional[aio.Channel]): A ``Channel`` instance through + which to make calls. + api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. + If provided, it overrides the ``host`` argument and tries to create + a mutual TLS channel with client SSL credentials from + ``client_cert_source`` or applicatin default SSL credentials. + client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): + Deprecated. A callback to provide client SSL certificate bytes and + private key bytes, both in PEM format. It is ignored if + ``api_mtls_endpoint`` is None. + ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials + for grpc channel. It is ignored if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you're developing + your own client library. + + Raises: + google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport + creation failed for any reason. + google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` + and ``credentials_file`` are passed. + """ + if channel: + # Sanity check: Ensure that channel and credentials are not both + # provided. + credentials = False + + # If a channel was explicitly provided, set it. + self._grpc_channel = channel + elif api_mtls_endpoint: + warnings.warn( + "api_mtls_endpoint and client_cert_source are deprecated", + DeprecationWarning, + ) + + host = ( + api_mtls_endpoint + if ":" in api_mtls_endpoint + else api_mtls_endpoint + ":443" + ) + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # Create SSL credentials with client_cert_source or application + # default SSL credentials. + if client_cert_source: + cert, key = client_cert_source() + ssl_credentials = grpc.ssl_channel_credentials( + certificate_chain=cert, private_key=key + ) + else: + ssl_credentials = SslCredentials().ssl_credentials + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) + else: + host = host if ":" in host else host + ":443" + + if credentials is None: + credentials, _ = auth.default( + scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id + ) + + # create a new channel. The provided one is ignored. + self._grpc_channel = type(self).create_channel( + host, + credentials=credentials, + credentials_file=credentials_file, + ssl_credentials=ssl_channel_credentials, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + ) + + # Run the base constructor. + super().__init__( + host=host, + credentials=credentials, + credentials_file=credentials_file, + scopes=scopes or self.AUTH_SCOPES, + quota_project_id=quota_project_id, + client_info=client_info, + ) + + self._stubs = {} + + @property + def grpc_channel(self) -> aio.Channel: + """Create the channel designed to connect to this service. + + This property caches on the instance; repeated calls return + the same channel. + """ + # Return the channel from cache. + return self._grpc_channel + + @property + def lookup( + self, + ) -> Callable[[datastore.LookupRequest], Awaitable[datastore.LookupResponse]]: + r"""Return a callable for the lookup method over gRPC. + + Looks up entities by key. + + Returns: + Callable[[~.LookupRequest], + Awaitable[~.LookupResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "lookup" not in self._stubs: + self._stubs["lookup"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/Lookup", + request_serializer=datastore.LookupRequest.serialize, + response_deserializer=datastore.LookupResponse.deserialize, + ) + return self._stubs["lookup"] + + @property + def run_query( + self, + ) -> Callable[[datastore.RunQueryRequest], Awaitable[datastore.RunQueryResponse]]: + r"""Return a callable for the run query method over gRPC. + + Queries for entities. + + Returns: + Callable[[~.RunQueryRequest], + Awaitable[~.RunQueryResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "run_query" not in self._stubs: + self._stubs["run_query"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/RunQuery", + request_serializer=datastore.RunQueryRequest.serialize, + response_deserializer=datastore.RunQueryResponse.deserialize, + ) + return self._stubs["run_query"] + + @property + def begin_transaction( + self, + ) -> Callable[ + [datastore.BeginTransactionRequest], + Awaitable[datastore.BeginTransactionResponse], + ]: + r"""Return a callable for the begin transaction method over gRPC. + + Begins a new transaction. + + Returns: + Callable[[~.BeginTransactionRequest], + Awaitable[~.BeginTransactionResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "begin_transaction" not in self._stubs: + self._stubs["begin_transaction"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/BeginTransaction", + request_serializer=datastore.BeginTransactionRequest.serialize, + response_deserializer=datastore.BeginTransactionResponse.deserialize, + ) + return self._stubs["begin_transaction"] + + @property + def commit( + self, + ) -> Callable[[datastore.CommitRequest], Awaitable[datastore.CommitResponse]]: + r"""Return a callable for the commit method over gRPC. + + Commits a transaction, optionally creating, deleting + or modifying some entities. + + Returns: + Callable[[~.CommitRequest], + Awaitable[~.CommitResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "commit" not in self._stubs: + self._stubs["commit"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/Commit", + request_serializer=datastore.CommitRequest.serialize, + response_deserializer=datastore.CommitResponse.deserialize, + ) + return self._stubs["commit"] + + @property + def rollback( + self, + ) -> Callable[[datastore.RollbackRequest], Awaitable[datastore.RollbackResponse]]: + r"""Return a callable for the rollback method over gRPC. + + Rolls back a transaction. + + Returns: + Callable[[~.RollbackRequest], + Awaitable[~.RollbackResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "rollback" not in self._stubs: + self._stubs["rollback"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/Rollback", + request_serializer=datastore.RollbackRequest.serialize, + response_deserializer=datastore.RollbackResponse.deserialize, + ) + return self._stubs["rollback"] + + @property + def allocate_ids( + self, + ) -> Callable[ + [datastore.AllocateIdsRequest], Awaitable[datastore.AllocateIdsResponse] + ]: + r"""Return a callable for the allocate ids method over gRPC. + + Allocates IDs for the given keys, which is useful for + referencing an entity before it is inserted. + + Returns: + Callable[[~.AllocateIdsRequest], + Awaitable[~.AllocateIdsResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "allocate_ids" not in self._stubs: + self._stubs["allocate_ids"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/AllocateIds", + request_serializer=datastore.AllocateIdsRequest.serialize, + response_deserializer=datastore.AllocateIdsResponse.deserialize, + ) + return self._stubs["allocate_ids"] + + @property + def reserve_ids( + self, + ) -> Callable[ + [datastore.ReserveIdsRequest], Awaitable[datastore.ReserveIdsResponse] + ]: + r"""Return a callable for the reserve ids method over gRPC. + + Prevents the supplied keys' IDs from being auto- + llocated by Cloud Datastore. + + Returns: + Callable[[~.ReserveIdsRequest], + Awaitable[~.ReserveIdsResponse]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "reserve_ids" not in self._stubs: + self._stubs["reserve_ids"] = self.grpc_channel.unary_unary( + "/google.datastore.v1.Datastore/ReserveIds", + request_serializer=datastore.ReserveIdsRequest.serialize, + response_deserializer=datastore.ReserveIdsResponse.deserialize, + ) + return self._stubs["reserve_ids"] + + +__all__ = ("DatastoreGrpcAsyncIOTransport",) diff --git a/google/cloud/datastore_v1/types.py b/google/cloud/datastore_v1/types.py deleted file mode 100644 index ac154bda..00000000 --- a/google/cloud/datastore_v1/types.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2018 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 -# -# https://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 __future__ import absolute_import -import sys - -from google.api import http_pb2 -from google.protobuf import descriptor_pb2 -from google.protobuf import struct_pb2 -from google.protobuf import timestamp_pb2 -from google.protobuf import wrappers_pb2 -from google.type import latlng_pb2 - -from google.api_core.protobuf_helpers import get_messages -from google.cloud.datastore_v1.proto import datastore_pb2 -from google.cloud.datastore_v1.proto import entity_pb2 -from google.cloud.datastore_v1.proto import query_pb2 - - -_shared_modules = [ - http_pb2, - descriptor_pb2, - struct_pb2, - timestamp_pb2, - wrappers_pb2, - latlng_pb2, -] - -_local_modules = [datastore_pb2, entity_pb2, query_pb2] - -names = [] - -for module in _shared_modules: - for name, message in get_messages(module).items(): - setattr(sys.modules[__name__], name, message) - names.append(name) - -for module in _local_modules: - for name, message in get_messages(module).items(): - message.__module__ = "google.cloud.datastore_v1.types" - setattr(sys.modules[__name__], name, message) - names.append(name) - -__all__ = tuple(sorted(names)) diff --git a/google/cloud/datastore_v1/types/__init__.py b/google/cloud/datastore_v1/types/__init__.py new file mode 100644 index 00000000..2148caa0 --- /dev/null +++ b/google/cloud/datastore_v1/types/__init__.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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 .entity import ( + PartitionId, + Key, + ArrayValue, + Value, + Entity, +) +from .query import ( + EntityResult, + Query, + KindExpression, + PropertyReference, + Projection, + PropertyOrder, + Filter, + CompositeFilter, + PropertyFilter, + GqlQuery, + GqlQueryParameter, + QueryResultBatch, +) +from .datastore import ( + LookupRequest, + LookupResponse, + RunQueryRequest, + RunQueryResponse, + BeginTransactionRequest, + BeginTransactionResponse, + RollbackRequest, + RollbackResponse, + CommitRequest, + CommitResponse, + AllocateIdsRequest, + AllocateIdsResponse, + ReserveIdsRequest, + ReserveIdsResponse, + Mutation, + MutationResult, + ReadOptions, + TransactionOptions, +) + + +__all__ = ( + "PartitionId", + "Key", + "ArrayValue", + "Value", + "Entity", + "EntityResult", + "Query", + "KindExpression", + "PropertyReference", + "Projection", + "PropertyOrder", + "Filter", + "CompositeFilter", + "PropertyFilter", + "GqlQuery", + "GqlQueryParameter", + "QueryResultBatch", + "LookupRequest", + "LookupResponse", + "RunQueryRequest", + "RunQueryResponse", + "BeginTransactionRequest", + "BeginTransactionResponse", + "RollbackRequest", + "RollbackResponse", + "CommitRequest", + "CommitResponse", + "AllocateIdsRequest", + "AllocateIdsResponse", + "ReserveIdsRequest", + "ReserveIdsResponse", + "Mutation", + "MutationResult", + "ReadOptions", + "TransactionOptions", +) diff --git a/google/cloud/datastore_v1/types/datastore.py b/google/cloud/datastore_v1/types/datastore.py new file mode 100644 index 00000000..e1124457 --- /dev/null +++ b/google/cloud/datastore_v1/types/datastore.py @@ -0,0 +1,480 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import proto # type: ignore + + +from google.cloud.datastore_v1.types import entity +from google.cloud.datastore_v1.types import query as gd_query + + +__protobuf__ = proto.module( + package="google.datastore.v1", + manifest={ + "LookupRequest", + "LookupResponse", + "RunQueryRequest", + "RunQueryResponse", + "BeginTransactionRequest", + "BeginTransactionResponse", + "RollbackRequest", + "RollbackResponse", + "CommitRequest", + "CommitResponse", + "AllocateIdsRequest", + "AllocateIdsResponse", + "ReserveIdsRequest", + "ReserveIdsResponse", + "Mutation", + "MutationResult", + "ReadOptions", + "TransactionOptions", + }, +) + + +class LookupRequest(proto.Message): + r"""The request for + [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. + + Attributes: + project_id (str): + Required. The ID of the project against which + to make the request. + read_options (~.datastore.ReadOptions): + The options for this lookup request. + keys (Sequence[~.entity.Key]): + Required. Keys of entities to look up. + """ + + project_id = proto.Field(proto.STRING, number=8) + + read_options = proto.Field(proto.MESSAGE, number=1, message="ReadOptions",) + + keys = proto.RepeatedField(proto.MESSAGE, number=3, message=entity.Key,) + + +class LookupResponse(proto.Message): + r"""The response for + [Datastore.Lookup][google.datastore.v1.Datastore.Lookup]. + + Attributes: + found (Sequence[~.gd_query.EntityResult]): + Entities found as ``ResultType.FULL`` entities. The order of + results in this field is undefined and has no relation to + the order of the keys in the input. + missing (Sequence[~.gd_query.EntityResult]): + Entities not found as ``ResultType.KEY_ONLY`` entities. The + order of results in this field is undefined and has no + relation to the order of the keys in the input. + deferred (Sequence[~.entity.Key]): + A list of keys that were not looked up due to + resource constraints. The order of results in + this field is undefined and has no relation to + the order of the keys in the input. + """ + + found = proto.RepeatedField(proto.MESSAGE, number=1, message=gd_query.EntityResult,) + + missing = proto.RepeatedField( + proto.MESSAGE, number=2, message=gd_query.EntityResult, + ) + + deferred = proto.RepeatedField(proto.MESSAGE, number=3, message=entity.Key,) + + +class RunQueryRequest(proto.Message): + r"""The request for + [Datastore.RunQuery][google.datastore.v1.Datastore.RunQuery]. + + Attributes: + project_id (str): + Required. The ID of the project against which + to make the request. + partition_id (~.entity.PartitionId): + Entities are partitioned into subsets, + identified by a partition ID. Queries are scoped + to a single partition. This partition ID is + normalized with the standard default context + partition ID. + read_options (~.datastore.ReadOptions): + The options for this query. + query (~.gd_query.Query): + The query to run. + gql_query (~.gd_query.GqlQuery): + The GQL query to run. + """ + + project_id = proto.Field(proto.STRING, number=8) + + partition_id = proto.Field(proto.MESSAGE, number=2, message=entity.PartitionId,) + + read_options = proto.Field(proto.MESSAGE, number=1, message="ReadOptions",) + + query = proto.Field( + proto.MESSAGE, number=3, oneof="query_type", message=gd_query.Query, + ) + + gql_query = proto.Field( + proto.MESSAGE, number=7, oneof="query_type", message=gd_query.GqlQuery, + ) + + +class RunQueryResponse(proto.Message): + r"""The response for + [Datastore.RunQuery][google.datastore.v1.Datastore.RunQuery]. + + Attributes: + batch (~.gd_query.QueryResultBatch): + A batch of query results (always present). + query (~.gd_query.Query): + The parsed form of the ``GqlQuery`` from the request, if it + was set. + """ + + batch = proto.Field(proto.MESSAGE, number=1, message=gd_query.QueryResultBatch,) + + query = proto.Field(proto.MESSAGE, number=2, message=gd_query.Query,) + + +class BeginTransactionRequest(proto.Message): + r"""The request for + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + + Attributes: + project_id (str): + Required. The ID of the project against which + to make the request. + transaction_options (~.datastore.TransactionOptions): + Options for a new transaction. + """ + + project_id = proto.Field(proto.STRING, number=8) + + transaction_options = proto.Field( + proto.MESSAGE, number=10, message="TransactionOptions", + ) + + +class BeginTransactionResponse(proto.Message): + r"""The response for + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + + Attributes: + transaction (bytes): + The transaction identifier (always present). + """ + + transaction = proto.Field(proto.BYTES, number=1) + + +class RollbackRequest(proto.Message): + r"""The request for + [Datastore.Rollback][google.datastore.v1.Datastore.Rollback]. + + Attributes: + project_id (str): + Required. The ID of the project against which + to make the request. + transaction (bytes): + Required. The transaction identifier, returned by a call to + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + """ + + project_id = proto.Field(proto.STRING, number=8) + + transaction = proto.Field(proto.BYTES, number=1) + + +class RollbackResponse(proto.Message): + r"""The response for + [Datastore.Rollback][google.datastore.v1.Datastore.Rollback]. (an + empty message). + """ + + +class CommitRequest(proto.Message): + r"""The request for + [Datastore.Commit][google.datastore.v1.Datastore.Commit]. + + Attributes: + project_id (str): + Required. The ID of the project against which + to make the request. + mode (~.datastore.CommitRequest.Mode): + The type of commit to perform. Defaults to + ``TRANSACTIONAL``. + transaction (bytes): + The identifier of the transaction associated with the + commit. A transaction identifier is returned by a call to + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + mutations (Sequence[~.datastore.Mutation]): + The mutations to perform. + + When mode is ``TRANSACTIONAL``, mutations affecting a single + entity are applied in order. The following sequences of + mutations affecting a single entity are not permitted in a + single ``Commit`` request: + + - ``insert`` followed by ``insert`` + - ``update`` followed by ``insert`` + - ``upsert`` followed by ``insert`` + - ``delete`` followed by ``update`` + + When mode is ``NON_TRANSACTIONAL``, no two mutations may + affect a single entity. + """ + + class Mode(proto.Enum): + r"""The modes available for commits.""" + MODE_UNSPECIFIED = 0 + TRANSACTIONAL = 1 + NON_TRANSACTIONAL = 2 + + project_id = proto.Field(proto.STRING, number=8) + + mode = proto.Field(proto.ENUM, number=5, enum=Mode,) + + transaction = proto.Field(proto.BYTES, number=1, oneof="transaction_selector") + + mutations = proto.RepeatedField(proto.MESSAGE, number=6, message="Mutation",) + + +class CommitResponse(proto.Message): + r"""The response for + [Datastore.Commit][google.datastore.v1.Datastore.Commit]. + + Attributes: + mutation_results (Sequence[~.datastore.MutationResult]): + The result of performing the mutations. + The i-th mutation result corresponds to the i-th + mutation in the request. + index_updates (int): + The number of index entries updated during + the commit, or zero if none were updated. + """ + + mutation_results = proto.RepeatedField( + proto.MESSAGE, number=3, message="MutationResult", + ) + + index_updates = proto.Field(proto.INT32, number=4) + + +class AllocateIdsRequest(proto.Message): + r"""The request for + [Datastore.AllocateIds][google.datastore.v1.Datastore.AllocateIds]. + + Attributes: + project_id (str): + Required. The ID of the project against which + to make the request. + keys (Sequence[~.entity.Key]): + Required. A list of keys with incomplete key + paths for which to allocate IDs. No key may be + reserved/read-only. + """ + + project_id = proto.Field(proto.STRING, number=8) + + keys = proto.RepeatedField(proto.MESSAGE, number=1, message=entity.Key,) + + +class AllocateIdsResponse(proto.Message): + r"""The response for + [Datastore.AllocateIds][google.datastore.v1.Datastore.AllocateIds]. + + Attributes: + keys (Sequence[~.entity.Key]): + The keys specified in the request (in the + same order), each with its key path completed + with a newly allocated ID. + """ + + keys = proto.RepeatedField(proto.MESSAGE, number=1, message=entity.Key,) + + +class ReserveIdsRequest(proto.Message): + r"""The request for + [Datastore.ReserveIds][google.datastore.v1.Datastore.ReserveIds]. + + Attributes: + project_id (str): + Required. The ID of the project against which + to make the request. + database_id (str): + If not empty, the ID of the database against + which to make the request. + keys (Sequence[~.entity.Key]): + Required. A list of keys with complete key + paths whose numeric IDs should not be auto- + allocated. + """ + + project_id = proto.Field(proto.STRING, number=8) + + database_id = proto.Field(proto.STRING, number=9) + + keys = proto.RepeatedField(proto.MESSAGE, number=1, message=entity.Key,) + + +class ReserveIdsResponse(proto.Message): + r"""The response for + [Datastore.ReserveIds][google.datastore.v1.Datastore.ReserveIds]. + """ + + +class Mutation(proto.Message): + r"""A mutation to apply to an entity. + + Attributes: + insert (~.entity.Entity): + The entity to insert. The entity must not + already exist. The entity key's final path + element may be incomplete. + update (~.entity.Entity): + The entity to update. The entity must already + exist. Must have a complete key path. + upsert (~.entity.Entity): + The entity to upsert. The entity may or may + not already exist. The entity key's final path + element may be incomplete. + delete (~.entity.Key): + The key of the entity to delete. The entity + may or may not already exist. Must have a + complete key path and must not be reserved/read- + only. + base_version (int): + The version of the entity that this mutation + is being applied to. If this does not match the + current version on the server, the mutation + conflicts. + """ + + insert = proto.Field( + proto.MESSAGE, number=4, oneof="operation", message=entity.Entity, + ) + + update = proto.Field( + proto.MESSAGE, number=5, oneof="operation", message=entity.Entity, + ) + + upsert = proto.Field( + proto.MESSAGE, number=6, oneof="operation", message=entity.Entity, + ) + + delete = proto.Field( + proto.MESSAGE, number=7, oneof="operation", message=entity.Key, + ) + + base_version = proto.Field( + proto.INT64, number=8, oneof="conflict_detection_strategy" + ) + + +class MutationResult(proto.Message): + r"""The result of applying a mutation. + + Attributes: + key (~.entity.Key): + The automatically allocated key. + Set only when the mutation allocated a key. + version (int): + The version of the entity on the server after + processing the mutation. If the mutation doesn't + change anything on the server, then the version + will be the version of the current entity or, if + no entity is present, a version that is strictly + greater than the version of any previous entity + and less than the version of any possible future + entity. + conflict_detected (bool): + Whether a conflict was detected for this + mutation. Always false when a conflict detection + strategy field is not set in the mutation. + """ + + key = proto.Field(proto.MESSAGE, number=3, message=entity.Key,) + + version = proto.Field(proto.INT64, number=4) + + conflict_detected = proto.Field(proto.BOOL, number=5) + + +class ReadOptions(proto.Message): + r"""The options shared by read requests. + + Attributes: + read_consistency (~.datastore.ReadOptions.ReadConsistency): + The non-transactional read consistency to use. Cannot be set + to ``STRONG`` for global queries. + transaction (bytes): + The identifier of the transaction in which to read. A + transaction identifier is returned by a call to + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction]. + """ + + class ReadConsistency(proto.Enum): + r"""The possible values for read consistencies.""" + READ_CONSISTENCY_UNSPECIFIED = 0 + STRONG = 1 + EVENTUAL = 2 + + read_consistency = proto.Field( + proto.ENUM, number=1, oneof="consistency_type", enum=ReadConsistency, + ) + + transaction = proto.Field(proto.BYTES, number=2, oneof="consistency_type") + + +class TransactionOptions(proto.Message): + r"""Options for beginning a new transaction. + + Transactions can be created explicitly with calls to + [Datastore.BeginTransaction][google.datastore.v1.Datastore.BeginTransaction] + or implicitly by setting + [ReadOptions.new_transaction][google.datastore.v1.ReadOptions.new_transaction] + in read requests. + + Attributes: + read_write (~.datastore.TransactionOptions.ReadWrite): + The transaction should allow both reads and + writes. + read_only (~.datastore.TransactionOptions.ReadOnly): + The transaction should only allow reads. + """ + + class ReadWrite(proto.Message): + r"""Options specific to read / write transactions. + + Attributes: + previous_transaction (bytes): + The transaction identifier of the transaction + being retried. + """ + + previous_transaction = proto.Field(proto.BYTES, number=1) + + class ReadOnly(proto.Message): + r"""Options specific to read-only transactions.""" + + read_write = proto.Field(proto.MESSAGE, number=1, oneof="mode", message=ReadWrite,) + + read_only = proto.Field(proto.MESSAGE, number=2, oneof="mode", message=ReadOnly,) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/datastore_v1/types/entity.py b/google/cloud/datastore_v1/types/entity.py new file mode 100644 index 00000000..96d4a6f4 --- /dev/null +++ b/google/cloud/datastore_v1/types/entity.py @@ -0,0 +1,260 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import proto # type: ignore + + +from google.protobuf import struct_pb2 as struct # type: ignore +from google.protobuf import timestamp_pb2 as timestamp # type: ignore +from google.type import latlng_pb2 as latlng # type: ignore + + +__protobuf__ = proto.module( + package="google.datastore.v1", + manifest={"PartitionId", "Key", "ArrayValue", "Value", "Entity",}, +) + + +class PartitionId(proto.Message): + r"""A partition ID identifies a grouping of entities. The grouping is + always by project and namespace, however the namespace ID may be + empty. + + A partition ID contains several dimensions: project ID and namespace + ID. + + Partition dimensions: + + - May be ``""``. + - Must be valid UTF-8 bytes. + - Must have values that match regex ``[A-Za-z\d\.\-_]{1,100}`` If + the value of any dimension matches regex ``__.*__``, the + partition is reserved/read-only. A reserved/read-only partition + ID is forbidden in certain documented contexts. + + Foreign partition IDs (in which the project ID does not match the + context project ID ) are discouraged. Reads and writes of foreign + partition IDs may fail if the project is not in an active state. + + Attributes: + project_id (str): + The ID of the project to which the entities + belong. + namespace_id (str): + If not empty, the ID of the namespace to + which the entities belong. + """ + + project_id = proto.Field(proto.STRING, number=2) + + namespace_id = proto.Field(proto.STRING, number=4) + + +class Key(proto.Message): + r"""A unique identifier for an entity. + If a key's partition ID or any of its path kinds or names are + reserved/read-only, the key is reserved/read-only. + A reserved/read-only key is forbidden in certain documented + contexts. + + Attributes: + partition_id (~.entity.PartitionId): + Entities are partitioned into subsets, + currently identified by a project ID and + namespace ID. Queries are scoped to a single + partition. + path (Sequence[~.entity.Key.PathElement]): + The entity path. An entity path consists of one or more + elements composed of a kind and a string or numerical + identifier, which identify entities. The first element + identifies a *root entity*, the second element identifies a + *child* of the root entity, the third element identifies a + child of the second entity, and so forth. The entities + identified by all prefixes of the path are called the + element's *ancestors*. + + An entity path is always fully complete: *all* of the + entity's ancestors are required to be in the path along with + the entity identifier itself. The only exception is that in + some documented cases, the identifier in the last path + element (for the entity) itself may be omitted. For example, + the last path element of the key of ``Mutation.insert`` may + have no identifier. + + A path can never be empty, and a path can have at most 100 + elements. + """ + + class PathElement(proto.Message): + r"""A (kind, ID/name) pair used to construct a key path. + If either name or ID is set, the element is complete. If neither + is set, the element is incomplete. + + Attributes: + kind (str): + The kind of the entity. A kind matching regex ``__.*__`` is + reserved/read-only. A kind must not contain more than 1500 + bytes when UTF-8 encoded. Cannot be ``""``. + id (int): + The auto-allocated ID of the entity. + Never equal to zero. Values less than zero are + discouraged and may not be supported in the + future. + name (str): + The name of the entity. A name matching regex ``__.*__`` is + reserved/read-only. A name must not be more than 1500 bytes + when UTF-8 encoded. Cannot be ``""``. + """ + + kind = proto.Field(proto.STRING, number=1) + + id = proto.Field(proto.INT64, number=2, oneof="id_type") + + name = proto.Field(proto.STRING, number=3, oneof="id_type") + + partition_id = proto.Field(proto.MESSAGE, number=1, message=PartitionId,) + + path = proto.RepeatedField(proto.MESSAGE, number=2, message=PathElement,) + + +class ArrayValue(proto.Message): + r"""An array value. + + Attributes: + values (Sequence[~.entity.Value]): + Values in the array. The order of values in an array is + preserved as long as all values have identical settings for + 'exclude_from_indexes'. + """ + + values = proto.RepeatedField(proto.MESSAGE, number=1, message="Value",) + + +class Value(proto.Message): + r"""A message that can hold any of the supported value types and + associated metadata. + + Attributes: + null_value (~.struct.NullValue): + A null value. + boolean_value (bool): + A boolean value. + integer_value (int): + An integer value. + double_value (float): + A double value. + timestamp_value (~.timestamp.Timestamp): + A timestamp value. + When stored in the Datastore, precise only to + microseconds; any additional precision is + rounded down. + key_value (~.entity.Key): + A key value. + string_value (str): + A UTF-8 encoded string value. When ``exclude_from_indexes`` + is false (it is indexed), may have at most 1500 bytes. + Otherwise, may be set to at most 1,000,000 bytes. + blob_value (bytes): + A blob value. May have at most 1,000,000 bytes. When + ``exclude_from_indexes`` is false, may have at most 1500 + bytes. In JSON requests, must be base64-encoded. + geo_point_value (~.latlng.LatLng): + A geo point value representing a point on the + surface of Earth. + entity_value (~.entity.Entity): + An entity value. + - May have no key. + - May have a key with an incomplete key path. - + May have a reserved/read-only key. + array_value (~.entity.ArrayValue): + An array value. Cannot contain another array value. A + ``Value`` instance that sets field ``array_value`` must not + set fields ``meaning`` or ``exclude_from_indexes``. + meaning (int): + The ``meaning`` field should only be populated for backwards + compatibility. + exclude_from_indexes (bool): + If the value should be excluded from all + indexes including those defined explicitly. + """ + + null_value = proto.Field( + proto.ENUM, number=11, oneof="value_type", enum=struct.NullValue, + ) + + boolean_value = proto.Field(proto.BOOL, number=1, oneof="value_type") + + integer_value = proto.Field(proto.INT64, number=2, oneof="value_type") + + double_value = proto.Field(proto.DOUBLE, number=3, oneof="value_type") + + timestamp_value = proto.Field( + proto.MESSAGE, number=10, oneof="value_type", message=timestamp.Timestamp, + ) + + key_value = proto.Field(proto.MESSAGE, number=5, oneof="value_type", message=Key,) + + string_value = proto.Field(proto.STRING, number=17, oneof="value_type") + + blob_value = proto.Field(proto.BYTES, number=18, oneof="value_type") + + geo_point_value = proto.Field( + proto.MESSAGE, number=8, oneof="value_type", message=latlng.LatLng, + ) + + entity_value = proto.Field( + proto.MESSAGE, number=6, oneof="value_type", message="Entity", + ) + + array_value = proto.Field( + proto.MESSAGE, number=9, oneof="value_type", message=ArrayValue, + ) + + meaning = proto.Field(proto.INT32, number=14) + + exclude_from_indexes = proto.Field(proto.BOOL, number=19) + + +class Entity(proto.Message): + r"""A Datastore data object. + + An entity is limited to 1 megabyte when stored. That *roughly* + corresponds to a limit of 1 megabyte for the serialized form of this + message. + + Attributes: + key (~.entity.Key): + The entity's key. + + An entity must have a key, unless otherwise documented (for + example, an entity in ``Value.entity_value`` may have no + key). An entity's kind is its key path's last element's + kind, or null if it has no key. + properties (Sequence[~.entity.Entity.PropertiesEntry]): + The entity's properties. The map's keys are property names. + A property name matching regex ``__.*__`` is reserved. A + reserved property name is forbidden in certain documented + contexts. The name must not contain more than 500 + characters. The name cannot be ``""``. + """ + + key = proto.Field(proto.MESSAGE, number=1, message=Key,) + + properties = proto.MapField(proto.STRING, proto.MESSAGE, number=3, message=Value,) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/google/cloud/datastore_v1/types/query.py b/google/cloud/datastore_v1/types/query.py new file mode 100644 index 00000000..87ed724d --- /dev/null +++ b/google/cloud/datastore_v1/types/query.py @@ -0,0 +1,397 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import proto # type: ignore + + +from google.cloud.datastore_v1.types import entity as gd_entity +from google.protobuf import wrappers_pb2 as wrappers # type: ignore + + +__protobuf__ = proto.module( + package="google.datastore.v1", + manifest={ + "EntityResult", + "Query", + "KindExpression", + "PropertyReference", + "Projection", + "PropertyOrder", + "Filter", + "CompositeFilter", + "PropertyFilter", + "GqlQuery", + "GqlQueryParameter", + "QueryResultBatch", + }, +) + + +class EntityResult(proto.Message): + r"""The result of fetching an entity from Datastore. + + Attributes: + entity (~.gd_entity.Entity): + The resulting entity. + version (int): + The version of the entity, a strictly positive number that + monotonically increases with changes to the entity. + + This field is set for + [``FULL``][google.datastore.v1.EntityResult.ResultType.FULL] + entity results. + + For [missing][google.datastore.v1.LookupResponse.missing] + entities in ``LookupResponse``, this is the version of the + snapshot that was used to look up the entity, and it is + always set except for eventually consistent reads. + cursor (bytes): + A cursor that points to the position after the result + entity. Set only when the ``EntityResult`` is part of a + ``QueryResultBatch`` message. + """ + + class ResultType(proto.Enum): + r"""Specifies what data the 'entity' field contains. A ``ResultType`` is + either implied (for example, in ``LookupResponse.missing`` from + ``datastore.proto``, it is always ``KEY_ONLY``) or specified by + context (for example, in message ``QueryResultBatch``, field + ``entity_result_type`` specifies a ``ResultType`` for all the values + in field ``entity_results``). + """ + RESULT_TYPE_UNSPECIFIED = 0 + FULL = 1 + PROJECTION = 2 + KEY_ONLY = 3 + + entity = proto.Field(proto.MESSAGE, number=1, message=gd_entity.Entity,) + + version = proto.Field(proto.INT64, number=4) + + cursor = proto.Field(proto.BYTES, number=3) + + +class Query(proto.Message): + r"""A query for entities. + + Attributes: + projection (Sequence[~.query.Projection]): + The projection to return. Defaults to + returning all properties. + kind (Sequence[~.query.KindExpression]): + The kinds to query (if empty, returns + entities of all kinds). Currently at most 1 kind + may be specified. + filter (~.query.Filter): + The filter to apply. + order (Sequence[~.query.PropertyOrder]): + The order to apply to the query results (if + empty, order is unspecified). + distinct_on (Sequence[~.query.PropertyReference]): + The properties to make distinct. The query + results will contain the first result for each + distinct combination of values for the given + properties (if empty, all results are returned). + start_cursor (bytes): + A starting point for the query results. Query cursors are + returned in query result batches and `can only be used to + continue the same + query `__. + end_cursor (bytes): + An ending point for the query results. Query cursors are + returned in query result batches and `can only be used to + limit the same + query `__. + offset (int): + The number of results to skip. Applies before + limit, but after all other constraints. + Optional. Must be >= 0 if specified. + limit (~.wrappers.Int32Value): + The maximum number of results to return. + Applies after all other constraints. Optional. + Unspecified is interpreted as no limit. + Must be >= 0 if specified. + """ + + projection = proto.RepeatedField(proto.MESSAGE, number=2, message="Projection",) + + kind = proto.RepeatedField(proto.MESSAGE, number=3, message="KindExpression",) + + filter = proto.Field(proto.MESSAGE, number=4, message="Filter",) + + order = proto.RepeatedField(proto.MESSAGE, number=5, message="PropertyOrder",) + + distinct_on = proto.RepeatedField( + proto.MESSAGE, number=6, message="PropertyReference", + ) + + start_cursor = proto.Field(proto.BYTES, number=7) + + end_cursor = proto.Field(proto.BYTES, number=8) + + offset = proto.Field(proto.INT32, number=10) + + limit = proto.Field(proto.MESSAGE, number=12, message=wrappers.Int32Value,) + + +class KindExpression(proto.Message): + r"""A representation of a kind. + + Attributes: + name (str): + The name of the kind. + """ + + name = proto.Field(proto.STRING, number=1) + + +class PropertyReference(proto.Message): + r"""A reference to a property relative to the kind expressions. + + Attributes: + name (str): + The name of the property. + If name includes "."s, it may be interpreted as + a property name path. + """ + + name = proto.Field(proto.STRING, number=2) + + +class Projection(proto.Message): + r"""A representation of a property in a projection. + + Attributes: + property (~.query.PropertyReference): + The property to project. + """ + + property = proto.Field(proto.MESSAGE, number=1, message=PropertyReference,) + + +class PropertyOrder(proto.Message): + r"""The desired order for a specific property. + + Attributes: + property (~.query.PropertyReference): + The property to order by. + direction (~.query.PropertyOrder.Direction): + The direction to order by. Defaults to ``ASCENDING``. + """ + + class Direction(proto.Enum): + r"""The sort direction.""" + DIRECTION_UNSPECIFIED = 0 + ASCENDING = 1 + DESCENDING = 2 + + property = proto.Field(proto.MESSAGE, number=1, message=PropertyReference,) + + direction = proto.Field(proto.ENUM, number=2, enum=Direction,) + + +class Filter(proto.Message): + r"""A holder for any type of filter. + + Attributes: + composite_filter (~.query.CompositeFilter): + A composite filter. + property_filter (~.query.PropertyFilter): + A filter on a property. + """ + + composite_filter = proto.Field( + proto.MESSAGE, number=1, oneof="filter_type", message="CompositeFilter", + ) + + property_filter = proto.Field( + proto.MESSAGE, number=2, oneof="filter_type", message="PropertyFilter", + ) + + +class CompositeFilter(proto.Message): + r"""A filter that merges multiple other filters using the given + operator. + + Attributes: + op (~.query.CompositeFilter.Operator): + The operator for combining multiple filters. + filters (Sequence[~.query.Filter]): + The list of filters to combine. + Must contain at least one filter. + """ + + class Operator(proto.Enum): + r"""A composite filter operator.""" + OPERATOR_UNSPECIFIED = 0 + AND = 1 + + op = proto.Field(proto.ENUM, number=1, enum=Operator,) + + filters = proto.RepeatedField(proto.MESSAGE, number=2, message=Filter,) + + +class PropertyFilter(proto.Message): + r"""A filter on a specific property. + + Attributes: + property (~.query.PropertyReference): + The property to filter by. + op (~.query.PropertyFilter.Operator): + The operator to filter by. + value (~.gd_entity.Value): + The value to compare the property to. + """ + + class Operator(proto.Enum): + r"""A property filter operator.""" + OPERATOR_UNSPECIFIED = 0 + LESS_THAN = 1 + LESS_THAN_OR_EQUAL = 2 + GREATER_THAN = 3 + GREATER_THAN_OR_EQUAL = 4 + EQUAL = 5 + HAS_ANCESTOR = 11 + + property = proto.Field(proto.MESSAGE, number=1, message=PropertyReference,) + + op = proto.Field(proto.ENUM, number=2, enum=Operator,) + + value = proto.Field(proto.MESSAGE, number=3, message=gd_entity.Value,) + + +class GqlQuery(proto.Message): + r"""A `GQL + query `__. + + Attributes: + query_string (str): + A string of the format described + `here `__. + allow_literals (bool): + When false, the query string must not contain any literals + and instead must bind all values. For example, + ``SELECT * FROM Kind WHERE a = 'string literal'`` is not + allowed, while ``SELECT * FROM Kind WHERE a = @value`` is. + named_bindings (Sequence[~.query.GqlQuery.NamedBindingsEntry]): + For each non-reserved named binding site in the query + string, there must be a named parameter with that name, but + not necessarily the inverse. + + Key must match regex ``[A-Za-z_$][A-Za-z_$0-9]*``, must not + match regex ``__.*__``, and must not be ``""``. + positional_bindings (Sequence[~.query.GqlQueryParameter]): + Numbered binding site @1 references the first numbered + parameter, effectively using 1-based indexing, rather than + the usual 0. + + For each binding site numbered i in ``query_string``, there + must be an i-th numbered parameter. The inverse must also be + true. + """ + + query_string = proto.Field(proto.STRING, number=1) + + allow_literals = proto.Field(proto.BOOL, number=2) + + named_bindings = proto.MapField( + proto.STRING, proto.MESSAGE, number=5, message="GqlQueryParameter", + ) + + positional_bindings = proto.RepeatedField( + proto.MESSAGE, number=4, message="GqlQueryParameter", + ) + + +class GqlQueryParameter(proto.Message): + r"""A binding parameter for a GQL query. + + Attributes: + value (~.gd_entity.Value): + A value parameter. + cursor (bytes): + A query cursor. Query cursors are returned in + query result batches. + """ + + value = proto.Field( + proto.MESSAGE, number=2, oneof="parameter_type", message=gd_entity.Value, + ) + + cursor = proto.Field(proto.BYTES, number=3, oneof="parameter_type") + + +class QueryResultBatch(proto.Message): + r"""A batch of results produced by a query. + + Attributes: + skipped_results (int): + The number of results skipped, typically + because of an offset. + skipped_cursor (bytes): + A cursor that points to the position after the last skipped + result. Will be set when ``skipped_results`` != 0. + entity_result_type (~.query.EntityResult.ResultType): + The result type for every entity in ``entity_results``. + entity_results (Sequence[~.query.EntityResult]): + The results for this batch. + end_cursor (bytes): + A cursor that points to the position after + the last result in the batch. + more_results (~.query.QueryResultBatch.MoreResultsType): + The state of the query after the current + batch. + snapshot_version (int): + The version number of the snapshot this batch was returned + from. This applies to the range of results from the query's + ``start_cursor`` (or the beginning of the query if no cursor + was given) to this batch's ``end_cursor`` (not the query's + ``end_cursor``). + + In a single transaction, subsequent query result batches for + the same query can have a greater snapshot version number. + Each batch's snapshot version is valid for all preceding + batches. The value will be zero for eventually consistent + queries. + """ + + class MoreResultsType(proto.Enum): + r"""The possible values for the ``more_results`` field.""" + MORE_RESULTS_TYPE_UNSPECIFIED = 0 + NOT_FINISHED = 1 + MORE_RESULTS_AFTER_LIMIT = 2 + MORE_RESULTS_AFTER_CURSOR = 4 + NO_MORE_RESULTS = 3 + + skipped_results = proto.Field(proto.INT32, number=6) + + skipped_cursor = proto.Field(proto.BYTES, number=3) + + entity_result_type = proto.Field( + proto.ENUM, number=1, enum=EntityResult.ResultType, + ) + + entity_results = proto.RepeatedField(proto.MESSAGE, number=2, message=EntityResult,) + + end_cursor = proto.Field(proto.BYTES, number=4) + + more_results = proto.Field(proto.ENUM, number=5, enum=MoreResultsType,) + + snapshot_version = proto.Field(proto.INT64, number=7) + + +__all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/noxfile.py b/noxfile.py index 5ee23c5c..acdde3e6 100644 --- a/noxfile.py +++ b/noxfile.py @@ -27,8 +27,8 @@ BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"] DEFAULT_PYTHON_VERSION = "3.8" -SYSTEM_TEST_PYTHON_VERSIONS = ["2.7", "3.8"] -UNIT_TEST_PYTHON_VERSIONS = ["2.7", "3.5", "3.6", "3.7", "3.8"] +SYSTEM_TEST_PYTHON_VERSIONS = ["3.8"] +UNIT_TEST_PYTHON_VERSIONS = ["3.6", "3.7", "3.8"] @nox.session(python=DEFAULT_PYTHON_VERSION) @@ -70,7 +70,7 @@ def lint_setup_py(session): def default(session): # Install all test dependencies, then install this package in-place. - session.install("mock", "pytest", "pytest-cov") + session.install("mock", "pytest", "pytest-asyncio", "pytest-cov") session.install("-e", ".") # Run py.test against the unit tests. diff --git a/scripts/fixup_datastore_admin_v1_keywords.py b/scripts/fixup_datastore_admin_v1_keywords.py new file mode 100644 index 00000000..12009ba0 --- /dev/null +++ b/scripts/fixup_datastore_admin_v1_keywords.py @@ -0,0 +1,181 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import argparse +import os +import libcst as cst +import pathlib +import sys +from typing import (Any, Callable, Dict, List, Sequence, Tuple) + + +def partition( + predicate: Callable[[Any], bool], + iterator: Sequence[Any] +) -> Tuple[List[Any], List[Any]]: + """A stable, out-of-place partition.""" + results = ([], []) + + for i in iterator: + results[int(predicate(i))].append(i) + + # Returns trueList, falseList + return results[1], results[0] + + +class datastore_adminCallTransformer(cst.CSTTransformer): + CTRL_PARAMS: Tuple[str] = ('retry', 'timeout', 'metadata') + METHOD_TO_PARAMS: Dict[str, Tuple[str]] = { + 'export_entities': ('project_id', 'output_url_prefix', 'labels', 'entity_filter', ), + 'get_index': ('project_id', 'index_id', ), + 'import_entities': ('project_id', 'input_url', 'labels', 'entity_filter', ), + 'list_indexes': ('project_id', 'filter', 'page_size', 'page_token', ), + + } + + def leave_Call(self, original: cst.Call, updated: cst.Call) -> cst.CSTNode: + try: + key = original.func.attr.value + kword_params = self.METHOD_TO_PARAMS[key] + except (AttributeError, KeyError): + # Either not a method from the API or too convoluted to be sure. + return updated + + # If the existing code is valid, keyword args come after positional args. + # Therefore, all positional args must map to the first parameters. + args, kwargs = partition(lambda a: not bool(a.keyword), updated.args) + if any(k.keyword.value == "request" for k in kwargs): + # We've already fixed this file, don't fix it again. + return updated + + kwargs, ctrl_kwargs = partition( + lambda a: not a.keyword.value in self.CTRL_PARAMS, + kwargs + ) + + args, ctrl_args = args[:len(kword_params)], args[len(kword_params):] + ctrl_kwargs.extend(cst.Arg(value=a.value, keyword=cst.Name(value=ctrl)) + for a, ctrl in zip(ctrl_args, self.CTRL_PARAMS)) + + request_arg = cst.Arg( + value=cst.Dict([ + cst.DictElement( + cst.SimpleString("'{}'".format(name)), + cst.Element(value=arg.value) + ) + # Note: the args + kwargs looks silly, but keep in mind that + # the control parameters had to be stripped out, and that + # those could have been passed positionally or by keyword. + for name, arg in zip(kword_params, args + kwargs)]), + keyword=cst.Name("request") + ) + + return updated.with_changes( + args=[request_arg] + ctrl_kwargs + ) + + +def fix_files( + in_dir: pathlib.Path, + out_dir: pathlib.Path, + *, + transformer=datastore_adminCallTransformer(), +): + """Duplicate the input dir to the output dir, fixing file method calls. + + Preconditions: + * in_dir is a real directory + * out_dir is a real, empty directory + """ + pyfile_gen = ( + pathlib.Path(os.path.join(root, f)) + for root, _, files in os.walk(in_dir) + for f in files if os.path.splitext(f)[1] == ".py" + ) + + for fpath in pyfile_gen: + with open(fpath, 'r') as f: + src = f.read() + + # Parse the code and insert method call fixes. + tree = cst.parse_module(src) + updated = tree.visit(transformer) + + # Create the path and directory structure for the new file. + updated_path = out_dir.joinpath(fpath.relative_to(in_dir)) + updated_path.parent.mkdir(parents=True, exist_ok=True) + + # Generate the updated source file at the corresponding path. + with open(updated_path, 'w') as f: + f.write(updated.code) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description="""Fix up source that uses the datastore_admin client library. + +The existing sources are NOT overwritten but are copied to output_dir with changes made. + +Note: This tool operates at a best-effort level at converting positional + parameters in client method calls to keyword based parameters. + Cases where it WILL FAIL include + A) * or ** expansion in a method call. + B) Calls via function or method alias (includes free function calls) + C) Indirect or dispatched calls (e.g. the method is looked up dynamically) + + These all constitute false negatives. The tool will also detect false + positives when an API method shares a name with another method. +""") + parser.add_argument( + '-d', + '--input-directory', + required=True, + dest='input_dir', + help='the input directory to walk for python files to fix up', + ) + parser.add_argument( + '-o', + '--output-directory', + required=True, + dest='output_dir', + help='the directory to output files fixed via un-flattening', + ) + args = parser.parse_args() + input_dir = pathlib.Path(args.input_dir) + output_dir = pathlib.Path(args.output_dir) + if not input_dir.is_dir(): + print( + f"input directory '{input_dir}' does not exist or is not a directory", + file=sys.stderr, + ) + sys.exit(-1) + + if not output_dir.is_dir(): + print( + f"output directory '{output_dir}' does not exist or is not a directory", + file=sys.stderr, + ) + sys.exit(-1) + + if os.listdir(output_dir): + print( + f"output directory '{output_dir}' is not empty", + file=sys.stderr, + ) + sys.exit(-1) + + fix_files(input_dir, output_dir) diff --git a/scripts/fixup_datastore_v1_keywords.py b/scripts/fixup_datastore_v1_keywords.py new file mode 100644 index 00000000..30d643d2 --- /dev/null +++ b/scripts/fixup_datastore_v1_keywords.py @@ -0,0 +1,184 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import argparse +import os +import libcst as cst +import pathlib +import sys +from typing import (Any, Callable, Dict, List, Sequence, Tuple) + + +def partition( + predicate: Callable[[Any], bool], + iterator: Sequence[Any] +) -> Tuple[List[Any], List[Any]]: + """A stable, out-of-place partition.""" + results = ([], []) + + for i in iterator: + results[int(predicate(i))].append(i) + + # Returns trueList, falseList + return results[1], results[0] + + +class datastoreCallTransformer(cst.CSTTransformer): + CTRL_PARAMS: Tuple[str] = ('retry', 'timeout', 'metadata') + METHOD_TO_PARAMS: Dict[str, Tuple[str]] = { + 'allocate_ids': ('project_id', 'keys', ), + 'begin_transaction': ('project_id', 'transaction_options', ), + 'commit': ('project_id', 'mode', 'transaction', 'mutations', ), + 'lookup': ('project_id', 'keys', 'read_options', ), + 'reserve_ids': ('project_id', 'keys', 'database_id', ), + 'rollback': ('project_id', 'transaction', ), + 'run_query': ('project_id', 'partition_id', 'read_options', 'query', 'gql_query', ), + + } + + def leave_Call(self, original: cst.Call, updated: cst.Call) -> cst.CSTNode: + try: + key = original.func.attr.value + kword_params = self.METHOD_TO_PARAMS[key] + except (AttributeError, KeyError): + # Either not a method from the API or too convoluted to be sure. + return updated + + # If the existing code is valid, keyword args come after positional args. + # Therefore, all positional args must map to the first parameters. + args, kwargs = partition(lambda a: not bool(a.keyword), updated.args) + if any(k.keyword.value == "request" for k in kwargs): + # We've already fixed this file, don't fix it again. + return updated + + kwargs, ctrl_kwargs = partition( + lambda a: not a.keyword.value in self.CTRL_PARAMS, + kwargs + ) + + args, ctrl_args = args[:len(kword_params)], args[len(kword_params):] + ctrl_kwargs.extend(cst.Arg(value=a.value, keyword=cst.Name(value=ctrl)) + for a, ctrl in zip(ctrl_args, self.CTRL_PARAMS)) + + request_arg = cst.Arg( + value=cst.Dict([ + cst.DictElement( + cst.SimpleString("'{}'".format(name)), + cst.Element(value=arg.value) + ) + # Note: the args + kwargs looks silly, but keep in mind that + # the control parameters had to be stripped out, and that + # those could have been passed positionally or by keyword. + for name, arg in zip(kword_params, args + kwargs)]), + keyword=cst.Name("request") + ) + + return updated.with_changes( + args=[request_arg] + ctrl_kwargs + ) + + +def fix_files( + in_dir: pathlib.Path, + out_dir: pathlib.Path, + *, + transformer=datastoreCallTransformer(), +): + """Duplicate the input dir to the output dir, fixing file method calls. + + Preconditions: + * in_dir is a real directory + * out_dir is a real, empty directory + """ + pyfile_gen = ( + pathlib.Path(os.path.join(root, f)) + for root, _, files in os.walk(in_dir) + for f in files if os.path.splitext(f)[1] == ".py" + ) + + for fpath in pyfile_gen: + with open(fpath, 'r') as f: + src = f.read() + + # Parse the code and insert method call fixes. + tree = cst.parse_module(src) + updated = tree.visit(transformer) + + # Create the path and directory structure for the new file. + updated_path = out_dir.joinpath(fpath.relative_to(in_dir)) + updated_path.parent.mkdir(parents=True, exist_ok=True) + + # Generate the updated source file at the corresponding path. + with open(updated_path, 'w') as f: + f.write(updated.code) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description="""Fix up source that uses the datastore client library. + +The existing sources are NOT overwritten but are copied to output_dir with changes made. + +Note: This tool operates at a best-effort level at converting positional + parameters in client method calls to keyword based parameters. + Cases where it WILL FAIL include + A) * or ** expansion in a method call. + B) Calls via function or method alias (includes free function calls) + C) Indirect or dispatched calls (e.g. the method is looked up dynamically) + + These all constitute false negatives. The tool will also detect false + positives when an API method shares a name with another method. +""") + parser.add_argument( + '-d', + '--input-directory', + required=True, + dest='input_dir', + help='the input directory to walk for python files to fix up', + ) + parser.add_argument( + '-o', + '--output-directory', + required=True, + dest='output_dir', + help='the directory to output files fixed via un-flattening', + ) + args = parser.parse_args() + input_dir = pathlib.Path(args.input_dir) + output_dir = pathlib.Path(args.output_dir) + if not input_dir.is_dir(): + print( + f"input directory '{input_dir}' does not exist or is not a directory", + file=sys.stderr, + ) + sys.exit(-1) + + if not output_dir.is_dir(): + print( + f"output directory '{output_dir}' does not exist or is not a directory", + file=sys.stderr, + ) + sys.exit(-1) + + if os.listdir(output_dir): + print( + f"output directory '{output_dir}' is not empty", + file=sys.stderr, + ) + sys.exit(-1) + + fix_files(input_dir, output_dir) diff --git a/setup.py b/setup.py index 3a8b88af..2df2e821 100644 --- a/setup.py +++ b/setup.py @@ -29,8 +29,10 @@ # 'Development Status :: 5 - Production/Stable' release_status = "Development Status :: 5 - Production/Stable" dependencies = [ - "google-api-core[grpc] >= 1.14.0, < 2.0.0dev", + "google-api-core[grpc] >= 1.22.2, < 2.0.0dev", "google-cloud-core >= 1.4.0, < 2.0dev", + "proto-plus >= 1.4.0", + "libcst >= 0.2.5", ] extras = {} @@ -51,7 +53,9 @@ # Only include packages under the 'google' namespace. Do not include tests, # benchmarks, etc. packages = [ - package for package in setuptools.find_packages() if package.startswith("google") + package + for package in setuptools.PEP420PackageFinder.find() + if package.startswith("google") ] # Determine which namespaces are needed. @@ -74,21 +78,25 @@ "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python", - "Programming Language :: Python :: 2", - "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", "Operating System :: OS Independent", "Topic :: Internet", + "Topic :: Software Development :: Libraries :: Python Modules", ], platforms="Posix; MacOS X; Windows", packages=packages, namespace_packages=namespaces, install_requires=dependencies, extras_require=extras, - python_requires=">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*", + python_requires=">=3.6", + scripts=[ + "scripts/fixup_datastore_v1_keywords.py", + "scripts/fixup_datastore_admin_v1_keywords.py", + ], include_package_data=True, zip_safe=False, ) diff --git a/synth.metadata b/synth.metadata index 4c3400d3..279f3489 100644 --- a/synth.metadata +++ b/synth.metadata @@ -3,23 +3,15 @@ { "git": { "name": ".", - "remote": "https://github.com/googleapis/python-datastore.git", - "sha": "8aa3eac28e0e733b61d6ab9e1d233a99467b7081" - } - }, - { - "git": { - "name": "googleapis", - "remote": "https://github.com/googleapis/googleapis.git", - "sha": "8d73f9486fc193a150f6c907dfb9f49431aff3ff", - "internalRef": "332497859" + "remote": "git@github.com:crwilcox/python-datastore.git", + "sha": "10442b5a6aec7a352612f02368bf1257ddbfc855" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "f3c04883d6c43261ff13db1f52d03a283be06871" + "sha": "487eba79f8260e34205d8ceb1ebcc65685085e19" } } ], @@ -42,93 +34,5 @@ "generator": "bazel" } } - ], - "generatedFiles": [ - ".coveragerc", - ".flake8", - ".github/CONTRIBUTING.md", - ".github/ISSUE_TEMPLATE/bug_report.md", - ".github/ISSUE_TEMPLATE/feature_request.md", - ".github/ISSUE_TEMPLATE/support_request.md", - ".github/PULL_REQUEST_TEMPLATE.md", - ".github/release-please.yml", - ".github/snippet-bot.yml", - ".gitignore", - ".kokoro/build.sh", - ".kokoro/continuous/common.cfg", - ".kokoro/continuous/continuous.cfg", - ".kokoro/docker/docs/Dockerfile", - ".kokoro/docker/docs/fetch_gpg_keys.sh", - ".kokoro/docs/common.cfg", - ".kokoro/docs/docs-presubmit.cfg", - ".kokoro/docs/docs.cfg", - ".kokoro/populate-secrets.sh", - ".kokoro/presubmit/common.cfg", - ".kokoro/presubmit/presubmit.cfg", - ".kokoro/publish-docs.sh", - ".kokoro/release.sh", - ".kokoro/release/common.cfg", - ".kokoro/release/release.cfg", - ".kokoro/samples/lint/common.cfg", - ".kokoro/samples/lint/continuous.cfg", - ".kokoro/samples/lint/periodic.cfg", - ".kokoro/samples/lint/presubmit.cfg", - ".kokoro/samples/python3.6/common.cfg", - ".kokoro/samples/python3.6/continuous.cfg", - ".kokoro/samples/python3.6/periodic.cfg", - ".kokoro/samples/python3.6/presubmit.cfg", - ".kokoro/samples/python3.7/common.cfg", - ".kokoro/samples/python3.7/continuous.cfg", - ".kokoro/samples/python3.7/periodic.cfg", - ".kokoro/samples/python3.7/presubmit.cfg", - ".kokoro/samples/python3.8/common.cfg", - ".kokoro/samples/python3.8/continuous.cfg", - ".kokoro/samples/python3.8/periodic.cfg", - ".kokoro/samples/python3.8/presubmit.cfg", - ".kokoro/test-samples.sh", - ".kokoro/trampoline.sh", - ".kokoro/trampoline_v2.sh", - ".trampolinerc", - "CODE_OF_CONDUCT.md", - "CONTRIBUTING.rst", - "LICENSE", - "MANIFEST.in", - "docs/_static/custom.css", - "docs/_templates/layout.html", - "docs/conf.py", - "google/cloud/datastore_admin_v1/proto/__init__.py", - "google/cloud/datastore_admin_v1/proto/datastore_admin.proto", - "google/cloud/datastore_admin_v1/proto/datastore_admin_pb2.py", - "google/cloud/datastore_admin_v1/proto/datastore_admin_pb2_grpc.py", - "google/cloud/datastore_admin_v1/proto/index.proto", - "google/cloud/datastore_admin_v1/proto/index_pb2.py", - "google/cloud/datastore_admin_v1/proto/index_pb2_grpc.py", - "google/cloud/datastore_v1/gapic/__init__.py", - "google/cloud/datastore_v1/gapic/datastore_client.py", - "google/cloud/datastore_v1/gapic/datastore_client_config.py", - "google/cloud/datastore_v1/gapic/enums.py", - "google/cloud/datastore_v1/gapic/transports/__init__.py", - "google/cloud/datastore_v1/gapic/transports/datastore_grpc_transport.py", - "google/cloud/datastore_v1/proto/__init__.py", - "google/cloud/datastore_v1/proto/datastore.proto", - "google/cloud/datastore_v1/proto/datastore_pb2.py", - "google/cloud/datastore_v1/proto/datastore_pb2_grpc.py", - "google/cloud/datastore_v1/proto/entity.proto", - "google/cloud/datastore_v1/proto/entity_pb2.py", - "google/cloud/datastore_v1/proto/entity_pb2_grpc.py", - "google/cloud/datastore_v1/proto/query.proto", - "google/cloud/datastore_v1/proto/query_pb2.py", - "google/cloud/datastore_v1/proto/query_pb2_grpc.py", - "noxfile.py", - "renovate.json", - "scripts/decrypt-secrets.sh", - "scripts/readme-gen/readme_gen.py", - "scripts/readme-gen/templates/README.tmpl.rst", - "scripts/readme-gen/templates/auth.tmpl.rst", - "scripts/readme-gen/templates/auth_api_key.tmpl.rst", - "scripts/readme-gen/templates/install_deps.tmpl.rst", - "scripts/readme-gen/templates/install_portaudio.tmpl.rst", - "setup.cfg", - "testing/.gitignore" ] } \ No newline at end of file diff --git a/synth.py b/synth.py index af705849..251fcc86 100644 --- a/synth.py +++ b/synth.py @@ -29,8 +29,13 @@ include_protos=True, ) -s.move(library / "google/cloud/datastore_v1/proto") -s.move(library / "google/cloud/datastore_v1/gapic") +s.move(library / "google/cloud/datastore_v1") + +s.move( + library / f"tests/", + f"tests", +) +s.move(library / "scripts") # ---------------------------------------------------------------------------- # Generate datastore admin GAPIC layer @@ -43,12 +48,17 @@ ) s.move( - library / "datastore-admin-v1-py/google/cloud/datastore_admin_v1", + library / "google/cloud/datastore_admin_v1", "google/cloud/datastore_admin_v1" ) -s.move(library / "google/cloud/datastore_admin_v1/proto") +s.move( + library / f"tests/", + f"tests", +) + +s.move(library / "scripts") s.replace( "google/**/datastore_admin_client.py", "google-cloud-datastore-admin", @@ -87,14 +97,14 @@ ):""" ) -if num != 1: - raise Exception("Required replacement not made.") +#if num != 1: +# raise Exception("Required replacement not made.") # ---------------------------------------------------------------------------- # Add templated files # ---------------------------------------------------------------------------- templated_files = common.py_library(unit_cov_level=97, cov_level=99) -s.move(templated_files, excludes=["docs/multiprocessing.rst"]) +s.move(templated_files, excludes=["docs/multiprocessing.rst", ".coveragerc"]) s.replace("noxfile.py", """["']sphinx['"]""", '''"sphinx<3.0.0"''') diff --git a/tests/doctests.py b/tests/doctests.py index 329b3d41..cc8d6a3a 100644 --- a/tests/doctests.py +++ b/tests/doctests.py @@ -17,8 +17,6 @@ import tempfile import unittest -import six - from google.cloud import datastore @@ -39,7 +37,6 @@ """ -@unittest.skipIf(six.PY2, "Doctests run against Python 3 only.") class TestDoctest(unittest.TestCase): def _submodules(self): pkg_iter = pkgutil.iter_modules(datastore.__path__) diff --git a/tests/unit/gapic/datastore_admin_v1/__init__.py b/tests/unit/gapic/datastore_admin_v1/__init__.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/tests/unit/gapic/datastore_admin_v1/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/unit/gapic/datastore_admin_v1/test_datastore_admin.py b/tests/unit/gapic/datastore_admin_v1/test_datastore_admin.py new file mode 100644 index 00000000..1efc1dca --- /dev/null +++ b/tests/unit/gapic/datastore_admin_v1/test_datastore_admin.py @@ -0,0 +1,1425 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import os +import mock + +import grpc +from grpc.experimental import aio +import math +import pytest +from proto.marshal.rules.dates import DurationRule, TimestampRule + +from google import auth +from google.api_core import client_options +from google.api_core import exceptions +from google.api_core import future +from google.api_core import gapic_v1 +from google.api_core import grpc_helpers +from google.api_core import grpc_helpers_async +from google.api_core import operation_async +from google.api_core import operations_v1 +from google.auth import credentials +from google.auth.exceptions import MutualTLSChannelError +from google.cloud.datastore_admin_v1.services.datastore_admin import ( + DatastoreAdminAsyncClient, +) +from google.cloud.datastore_admin_v1.services.datastore_admin import ( + DatastoreAdminClient, +) +from google.cloud.datastore_admin_v1.services.datastore_admin import pagers +from google.cloud.datastore_admin_v1.services.datastore_admin import transports +from google.cloud.datastore_admin_v1.types import datastore_admin +from google.cloud.datastore_admin_v1.types import index +from google.longrunning import operations_pb2 +from google.oauth2 import service_account + + +def client_cert_source_callback(): + return b"cert bytes", b"key bytes" + + +# If default endpoint is localhost, then default mtls endpoint will be the same. +# This method modifies the default endpoint so the client can produce a different +# mtls endpoint for endpoint testing purposes. +def modify_default_endpoint(client): + return ( + "foo.googleapis.com" + if ("localhost" in client.DEFAULT_ENDPOINT) + else client.DEFAULT_ENDPOINT + ) + + +def test__get_default_mtls_endpoint(): + api_endpoint = "example.googleapis.com" + api_mtls_endpoint = "example.mtls.googleapis.com" + sandbox_endpoint = "example.sandbox.googleapis.com" + sandbox_mtls_endpoint = "example.mtls.sandbox.googleapis.com" + non_googleapi = "api.example.com" + + assert DatastoreAdminClient._get_default_mtls_endpoint(None) is None + assert ( + DatastoreAdminClient._get_default_mtls_endpoint(api_endpoint) + == api_mtls_endpoint + ) + assert ( + DatastoreAdminClient._get_default_mtls_endpoint(api_mtls_endpoint) + == api_mtls_endpoint + ) + assert ( + DatastoreAdminClient._get_default_mtls_endpoint(sandbox_endpoint) + == sandbox_mtls_endpoint + ) + assert ( + DatastoreAdminClient._get_default_mtls_endpoint(sandbox_mtls_endpoint) + == sandbox_mtls_endpoint + ) + assert ( + DatastoreAdminClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi + ) + + +@pytest.mark.parametrize( + "client_class", [DatastoreAdminClient, DatastoreAdminAsyncClient] +) +def test_datastore_admin_client_from_service_account_file(client_class): + creds = credentials.AnonymousCredentials() + with mock.patch.object( + service_account.Credentials, "from_service_account_file" + ) as factory: + factory.return_value = creds + client = client_class.from_service_account_file("dummy/file/path.json") + assert client._transport._credentials == creds + + client = client_class.from_service_account_json("dummy/file/path.json") + assert client._transport._credentials == creds + + assert client._transport._host == "datastore.googleapis.com:443" + + +def test_datastore_admin_client_get_transport_class(): + transport = DatastoreAdminClient.get_transport_class() + assert transport == transports.DatastoreAdminGrpcTransport + + transport = DatastoreAdminClient.get_transport_class("grpc") + assert transport == transports.DatastoreAdminGrpcTransport + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + (DatastoreAdminClient, transports.DatastoreAdminGrpcTransport, "grpc"), + ( + DatastoreAdminAsyncClient, + transports.DatastoreAdminGrpcAsyncIOTransport, + "grpc_asyncio", + ), + ], +) +@mock.patch.object( + DatastoreAdminClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(DatastoreAdminClient), +) +@mock.patch.object( + DatastoreAdminAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(DatastoreAdminAsyncClient), +) +def test_datastore_admin_client_client_options( + client_class, transport_class, transport_name +): + # Check that if channel is provided we won't create a new one. + with mock.patch.object(DatastoreAdminClient, "get_transport_class") as gtc: + transport = transport_class(credentials=credentials.AnonymousCredentials()) + client = client_class(transport=transport) + gtc.assert_not_called() + + # Check that if channel is provided via str we will create a new one. + with mock.patch.object(DatastoreAdminClient, "get_transport_class") as gtc: + client = client_class(transport=transport_name) + gtc.assert_called() + + # Check the case api_endpoint is provided. + options = client_options.ClientOptions(api_endpoint="squid.clam.whelk") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is + # "never". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is + # "always". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_MTLS_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has + # unsupported value. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError): + client = client_class() + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError): + client = client_class() + + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,use_client_cert_env", + [ + (DatastoreAdminClient, transports.DatastoreAdminGrpcTransport, "grpc", "true"), + ( + DatastoreAdminAsyncClient, + transports.DatastoreAdminGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (DatastoreAdminClient, transports.DatastoreAdminGrpcTransport, "grpc", "false"), + ( + DatastoreAdminAsyncClient, + transports.DatastoreAdminGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), + ], +) +@mock.patch.object( + DatastoreAdminClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(DatastoreAdminClient), +) +@mock.patch.object( + DatastoreAdminAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(DatastoreAdminAsyncClient), +) +@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) +def test_datastore_admin_client_mtls_env_auto( + client_class, transport_class, transport_name, use_client_cert_env +): + # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default + # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. + + # Check the case client_cert_source is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + options = client_options.ClientOptions( + client_cert_source=client_cert_source_callback + ) + with mock.patch.object(transport_class, "__init__") as patched: + ssl_channel_creds = mock.Mock() + with mock.patch( + "grpc.ssl_channel_credentials", return_value=ssl_channel_creds + ): + patched.return_value = None + client = client_class(client_options=options) + + if use_client_cert_env == "false": + expected_ssl_channel_creds = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_ssl_channel_creds = ssl_channel_creds + expected_host = client.DEFAULT_MTLS_ENDPOINT + + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case ADC client cert is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.ssl_credentials", + new_callable=mock.PropertyMock, + ) as ssl_credentials_mock: + if use_client_cert_env == "false": + is_mtls_mock.return_value = False + ssl_credentials_mock.return_value = None + expected_host = client.DEFAULT_ENDPOINT + expected_ssl_channel_creds = None + else: + is_mtls_mock.return_value = True + ssl_credentials_mock.return_value = mock.Mock() + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_ssl_channel_creds = ( + ssl_credentials_mock.return_value + ) + + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + is_mtls_mock.return_value = False + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + (DatastoreAdminClient, transports.DatastoreAdminGrpcTransport, "grpc"), + ( + DatastoreAdminAsyncClient, + transports.DatastoreAdminGrpcAsyncIOTransport, + "grpc_asyncio", + ), + ], +) +def test_datastore_admin_client_client_options_scopes( + client_class, transport_class, transport_name +): + # Check the case scopes are provided. + options = client_options.ClientOptions(scopes=["1", "2"],) + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=["1", "2"], + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + (DatastoreAdminClient, transports.DatastoreAdminGrpcTransport, "grpc"), + ( + DatastoreAdminAsyncClient, + transports.DatastoreAdminGrpcAsyncIOTransport, + "grpc_asyncio", + ), + ], +) +def test_datastore_admin_client_client_options_credentials_file( + client_class, transport_class, transport_name +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +def test_datastore_admin_client_client_options_from_dict(): + with mock.patch( + "google.cloud.datastore_admin_v1.services.datastore_admin.transports.DatastoreAdminGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = DatastoreAdminClient( + client_options={"api_endpoint": "squid.clam.whelk"} + ) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +def test_export_entities( + transport: str = "grpc", request_type=datastore_admin.ExportEntitiesRequest +): + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.export_entities), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + + response = client.export_entities(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore_admin.ExportEntitiesRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_export_entities_from_dict(): + test_export_entities(request_type=dict) + + +@pytest.mark.asyncio +async def test_export_entities_async(transport: str = "grpc_asyncio"): + client = DatastoreAdminAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore_admin.ExportEntitiesRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.export_entities), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + + response = await client.export_entities(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_export_entities_flattened(): + client = DatastoreAdminClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.export_entities), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.export_entities( + project_id="project_id_value", + labels={"key_value": "value_value"}, + entity_filter=datastore_admin.EntityFilter(kinds=["kinds_value"]), + output_url_prefix="output_url_prefix_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].labels == {"key_value": "value_value"} + + assert args[0].entity_filter == datastore_admin.EntityFilter( + kinds=["kinds_value"] + ) + + assert args[0].output_url_prefix == "output_url_prefix_value" + + +def test_export_entities_flattened_error(): + client = DatastoreAdminClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.export_entities( + datastore_admin.ExportEntitiesRequest(), + project_id="project_id_value", + labels={"key_value": "value_value"}, + entity_filter=datastore_admin.EntityFilter(kinds=["kinds_value"]), + output_url_prefix="output_url_prefix_value", + ) + + +@pytest.mark.asyncio +async def test_export_entities_flattened_async(): + client = DatastoreAdminAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.export_entities), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.export_entities( + project_id="project_id_value", + labels={"key_value": "value_value"}, + entity_filter=datastore_admin.EntityFilter(kinds=["kinds_value"]), + output_url_prefix="output_url_prefix_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].labels == {"key_value": "value_value"} + + assert args[0].entity_filter == datastore_admin.EntityFilter( + kinds=["kinds_value"] + ) + + assert args[0].output_url_prefix == "output_url_prefix_value" + + +@pytest.mark.asyncio +async def test_export_entities_flattened_error_async(): + client = DatastoreAdminAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.export_entities( + datastore_admin.ExportEntitiesRequest(), + project_id="project_id_value", + labels={"key_value": "value_value"}, + entity_filter=datastore_admin.EntityFilter(kinds=["kinds_value"]), + output_url_prefix="output_url_prefix_value", + ) + + +def test_import_entities( + transport: str = "grpc", request_type=datastore_admin.ImportEntitiesRequest +): + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.import_entities), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/spam") + + response = client.import_entities(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore_admin.ImportEntitiesRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_import_entities_from_dict(): + test_import_entities(request_type=dict) + + +@pytest.mark.asyncio +async def test_import_entities_async(transport: str = "grpc_asyncio"): + client = DatastoreAdminAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore_admin.ImportEntitiesRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.import_entities), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + + response = await client.import_entities(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, future.Future) + + +def test_import_entities_flattened(): + client = DatastoreAdminClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.import_entities), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.import_entities( + project_id="project_id_value", + labels={"key_value": "value_value"}, + input_url="input_url_value", + entity_filter=datastore_admin.EntityFilter(kinds=["kinds_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].labels == {"key_value": "value_value"} + + assert args[0].input_url == "input_url_value" + + assert args[0].entity_filter == datastore_admin.EntityFilter( + kinds=["kinds_value"] + ) + + +def test_import_entities_flattened_error(): + client = DatastoreAdminClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.import_entities( + datastore_admin.ImportEntitiesRequest(), + project_id="project_id_value", + labels={"key_value": "value_value"}, + input_url="input_url_value", + entity_filter=datastore_admin.EntityFilter(kinds=["kinds_value"]), + ) + + +@pytest.mark.asyncio +async def test_import_entities_flattened_async(): + client = DatastoreAdminAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.import_entities), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = operations_pb2.Operation(name="operations/op") + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + operations_pb2.Operation(name="operations/spam") + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.import_entities( + project_id="project_id_value", + labels={"key_value": "value_value"}, + input_url="input_url_value", + entity_filter=datastore_admin.EntityFilter(kinds=["kinds_value"]), + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].labels == {"key_value": "value_value"} + + assert args[0].input_url == "input_url_value" + + assert args[0].entity_filter == datastore_admin.EntityFilter( + kinds=["kinds_value"] + ) + + +@pytest.mark.asyncio +async def test_import_entities_flattened_error_async(): + client = DatastoreAdminAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.import_entities( + datastore_admin.ImportEntitiesRequest(), + project_id="project_id_value", + labels={"key_value": "value_value"}, + input_url="input_url_value", + entity_filter=datastore_admin.EntityFilter(kinds=["kinds_value"]), + ) + + +def test_get_index( + transport: str = "grpc", request_type=datastore_admin.GetIndexRequest +): + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.get_index), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = index.Index( + project_id="project_id_value", + index_id="index_id_value", + kind="kind_value", + ancestor=index.Index.AncestorMode.NONE, + state=index.Index.State.CREATING, + ) + + response = client.get_index(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore_admin.GetIndexRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, index.Index) + + assert response.project_id == "project_id_value" + + assert response.index_id == "index_id_value" + + assert response.kind == "kind_value" + + assert response.ancestor == index.Index.AncestorMode.NONE + + assert response.state == index.Index.State.CREATING + + +def test_get_index_from_dict(): + test_get_index(request_type=dict) + + +@pytest.mark.asyncio +async def test_get_index_async(transport: str = "grpc_asyncio"): + client = DatastoreAdminAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore_admin.GetIndexRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.get_index), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + index.Index( + project_id="project_id_value", + index_id="index_id_value", + kind="kind_value", + ancestor=index.Index.AncestorMode.NONE, + state=index.Index.State.CREATING, + ) + ) + + response = await client.get_index(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, index.Index) + + assert response.project_id == "project_id_value" + + assert response.index_id == "index_id_value" + + assert response.kind == "kind_value" + + assert response.ancestor == index.Index.AncestorMode.NONE + + assert response.state == index.Index.State.CREATING + + +def test_list_indexes( + transport: str = "grpc", request_type=datastore_admin.ListIndexesRequest +): + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.list_indexes), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore_admin.ListIndexesResponse( + next_page_token="next_page_token_value", + ) + + response = client.list_indexes(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore_admin.ListIndexesRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListIndexesPager) + + assert response.next_page_token == "next_page_token_value" + + +def test_list_indexes_from_dict(): + test_list_indexes(request_type=dict) + + +@pytest.mark.asyncio +async def test_list_indexes_async(transport: str = "grpc_asyncio"): + client = DatastoreAdminAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore_admin.ListIndexesRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.list_indexes), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore_admin.ListIndexesResponse( + next_page_token="next_page_token_value", + ) + ) + + response = await client.list_indexes(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListIndexesAsyncPager) + + assert response.next_page_token == "next_page_token_value" + + +def test_list_indexes_pager(): + client = DatastoreAdminClient(credentials=credentials.AnonymousCredentials,) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.list_indexes), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + datastore_admin.ListIndexesResponse( + indexes=[index.Index(), index.Index(), index.Index(),], + next_page_token="abc", + ), + datastore_admin.ListIndexesResponse(indexes=[], next_page_token="def",), + datastore_admin.ListIndexesResponse( + indexes=[index.Index(),], next_page_token="ghi", + ), + datastore_admin.ListIndexesResponse( + indexes=[index.Index(), index.Index(),], + ), + RuntimeError, + ) + + metadata = () + pager = client.list_indexes(request={}) + + assert pager._metadata == metadata + + results = [i for i in pager] + assert len(results) == 6 + assert all(isinstance(i, index.Index) for i in results) + + +def test_list_indexes_pages(): + client = DatastoreAdminClient(credentials=credentials.AnonymousCredentials,) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.list_indexes), "__call__") as call: + # Set the response to a series of pages. + call.side_effect = ( + datastore_admin.ListIndexesResponse( + indexes=[index.Index(), index.Index(), index.Index(),], + next_page_token="abc", + ), + datastore_admin.ListIndexesResponse(indexes=[], next_page_token="def",), + datastore_admin.ListIndexesResponse( + indexes=[index.Index(),], next_page_token="ghi", + ), + datastore_admin.ListIndexesResponse( + indexes=[index.Index(), index.Index(),], + ), + RuntimeError, + ) + pages = list(client.list_indexes(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.asyncio +async def test_list_indexes_async_pager(): + client = DatastoreAdminAsyncClient(credentials=credentials.AnonymousCredentials,) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.list_indexes), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + datastore_admin.ListIndexesResponse( + indexes=[index.Index(), index.Index(), index.Index(),], + next_page_token="abc", + ), + datastore_admin.ListIndexesResponse(indexes=[], next_page_token="def",), + datastore_admin.ListIndexesResponse( + indexes=[index.Index(),], next_page_token="ghi", + ), + datastore_admin.ListIndexesResponse( + indexes=[index.Index(), index.Index(),], + ), + RuntimeError, + ) + async_pager = await client.list_indexes(request={},) + assert async_pager.next_page_token == "abc" + responses = [] + async for response in async_pager: + responses.append(response) + + assert len(responses) == 6 + assert all(isinstance(i, index.Index) for i in responses) + + +@pytest.mark.asyncio +async def test_list_indexes_async_pages(): + client = DatastoreAdminAsyncClient(credentials=credentials.AnonymousCredentials,) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.list_indexes), + "__call__", + new_callable=mock.AsyncMock, + ) as call: + # Set the response to a series of pages. + call.side_effect = ( + datastore_admin.ListIndexesResponse( + indexes=[index.Index(), index.Index(), index.Index(),], + next_page_token="abc", + ), + datastore_admin.ListIndexesResponse(indexes=[], next_page_token="def",), + datastore_admin.ListIndexesResponse( + indexes=[index.Index(),], next_page_token="ghi", + ), + datastore_admin.ListIndexesResponse( + indexes=[index.Index(), index.Index(),], + ), + RuntimeError, + ) + pages = [] + async for page_ in (await client.list_indexes(request={})).pages: + pages.append(page_) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.DatastoreAdminGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.DatastoreAdminGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DatastoreAdminClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.DatastoreAdminGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DatastoreAdminClient( + client_options={"scopes": ["1", "2"]}, transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.DatastoreAdminGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + client = DatastoreAdminClient(transport=transport) + assert client._transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.DatastoreAdminGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.DatastoreAdminGrpcAsyncIOTransport( + credentials=credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.DatastoreAdminGrpcTransport, + transports.DatastoreAdminGrpcAsyncIOTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = DatastoreAdminClient(credentials=credentials.AnonymousCredentials(),) + assert isinstance(client._transport, transports.DatastoreAdminGrpcTransport,) + + +def test_datastore_admin_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(exceptions.DuplicateCredentialArgs): + transport = transports.DatastoreAdminTransport( + credentials=credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_datastore_admin_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.datastore_admin_v1.services.datastore_admin.transports.DatastoreAdminTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.DatastoreAdminTransport( + credentials=credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "export_entities", + "import_entities", + "get_index", + "list_indexes", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + # Additionally, the LRO client (a property) should + # also raise NotImplementedError + with pytest.raises(NotImplementedError): + transport.operations_client + + +def test_datastore_admin_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + auth, "load_credentials_from_file" + ) as load_creds, mock.patch( + "google.cloud.datastore_admin_v1.services.datastore_admin.transports.DatastoreAdminTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.DatastoreAdminTransport( + credentials_file="credentials.json", quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + quota_project_id="octopus", + ) + + +def test_datastore_admin_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(auth, "default") as adc, mock.patch( + "google.cloud.datastore_admin_v1.services.datastore_admin.transports.DatastoreAdminTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.DatastoreAdminTransport() + adc.assert_called_once() + + +def test_datastore_admin_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + DatastoreAdminClient() + adc.assert_called_once_with( + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + quota_project_id=None, + ) + + +def test_datastore_admin_transport_auth_adc(): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transports.DatastoreAdminGrpcTransport( + host="squid.clam.whelk", quota_project_id="octopus" + ) + adc.assert_called_once_with( + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + quota_project_id="octopus", + ) + + +def test_datastore_admin_host_no_port(): + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="datastore.googleapis.com" + ), + ) + assert client._transport._host == "datastore.googleapis.com:443" + + +def test_datastore_admin_host_with_port(): + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="datastore.googleapis.com:8000" + ), + ) + assert client._transport._host == "datastore.googleapis.com:8000" + + +def test_datastore_admin_grpc_transport_channel(): + channel = grpc.insecure_channel("http://localhost/") + + # Check that channel is used if provided. + transport = transports.DatastoreAdminGrpcTransport( + host="squid.clam.whelk", channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + + +def test_datastore_admin_grpc_asyncio_transport_channel(): + channel = aio.insecure_channel("http://localhost/") + + # Check that channel is used if provided. + transport = transports.DatastoreAdminGrpcAsyncIOTransport( + host="squid.clam.whelk", channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.DatastoreAdminGrpcTransport, + transports.DatastoreAdminGrpcAsyncIOTransport, + ], +) +def test_datastore_admin_transport_channel_mtls_with_client_cert_source( + transport_class, +): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_ssl_cred = mock.Mock() + grpc_ssl_channel_cred.return_value = mock_ssl_cred + + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + + cred = credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(auth, "default") as adc: + adc.return_value = (cred, None) + transport = transport_class( + host="squid.clam.whelk", + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=client_cert_source_callback, + ) + adc.assert_called_once() + + grpc_ssl_channel_cred.assert_called_once_with( + certificate_chain=b"cert bytes", private_key=b"key bytes" + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + ) + assert transport.grpc_channel == mock_grpc_channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.DatastoreAdminGrpcTransport, + transports.DatastoreAdminGrpcAsyncIOTransport, + ], +) +def test_datastore_admin_transport_channel_mtls_with_adc(transport_class): + mock_ssl_cred = mock.Mock() + with mock.patch.multiple( + "google.auth.transport.grpc.SslCredentials", + __init__=mock.Mock(return_value=None), + ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), + ): + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + mock_cred = mock.Mock() + + with pytest.warns(DeprecationWarning): + transport = transport_class( + host="squid.clam.whelk", + credentials=mock_cred, + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=None, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_datastore_admin_grpc_lro_client(): + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), transport="grpc", + ) + transport = client._transport + + # Ensure that we have a api-core operations client. + assert isinstance(transport.operations_client, operations_v1.OperationsClient,) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + +def test_datastore_admin_grpc_lro_async_client(): + client = DatastoreAdminAsyncClient( + credentials=credentials.AnonymousCredentials(), transport="grpc_asyncio", + ) + transport = client._client._transport + + # Ensure that we have a api-core operations client. + assert isinstance(transport.operations_client, operations_v1.OperationsAsyncClient,) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + +def test_client_withDEFAULT_CLIENT_INFO(): + client_info = gapic_v1.client_info.ClientInfo() + + with mock.patch.object( + transports.DatastoreAdminTransport, "_prep_wrapped_messages" + ) as prep: + client = DatastoreAdminClient( + credentials=credentials.AnonymousCredentials(), client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.DatastoreAdminTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = DatastoreAdminClient.get_transport_class() + transport = transport_class( + credentials=credentials.AnonymousCredentials(), client_info=client_info, + ) + prep.assert_called_once_with(client_info) diff --git a/tests/unit/gapic/datastore_v1/__init__.py b/tests/unit/gapic/datastore_v1/__init__.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/tests/unit/gapic/datastore_v1/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/unit/gapic/datastore_v1/test_datastore.py b/tests/unit/gapic/datastore_v1/test_datastore.py new file mode 100644 index 00000000..e5201c3a --- /dev/null +++ b/tests/unit/gapic/datastore_v1/test_datastore.py @@ -0,0 +1,1817 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +import os +import mock + +import grpc +from grpc.experimental import aio +import math +import pytest +from proto.marshal.rules.dates import DurationRule, TimestampRule + +from google import auth +from google.api_core import client_options +from google.api_core import exceptions +from google.api_core import gapic_v1 +from google.api_core import grpc_helpers +from google.api_core import grpc_helpers_async +from google.auth import credentials +from google.auth.exceptions import MutualTLSChannelError +from google.cloud.datastore_v1.services.datastore import DatastoreAsyncClient +from google.cloud.datastore_v1.services.datastore import DatastoreClient +from google.cloud.datastore_v1.services.datastore import transports +from google.cloud.datastore_v1.types import datastore +from google.cloud.datastore_v1.types import entity +from google.cloud.datastore_v1.types import query +from google.oauth2 import service_account +from google.protobuf import struct_pb2 as struct # type: ignore +from google.protobuf import timestamp_pb2 as timestamp # type: ignore +from google.protobuf import wrappers_pb2 as wrappers # type: ignore +from google.type import latlng_pb2 as latlng # type: ignore + + +def client_cert_source_callback(): + return b"cert bytes", b"key bytes" + + +# If default endpoint is localhost, then default mtls endpoint will be the same. +# This method modifies the default endpoint so the client can produce a different +# mtls endpoint for endpoint testing purposes. +def modify_default_endpoint(client): + return ( + "foo.googleapis.com" + if ("localhost" in client.DEFAULT_ENDPOINT) + else client.DEFAULT_ENDPOINT + ) + + +def test__get_default_mtls_endpoint(): + api_endpoint = "example.googleapis.com" + api_mtls_endpoint = "example.mtls.googleapis.com" + sandbox_endpoint = "example.sandbox.googleapis.com" + sandbox_mtls_endpoint = "example.mtls.sandbox.googleapis.com" + non_googleapi = "api.example.com" + + assert DatastoreClient._get_default_mtls_endpoint(None) is None + assert DatastoreClient._get_default_mtls_endpoint(api_endpoint) == api_mtls_endpoint + assert ( + DatastoreClient._get_default_mtls_endpoint(api_mtls_endpoint) + == api_mtls_endpoint + ) + assert ( + DatastoreClient._get_default_mtls_endpoint(sandbox_endpoint) + == sandbox_mtls_endpoint + ) + assert ( + DatastoreClient._get_default_mtls_endpoint(sandbox_mtls_endpoint) + == sandbox_mtls_endpoint + ) + assert DatastoreClient._get_default_mtls_endpoint(non_googleapi) == non_googleapi + + +@pytest.mark.parametrize("client_class", [DatastoreClient, DatastoreAsyncClient]) +def test_datastore_client_from_service_account_file(client_class): + creds = credentials.AnonymousCredentials() + with mock.patch.object( + service_account.Credentials, "from_service_account_file" + ) as factory: + factory.return_value = creds + client = client_class.from_service_account_file("dummy/file/path.json") + assert client._transport._credentials == creds + + client = client_class.from_service_account_json("dummy/file/path.json") + assert client._transport._credentials == creds + + assert client._transport._host == "datastore.googleapis.com:443" + + +def test_datastore_client_get_transport_class(): + transport = DatastoreClient.get_transport_class() + assert transport == transports.DatastoreGrpcTransport + + transport = DatastoreClient.get_transport_class("grpc") + assert transport == transports.DatastoreGrpcTransport + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + (DatastoreClient, transports.DatastoreGrpcTransport, "grpc"), + ( + DatastoreAsyncClient, + transports.DatastoreGrpcAsyncIOTransport, + "grpc_asyncio", + ), + ], +) +@mock.patch.object( + DatastoreClient, "DEFAULT_ENDPOINT", modify_default_endpoint(DatastoreClient) +) +@mock.patch.object( + DatastoreAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(DatastoreAsyncClient), +) +def test_datastore_client_client_options(client_class, transport_class, transport_name): + # Check that if channel is provided we won't create a new one. + with mock.patch.object(DatastoreClient, "get_transport_class") as gtc: + transport = transport_class(credentials=credentials.AnonymousCredentials()) + client = client_class(transport=transport) + gtc.assert_not_called() + + # Check that if channel is provided via str we will create a new one. + with mock.patch.object(DatastoreClient, "get_transport_class") as gtc: + client = client_class(transport=transport_name) + gtc.assert_called() + + # Check the case api_endpoint is provided. + options = client_options.ClientOptions(api_endpoint="squid.clam.whelk") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is + # "never". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}): + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is + # "always". + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}): + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_MTLS_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has + # unsupported value. + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}): + with pytest.raises(MutualTLSChannelError): + client = client_class() + + # Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"} + ): + with pytest.raises(ValueError): + client = client_class() + + # Check the case quota_project_id is provided + options = client_options.ClientOptions(quota_project_id="octopus") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id="octopus", + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name,use_client_cert_env", + [ + (DatastoreClient, transports.DatastoreGrpcTransport, "grpc", "true"), + ( + DatastoreAsyncClient, + transports.DatastoreGrpcAsyncIOTransport, + "grpc_asyncio", + "true", + ), + (DatastoreClient, transports.DatastoreGrpcTransport, "grpc", "false"), + ( + DatastoreAsyncClient, + transports.DatastoreGrpcAsyncIOTransport, + "grpc_asyncio", + "false", + ), + ], +) +@mock.patch.object( + DatastoreClient, "DEFAULT_ENDPOINT", modify_default_endpoint(DatastoreClient) +) +@mock.patch.object( + DatastoreAsyncClient, + "DEFAULT_ENDPOINT", + modify_default_endpoint(DatastoreAsyncClient), +) +@mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}) +def test_datastore_client_mtls_env_auto( + client_class, transport_class, transport_name, use_client_cert_env +): + # This tests the endpoint autoswitch behavior. Endpoint is autoswitched to the default + # mtls endpoint, if GOOGLE_API_USE_CLIENT_CERTIFICATE is "true" and client cert exists. + + # Check the case client_cert_source is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + options = client_options.ClientOptions( + client_cert_source=client_cert_source_callback + ) + with mock.patch.object(transport_class, "__init__") as patched: + ssl_channel_creds = mock.Mock() + with mock.patch( + "grpc.ssl_channel_credentials", return_value=ssl_channel_creds + ): + patched.return_value = None + client = client_class(client_options=options) + + if use_client_cert_env == "false": + expected_ssl_channel_creds = None + expected_host = client.DEFAULT_ENDPOINT + else: + expected_ssl_channel_creds = ssl_channel_creds + expected_host = client.DEFAULT_MTLS_ENDPOINT + + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case ADC client cert is provided. Whether client cert is used depends on + # GOOGLE_API_USE_CLIENT_CERTIFICATE value. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.ssl_credentials", + new_callable=mock.PropertyMock, + ) as ssl_credentials_mock: + if use_client_cert_env == "false": + is_mtls_mock.return_value = False + ssl_credentials_mock.return_value = None + expected_host = client.DEFAULT_ENDPOINT + expected_ssl_channel_creds = None + else: + is_mtls_mock.return_value = True + ssl_credentials_mock.return_value = mock.Mock() + expected_host = client.DEFAULT_MTLS_ENDPOINT + expected_ssl_channel_creds = ( + ssl_credentials_mock.return_value + ) + + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=expected_host, + scopes=None, + ssl_channel_credentials=expected_ssl_channel_creds, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + # Check the case client_cert_source and ADC client cert are not provided. + with mock.patch.dict( + os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert_env} + ): + with mock.patch.object(transport_class, "__init__") as patched: + with mock.patch( + "google.auth.transport.grpc.SslCredentials.__init__", return_value=None + ): + with mock.patch( + "google.auth.transport.grpc.SslCredentials.is_mtls", + new_callable=mock.PropertyMock, + ) as is_mtls_mock: + is_mtls_mock.return_value = False + patched.return_value = None + client = client_class() + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + (DatastoreClient, transports.DatastoreGrpcTransport, "grpc"), + ( + DatastoreAsyncClient, + transports.DatastoreGrpcAsyncIOTransport, + "grpc_asyncio", + ), + ], +) +def test_datastore_client_client_options_scopes( + client_class, transport_class, transport_name +): + # Check the case scopes are provided. + options = client_options.ClientOptions(scopes=["1", "2"],) + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file=None, + host=client.DEFAULT_ENDPOINT, + scopes=["1", "2"], + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +@pytest.mark.parametrize( + "client_class,transport_class,transport_name", + [ + (DatastoreClient, transports.DatastoreGrpcTransport, "grpc"), + ( + DatastoreAsyncClient, + transports.DatastoreGrpcAsyncIOTransport, + "grpc_asyncio", + ), + ], +) +def test_datastore_client_client_options_credentials_file( + client_class, transport_class, transport_name +): + # Check the case credentials file is provided. + options = client_options.ClientOptions(credentials_file="credentials.json") + with mock.patch.object(transport_class, "__init__") as patched: + patched.return_value = None + client = client_class(client_options=options) + patched.assert_called_once_with( + credentials=None, + credentials_file="credentials.json", + host=client.DEFAULT_ENDPOINT, + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +def test_datastore_client_client_options_from_dict(): + with mock.patch( + "google.cloud.datastore_v1.services.datastore.transports.DatastoreGrpcTransport.__init__" + ) as grpc_transport: + grpc_transport.return_value = None + client = DatastoreClient(client_options={"api_endpoint": "squid.clam.whelk"}) + grpc_transport.assert_called_once_with( + credentials=None, + credentials_file=None, + host="squid.clam.whelk", + scopes=None, + ssl_channel_credentials=None, + quota_project_id=None, + client_info=transports.base.DEFAULT_CLIENT_INFO, + ) + + +def test_lookup(transport: str = "grpc", request_type=datastore.LookupRequest): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.lookup), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.LookupResponse() + + response = client.lookup(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore.LookupRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.LookupResponse) + + +def test_lookup_from_dict(): + test_lookup(request_type=dict) + + +@pytest.mark.asyncio +async def test_lookup_async(transport: str = "grpc_asyncio"): + client = DatastoreAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore.LookupRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._client._transport.lookup), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.LookupResponse() + ) + + response = await client.lookup(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.LookupResponse) + + +def test_lookup_flattened(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.lookup), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.LookupResponse() + + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.lookup( + project_id="project_id_value", + read_options=datastore.ReadOptions( + read_consistency=datastore.ReadOptions.ReadConsistency.STRONG + ), + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].read_options == datastore.ReadOptions( + read_consistency=datastore.ReadOptions.ReadConsistency.STRONG + ) + + assert args[0].keys == [ + entity.Key(partition_id=entity.PartitionId(project_id="project_id_value")) + ] + + +def test_lookup_flattened_error(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.lookup( + datastore.LookupRequest(), + project_id="project_id_value", + read_options=datastore.ReadOptions( + read_consistency=datastore.ReadOptions.ReadConsistency.STRONG + ), + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + +@pytest.mark.asyncio +async def test_lookup_flattened_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._client._transport.lookup), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.LookupResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.LookupResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.lookup( + project_id="project_id_value", + read_options=datastore.ReadOptions( + read_consistency=datastore.ReadOptions.ReadConsistency.STRONG + ), + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].read_options == datastore.ReadOptions( + read_consistency=datastore.ReadOptions.ReadConsistency.STRONG + ) + + assert args[0].keys == [ + entity.Key(partition_id=entity.PartitionId(project_id="project_id_value")) + ] + + +@pytest.mark.asyncio +async def test_lookup_flattened_error_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.lookup( + datastore.LookupRequest(), + project_id="project_id_value", + read_options=datastore.ReadOptions( + read_consistency=datastore.ReadOptions.ReadConsistency.STRONG + ), + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + +def test_run_query(transport: str = "grpc", request_type=datastore.RunQueryRequest): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.run_query), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.RunQueryResponse() + + response = client.run_query(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore.RunQueryRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.RunQueryResponse) + + +def test_run_query_from_dict(): + test_run_query(request_type=dict) + + +@pytest.mark.asyncio +async def test_run_query_async(transport: str = "grpc_asyncio"): + client = DatastoreAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore.RunQueryRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.run_query), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.RunQueryResponse() + ) + + response = await client.run_query(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.RunQueryResponse) + + +def test_begin_transaction( + transport: str = "grpc", request_type=datastore.BeginTransactionRequest +): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._transport.begin_transaction), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.BeginTransactionResponse( + transaction=b"transaction_blob", + ) + + response = client.begin_transaction(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore.BeginTransactionRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.BeginTransactionResponse) + + assert response.transaction == b"transaction_blob" + + +def test_begin_transaction_from_dict(): + test_begin_transaction(request_type=dict) + + +@pytest.mark.asyncio +async def test_begin_transaction_async(transport: str = "grpc_asyncio"): + client = DatastoreAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore.BeginTransactionRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.begin_transaction), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.BeginTransactionResponse(transaction=b"transaction_blob",) + ) + + response = await client.begin_transaction(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.BeginTransactionResponse) + + assert response.transaction == b"transaction_blob" + + +def test_begin_transaction_flattened(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._transport.begin_transaction), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.BeginTransactionResponse() + + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.begin_transaction(project_id="project_id_value",) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + +def test_begin_transaction_flattened_error(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.begin_transaction( + datastore.BeginTransactionRequest(), project_id="project_id_value", + ) + + +@pytest.mark.asyncio +async def test_begin_transaction_flattened_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.begin_transaction), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.BeginTransactionResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.BeginTransactionResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.begin_transaction(project_id="project_id_value",) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + +@pytest.mark.asyncio +async def test_begin_transaction_flattened_error_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.begin_transaction( + datastore.BeginTransactionRequest(), project_id="project_id_value", + ) + + +def test_commit(transport: str = "grpc", request_type=datastore.CommitRequest): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.commit), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.CommitResponse(index_updates=1389,) + + response = client.commit(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore.CommitRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.CommitResponse) + + assert response.index_updates == 1389 + + +def test_commit_from_dict(): + test_commit(request_type=dict) + + +@pytest.mark.asyncio +async def test_commit_async(transport: str = "grpc_asyncio"): + client = DatastoreAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore.CommitRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._client._transport.commit), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.CommitResponse(index_updates=1389,) + ) + + response = await client.commit(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.CommitResponse) + + assert response.index_updates == 1389 + + +def test_commit_flattened(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.commit), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.CommitResponse() + + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.commit( + project_id="project_id_value", + mode=datastore.CommitRequest.Mode.TRANSACTIONAL, + transaction=b"transaction_blob", + mutations=[ + datastore.Mutation( + insert=entity.Entity( + key=entity.Key( + partition_id=entity.PartitionId( + project_id="project_id_value" + ) + ) + ) + ) + ], + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].mode == datastore.CommitRequest.Mode.TRANSACTIONAL + + assert args[0].mutations == [ + datastore.Mutation( + insert=entity.Entity( + key=entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ) + ) + ] + + assert args[0].transaction == b"transaction_blob" + + +def test_commit_flattened_error(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.commit( + datastore.CommitRequest(), + project_id="project_id_value", + mode=datastore.CommitRequest.Mode.TRANSACTIONAL, + transaction=b"transaction_blob", + mutations=[ + datastore.Mutation( + insert=entity.Entity( + key=entity.Key( + partition_id=entity.PartitionId( + project_id="project_id_value" + ) + ) + ) + ) + ], + ) + + +@pytest.mark.asyncio +async def test_commit_flattened_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._client._transport.commit), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.CommitResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.CommitResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.commit( + project_id="project_id_value", + mode=datastore.CommitRequest.Mode.TRANSACTIONAL, + transaction=b"transaction_blob", + mutations=[ + datastore.Mutation( + insert=entity.Entity( + key=entity.Key( + partition_id=entity.PartitionId( + project_id="project_id_value" + ) + ) + ) + ) + ], + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].mode == datastore.CommitRequest.Mode.TRANSACTIONAL + + assert args[0].mutations == [ + datastore.Mutation( + insert=entity.Entity( + key=entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ) + ) + ] + + assert args[0].transaction == b"transaction_blob" + + +@pytest.mark.asyncio +async def test_commit_flattened_error_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.commit( + datastore.CommitRequest(), + project_id="project_id_value", + mode=datastore.CommitRequest.Mode.TRANSACTIONAL, + transaction=b"transaction_blob", + mutations=[ + datastore.Mutation( + insert=entity.Entity( + key=entity.Key( + partition_id=entity.PartitionId( + project_id="project_id_value" + ) + ) + ) + ) + ], + ) + + +def test_rollback(transport: str = "grpc", request_type=datastore.RollbackRequest): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.rollback), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.RollbackResponse() + + response = client.rollback(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore.RollbackRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.RollbackResponse) + + +def test_rollback_from_dict(): + test_rollback(request_type=dict) + + +@pytest.mark.asyncio +async def test_rollback_async(transport: str = "grpc_asyncio"): + client = DatastoreAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore.RollbackRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.rollback), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.RollbackResponse() + ) + + response = await client.rollback(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.RollbackResponse) + + +def test_rollback_flattened(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.rollback), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.RollbackResponse() + + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.rollback( + project_id="project_id_value", transaction=b"transaction_blob", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].transaction == b"transaction_blob" + + +def test_rollback_flattened_error(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.rollback( + datastore.RollbackRequest(), + project_id="project_id_value", + transaction=b"transaction_blob", + ) + + +@pytest.mark.asyncio +async def test_rollback_flattened_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.rollback), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.RollbackResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.RollbackResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.rollback( + project_id="project_id_value", transaction=b"transaction_blob", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].transaction == b"transaction_blob" + + +@pytest.mark.asyncio +async def test_rollback_flattened_error_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.rollback( + datastore.RollbackRequest(), + project_id="project_id_value", + transaction=b"transaction_blob", + ) + + +def test_allocate_ids( + transport: str = "grpc", request_type=datastore.AllocateIdsRequest +): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.allocate_ids), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.AllocateIdsResponse() + + response = client.allocate_ids(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore.AllocateIdsRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.AllocateIdsResponse) + + +def test_allocate_ids_from_dict(): + test_allocate_ids(request_type=dict) + + +@pytest.mark.asyncio +async def test_allocate_ids_async(transport: str = "grpc_asyncio"): + client = DatastoreAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore.AllocateIdsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.allocate_ids), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.AllocateIdsResponse() + ) + + response = await client.allocate_ids(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.AllocateIdsResponse) + + +def test_allocate_ids_flattened(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.allocate_ids), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.AllocateIdsResponse() + + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.allocate_ids( + project_id="project_id_value", + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].keys == [ + entity.Key(partition_id=entity.PartitionId(project_id="project_id_value")) + ] + + +def test_allocate_ids_flattened_error(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.allocate_ids( + datastore.AllocateIdsRequest(), + project_id="project_id_value", + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + +@pytest.mark.asyncio +async def test_allocate_ids_flattened_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.allocate_ids), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.AllocateIdsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.AllocateIdsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.allocate_ids( + project_id="project_id_value", + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].keys == [ + entity.Key(partition_id=entity.PartitionId(project_id="project_id_value")) + ] + + +@pytest.mark.asyncio +async def test_allocate_ids_flattened_error_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.allocate_ids( + datastore.AllocateIdsRequest(), + project_id="project_id_value", + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + +def test_reserve_ids(transport: str = "grpc", request_type=datastore.ReserveIdsRequest): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.reserve_ids), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.ReserveIdsResponse() + + response = client.reserve_ids(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0] == datastore.ReserveIdsRequest() + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.ReserveIdsResponse) + + +def test_reserve_ids_from_dict(): + test_reserve_ids(request_type=dict) + + +@pytest.mark.asyncio +async def test_reserve_ids_async(transport: str = "grpc_asyncio"): + client = DatastoreAsyncClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = datastore.ReserveIdsRequest() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.reserve_ids), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.ReserveIdsResponse() + ) + + response = await client.reserve_ids(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, datastore.ReserveIdsResponse) + + +def test_reserve_ids_flattened(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client._transport.reserve_ids), "__call__") as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.ReserveIdsResponse() + + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.reserve_ids( + project_id="project_id_value", + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].keys == [ + entity.Key(partition_id=entity.PartitionId(project_id="project_id_value")) + ] + + +def test_reserve_ids_flattened_error(): + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.reserve_ids( + datastore.ReserveIdsRequest(), + project_id="project_id_value", + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + +@pytest.mark.asyncio +async def test_reserve_ids_flattened_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client._client._transport.reserve_ids), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = datastore.ReserveIdsResponse() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + datastore.ReserveIdsResponse() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.reserve_ids( + project_id="project_id_value", + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + + assert args[0].project_id == "project_id_value" + + assert args[0].keys == [ + entity.Key(partition_id=entity.PartitionId(project_id="project_id_value")) + ] + + +@pytest.mark.asyncio +async def test_reserve_ids_flattened_error_async(): + client = DatastoreAsyncClient(credentials=credentials.AnonymousCredentials(),) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.reserve_ids( + datastore.ReserveIdsRequest(), + project_id="project_id_value", + keys=[ + entity.Key( + partition_id=entity.PartitionId(project_id="project_id_value") + ) + ], + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.DatastoreGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.DatastoreGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DatastoreClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.DatastoreGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = DatastoreClient( + client_options={"scopes": ["1", "2"]}, transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.DatastoreGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + client = DatastoreClient(transport=transport) + assert client._transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.DatastoreGrpcTransport( + credentials=credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.DatastoreGrpcAsyncIOTransport( + credentials=credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [transports.DatastoreGrpcTransport, transports.DatastoreGrpcAsyncIOTransport], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = DatastoreClient(credentials=credentials.AnonymousCredentials(),) + assert isinstance(client._transport, transports.DatastoreGrpcTransport,) + + +def test_datastore_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(exceptions.DuplicateCredentialArgs): + transport = transports.DatastoreTransport( + credentials=credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_datastore_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.datastore_v1.services.datastore.transports.DatastoreTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.DatastoreTransport( + credentials=credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "lookup", + "run_query", + "begin_transaction", + "commit", + "rollback", + "allocate_ids", + "reserve_ids", + ) + for method in methods: + with pytest.raises(NotImplementedError): + getattr(transport, method)(request=object()) + + +def test_datastore_base_transport_with_credentials_file(): + # Instantiate the base transport with a credentials file + with mock.patch.object( + auth, "load_credentials_from_file" + ) as load_creds, mock.patch( + "google.cloud.datastore_v1.services.datastore.transports.DatastoreTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + load_creds.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.DatastoreTransport( + credentials_file="credentials.json", quota_project_id="octopus", + ) + load_creds.assert_called_once_with( + "credentials.json", + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + quota_project_id="octopus", + ) + + +def test_datastore_base_transport_with_adc(): + # Test the default credentials are used if credentials and credentials_file are None. + with mock.patch.object(auth, "default") as adc, mock.patch( + "google.cloud.datastore_v1.services.datastore.transports.DatastoreTransport._prep_wrapped_messages" + ) as Transport: + Transport.return_value = None + adc.return_value = (credentials.AnonymousCredentials(), None) + transport = transports.DatastoreTransport() + adc.assert_called_once() + + +def test_datastore_auth_adc(): + # If no credentials are provided, we should use ADC credentials. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + DatastoreClient() + adc.assert_called_once_with( + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + quota_project_id=None, + ) + + +def test_datastore_transport_auth_adc(): + # If credentials and host are not provided, the transport class should use + # ADC credentials. + with mock.patch.object(auth, "default") as adc: + adc.return_value = (credentials.AnonymousCredentials(), None) + transports.DatastoreGrpcTransport( + host="squid.clam.whelk", quota_project_id="octopus" + ) + adc.assert_called_once_with( + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + quota_project_id="octopus", + ) + + +def test_datastore_host_no_port(): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="datastore.googleapis.com" + ), + ) + assert client._transport._host == "datastore.googleapis.com:443" + + +def test_datastore_host_with_port(): + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), + client_options=client_options.ClientOptions( + api_endpoint="datastore.googleapis.com:8000" + ), + ) + assert client._transport._host == "datastore.googleapis.com:8000" + + +def test_datastore_grpc_transport_channel(): + channel = grpc.insecure_channel("http://localhost/") + + # Check that channel is used if provided. + transport = transports.DatastoreGrpcTransport( + host="squid.clam.whelk", channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + + +def test_datastore_grpc_asyncio_transport_channel(): + channel = aio.insecure_channel("http://localhost/") + + # Check that channel is used if provided. + transport = transports.DatastoreGrpcAsyncIOTransport( + host="squid.clam.whelk", channel=channel, + ) + assert transport.grpc_channel == channel + assert transport._host == "squid.clam.whelk:443" + + +@pytest.mark.parametrize( + "transport_class", + [transports.DatastoreGrpcTransport, transports.DatastoreGrpcAsyncIOTransport], +) +def test_datastore_transport_channel_mtls_with_client_cert_source(transport_class): + with mock.patch( + "grpc.ssl_channel_credentials", autospec=True + ) as grpc_ssl_channel_cred: + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_ssl_cred = mock.Mock() + grpc_ssl_channel_cred.return_value = mock_ssl_cred + + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + + cred = credentials.AnonymousCredentials() + with pytest.warns(DeprecationWarning): + with mock.patch.object(auth, "default") as adc: + adc.return_value = (cred, None) + transport = transport_class( + host="squid.clam.whelk", + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=client_cert_source_callback, + ) + adc.assert_called_once() + + grpc_ssl_channel_cred.assert_called_once_with( + certificate_chain=b"cert bytes", private_key=b"key bytes" + ) + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=cred, + credentials_file=None, + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + ) + assert transport.grpc_channel == mock_grpc_channel + + +@pytest.mark.parametrize( + "transport_class", + [transports.DatastoreGrpcTransport, transports.DatastoreGrpcAsyncIOTransport], +) +def test_datastore_transport_channel_mtls_with_adc(transport_class): + mock_ssl_cred = mock.Mock() + with mock.patch.multiple( + "google.auth.transport.grpc.SslCredentials", + __init__=mock.Mock(return_value=None), + ssl_credentials=mock.PropertyMock(return_value=mock_ssl_cred), + ): + with mock.patch.object( + transport_class, "create_channel", autospec=True + ) as grpc_create_channel: + mock_grpc_channel = mock.Mock() + grpc_create_channel.return_value = mock_grpc_channel + mock_cred = mock.Mock() + + with pytest.warns(DeprecationWarning): + transport = transport_class( + host="squid.clam.whelk", + credentials=mock_cred, + api_mtls_endpoint="mtls.squid.clam.whelk", + client_cert_source=None, + ) + + grpc_create_channel.assert_called_once_with( + "mtls.squid.clam.whelk:443", + credentials=mock_cred, + credentials_file=None, + scopes=( + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/datastore", + ), + ssl_credentials=mock_ssl_cred, + quota_project_id=None, + ) + assert transport.grpc_channel == mock_grpc_channel + + +def test_client_withDEFAULT_CLIENT_INFO(): + client_info = gapic_v1.client_info.ClientInfo() + + with mock.patch.object( + transports.DatastoreTransport, "_prep_wrapped_messages" + ) as prep: + client = DatastoreClient( + credentials=credentials.AnonymousCredentials(), client_info=client_info, + ) + prep.assert_called_once_with(client_info) + + with mock.patch.object( + transports.DatastoreTransport, "_prep_wrapped_messages" + ) as prep: + transport_class = DatastoreClient.get_transport_class() + transport = transport_class( + credentials=credentials.AnonymousCredentials(), client_info=client_info, + ) + prep.assert_called_once_with(client_info) diff --git a/tests/unit/gapic/v1/test_datastore_client_v1.py b/tests/unit/gapic/v1/test_datastore_client_v1.py deleted file mode 100644 index 7dfb27ed..00000000 --- a/tests/unit/gapic/v1/test_datastore_client_v1.py +++ /dev/null @@ -1,302 +0,0 @@ -# Copyright 2018 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 -# -# https://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. -"""Unit tests.""" - -import pytest - -from google.cloud import datastore_v1 -from google.cloud.datastore_v1 import enums -from google.cloud.datastore_v1.proto import datastore_pb2 -from google.cloud.datastore_v1.proto import entity_pb2 - - -class MultiCallableStub(object): - """Stub for the grpc.UnaryUnaryMultiCallable interface.""" - - def __init__(self, method, channel_stub): - self.method = method - self.channel_stub = channel_stub - - def __call__(self, request, timeout=None, metadata=None, credentials=None): - self.channel_stub.requests.append((self.method, request)) - - response = None - if self.channel_stub.responses: - response = self.channel_stub.responses.pop() - - if isinstance(response, Exception): - raise response - - if response: - return response - - -class ChannelStub(object): - """Stub for the grpc.Channel interface.""" - - def __init__(self, responses=[]): - self.responses = responses - self.requests = [] - - def unary_unary(self, method, request_serializer=None, response_deserializer=None): - return MultiCallableStub(method, self) - - -class CustomException(Exception): - pass - - -class TestDatastoreClient(object): - def test_lookup(self): - # Setup Expected Response - expected_response = {} - expected_response = datastore_pb2.LookupResponse(**expected_response) - - # Mock the API response - channel = ChannelStub(responses=[expected_response]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup Request - project_id = "projectId-1969970175" - keys = [] - - response = client.lookup(project_id, keys) - assert expected_response == response - - assert len(channel.requests) == 1 - expected_request = datastore_pb2.LookupRequest(project_id=project_id, keys=keys) - actual_request = channel.requests[0][1] - assert expected_request == actual_request - - def test_lookup_exception(self): - # Mock the API response - channel = ChannelStub(responses=[CustomException()]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup request - project_id = "projectId-1969970175" - keys = [] - - with pytest.raises(CustomException): - client.lookup(project_id, keys) - - def test_run_query(self): - # Setup Expected Response - expected_response = {} - expected_response = datastore_pb2.RunQueryResponse(**expected_response) - - # Mock the API response - channel = ChannelStub(responses=[expected_response]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup Request - project_id = "projectId-1969970175" - partition_id = {} - - response = client.run_query(project_id, partition_id) - assert expected_response == response - - assert len(channel.requests) == 1 - expected_request = datastore_pb2.RunQueryRequest( - project_id=project_id, partition_id=partition_id - ) - actual_request = channel.requests[0][1] - assert expected_request == actual_request - - def test_run_query_exception(self): - # Mock the API response - channel = ChannelStub(responses=[CustomException()]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup request - project_id = "projectId-1969970175" - partition_id = {} - - with pytest.raises(CustomException): - client.run_query(project_id, partition_id) - - def test_begin_transaction(self): - # Setup Expected Response - transaction = b"-34" - expected_response = {"transaction": transaction} - expected_response = datastore_pb2.BeginTransactionResponse(**expected_response) - - # Mock the API response - channel = ChannelStub(responses=[expected_response]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup Request - project_id = "projectId-1969970175" - - response = client.begin_transaction(project_id) - assert expected_response == response - - assert len(channel.requests) == 1 - expected_request = datastore_pb2.BeginTransactionRequest(project_id=project_id) - actual_request = channel.requests[0][1] - assert expected_request == actual_request - - def test_begin_transaction_exception(self): - # Mock the API response - channel = ChannelStub(responses=[CustomException()]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup request - project_id = "projectId-1969970175" - - with pytest.raises(CustomException): - client.begin_transaction(project_id) - - def test_commit(self): - # Setup Expected Response - index_updates = 1425228195 - expected_response = {"index_updates": index_updates} - expected_response = datastore_pb2.CommitResponse(**expected_response) - - # Mock the API response - channel = ChannelStub(responses=[expected_response]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup Request - project_id = "projectId-1969970175" - mode = enums.CommitRequest.Mode.MODE_UNSPECIFIED - mutations = [] - - response = client.commit(project_id, mode, mutations) - assert expected_response == response - - assert len(channel.requests) == 1 - expected_request = datastore_pb2.CommitRequest( - project_id=project_id, mode=mode, mutations=mutations - ) - actual_request = channel.requests[0][1] - assert expected_request == actual_request - - def test_commit_exception(self): - # Mock the API response - channel = ChannelStub(responses=[CustomException()]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup request - project_id = "projectId-1969970175" - mode = enums.CommitRequest.Mode.MODE_UNSPECIFIED - mutations = [] - - with pytest.raises(CustomException): - client.commit(project_id, mode, mutations) - - def test_rollback(self): - # Setup Expected Response - expected_response = {} - expected_response = datastore_pb2.RollbackResponse(**expected_response) - - # Mock the API response - channel = ChannelStub(responses=[expected_response]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup Request - project_id = "projectId-1969970175" - transaction = b"-34" - - response = client.rollback(project_id, transaction) - assert expected_response == response - - assert len(channel.requests) == 1 - expected_request = datastore_pb2.RollbackRequest( - project_id=project_id, transaction=transaction - ) - actual_request = channel.requests[0][1] - assert expected_request == actual_request - - def test_rollback_exception(self): - # Mock the API response - channel = ChannelStub(responses=[CustomException()]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup request - project_id = "projectId-1969970175" - transaction = b"-34" - - with pytest.raises(CustomException): - client.rollback(project_id, transaction) - - def test_allocate_ids(self): - # Setup Expected Response - expected_response = {} - expected_response = datastore_pb2.AllocateIdsResponse(**expected_response) - - # Mock the API response - channel = ChannelStub(responses=[expected_response]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup Request - project_id = "projectId-1969970175" - keys = [] - - response = client.allocate_ids(project_id, keys) - assert expected_response == response - - assert len(channel.requests) == 1 - expected_request = datastore_pb2.AllocateIdsRequest( - project_id=project_id, keys=keys - ) - actual_request = channel.requests[0][1] - assert expected_request == actual_request - - def test_allocate_ids_exception(self): - # Mock the API response - channel = ChannelStub(responses=[CustomException()]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup request - project_id = "projectId-1969970175" - keys = [] - - with pytest.raises(CustomException): - client.allocate_ids(project_id, keys) - - def test_reserve_ids(self): - # Setup Expected Response - expected_response = {} - expected_response = datastore_pb2.ReserveIdsResponse(**expected_response) - - # Mock the API response - channel = ChannelStub(responses=[expected_response]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup Request - project_id = "projectId-1969970175" - keys = [] - - response = client.reserve_ids(project_id, keys) - assert expected_response == response - - assert len(channel.requests) == 1 - expected_request = datastore_pb2.ReserveIdsRequest( - project_id=project_id, keys=keys - ) - actual_request = channel.requests[0][1] - assert expected_request == actual_request - - def test_reserve_ids_exception(self): - # Mock the API response - channel = ChannelStub(responses=[CustomException()]) - client = datastore_v1.DatastoreClient(channel=channel) - - # Setup request - project_id = "projectId-1969970175" - keys = [] - - with pytest.raises(CustomException): - client.reserve_ids(project_id, keys) diff --git a/tests/unit/test__gapic.py b/tests/unit/test__gapic.py index c404dc79..4543dba9 100644 --- a/tests/unit/test__gapic.py +++ b/tests/unit/test__gapic.py @@ -27,14 +27,18 @@ def _call_fut(self, client): return make_datastore_api(client) @mock.patch( - "google.cloud.datastore_v1.gapic.datastore_client.DatastoreClient", + "google.cloud.datastore_v1.services.datastore.client.DatastoreClient", return_value=mock.sentinel.ds_client, ) + @mock.patch( + "google.cloud.datastore_v1.services.datastore.transports.grpc.DatastoreGrpcTransport", + return_value=mock.sentinel.transport, + ) @mock.patch( "google.cloud.datastore._gapic.make_secure_channel", return_value=mock.sentinel.channel, ) - def test_live_api(self, make_chan, mock_klass): + def test_live_api(self, make_chan, mock_transport, mock_klass): from google.cloud._http import DEFAULT_USER_AGENT base_url = "https://datastore.googleapis.com:443" @@ -47,24 +51,31 @@ def test_live_api(self, make_chan, mock_klass): ds_api = self._call_fut(client) self.assertIs(ds_api, mock.sentinel.ds_client) + mock_transport.assert_called_once_with(channel=mock.sentinel.channel) + make_chan.assert_called_once_with( mock.sentinel.credentials, DEFAULT_USER_AGENT, "datastore.googleapis.com:443", ) + mock_klass.assert_called_once_with( - channel=mock.sentinel.channel, client_info=mock.sentinel.client_info + transport=mock.sentinel.transport, client_info=mock.sentinel.client_info ) @mock.patch( - "google.cloud.datastore_v1.gapic.datastore_client.DatastoreClient", + "google.cloud.datastore_v1.services.datastore.client.DatastoreClient", return_value=mock.sentinel.ds_client, ) + @mock.patch( + "google.cloud.datastore_v1.services.datastore.transports.grpc.DatastoreGrpcTransport", + return_value=mock.sentinel.transport, + ) @mock.patch( "google.cloud.datastore._gapic.insecure_channel", return_value=mock.sentinel.channel, ) - def test_emulator(self, make_chan, mock_klass): + def test_emulator(self, make_chan, mock_transport, mock_klass): host = "localhost:8901" base_url = "http://" + host @@ -77,7 +88,10 @@ def test_emulator(self, make_chan, mock_klass): ds_api = self._call_fut(client) self.assertIs(ds_api, mock.sentinel.ds_client) + mock_transport.assert_called_once_with(channel=mock.sentinel.channel) + make_chan.assert_called_once_with(host) + mock_klass.assert_called_once_with( - channel=mock.sentinel.channel, client_info=mock.sentinel.client_info + transport=mock.sentinel.transport, client_info=mock.sentinel.client_info ) diff --git a/tests/unit/test__http.py b/tests/unit/test__http.py index b332c946..700429ff 100644 --- a/tests/unit/test__http.py +++ b/tests/unit/test__http.py @@ -90,7 +90,7 @@ def _call_fut(*args, **kwargs): return _rpc(*args, **kwargs) def test_it(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 http = object() project = "projectOK" @@ -102,7 +102,7 @@ def test_it(self): response_pb = datastore_pb2.BeginTransactionResponse(transaction=b"7830rmc") patch = mock.patch( "google.cloud.datastore._http._request", - return_value=response_pb.SerializeToString(), + return_value=response_pb._pb.SerializeToString(), ) with patch as mock_request: result = self._call_fut( @@ -114,13 +114,13 @@ def test_it(self): request_pb, datastore_pb2.BeginTransactionResponse, ) - self.assertEqual(result, response_pb) + self.assertEqual(result, response_pb._pb) mock_request.assert_called_once_with( http, project, method, - request_pb.SerializeToString(), + request_pb._pb.SerializeToString(), base_url, client_info, ) @@ -138,7 +138,7 @@ def _make_one(self, *args, **kwargs): @staticmethod def _make_query_pb(kind): - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 return query_pb2.Query(kind=[query_pb2.KindExpression(name=kind)]) @@ -148,7 +148,7 @@ def test_constructor(self): self.assertIs(ds_api.client, client) def test_lookup_single_key_empty_response(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" key_pb = _make_key_pb(project) @@ -157,7 +157,7 @@ def test_lookup_single_key_empty_response(self): # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -172,29 +172,29 @@ def test_lookup_single_key_empty_response(self): response = ds_api.lookup(project, [key_pb], read_options=read_options) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 0) self.assertEqual(len(response.missing), 0) self.assertEqual(len(response.deferred), 0) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) - self.assertEqual(list(request.keys), [key_pb]) - self.assertEqual(request.read_options, read_options) + self.assertEqual(list(request.keys), [key_pb._pb]) + self.assertEqual(request.read_options, read_options._pb) def test_lookup_single_key_empty_response_w_eventual(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" key_pb = _make_key_pb(project) rsp_pb = datastore_pb2.LookupResponse() read_options = datastore_pb2.ReadOptions( - read_consistency=datastore_pb2.ReadOptions.EVENTUAL + read_consistency=datastore_pb2.ReadOptions.ReadConsistency.EVENTUAL ) # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -209,18 +209,18 @@ def test_lookup_single_key_empty_response_w_eventual(self): response = ds_api.lookup(project, [key_pb], read_options=read_options) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 0) self.assertEqual(len(response.missing), 0) self.assertEqual(len(response.deferred), 0) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) - self.assertEqual(list(request.keys), [key_pb]) - self.assertEqual(request.read_options, read_options) + self.assertEqual(list(request.keys), [key_pb._pb]) + self.assertEqual(request.read_options, read_options._pb) def test_lookup_single_key_empty_response_w_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" transaction = b"TRANSACTION" @@ -230,7 +230,7 @@ def test_lookup_single_key_empty_response_w_transaction(self): # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -245,31 +245,31 @@ def test_lookup_single_key_empty_response_w_transaction(self): response = ds_api.lookup(project, [key_pb], read_options=read_options) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 0) self.assertEqual(len(response.missing), 0) self.assertEqual(len(response.deferred), 0) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) - self.assertEqual(list(request.keys), [key_pb]) - self.assertEqual(request.read_options, read_options) + self.assertEqual(list(request.keys), [key_pb._pb]) + self.assertEqual(request.read_options, read_options._pb) def test_lookup_single_key_nonempty_response(self): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 project = "PROJECT" key_pb = _make_key_pb(project) rsp_pb = datastore_pb2.LookupResponse() entity = entity_pb2.Entity() - entity.key.CopyFrom(key_pb) - rsp_pb.found.add(entity=entity) + entity.key._pb.CopyFrom(key_pb._pb) + rsp_pb._pb.found.add(entity=entity._pb) read_options = datastore_pb2.ReadOptions() # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -284,7 +284,7 @@ def test_lookup_single_key_nonempty_response(self): response = ds_api.lookup(project, [key_pb], read_options=read_options) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 1) self.assertEqual(len(response.missing), 0) @@ -294,11 +294,11 @@ def test_lookup_single_key_nonempty_response(self): self.assertEqual(found.key.path[0].id, 1234) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) - self.assertEqual(list(request.keys), [key_pb]) - self.assertEqual(request.read_options, read_options) + self.assertEqual(list(request.keys), [key_pb._pb]) + self.assertEqual(request.read_options, read_options._pb) def test_lookup_multiple_keys_empty_response(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" key_pb1 = _make_key_pb(project) @@ -308,7 +308,7 @@ def test_lookup_multiple_keys_empty_response(self): # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -323,32 +323,32 @@ def test_lookup_multiple_keys_empty_response(self): response = ds_api.lookup(project, [key_pb1, key_pb2], read_options=read_options) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 0) self.assertEqual(len(response.missing), 0) self.assertEqual(len(response.deferred), 0) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) - self.assertEqual(list(request.keys), [key_pb1, key_pb2]) - self.assertEqual(request.read_options, read_options) + self.assertEqual(list(request.keys), [key_pb1._pb, key_pb2._pb]) + self.assertEqual(request.read_options, read_options._pb) def test_lookup_multiple_keys_w_missing(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" key_pb1 = _make_key_pb(project) key_pb2 = _make_key_pb(project, id_=2345) rsp_pb = datastore_pb2.LookupResponse() - er_1 = rsp_pb.missing.add() - er_1.entity.key.CopyFrom(key_pb1) - er_2 = rsp_pb.missing.add() - er_2.entity.key.CopyFrom(key_pb2) + er_1 = rsp_pb._pb.missing.add() + er_1.entity.key.CopyFrom(key_pb1._pb) + er_2 = rsp_pb._pb.missing.add() + er_2.entity.key.CopyFrom(key_pb2._pb) read_options = datastore_pb2.ReadOptions() # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -363,31 +363,31 @@ def test_lookup_multiple_keys_w_missing(self): response = ds_api.lookup(project, [key_pb1, key_pb2], read_options=read_options) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 0) self.assertEqual(len(response.deferred), 0) missing_keys = [result.entity.key for result in response.missing] - self.assertEqual(missing_keys, [key_pb1, key_pb2]) + self.assertEqual(missing_keys, [key_pb1._pb, key_pb2._pb]) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) - self.assertEqual(list(request.keys), [key_pb1, key_pb2]) - self.assertEqual(request.read_options, read_options) + self.assertEqual(list(request.keys), [key_pb1._pb, key_pb2._pb]) + self.assertEqual(request.read_options, read_options._pb) def test_lookup_multiple_keys_w_deferred(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" key_pb1 = _make_key_pb(project) key_pb2 = _make_key_pb(project, id_=2345) rsp_pb = datastore_pb2.LookupResponse() - rsp_pb.deferred.add().CopyFrom(key_pb1) - rsp_pb.deferred.add().CopyFrom(key_pb2) + rsp_pb._pb.deferred.add().CopyFrom(key_pb1._pb) + rsp_pb._pb.deferred.add().CopyFrom(key_pb2._pb) read_options = datastore_pb2.ReadOptions() # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -402,20 +402,20 @@ def test_lookup_multiple_keys_w_deferred(self): response = ds_api.lookup(project, [key_pb1, key_pb2], read_options=read_options) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "lookup") self.assertEqual(len(response.found), 0) self.assertEqual(len(response.missing), 0) - self.assertEqual(list(response.deferred), [key_pb1, key_pb2]) + self.assertEqual(list(response.deferred), [key_pb1._pb, key_pb2._pb]) request = _verify_protobuf_call(http, uri, datastore_pb2.LookupRequest()) - self.assertEqual(list(request.keys), [key_pb1, key_pb2]) - self.assertEqual(request.read_options, read_options) + self.assertEqual(list(request.keys), [key_pb1._pb, key_pb2._pb]) + self.assertEqual(request.read_options, read_options._pb) def test_run_query_w_eventual_no_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 project = "PROJECT" kind = "Nonesuch" @@ -423,19 +423,19 @@ def test_run_query_w_eventual_no_transaction(self): query_pb = self._make_query_pb(kind) partition_id = entity_pb2.PartitionId(project_id=project) read_options = datastore_pb2.ReadOptions( - read_consistency=datastore_pb2.ReadOptions.EVENTUAL + read_consistency=datastore_pb2.ReadOptions.ReadConsistency.EVENTUAL ) rsp_pb = datastore_pb2.RunQueryResponse( batch=query_pb2.QueryResultBatch( - entity_result_type=query_pb2.EntityResult.FULL, + entity_result_type=query_pb2.EntityResult.ResultType.FULL, end_cursor=cursor, - more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS, + more_results=query_pb2.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS, ) ) # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -450,18 +450,18 @@ def test_run_query_w_eventual_no_transaction(self): response = ds_api.run_query(project, partition_id, read_options, query=query_pb) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "runQuery") request = _verify_protobuf_call(http, uri, datastore_pb2.RunQueryRequest()) - self.assertEqual(request.partition_id, partition_id) - self.assertEqual(request.query, query_pb) - self.assertEqual(request.read_options, read_options) + self.assertEqual(request.partition_id, partition_id._pb) + self.assertEqual(request.query, query_pb._pb) + self.assertEqual(request.read_options, read_options._pb) def test_run_query_wo_eventual_w_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 project = "PROJECT" kind = "Nonesuch" @@ -472,15 +472,15 @@ def test_run_query_wo_eventual_w_transaction(self): read_options = datastore_pb2.ReadOptions(transaction=transaction) rsp_pb = datastore_pb2.RunQueryResponse( batch=query_pb2.QueryResultBatch( - entity_result_type=query_pb2.EntityResult.FULL, + entity_result_type=query_pb2.EntityResult.ResultType.FULL, end_cursor=cursor, - more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS, + more_results=query_pb2.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS, ) ) # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -495,18 +495,18 @@ def test_run_query_wo_eventual_w_transaction(self): response = ds_api.run_query(project, partition_id, read_options, query=query_pb) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "runQuery") request = _verify_protobuf_call(http, uri, datastore_pb2.RunQueryRequest()) - self.assertEqual(request.partition_id, partition_id) - self.assertEqual(request.query, query_pb) - self.assertEqual(request.read_options, read_options) + self.assertEqual(request.partition_id, partition_id._pb) + self.assertEqual(request.query, query_pb._pb) + self.assertEqual(request.read_options, read_options._pb) def test_run_query_wo_namespace_empty_result(self): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 project = "PROJECT" kind = "Nonesuch" @@ -516,15 +516,15 @@ def test_run_query_wo_namespace_empty_result(self): read_options = datastore_pb2.ReadOptions() rsp_pb = datastore_pb2.RunQueryResponse( batch=query_pb2.QueryResultBatch( - entity_result_type=query_pb2.EntityResult.FULL, + entity_result_type=query_pb2.EntityResult.ResultType.FULL, end_cursor=cursor, - more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS, + more_results=query_pb2.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS, ) ) # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -539,18 +539,18 @@ def test_run_query_wo_namespace_empty_result(self): response = ds_api.run_query(project, partition_id, read_options, query=query_pb) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "runQuery") request = _verify_protobuf_call(http, uri, datastore_pb2.RunQueryRequest()) - self.assertEqual(request.partition_id, partition_id) - self.assertEqual(request.query, query_pb) - self.assertEqual(request.read_options, read_options) + self.assertEqual(request.partition_id, partition_id._pb) + self.assertEqual(request.query, query_pb._pb) + self.assertEqual(request.read_options, read_options._pb) def test_run_query_w_namespace_nonempty_result(self): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 project = "PROJECT" kind = "Kind" @@ -562,15 +562,15 @@ def test_run_query_w_namespace_nonempty_result(self): read_options = datastore_pb2.ReadOptions() rsp_pb = datastore_pb2.RunQueryResponse( batch=query_pb2.QueryResultBatch( - entity_result_type=query_pb2.EntityResult.FULL, + entity_result_type=query_pb2.EntityResult.ResultType.FULL, entity_results=[query_pb2.EntityResult(entity=entity_pb2.Entity())], - more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS, + more_results=query_pb2.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS, ) ) # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -585,15 +585,15 @@ def test_run_query_w_namespace_nonempty_result(self): response = ds_api.run_query(project, partition_id, read_options, query=query_pb) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "runQuery") request = _verify_protobuf_call(http, uri, datastore_pb2.RunQueryRequest()) - self.assertEqual(request.partition_id, partition_id) - self.assertEqual(request.query, query_pb) + self.assertEqual(request.partition_id, partition_id._pb) + self.assertEqual(request.query, query_pb._pb) def test_begin_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" transaction = b"TRANSACTION" @@ -602,7 +602,7 @@ def test_begin_transaction(self): # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -617,7 +617,7 @@ def test_begin_transaction(self): response = ds_api.begin_transaction(project) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "beginTransaction") request = _verify_protobuf_call( @@ -627,22 +627,22 @@ def test_begin_transaction(self): self.assertEqual(request.project_id, u"") def test_commit_wo_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore.helpers import _new_value_pb project = "PROJECT" key_pb = _make_key_pb(project) rsp_pb = datastore_pb2.CommitResponse() req_pb = datastore_pb2.CommitRequest() - mutation = req_pb.mutations.add() + mutation = req_pb._pb.mutations.add() insert = mutation.upsert - insert.key.CopyFrom(key_pb) + insert.key.CopyFrom(key_pb._pb) value_pb = _new_value_pb(insert, "foo") value_pb.string_value = u"Foo" # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -655,35 +655,35 @@ def test_commit_wo_transaction(self): # Make request. rq_class = datastore_pb2.CommitRequest ds_api = self._make_one(client) - mode = rq_class.NON_TRANSACTIONAL + mode = rq_class.Mode.NON_TRANSACTIONAL result = ds_api.commit(project, mode, [mutation]) # Check the result and verify the callers. - self.assertEqual(result, rsp_pb) + self.assertEqual(result, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "commit") request = _verify_protobuf_call(http, uri, rq_class()) self.assertEqual(request.transaction, b"") self.assertEqual(list(request.mutations), [mutation]) - self.assertEqual(request.mode, rq_class.NON_TRANSACTIONAL) + self.assertEqual(request.mode, rq_class.Mode.NON_TRANSACTIONAL) def test_commit_w_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore.helpers import _new_value_pb project = "PROJECT" key_pb = _make_key_pb(project) rsp_pb = datastore_pb2.CommitResponse() req_pb = datastore_pb2.CommitRequest() - mutation = req_pb.mutations.add() + mutation = req_pb._pb.mutations.add() insert = mutation.upsert - insert.key.CopyFrom(key_pb) + insert.key.CopyFrom(key_pb._pb) value_pb = _new_value_pb(insert, "foo") value_pb.string_value = u"Foo" # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -696,20 +696,20 @@ def test_commit_w_transaction(self): # Make request. rq_class = datastore_pb2.CommitRequest ds_api = self._make_one(client) - mode = rq_class.TRANSACTIONAL + mode = rq_class.Mode.TRANSACTIONAL result = ds_api.commit(project, mode, [mutation], transaction=b"xact") # Check the result and verify the callers. - self.assertEqual(result, rsp_pb) + self.assertEqual(result, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "commit") request = _verify_protobuf_call(http, uri, rq_class()) self.assertEqual(request.transaction, b"xact") self.assertEqual(list(request.mutations), [mutation]) - self.assertEqual(request.mode, rq_class.TRANSACTIONAL) + self.assertEqual(request.mode, rq_class.Mode.TRANSACTIONAL) def test_rollback_ok(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" transaction = b"xact" @@ -717,7 +717,7 @@ def test_rollback_ok(self): # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -732,21 +732,21 @@ def test_rollback_ok(self): response = ds_api.rollback(project, transaction) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "rollback") request = _verify_protobuf_call(http, uri, datastore_pb2.RollbackRequest()) self.assertEqual(request.transaction, transaction) def test_allocate_ids_empty(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" rsp_pb = datastore_pb2.AllocateIdsResponse() # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -761,7 +761,7 @@ def test_allocate_ids_empty(self): response = ds_api.allocate_ids(project, []) # Check the result and verify the callers. - self.assertEqual(response, rsp_pb) + self.assertEqual(response, rsp_pb._pb) self.assertEqual(list(response.keys), []) uri = _build_expected_url(client._base_url, project, "allocateIds") @@ -769,7 +769,7 @@ def test_allocate_ids_empty(self): self.assertEqual(list(request.keys), []) def test_allocate_ids_non_empty(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" before_key_pbs = [ @@ -778,12 +778,12 @@ def test_allocate_ids_non_empty(self): ] after_key_pbs = [_make_key_pb(project), _make_key_pb(project, id_=2345)] rsp_pb = datastore_pb2.AllocateIdsResponse() - rsp_pb.keys.add().CopyFrom(after_key_pbs[0]) - rsp_pb.keys.add().CopyFrom(after_key_pbs[1]) + rsp_pb._pb.keys.add().CopyFrom(after_key_pbs[0]._pb) + rsp_pb._pb.keys.add().CopyFrom(after_key_pbs[1]._pb) # Create mock HTTP and client with response. http = _make_requests_session( - [_make_response(content=rsp_pb.SerializeToString())] + [_make_response(content=rsp_pb._pb.SerializeToString())] ) client_info = _make_client_info() client = mock.Mock( @@ -798,8 +798,8 @@ def test_allocate_ids_non_empty(self): response = ds_api.allocate_ids(project, before_key_pbs) # Check the result and verify the callers. - self.assertEqual(list(response.keys), after_key_pbs) - self.assertEqual(response, rsp_pb) + self.assertEqual(list(response.keys), [i._pb for i in after_key_pbs]) + self.assertEqual(response, rsp_pb._pb) uri = _build_expected_url(client._base_url, project, "allocateIds") request = _verify_protobuf_call(http, uri, datastore_pb2.AllocateIdsRequest()) @@ -863,5 +863,5 @@ def _verify_protobuf_call(http, expected_url, pb): ) data = http.request.mock_calls[0][2]["data"] - pb.ParseFromString(data) + pb._pb.ParseFromString(data) return pb diff --git a/tests/unit/test_batch.py b/tests/unit/test_batch.py index 7ad2aeab..78c1db20 100644 --- a/tests/unit/test_batch.py +++ b/tests/unit/test_batch.py @@ -42,7 +42,7 @@ def test_ctor(self): self.assertEqual(batch._partial_key_entities, []) def test_current(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" client = _Client(project) @@ -63,8 +63,15 @@ def test_current(self): commit_method = client._datastore_api.commit self.assertEqual(commit_method.call_count, 2) - mode = datastore_pb2.CommitRequest.NON_TRANSACTIONAL - commit_method.assert_called_with(project, mode, [], transaction=None) + mode = datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL + commit_method.assert_called_with( + request={ + "project_id": project, + "mode": mode, + "mutations": [], + "transaction": None, + } + ) def test_put_entity_wo_key(self): project = "PROJECT" @@ -213,7 +220,7 @@ def test_rollback_wrong_status(self): self.assertRaises(ValueError, batch.rollback) def test_commit(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" client = _Client(project) @@ -226,11 +233,18 @@ def test_commit(self): self.assertEqual(batch._status, batch._FINISHED) commit_method = client._datastore_api.commit - mode = datastore_pb2.CommitRequest.NON_TRANSACTIONAL - commit_method.assert_called_with(project, mode, [], transaction=None) + mode = datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL + commit_method.assert_called_with( + request={ + "project_id": project, + "mode": mode, + "mutations": [], + "transaction": None, + } + ) def test_commit_w_timeout(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" client = _Client(project) @@ -244,13 +258,19 @@ def test_commit_w_timeout(self): self.assertEqual(batch._status, batch._FINISHED) commit_method = client._datastore_api.commit - mode = datastore_pb2.CommitRequest.NON_TRANSACTIONAL + mode = datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL commit_method.assert_called_with( - project, mode, [], transaction=None, timeout=timeout + request={ + "project_id": project, + "mode": mode, + "mutations": [], + "transaction": None, + }, + timeout=timeout, ) def test_commit_w_retry(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" client = _Client(project) @@ -264,9 +284,15 @@ def test_commit_w_retry(self): self.assertEqual(batch._status, batch._FINISHED) commit_method = client._datastore_api.commit - mode = datastore_pb2.CommitRequest.NON_TRANSACTIONAL + mode = datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL commit_method.assert_called_with( - project, mode, [], transaction=None, retry=retry + request={ + "project_id": project, + "mode": mode, + "mutations": [], + "transaction": None, + }, + retry=retry, ) def test_commit_wrong_status(self): @@ -278,7 +304,7 @@ def test_commit_wrong_status(self): self.assertRaises(ValueError, batch.commit) def test_commit_w_partial_key_entities(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" new_id = 1234 @@ -296,13 +322,20 @@ def test_commit_w_partial_key_entities(self): batch.commit() self.assertEqual(batch._status, batch._FINISHED) - mode = datastore_pb2.CommitRequest.NON_TRANSACTIONAL - ds_api.commit.assert_called_once_with(project, mode, [], transaction=None) + mode = datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL + ds_api.commit.assert_called_once_with( + request={ + "project_id": project, + "mode": mode, + "mutations": [], + "transaction": None, + } + ) self.assertFalse(entity.key.is_partial) self.assertEqual(entity.key._id, new_id) def test_as_context_mgr_wo_error(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" properties = {"foo": "bar"} @@ -321,13 +354,18 @@ def test_as_context_mgr_wo_error(self): mutated_entity = _mutated_pb(self, batch.mutations, "upsert") self.assertEqual(mutated_entity.key, key._key) commit_method = client._datastore_api.commit - mode = datastore_pb2.CommitRequest.NON_TRANSACTIONAL + mode = datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL commit_method.assert_called_with( - project, mode, batch.mutations, transaction=None + request={ + "project_id": project, + "mode": mode, + "mutations": batch.mutations, + "transaction": None, + } ) def test_as_context_mgr_nested(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" properties = {"foo": "bar"} @@ -358,12 +396,22 @@ def test_as_context_mgr_nested(self): commit_method = client._datastore_api.commit self.assertEqual(commit_method.call_count, 2) - mode = datastore_pb2.CommitRequest.NON_TRANSACTIONAL + mode = datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL commit_method.assert_called_with( - project, mode, batch1.mutations, transaction=None + request={ + "project_id": project, + "mode": mode, + "mutations": batch1.mutations, + "transaction": None, + } ) commit_method.assert_called_with( - project, mode, batch2.mutations, transaction=None + request={ + "project_id": project, + "mode": mode, + "mutations": batch2.mutations, + "transaction": None, + } ) def test_as_context_mgr_w_error(self): @@ -415,8 +463,8 @@ def _call_fut(self, commit_response_pb): return _parse_commit_response(commit_response_pb) def test_it(self): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 index_updates = 1337 keys = [ @@ -428,7 +476,7 @@ def test_it(self): index_updates=index_updates, ) result = self._call_fut(response) - self.assertEqual(result, (index_updates, keys)) + self.assertEqual(result, (index_updates, [i._pb for i in keys])) class _Entity(dict): @@ -452,13 +500,13 @@ def is_partial(self): return self._id is None def to_protobuf(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 key = self._key = entity_pb2.Key() # Don't assign it, because it will just get ripped out # key.partition_id.project_id = self.project - element = key.path.add() + element = key._pb.path.add() element.kind = self._kind if self._id is not None: element.id = self._id @@ -504,25 +552,25 @@ def _mutated_pb(test_case, mutation_pb_list, mutation_type): # We grab the only mutation. mutated_pb = mutation_pb_list[0] # Then check if it is the correct type. - test_case.assertEqual(mutated_pb.WhichOneof("operation"), mutation_type) + test_case.assertEqual(mutated_pb._pb.WhichOneof("operation"), mutation_type) return getattr(mutated_pb, mutation_type) def _make_mutation(id_): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 key = entity_pb2.Key() key.partition_id.project_id = "PROJECT" - elem = key.path.add() + elem = key._pb.path.add() elem.kind = "Kind" elem.id = id_ return datastore_pb2.MutationResult(key=key) def _make_commit_response(*new_key_ids): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 mutation_results = [_make_mutation(key_id) for key_id in new_key_ids] return datastore_pb2.CommitResponse(mutation_results=mutation_results) diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 61f8af7b..59588f10 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -24,12 +24,12 @@ def _make_credentials(): def _make_entity_pb(project, kind, integer_id, name=None, str_val=None): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.helpers import _new_value_pb entity_pb = entity_pb2.Entity() entity_pb.key.partition_id.project_id = project - path_element = entity_pb.key.path.add() + path_element = entity_pb._pb.key.path.add() path_element.kind = kind path_element.id = integer_id if name is not None and str_val is not None: @@ -420,7 +420,7 @@ def test_get_multi_no_keys(self): self.assertEqual(results, []) def test_get_multi_miss(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore.key import Key creds = _make_credentials() @@ -434,12 +434,16 @@ def test_get_multi_miss(self): read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with( - self.PROJECT, [key.to_protobuf()], read_options=read_options + request={ + "project_id": self.PROJECT, + "keys": [key.to_protobuf()], + "read_options": read_options, + } ) def test_get_multi_miss_w_missing(self): - from google.cloud.datastore_v1.proto import entity_pb2 - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.key import Key KIND = "Kind" @@ -448,14 +452,14 @@ def test_get_multi_miss_w_missing(self): # Make a missing entity pb to be returned from mock backend. missed = entity_pb2.Entity() missed.key.partition_id.project_id = self.PROJECT - path_element = missed.key.path.add() + path_element = missed._pb.key.path.add() path_element.kind = KIND path_element.id = ID creds = _make_credentials() client = self._make_one(credentials=creds) # Set missing entity on mock connection. - lookup_response = _make_lookup_response(missing=[missed]) + lookup_response = _make_lookup_response(missing=[missed._pb]) ds_api = _make_datastore_api(lookup_response=lookup_response) client._datastore_api_internal = ds_api @@ -464,11 +468,15 @@ def test_get_multi_miss_w_missing(self): entities = client.get_multi([key], missing=missing) self.assertEqual(entities, []) key_pb = key.to_protobuf() - self.assertEqual([missed.key.to_protobuf() for missed in missing], [key_pb]) + self.assertEqual([missed.key.to_protobuf() for missed in missing], [key_pb._pb]) read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with( - self.PROJECT, [key_pb], read_options=read_options + request={ + "project_id": self.PROJECT, + "keys": [key_pb], + "read_options": read_options, + } ) def test_get_multi_w_missing_non_empty(self): @@ -492,7 +500,7 @@ def test_get_multi_w_deferred_non_empty(self): self.assertRaises(ValueError, client.get_multi, [key], deferred=deferred) def test_get_multi_miss_w_deferred(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore.key import Key key = Key("Kind", 1234, project=self.PROJECT) @@ -512,12 +520,16 @@ def test_get_multi_miss_w_deferred(self): read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with( - self.PROJECT, [key_pb], read_options=read_options + request={ + "project_id": self.PROJECT, + "keys": [key_pb], + "read_options": read_options, + } ) def test_get_multi_w_deferred_from_backend_but_not_passed(self): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.key import Key @@ -527,9 +539,9 @@ def test_get_multi_w_deferred_from_backend_but_not_passed(self): key2_pb = key2.to_protobuf() entity1_pb = entity_pb2.Entity() - entity1_pb.key.CopyFrom(key1_pb) + entity1_pb._pb.key.CopyFrom(key1_pb._pb) entity2_pb = entity_pb2.Entity() - entity2_pb.key.CopyFrom(key2_pb) + entity2_pb._pb.key.CopyFrom(key2_pb._pb) creds = _make_credentials() client = self._make_one(credentials=creds) @@ -561,15 +573,25 @@ def test_get_multi_w_deferred_from_backend_but_not_passed(self): self.assertEqual(ds_api.lookup.call_count, 2) read_options = datastore_pb2.ReadOptions() + ds_api.lookup.assert_any_call( - self.PROJECT, [key2_pb], read_options=read_options + request={ + "project_id": self.PROJECT, + "keys": [key2_pb], + "read_options": read_options, + }, ) + ds_api.lookup.assert_any_call( - self.PROJECT, [key1_pb, key2_pb], read_options=read_options + request={ + "project_id": self.PROJECT, + "keys": [key1_pb, key2_pb], + "read_options": read_options, + }, ) def test_get_multi_hit_w_retry_w_timeout(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore.key import Key kind = "Kind" @@ -600,16 +622,19 @@ def test_get_multi_hit_w_retry_w_timeout(self): self.assertEqual(result["foo"], "Foo") read_options = datastore_pb2.ReadOptions() + ds_api.lookup.assert_called_once_with( - self.PROJECT, - [key.to_protobuf()], - read_options=read_options, + request={ + "project_id": self.PROJECT, + "keys": [key.to_protobuf()], + "read_options": read_options, + }, retry=retry, timeout=timeout, ) def test_get_multi_hit_w_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore.key import Key txn_id = b"123" @@ -642,11 +667,15 @@ def test_get_multi_hit_w_transaction(self): read_options = datastore_pb2.ReadOptions(transaction=txn_id) ds_api.lookup.assert_called_once_with( - self.PROJECT, [key.to_protobuf()], read_options=read_options + request={ + "project_id": self.PROJECT, + "keys": [key.to_protobuf()], + "read_options": read_options, + } ) def test_get_multi_hit_multiple_keys_same_project(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore.key import Key kind = "Kind" @@ -676,9 +705,11 @@ def test_get_multi_hit_multiple_keys_same_project(self): read_options = datastore_pb2.ReadOptions() ds_api.lookup.assert_called_once_with( - self.PROJECT, - [key1.to_protobuf(), key2.to_protobuf()], - read_options=read_options, + request={ + "project_id": self.PROJECT, + "keys": [key1.to_protobuf(), key2.to_protobuf()], + "read_options": read_options, + } ) def test_get_multi_hit_multiple_keys_different_project(self): @@ -770,7 +801,7 @@ def test_put_multi_w_single_empty_entity(self): self.assertRaises(ValueError, client.put_multi, Entity()) def test_put_multi_no_batch_w_partial_key_w_retry_w_timeout(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 from google.cloud.datastore.helpers import _property_tuples entity = _Entity(foo=u"bar") @@ -789,14 +820,21 @@ def test_put_multi_no_batch_w_partial_key_w_retry_w_timeout(self): self.assertEqual(ds_api.commit.call_count, 1) _, positional, keyword = ds_api.commit.mock_calls[0] - expected_kw = {"transaction": None, "retry": retry, "timeout": timeout} - self.assertEqual(keyword, expected_kw) - self.assertEqual(len(positional), 3) - self.assertEqual(positional[0], self.PROJECT) - self.assertEqual(positional[1], datastore_pb2.CommitRequest.NON_TRANSACTIONAL) + self.assertEqual(len(positional), 0) + + self.assertEqual(len(keyword), 3) + self.assertEqual(keyword["retry"], retry) + self.assertEqual(keyword["timeout"], timeout) - mutations = positional[2] + self.assertEqual(len(keyword["request"]), 4) + self.assertEqual(keyword["request"]["project_id"], self.PROJECT) + self.assertEqual( + keyword["request"]["mode"], + datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL, + ) + self.assertEqual(keyword["request"]["transaction"], None) + mutations = keyword["request"]["mutations"] mutated_entity = _mutated_pb(self, mutations, "insert") self.assertEqual(mutated_entity.key, key.to_protobuf()) @@ -859,7 +897,7 @@ def test_delete_multi_no_keys(self): client._datastore_api_internal.commit.assert_not_called() def test_delete_multi_no_batch_w_retry_w_timeout(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 key = _Key() retry = mock.Mock() @@ -875,14 +913,21 @@ def test_delete_multi_no_batch_w_retry_w_timeout(self): self.assertEqual(ds_api.commit.call_count, 1) _, positional, keyword = ds_api.commit.mock_calls[0] - expected_kw = {"transaction": None, "retry": retry, "timeout": timeout} - self.assertEqual(keyword, expected_kw) - self.assertEqual(len(positional), 3) - self.assertEqual(positional[0], self.PROJECT) - self.assertEqual(positional[1], datastore_pb2.CommitRequest.NON_TRANSACTIONAL) + self.assertEqual(len(positional), 0) + + self.assertEqual(len(keyword), 3) + self.assertEqual(keyword["retry"], retry) + self.assertEqual(keyword["timeout"], timeout) - mutations = positional[2] + self.assertEqual(len(keyword["request"]), 4) + self.assertEqual(keyword["request"]["project_id"], self.PROJECT) + self.assertEqual( + keyword["request"]["mode"], + datastore_pb2.CommitRequest.Mode.NON_TRANSACTIONAL, + ) + self.assertEqual(keyword["request"]["transaction"], None) + mutations = keyword["request"]["mutations"] mutated_key = _mutated_pb(self, mutations, "delete") self.assertEqual(mutated_key, key.to_protobuf()) @@ -934,7 +979,9 @@ def test_allocate_ids_w_partial_key(self): self.assertEqual([key.id for key in result], list(range(num_ids))) expected_keys = [incomplete_key.to_protobuf()] * num_ids - alloc_ids.assert_called_once_with(self.PROJECT, expected_keys) + alloc_ids.assert_called_once_with( + request={"project_id": self.PROJECT, "keys": expected_keys} + ) def test_allocate_ids_w_partial_key_w_retry_w_timeout(self): num_ids = 2 @@ -959,7 +1006,9 @@ def test_allocate_ids_w_partial_key_w_retry_w_timeout(self): expected_keys = [incomplete_key.to_protobuf()] * num_ids alloc_ids.assert_called_once_with( - self.PROJECT, expected_keys, retry=retry, timeout=timeout + request={"project_id": self.PROJECT, "keys": expected_keys}, + retry=retry, + timeout=timeout, ) def test_allocate_ids_w_completed_key(self): @@ -986,7 +1035,9 @@ def test_reserve_ids_sequential_w_completed_key(self): for id in range(complete_key.id, complete_key.id + num_ids) ) expected_keys = [key.to_protobuf() for key in reserved_keys] - reserve_ids.assert_called_once_with(self.PROJECT, expected_keys) + reserve_ids.assert_called_once_with( + request={"project_id": self.PROJECT, "keys": expected_keys} + ) def test_reserve_ids_sequential_w_completed_key_w_retry_w_timeout(self): num_ids = 2 @@ -1011,7 +1062,9 @@ def test_reserve_ids_sequential_w_completed_key_w_retry_w_timeout(self): ) expected_keys = [key.to_protobuf() for key in reserved_keys] reserve_ids.assert_called_once_with( - self.PROJECT, expected_keys, retry=retry, timeout=timeout + request={"project_id": self.PROJECT, "keys": expected_keys}, + retry=retry, + timeout=timeout, ) def test_reserve_ids_sequential_w_completed_key_w_ancestor(self): @@ -1031,7 +1084,9 @@ def test_reserve_ids_sequential_w_completed_key_w_ancestor(self): for id in range(complete_key.id, complete_key.id + num_ids) ) expected_keys = [key.to_protobuf() for key in reserved_keys] - reserve_ids.assert_called_once_with(self.PROJECT, expected_keys) + reserve_ids.assert_called_once_with( + request={"project_id": self.PROJECT, "keys": expected_keys} + ) def test_reserve_ids_sequential_w_partial_key(self): num_ids = 2 @@ -1074,7 +1129,9 @@ def test_reserve_ids_w_completed_key(self): for id in range(complete_key.id, complete_key.id + num_ids) ) expected_keys = [key.to_protobuf() for key in reserved_keys] - reserve_ids.assert_called_once_with(self.PROJECT, expected_keys) + reserve_ids.assert_called_once_with( + request={"project_id": self.PROJECT, "keys": expected_keys} + ) def test_reserve_ids_w_completed_key_w_retry_w_timeout(self): num_ids = 2 @@ -1097,7 +1154,9 @@ def test_reserve_ids_w_completed_key_w_retry_w_timeout(self): ) expected_keys = [key.to_protobuf() for key in reserved_keys] reserve_ids.assert_called_once_with( - self.PROJECT, expected_keys, retry=retry, timeout=timeout + request={"project_id": self.PROJECT, "keys": expected_keys}, + retry=retry, + timeout=timeout, ) def test_reserve_ids_w_completed_key_w_ancestor(self): @@ -1117,7 +1176,9 @@ def test_reserve_ids_w_completed_key_w_ancestor(self): for id in range(complete_key.id, complete_key.id + num_ids) ) expected_keys = [key.to_protobuf() for key in reserved_keys] - reserve_ids.assert_called_once_with(self.PROJECT, expected_keys) + reserve_ids.assert_called_once_with( + request={"project_id": self.PROJECT, "keys": expected_keys} + ) def test_reserve_ids_w_partial_key(self): num_ids = 2 @@ -1155,7 +1216,9 @@ def test_reserve_ids_multi(self): client.reserve_ids_multi([key1, key2]) expected_keys = [key1.to_protobuf(), key2.to_protobuf()] - reserve_ids.assert_called_once_with(self.PROJECT, expected_keys) + reserve_ids.assert_called_once_with( + request={"project_id": self.PROJECT, "keys": expected_keys} + ) def test_reserve_ids_multi_w_partial_key(self): incomplete_key = _Key(_Key.kind, None) @@ -1252,9 +1315,9 @@ def test_read_only_transaction_defaults(self): self.assertEqual( xact._options, TransactionOptions(read_only=TransactionOptions.ReadOnly()) ) - self.assertFalse(xact._options.HasField("read_write")) - self.assertTrue(xact._options.HasField("read_only")) - self.assertEqual(xact._options.read_only, TransactionOptions.ReadOnly()) + self.assertFalse(xact._options._pb.HasField("read_write")) + self.assertTrue(xact._options._pb.HasField("read_only")) + self.assertEqual(xact._options._pb.read_only, TransactionOptions.ReadOnly()._pb) def test_query_w_client(self): KIND = "KIND" @@ -1426,13 +1489,13 @@ def is_partial(self): return self.id is None and self.name is None def to_protobuf(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 key = self._key = entity_pb2.Key() path = self._flat_path while path: - element = key.path.add() + element = key._pb.path.add() kind, id_or_name = path[:2] element.kind = kind if isinstance(id_or_name, int): @@ -1476,22 +1539,22 @@ def _mutated_pb(test_case, mutation_pb_list, mutation_type): # We grab the only mutation. mutated_pb = mutation_pb_list[0] # Then check if it is the correct type. - test_case.assertEqual(mutated_pb.WhichOneof("operation"), mutation_type) + test_case.assertEqual(mutated_pb._pb.WhichOneof("operation"), mutation_type) return getattr(mutated_pb, mutation_type) def _make_key(id_): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 key = entity_pb2.Key() - elem = key.path.add() + elem = key._pb.path.add() elem.id = id_ return key def _make_commit_response(*keys): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 mutation_results = [datastore_pb2.MutationResult(key=key) for key in keys] return datastore_pb2.CommitResponse(mutation_results=mutation_results) diff --git a/tests/unit/test_helpers.py b/tests/unit/test_helpers.py index 995d9cfa..81cae0f3 100644 --- a/tests/unit/test_helpers.py +++ b/tests/unit/test_helpers.py @@ -22,15 +22,15 @@ def _call_fut(self, entity_pb, name): return _new_value_pb(entity_pb, name) def test_it(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 entity_pb = entity_pb2.Entity() name = "foo" result = self._call_fut(entity_pb, name) - self.assertIsInstance(result, entity_pb2.Value) - self.assertEqual(len(entity_pb.properties), 1) - self.assertEqual(entity_pb.properties[name], result) + self.assertIsInstance(result, type(entity_pb2.Value()._pb)) + self.assertEqual(len(entity_pb._pb.properties), 1) + self.assertEqual(entity_pb._pb.properties[name], result) class Test__property_tuples(unittest.TestCase): @@ -41,7 +41,7 @@ def _call_fut(self, entity_pb): def test_it(self): import types - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.helpers import _new_value_pb entity_pb = entity_pb2.Entity() @@ -62,7 +62,7 @@ def _call_fut(self, val): return entity_from_protobuf(val) def test_it(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.helpers import _new_value_pb _PROJECT = "PROJECT" @@ -70,7 +70,7 @@ def test_it(self): _ID = 1234 entity_pb = entity_pb2.Entity() entity_pb.key.partition_id.project_id = _PROJECT - entity_pb.key.path.add(kind=_KIND, id=_ID) + entity_pb._pb.key.path.add(kind=_KIND, id=_ID) value_pb = _new_value_pb(entity_pb, "foo") value_pb.string_value = "Foo" @@ -92,7 +92,7 @@ def test_it(self): indexed_array_val_pb = array_pb2.add() indexed_array_val_pb.integer_value = 12 - entity = self._call_fut(entity_pb) + entity = self._call_fut(entity_pb._pb) self.assertEqual(entity.kind, _KIND) self.assertEqual(entity.exclude_from_indexes, frozenset(["bar", "baz"])) entity_props = dict(entity) @@ -108,7 +108,7 @@ def test_it(self): self.assertEqual(key.id, _ID) def test_mismatched_value_indexed(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.helpers import _new_value_pb _PROJECT = "PROJECT" @@ -116,7 +116,7 @@ def test_mismatched_value_indexed(self): _ID = 1234 entity_pb = entity_pb2.Entity() entity_pb.key.partition_id.project_id = _PROJECT - entity_pb.key.path.add(kind=_KIND, id=_ID) + entity_pb._pb.key.path.add(kind=_KIND, id=_ID) array_val_pb = _new_value_pb(entity_pb, "baz") array_pb = array_val_pb.array_value.values @@ -129,19 +129,19 @@ def test_mismatched_value_indexed(self): unindexed_value_pb2.integer_value = 11 with self.assertRaises(ValueError): - self._call_fut(entity_pb) + self._call_fut(entity_pb._pb) def test_entity_no_key(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 entity_pb = entity_pb2.Entity() - entity = self._call_fut(entity_pb) + entity = self._call_fut(entity_pb._pb) self.assertIsNone(entity.key) self.assertEqual(dict(entity), {}) def test_entity_with_meaning(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.helpers import _new_value_pb entity_pb = entity_pb2.Entity() @@ -156,7 +156,7 @@ def test_entity_with_meaning(self): self.assertEqual(entity._meanings, {name: (meaning, val)}) def test_nested_entity_no_key(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.helpers import _new_value_pb PROJECT = "FOO" @@ -171,13 +171,13 @@ def test_nested_entity_no_key(self): entity_pb = entity_pb2.Entity() entity_pb.key.partition_id.project_id = PROJECT - element = entity_pb.key.path.add() + element = entity_pb._pb.key.path.add() element.kind = KIND outside_val_pb = _new_value_pb(entity_pb, OUTSIDE_NAME) - outside_val_pb.entity_value.CopyFrom(entity_inside) + outside_val_pb.entity_value.CopyFrom(entity_inside._pb) - entity = self._call_fut(entity_pb) + entity = self._call_fut(entity_pb._pb) self.assertEqual(entity.key.project, PROJECT) self.assertEqual(entity.key.flat_path, (KIND,)) self.assertEqual(len(entity), 1) @@ -188,7 +188,7 @@ def test_nested_entity_no_key(self): self.assertEqual(inside_entity[INSIDE_NAME], INSIDE_VALUE) def test_index_mismatch_ignores_empty_list(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 _PROJECT = "PROJECT" _KIND = "KIND" @@ -198,9 +198,9 @@ def test_index_mismatch_ignores_empty_list(self): entity_pb = entity_pb2.Entity(properties={"baz": array_val_pb}) entity_pb.key.partition_id.project_id = _PROJECT - entity_pb.key.path.add(kind=_KIND, id=_ID) + entity_pb.key._pb.path.add(kind=_KIND, id=_ID) - entity = self._call_fut(entity_pb) + entity = self._call_fut(entity_pb._pb) entity_dict = dict(entity) self.assertEqual(entity_dict["baz"], []) @@ -222,14 +222,14 @@ def _compare_entity_proto(self, entity_pb1, entity_pb2): name1, val1 = pair1 name2, val2 = pair2 self.assertEqual(name1, name2) - if val1.HasField("entity_value"): # Message field (Entity) + if val1._pb.HasField("entity_value"): # Message field (Entity) self.assertEqual(val1.meaning, val2.meaning) self._compare_entity_proto(val1.entity_value, val2.entity_value) else: self.assertEqual(val1, val2) def test_empty(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity entity = Entity() @@ -237,7 +237,7 @@ def test_empty(self): self._compare_entity_proto(entity_pb, entity_pb2.Entity()) def test_key_only(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.key import Key @@ -249,14 +249,14 @@ def test_key_only(self): expected_pb = entity_pb2.Entity() expected_pb.key.partition_id.project_id = project - path_elt = expected_pb.key.path.add() + path_elt = expected_pb._pb.key.path.add() path_elt.kind = kind path_elt.name = name self._compare_entity_proto(entity_pb, expected_pb) def test_simple_fields(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.helpers import _new_value_pb @@ -276,7 +276,7 @@ def test_simple_fields(self): self._compare_entity_proto(entity_pb, expected_pb) def test_with_empty_list(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity entity = Entity() @@ -284,23 +284,23 @@ def test_with_empty_list(self): entity_pb = self._call_fut(entity) expected_pb = entity_pb2.Entity() - prop = expected_pb.properties.get_or_create("foo") - prop.array_value.CopyFrom(entity_pb2.ArrayValue(values=[])) + prop = expected_pb._pb.properties.get_or_create("foo") + prop.array_value.CopyFrom(entity_pb2.ArrayValue(values=[])._pb) self._compare_entity_proto(entity_pb, expected_pb) def test_inverts_to_protobuf(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.helpers import _new_value_pb from google.cloud.datastore.helpers import entity_from_protobuf original_pb = entity_pb2.Entity() # Add a key. original_pb.key.partition_id.project_id = project = "PROJECT" - elem1 = original_pb.key.path.add() + elem1 = original_pb._pb.key.path.add() elem1.kind = "Family" elem1.id = 1234 - elem2 = original_pb.key.path.add() + elem2 = original_pb._pb.key.path.add() elem2.kind = "King" elem2.name = "Spades" @@ -320,7 +320,7 @@ def test_inverts_to_protobuf(self): sub_val_pb2 = _new_value_pb(sub_pb, "y") sub_val_pb2.double_value = 2.718281828 val_pb3.meaning = 9 - val_pb3.entity_value.CopyFrom(sub_pb) + val_pb3.entity_value.CopyFrom(sub_pb._pb) # Add a list property. val_pb4 = _new_value_pb(original_pb, "list-quux") @@ -343,7 +343,7 @@ def test_inverts_to_protobuf(self): self._compare_entity_proto(original_pb, new_pb) def test_meaning_with_change(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.helpers import _new_value_pb @@ -361,7 +361,7 @@ def test_meaning_with_change(self): self._compare_entity_proto(entity_pb, expected_pb) def test_variable_meanings(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.helpers import _new_value_pb @@ -387,7 +387,7 @@ def test_variable_meanings(self): self._compare_entity_proto(entity_pb, expected_pb) def test_dict_to_entity(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity entity = Entity() @@ -406,7 +406,7 @@ def test_dict_to_entity(self): self.assertEqual(entity_pb, expected_pb) def test_dict_to_entity_recursive(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity entity = Entity() @@ -445,7 +445,7 @@ def _call_fut(self, val): return key_from_protobuf(val) def _makePB(self, project=None, namespace=None, path=()): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 pb = entity_pb2.Key() if project is not None: @@ -453,7 +453,7 @@ def _makePB(self, project=None, namespace=None, path=()): if namespace is not None: pb.partition_id.namespace_id = namespace for elem in path: - added = pb.path.add() + added = pb._pb.path.add() added.kind = elem["kind"] if "id" in elem: added.id = elem["id"] @@ -504,16 +504,16 @@ def test_eventual_w_transaction(self): self._call_fut(True, b"123") def test_eventual_wo_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 read_options = self._call_fut(True, None) expected = datastore_pb2.ReadOptions( - read_consistency=datastore_pb2.ReadOptions.EVENTUAL + read_consistency=datastore_pb2.ReadOptions.ReadConsistency.EVENTUAL ) self.assertEqual(read_options, expected) def test_default_w_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 txn_id = b"123abc-easy-as" read_options = self._call_fut(False, txn_id) @@ -521,7 +521,7 @@ def test_default_w_transaction(self): self.assertEqual(read_options, expected) def test_default_wo_transaction(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 read_options = self._call_fut(False, None) expected = datastore_pb2.ReadOptions() @@ -589,13 +589,9 @@ def test_long(self): self.assertEqual(value, must_be_long) def test_native_str(self): - import six - name, value = self._call_fut("str") - if six.PY2: - self.assertEqual(name, "blob_value") - else: # pragma: NO COVER Python 3 - self.assertEqual(name, "string_value") + + self.assertEqual(name, "string_value") self.assertEqual(value, "str") def test_bytes(self): @@ -664,7 +660,7 @@ def _call_fut(self, pb): return _get_value_from_value_pb(pb) def _makePB(self, attr_name, value): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 pb = entity_pb2.Value() setattr(pb, attr_name, value) @@ -674,22 +670,22 @@ def test_datetime(self): import calendar import datetime from google.cloud._helpers import UTC - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 micros = 4375 utc = datetime.datetime(2014, 9, 16, 10, 19, 32, micros, UTC) pb = entity_pb2.Value() - pb.timestamp_value.seconds = calendar.timegm(utc.timetuple()) - pb.timestamp_value.nanos = 1000 * micros + pb._pb.timestamp_value.seconds = calendar.timegm(utc.timetuple()) + pb._pb.timestamp_value.nanos = 1000 * micros self.assertEqual(self._call_fut(pb), utc) def test_key(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.key import Key pb = entity_pb2.Value() expected = Key("KIND", 1234, project="PROJECT").to_protobuf() - pb.key_value.CopyFrom(expected) + pb.key_value._pb.CopyFrom(expected._pb) found = self._call_fut(pb) self.assertEqual(found.to_protobuf(), expected) @@ -714,13 +710,13 @@ def test_unicode(self): self.assertEqual(self._call_fut(pb), u"str") def test_entity(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.entity import Entity from google.cloud.datastore.helpers import _new_value_pb pb = entity_pb2.Value() entity_pb = pb.entity_value - entity_pb.key.path.add(kind="KIND") + entity_pb._pb.key.path.add(kind="KIND") entity_pb.key.partition_id.project_id = "PROJECT" value_pb = _new_value_pb(entity_pb, "foo") @@ -730,20 +726,20 @@ def test_entity(self): self.assertEqual(entity["foo"], "Foo") def test_array(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 pb = entity_pb2.Value() array_pb = pb.array_value.values - item_pb = array_pb.add() + item_pb = array_pb._pb.add() item_pb.string_value = "Foo" - item_pb = array_pb.add() + item_pb = array_pb._pb.add() item_pb.string_value = "Bar" items = self._call_fut(pb) self.assertEqual(items, ["Foo", "Bar"]) def test_geo_point(self): from google.type import latlng_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 from google.cloud.datastore.helpers import GeoPoint lat = -3.14 @@ -757,14 +753,14 @@ def test_geo_point(self): def test_null(self): from google.protobuf import struct_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 pb = entity_pb2.Value(null_value=struct_pb2.NULL_VALUE) result = self._call_fut(pb) self.assertIsNone(result) def test_unknown(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 pb = entity_pb2.Value() with self.assertRaises(ValueError): @@ -778,9 +774,9 @@ def _call_fut(self, value_pb, val): return _set_protobuf_value(value_pb, val) def _makePB(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 - return entity_pb2.Value() + return entity_pb2.Value()._pb def test_datetime(self): import calendar @@ -802,7 +798,7 @@ def test_key(self): key = Key("KIND", 1234, project="PROJECT") self._call_fut(pb, key) value = pb.key_value - self.assertEqual(value, key.to_protobuf()) + self.assertEqual(value, key.to_protobuf()._pb) def test_none(self): pb = self._makePB() @@ -835,14 +831,10 @@ def test_long(self): self.assertEqual(value, must_be_long) def test_native_str(self): - import six - pb = self._makePB() self._call_fut(pb, "str") - if six.PY2: - value = pb.blob_value - else: # pragma: NO COVER Python 3 - value = pb.string_value + + value = pb.string_value self.assertEqual(value, "str") def test_bytes(self): @@ -881,7 +873,7 @@ def test_entity_w_key(self): entity[name] = value self._call_fut(pb, entity) entity_pb = pb.entity_value - self.assertEqual(entity_pb.key, key.to_protobuf()) + self.assertEqual(entity_pb.key, key.to_protobuf()._pb) prop_dict = dict(_property_tuples(entity_pb)) self.assertEqual(len(prop_dict), 1) @@ -918,14 +910,14 @@ def _call_fut(self, *args, **kwargs): return _get_meaning(*args, **kwargs) def test_no_meaning(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 value_pb = entity_pb2.Value() result = self._call_fut(value_pb) self.assertIsNone(result) def test_single(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 value_pb = entity_pb2.Value() value_pb.meaning = meaning = 22 @@ -934,22 +926,22 @@ def test_single(self): self.assertEqual(meaning, result) def test_empty_array_value(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 value_pb = entity_pb2.Value() - value_pb.array_value.values.add() - value_pb.array_value.values.pop() + value_pb._pb.array_value.values.add() + value_pb._pb.array_value.values.pop() result = self._call_fut(value_pb, is_list=True) self.assertEqual(None, result) def test_array_value(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 value_pb = entity_pb2.Value() meaning = 9 - sub_value_pb1 = value_pb.array_value.values.add() - sub_value_pb2 = value_pb.array_value.values.add() + sub_value_pb1 = value_pb._pb.array_value.values.add() + sub_value_pb2 = value_pb._pb.array_value.values.add() sub_value_pb1.meaning = sub_value_pb2.meaning = meaning sub_value_pb1.string_value = u"hi" @@ -959,13 +951,13 @@ def test_array_value(self): self.assertEqual(meaning, result) def test_array_value_multiple_meanings(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 value_pb = entity_pb2.Value() meaning1 = 9 meaning2 = 10 - sub_value_pb1 = value_pb.array_value.values.add() - sub_value_pb2 = value_pb.array_value.values.add() + sub_value_pb1 = value_pb._pb.array_value.values.add() + sub_value_pb2 = value_pb._pb.array_value.values.add() sub_value_pb1.meaning = meaning1 sub_value_pb2.meaning = meaning2 @@ -976,12 +968,12 @@ def test_array_value_multiple_meanings(self): self.assertEqual(result, [meaning1, meaning2]) def test_array_value_meaning_partially_unset(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 value_pb = entity_pb2.Value() meaning1 = 9 - sub_value_pb1 = value_pb.array_value.values.add() - sub_value_pb2 = value_pb.array_value.values.add() + sub_value_pb1 = value_pb._pb.array_value.values.add() + sub_value_pb2 = value_pb._pb.array_value.values.add() sub_value_pb1.meaning = meaning1 sub_value_pb1.string_value = u"hi" diff --git a/tests/unit/test_key.py b/tests/unit/test_key.py index 0478e2cb..73565ead 100644 --- a/tests/unit/test_key.py +++ b/tests/unit/test_key.py @@ -345,7 +345,7 @@ def test_completed_key_on_complete(self): self.assertRaises(ValueError, key.completed_key, 5678) def test_to_protobuf_defaults(self): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 _KIND = "KIND" key = self._make_one(_KIND, project=self._DEFAULT_PROJECT) diff --git a/tests/unit/test_query.py b/tests/unit/test_query.py index fbaadb28..97c1db88 100644 --- a/tests/unit/test_query.py +++ b/tests/unit/test_query.py @@ -152,7 +152,7 @@ def _assign(val): def test_ancestor_setter_w_key(self): from google.cloud.datastore.key import Key - _NAME = u"NAME" + _NAME = "NAME" key = Key("KIND", 123, project=self._PROJECT) query = self._make_one(self._make_client()) query.add_filter("name", "=", _NAME) @@ -173,38 +173,38 @@ def test_add_filter_setter_w_unknown_operator(self): def test_add_filter_w_known_operator(self): query = self._make_one(self._make_client()) - query.add_filter("firstname", "=", u"John") - self.assertEqual(query.filters, [("firstname", "=", u"John")]) + query.add_filter("firstname", "=", "John") + self.assertEqual(query.filters, [("firstname", "=", "John")]) def test_add_filter_w_all_operators(self): query = self._make_one(self._make_client()) - query.add_filter("leq_prop", "<=", u"val1") - query.add_filter("geq_prop", ">=", u"val2") - query.add_filter("lt_prop", "<", u"val3") - query.add_filter("gt_prop", ">", u"val4") - query.add_filter("eq_prop", "=", u"val5") + query.add_filter("leq_prop", "<=", "val1") + query.add_filter("geq_prop", ">=", "val2") + query.add_filter("lt_prop", "<", "val3") + query.add_filter("gt_prop", ">", "val4") + query.add_filter("eq_prop", "=", "val5") self.assertEqual(len(query.filters), 5) - self.assertEqual(query.filters[0], ("leq_prop", "<=", u"val1")) - self.assertEqual(query.filters[1], ("geq_prop", ">=", u"val2")) - self.assertEqual(query.filters[2], ("lt_prop", "<", u"val3")) - self.assertEqual(query.filters[3], ("gt_prop", ">", u"val4")) - self.assertEqual(query.filters[4], ("eq_prop", "=", u"val5")) + self.assertEqual(query.filters[0], ("leq_prop", "<=", "val1")) + self.assertEqual(query.filters[1], ("geq_prop", ">=", "val2")) + self.assertEqual(query.filters[2], ("lt_prop", "<", "val3")) + self.assertEqual(query.filters[3], ("gt_prop", ">", "val4")) + self.assertEqual(query.filters[4], ("eq_prop", "=", "val5")) def test_add_filter_w_known_operator_and_entity(self): from google.cloud.datastore.entity import Entity query = self._make_one(self._make_client()) other = Entity() - other["firstname"] = u"John" - other["lastname"] = u"Smith" + other["firstname"] = "John" + other["lastname"] = "Smith" query.add_filter("other", "=", other) self.assertEqual(query.filters, [("other", "=", other)]) def test_add_filter_w_whitespace_property_name(self): query = self._make_one(self._make_client()) PROPERTY_NAME = " property with lots of space " - query.add_filter(PROPERTY_NAME, "=", u"John") - self.assertEqual(query.filters, [(PROPERTY_NAME, "=", u"John")]) + query.add_filter(PROPERTY_NAME, "=", "John") + self.assertEqual(query.filters, [(PROPERTY_NAME, "=", "John")]) def test_add_filter___key__valid_key(self): from google.cloud.datastore.key import Key @@ -218,9 +218,9 @@ def test_add_filter_return_query_obj(self): from google.cloud.datastore.query import Query query = self._make_one(self._make_client()) - query_obj = query.add_filter("firstname", "=", u"John") + query_obj = query.add_filter("firstname", "=", "John") self.assertIsInstance(query_obj, Query) - self.assertEqual(query_obj.filters, [("firstname", "=", u"John")]) + self.assertEqual(query_obj.filters, [("firstname", "=", "John")]) def test_filter___key__not_equal_operator(self): from google.cloud.datastore.key import Key @@ -429,7 +429,7 @@ def test_constructor_explicit(self): self.assertEqual(iterator._timeout, timeout) def test__build_protobuf_empty(self): - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 from google.cloud.datastore.query import Query client = _Client(None) @@ -444,7 +444,7 @@ def test__build_protobuf_all_values_except_offset(self): # this test and the following (all_values_except_start_and_end_cursor) # test mutually exclusive states; the offset is ignored # if a start_cursor is supplied - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 from google.cloud.datastore.query import Query client = _Client(None) @@ -463,14 +463,14 @@ def test__build_protobuf_all_values_except_offset(self): pb = iterator._build_protobuf() expected_pb = query_pb2.Query(start_cursor=start_bytes, end_cursor=end_bytes) - expected_pb.limit.value = limit - iterator.num_results + expected_pb._pb.limit.value = limit - iterator.num_results self.assertEqual(pb, expected_pb) def test__build_protobuf_all_values_except_start_and_end_cursor(self): # this test and the previous (all_values_except_start_offset) # test mutually exclusive states; the offset is ignored # if a start_cursor is supplied - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 from google.cloud.datastore.query import Query client = _Client(None) @@ -483,11 +483,11 @@ def test__build_protobuf_all_values_except_start_and_end_cursor(self): pb = iterator._build_protobuf() expected_pb = query_pb2.Query(offset=offset - iterator._skipped_results) - expected_pb.limit.value = limit - iterator.num_results + expected_pb._pb.limit.value = limit - iterator.num_results self.assertEqual(pb, expected_pb) def test__process_query_results(self): - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 iterator = self._make_one(None, None, end_cursor="abcd") self.assertIsNotNone(iterator._end_cursor) @@ -496,7 +496,7 @@ def test__process_query_results(self): cursor_as_bytes = b"\x9ai\xe7" cursor = b"mmnn" skipped_results = 4 - more_results_enum = query_pb2.QueryResultBatch.NOT_FINISHED + more_results_enum = query_pb2.QueryResultBatch.MoreResultsType.NOT_FINISHED response_pb = _make_query_response( entity_pbs, cursor_as_bytes, more_results_enum, skipped_results ) @@ -508,7 +508,7 @@ def test__process_query_results(self): self.assertTrue(iterator._more_results) def test__process_query_results_done(self): - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 iterator = self._make_one(None, None, end_cursor="abcd") self.assertIsNotNone(iterator._end_cursor) @@ -516,7 +516,7 @@ def test__process_query_results_done(self): entity_pbs = [_make_entity("World", 1234, "PROJECT")] cursor_as_bytes = b"\x9ai\xe7" skipped_results = 44 - more_results_enum = query_pb2.QueryResultBatch.NO_MORE_RESULTS + more_results_enum = query_pb2.QueryResultBatch.MoreResultsType.NO_MORE_RESULTS response_pb = _make_query_response( entity_pbs, cursor_as_bytes, more_results_enum, skipped_results ) @@ -536,12 +536,12 @@ def test__process_query_results_bad_enum(self): def _next_page_helper(self, txn_id=None, retry=None, timeout=None): from google.api_core import page_iterator - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import entity_pb2 - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 from google.cloud.datastore.query import Query - more_enum = query_pb2.QueryResultBatch.NOT_FINISHED + more_enum = query_pb2.QueryResultBatch.MoreResultsType.NOT_FINISHED result = _make_query_response([], b"", more_enum, 0) project = "prujekt" ds_api = _make_datastore_api(result) @@ -574,7 +574,13 @@ def _next_page_helper(self, txn_id=None, retry=None, timeout=None): read_options = datastore_pb2.ReadOptions(transaction=txn_id) empty_query = query_pb2.Query() ds_api.run_query.assert_called_once_with( - project, partition_id, read_options, query=empty_query, **kwargs + request={ + "project_id": project, + "partition_id": partition_id, + "read_options": read_options, + "query": empty_query, + }, + **kwargs, ) def test__next_page(self): @@ -611,7 +617,8 @@ def _call_fut(self, iterator, entity_pb): return _item_to_entity(iterator, entity_pb) def test_it(self): - entity_pb = mock.sentinel.entity_pb + entity_pb = mock.Mock() + entity_pb._pb = mock.sentinel.entity_pb patch = mock.patch("google.cloud.datastore.helpers.entity_from_protobuf") with patch as entity_from_protobuf: result = self._call_fut(None, entity_pb) @@ -627,7 +634,7 @@ def _call_fut(self, query): return _pb_from_query(query) def test_empty(self): - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 pb = self._call_fut(_Query()) self.assertEqual(list(pb.projection), []) @@ -636,11 +643,13 @@ def test_empty(self): self.assertEqual(list(pb.distinct_on), []) self.assertEqual(pb.filter.property_filter.property.name, "") cfilter = pb.filter.composite_filter - self.assertEqual(cfilter.op, query_pb2.CompositeFilter.OPERATOR_UNSPECIFIED) + self.assertEqual( + cfilter.op, query_pb2.CompositeFilter.Operator.OPERATOR_UNSPECIFIED + ) self.assertEqual(list(cfilter.filters), []) self.assertEqual(pb.start_cursor, b"") self.assertEqual(pb.end_cursor, b"") - self.assertEqual(pb.limit.value, 0) + self.assertEqual(pb._pb.limit.value, 0) self.assertEqual(pb.offset, 0) def test_projection(self): @@ -655,12 +664,12 @@ def test_kind(self): def test_ancestor(self): from google.cloud.datastore.key import Key - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 ancestor = Key("Ancestor", 123, project="PROJECT") pb = self._call_fut(_Query(ancestor=ancestor)) cfilter = pb.filter.composite_filter - self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND) + self.assertEqual(cfilter.op, query_pb2.CompositeFilter.Operator.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, "__key__") @@ -668,28 +677,28 @@ def test_ancestor(self): self.assertEqual(pfilter.value.key_value, ancestor_pb) def test_filter(self): - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 - query = _Query(filters=[("name", "=", u"John")]) - query.OPERATORS = {"=": query_pb2.PropertyFilter.EQUAL} + query = _Query(filters=[("name", "=", "John")]) + query.OPERATORS = {"=": query_pb2.PropertyFilter.Operator.EQUAL} pb = self._call_fut(query) cfilter = pb.filter.composite_filter - self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND) + self.assertEqual(cfilter.op, query_pb2.CompositeFilter.Operator.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, "name") - self.assertEqual(pfilter.value.string_value, u"John") + self.assertEqual(pfilter.value.string_value, "John") def test_filter_key(self): from google.cloud.datastore.key import Key - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 key = Key("Kind", 123, project="PROJECT") query = _Query(filters=[("__key__", "=", key)]) - query.OPERATORS = {"=": query_pb2.PropertyFilter.EQUAL} + query.OPERATORS = {"=": query_pb2.PropertyFilter.Operator.EQUAL} pb = self._call_fut(query) cfilter = pb.filter.composite_filter - self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND) + self.assertEqual(cfilter.op, query_pb2.CompositeFilter.Operator.AND) self.assertEqual(len(cfilter.filters), 1) pfilter = cfilter.filters[0].property_filter self.assertEqual(pfilter.property.name, "__key__") @@ -697,16 +706,16 @@ def test_filter_key(self): self.assertEqual(pfilter.value.key_value, key_pb) def test_order(self): - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 pb = self._call_fut(_Query(order=["a", "-b", "c"])) self.assertEqual([item.property.name for item in pb.order], ["a", "b", "c"]) self.assertEqual( [item.direction for item in pb.order], [ - query_pb2.PropertyOrder.ASCENDING, - query_pb2.PropertyOrder.DESCENDING, - query_pb2.PropertyOrder.ASCENDING, + query_pb2.PropertyOrder.Direction.ASCENDING, + query_pb2.PropertyOrder.Direction.DESCENDING, + query_pb2.PropertyOrder.Direction.ASCENDING, ], ) @@ -752,11 +761,11 @@ def current_transaction(self): def _make_entity(kind, id_, project): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 key = entity_pb2.Key() key.partition_id.project_id = project - elem = key.path.add() + elem = key.path._pb.add() elem.kind = kind elem.id = id_ return entity_pb2.Entity(key=key) @@ -765,8 +774,8 @@ def _make_entity(kind, id_, project): def _make_query_response( entity_pbs, cursor_as_bytes, more_results_enum, skipped_results ): - from google.cloud.datastore_v1.proto import datastore_pb2 - from google.cloud.datastore_v1.proto import query_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 + from google.cloud.datastore_v1.types import query as query_pb2 return datastore_pb2.RunQueryResponse( batch=query_pb2.QueryResultBatch( diff --git a/tests/unit/test_transaction.py b/tests/unit/test_transaction.py index b285db1f..1bc355cc 100644 --- a/tests/unit/test_transaction.py +++ b/tests/unit/test_transaction.py @@ -47,7 +47,7 @@ def test_ctor_defaults(self): self.assertEqual(len(xact._partial_key_entities), 0) def test_current(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" id_ = 678 @@ -77,12 +77,19 @@ def test_current(self): ds_api.rollback.assert_not_called() commit_method = ds_api.commit self.assertEqual(commit_method.call_count, 2) - mode = datastore_pb2.CommitRequest.TRANSACTIONAL - commit_method.assert_called_with(project, mode, [], transaction=id_) + mode = datastore_pb2.CommitRequest.Mode.TRANSACTIONAL + commit_method.assert_called_with( + request={ + "project_id": project, + "mode": mode, + "mutations": [], + "transaction": id_, + } + ) begin_txn = ds_api.begin_transaction self.assertEqual(begin_txn.call_count, 2) - begin_txn.assert_called_with(project) + begin_txn.assert_called_with(request={"project_id": project}) def test_begin(self): project = "PROJECT" @@ -92,7 +99,9 @@ def test_begin(self): xact = self._make_one(client) xact.begin() self.assertEqual(xact.id, id_) - ds_api.begin_transaction.assert_called_once_with(project) + ds_api.begin_transaction.assert_called_once_with( + request={"project_id": project} + ) def test_begin_w_retry_w_timeout(self): project = "PROJECT" @@ -108,7 +117,7 @@ def test_begin_w_retry_w_timeout(self): self.assertEqual(xact.id, id_) ds_api.begin_transaction.assert_called_once_with( - project, retry=retry, timeout=timeout + request={"project_id": project}, retry=retry, timeout=timeout ) def test_begin_tombstoned(self): @@ -119,10 +128,14 @@ def test_begin_tombstoned(self): xact = self._make_one(client) xact.begin() self.assertEqual(xact.id, id_) - ds_api.begin_transaction.assert_called_once_with(project) + ds_api.begin_transaction.assert_called_once_with( + request={"project_id": project} + ) xact.rollback() - client._datastore_api.rollback.assert_called_once_with(project, id_) + client._datastore_api.rollback.assert_called_once_with( + request={"project_id": project, "transaction": id_} + ) self.assertIsNone(xact.id) self.assertRaises(ValueError, xact.begin) @@ -139,7 +152,9 @@ def test_begin_w_begin_transaction_failure(self): xact.begin() self.assertIsNone(xact.id) - ds_api.begin_transaction.assert_called_once_with(project) + ds_api.begin_transaction.assert_called_once_with( + request={"project_id": project} + ) def test_rollback(self): project = "PROJECT" @@ -152,7 +167,9 @@ def test_rollback(self): xact.rollback() self.assertIsNone(xact.id) - ds_api.rollback.assert_called_once_with(project, id_) + ds_api.rollback.assert_called_once_with( + request={"project_id": project, "transaction": id_} + ) def test_rollback_w_retry_w_timeout(self): project = "PROJECT" @@ -169,15 +186,17 @@ def test_rollback_w_retry_w_timeout(self): self.assertIsNone(xact.id) ds_api.rollback.assert_called_once_with( - project, id_, retry=retry, timeout=timeout + request={"project_id": project, "transaction": id_}, + retry=retry, + timeout=timeout, ) def test_commit_no_partial_keys(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" id_ = 1002930 - mode = datastore_pb2.CommitRequest.TRANSACTIONAL + mode = datastore_pb2.CommitRequest.Mode.TRANSACTIONAL ds_api = _make_datastore_api(xact_id=id_) client = _Client(project, datastore_api=ds_api) @@ -185,16 +204,23 @@ def test_commit_no_partial_keys(self): xact.begin() xact.commit() - ds_api.commit.assert_called_once_with(project, mode, [], transaction=id_) + ds_api.commit.assert_called_once_with( + request={ + "project_id": project, + "mode": mode, + "mutations": [], + "transaction": id_, + } + ) self.assertIsNone(xact.id) def test_commit_w_partial_keys_w_retry_w_timeout(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" kind = "KIND" id1 = 123 - mode = datastore_pb2.CommitRequest.TRANSACTIONAL + mode = datastore_pb2.CommitRequest.Mode.TRANSACTIONAL key = _make_key(kind, id1, project) id2 = 234 retry = mock.Mock() @@ -210,10 +236,12 @@ def test_commit_w_partial_keys_w_retry_w_timeout(self): xact.commit(retry=retry, timeout=timeout) ds_api.commit.assert_called_once_with( - project, - mode, - xact.mutations, - transaction=id2, + request={ + "project_id": project, + "mode": mode, + "mutations": xact.mutations, + "transaction": id2, + }, retry=retry, timeout=timeout, ) @@ -221,7 +249,7 @@ def test_commit_w_partial_keys_w_retry_w_timeout(self): self.assertEqual(entity.key.path, [{"kind": kind, "id": id1}]) def test_context_manager_no_raise(self): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 project = "PROJECT" id_ = 912830 @@ -230,12 +258,20 @@ def test_context_manager_no_raise(self): xact = self._make_one(client) with xact: self.assertEqual(xact.id, id_) - ds_api.begin_transaction.assert_called_once_with(project) + ds_api.begin_transaction.assert_called_once_with( + request={"project_id": project} + ) - mode = datastore_pb2.CommitRequest.TRANSACTIONAL + mode = datastore_pb2.CommitRequest.Mode.TRANSACTIONAL client._datastore_api.commit.assert_called_once_with( - project, mode, [], transaction=id_ + request={ + "project_id": project, + "mode": mode, + "mutations": [], + "transaction": id_, + }, ) + self.assertIsNone(xact.id) self.assertEqual(ds_api.begin_transaction.call_count, 1) @@ -252,11 +288,15 @@ class Foo(Exception): try: with xact: self.assertEqual(xact.id, id_) - ds_api.begin_transaction.assert_called_once_with(project) + ds_api.begin_transaction.assert_called_once_with( + request={"project_id": project} + ) raise Foo() except Foo: self.assertIsNone(xact.id) - client._datastore_api.rollback.assert_called_once_with(project, id_) + client._datastore_api.rollback.assert_called_once_with( + request={"project_id": project, "transaction": id_} + ) client._datastore_api.commit.assert_not_called() self.assertIsNone(xact.id) @@ -286,11 +326,11 @@ def test_put_read_only(self): def _make_key(kind, id_, project): - from google.cloud.datastore_v1.proto import entity_pb2 + from google.cloud.datastore_v1.types import entity as entity_pb2 key = entity_pb2.Key() key.partition_id.project_id = project - elem = key.path.add() + elem = key._pb.path.add() elem.kind = kind elem.id = id_ return key @@ -340,9 +380,9 @@ def __exit__(self, *args): def _make_commit_response(*keys): - from google.cloud.datastore_v1.proto import datastore_pb2 + from google.cloud.datastore_v1.types import datastore as datastore_pb2 - mutation_results = [datastore_pb2.MutationResult(key=key) for key in keys] + mutation_results = [datastore_pb2.MutationResult(key=key)._pb for key in keys] return datastore_pb2.CommitResponse(mutation_results=mutation_results)