google-cloud-spanner 3.55.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google/cloud/spanner.py +47 -0
- google/cloud/spanner_admin_database_v1/__init__.py +146 -0
- google/cloud/spanner_admin_database_v1/gapic_metadata.json +418 -0
- google/cloud/spanner_admin_database_v1/gapic_version.py +16 -0
- google/cloud/spanner_admin_database_v1/py.typed +2 -0
- google/cloud/spanner_admin_database_v1/services/__init__.py +15 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/__init__.py +22 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/async_client.py +4097 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/client.py +4602 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/pagers.py +989 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/transports/__init__.py +38 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/transports/base.py +820 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/transports/grpc.py +1303 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/transports/grpc_asyncio.py +1688 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/transports/rest.py +6512 -0
- google/cloud/spanner_admin_database_v1/services/database_admin/transports/rest_base.py +1650 -0
- google/cloud/spanner_admin_database_v1/types/__init__.py +144 -0
- google/cloud/spanner_admin_database_v1/types/backup.py +1106 -0
- google/cloud/spanner_admin_database_v1/types/backup_schedule.py +369 -0
- google/cloud/spanner_admin_database_v1/types/common.py +180 -0
- google/cloud/spanner_admin_database_v1/types/spanner_database_admin.py +1303 -0
- google/cloud/spanner_admin_instance_v1/__init__.py +110 -0
- google/cloud/spanner_admin_instance_v1/gapic_metadata.json +343 -0
- google/cloud/spanner_admin_instance_v1/gapic_version.py +16 -0
- google/cloud/spanner_admin_instance_v1/py.typed +2 -0
- google/cloud/spanner_admin_instance_v1/services/__init__.py +15 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/__init__.py +22 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/async_client.py +3466 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/client.py +3881 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/pagers.py +856 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/__init__.py +38 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/base.py +545 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/grpc.py +1347 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/grpc_asyncio.py +1539 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/rest.py +4834 -0
- google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/rest_base.py +1198 -0
- google/cloud/spanner_admin_instance_v1/types/__init__.py +104 -0
- google/cloud/spanner_admin_instance_v1/types/common.py +99 -0
- google/cloud/spanner_admin_instance_v1/types/spanner_instance_admin.py +2375 -0
- google/cloud/spanner_dbapi/__init__.py +93 -0
- google/cloud/spanner_dbapi/_helpers.py +113 -0
- google/cloud/spanner_dbapi/batch_dml_executor.py +135 -0
- google/cloud/spanner_dbapi/checksum.py +80 -0
- google/cloud/spanner_dbapi/client_side_statement_executor.py +140 -0
- google/cloud/spanner_dbapi/client_side_statement_parser.py +106 -0
- google/cloud/spanner_dbapi/connection.py +818 -0
- google/cloud/spanner_dbapi/cursor.py +609 -0
- google/cloud/spanner_dbapi/exceptions.py +172 -0
- google/cloud/spanner_dbapi/parse_utils.py +392 -0
- google/cloud/spanner_dbapi/parsed_statement.py +63 -0
- google/cloud/spanner_dbapi/parser.py +258 -0
- google/cloud/spanner_dbapi/partition_helper.py +41 -0
- google/cloud/spanner_dbapi/transaction_helper.py +294 -0
- google/cloud/spanner_dbapi/types.py +106 -0
- google/cloud/spanner_dbapi/utils.py +147 -0
- google/cloud/spanner_dbapi/version.py +20 -0
- google/cloud/spanner_v1/__init__.py +154 -0
- google/cloud/spanner_v1/_helpers.py +751 -0
- google/cloud/spanner_v1/_opentelemetry_tracing.py +165 -0
- google/cloud/spanner_v1/backup.py +397 -0
- google/cloud/spanner_v1/batch.py +433 -0
- google/cloud/spanner_v1/client.py +538 -0
- google/cloud/spanner_v1/data_types.py +350 -0
- google/cloud/spanner_v1/database.py +1968 -0
- google/cloud/spanner_v1/database_sessions_manager.py +249 -0
- google/cloud/spanner_v1/gapic_metadata.json +268 -0
- google/cloud/spanner_v1/gapic_version.py +16 -0
- google/cloud/spanner_v1/instance.py +735 -0
- google/cloud/spanner_v1/keyset.py +193 -0
- google/cloud/spanner_v1/merged_result_set.py +146 -0
- google/cloud/spanner_v1/metrics/constants.py +71 -0
- google/cloud/spanner_v1/metrics/metrics_capture.py +75 -0
- google/cloud/spanner_v1/metrics/metrics_exporter.py +384 -0
- google/cloud/spanner_v1/metrics/metrics_interceptor.py +156 -0
- google/cloud/spanner_v1/metrics/metrics_tracer.py +588 -0
- google/cloud/spanner_v1/metrics/metrics_tracer_factory.py +328 -0
- google/cloud/spanner_v1/metrics/spanner_metrics_tracer_factory.py +172 -0
- google/cloud/spanner_v1/param_types.py +110 -0
- google/cloud/spanner_v1/pool.py +813 -0
- google/cloud/spanner_v1/py.typed +2 -0
- google/cloud/spanner_v1/request_id_header.py +64 -0
- google/cloud/spanner_v1/services/__init__.py +15 -0
- google/cloud/spanner_v1/services/spanner/__init__.py +22 -0
- google/cloud/spanner_v1/services/spanner/async_client.py +2205 -0
- google/cloud/spanner_v1/services/spanner/client.py +2624 -0
- google/cloud/spanner_v1/services/spanner/pagers.py +196 -0
- google/cloud/spanner_v1/services/spanner/transports/__init__.py +38 -0
- google/cloud/spanner_v1/services/spanner/transports/base.py +520 -0
- google/cloud/spanner_v1/services/spanner/transports/grpc.py +911 -0
- google/cloud/spanner_v1/services/spanner/transports/grpc_asyncio.py +1144 -0
- google/cloud/spanner_v1/services/spanner/transports/rest.py +3468 -0
- google/cloud/spanner_v1/services/spanner/transports/rest_base.py +981 -0
- google/cloud/spanner_v1/session.py +631 -0
- google/cloud/spanner_v1/session_options.py +133 -0
- google/cloud/spanner_v1/snapshot.py +1057 -0
- google/cloud/spanner_v1/streamed.py +402 -0
- google/cloud/spanner_v1/table.py +181 -0
- google/cloud/spanner_v1/testing/__init__.py +0 -0
- google/cloud/spanner_v1/testing/database_test.py +121 -0
- google/cloud/spanner_v1/testing/interceptors.py +118 -0
- google/cloud/spanner_v1/testing/mock_database_admin.py +38 -0
- google/cloud/spanner_v1/testing/mock_spanner.py +261 -0
- google/cloud/spanner_v1/testing/spanner_database_admin_pb2_grpc.py +1267 -0
- google/cloud/spanner_v1/testing/spanner_pb2_grpc.py +882 -0
- google/cloud/spanner_v1/transaction.py +747 -0
- google/cloud/spanner_v1/types/__init__.py +118 -0
- google/cloud/spanner_v1/types/commit_response.py +94 -0
- google/cloud/spanner_v1/types/keys.py +248 -0
- google/cloud/spanner_v1/types/mutation.py +201 -0
- google/cloud/spanner_v1/types/query_plan.py +220 -0
- google/cloud/spanner_v1/types/result_set.py +379 -0
- google/cloud/spanner_v1/types/spanner.py +1815 -0
- google/cloud/spanner_v1/types/transaction.py +818 -0
- google/cloud/spanner_v1/types/type.py +288 -0
- google_cloud_spanner-3.55.0.dist-info/LICENSE +202 -0
- google_cloud_spanner-3.55.0.dist-info/METADATA +318 -0
- google_cloud_spanner-3.55.0.dist-info/RECORD +119 -0
- google_cloud_spanner-3.55.0.dist-info/WHEEL +5 -0
- google_cloud_spanner-3.55.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
# Copyright 2016 Google LLC All rights reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""User friendly container for Cloud Spanner Instance."""
|
|
16
|
+
|
|
17
|
+
import google.api_core.operation
|
|
18
|
+
from google.api_core.exceptions import InvalidArgument
|
|
19
|
+
import re
|
|
20
|
+
import typing
|
|
21
|
+
|
|
22
|
+
from google.protobuf.empty_pb2 import Empty
|
|
23
|
+
from google.protobuf.field_mask_pb2 import FieldMask
|
|
24
|
+
from google.cloud.exceptions import NotFound
|
|
25
|
+
|
|
26
|
+
from google.cloud.spanner_admin_instance_v1 import Instance as InstancePB
|
|
27
|
+
from google.cloud.spanner_admin_database_v1.types import backup
|
|
28
|
+
from google.cloud.spanner_admin_database_v1.types import spanner_database_admin
|
|
29
|
+
from google.cloud.spanner_admin_database_v1 import DatabaseDialect
|
|
30
|
+
from google.cloud.spanner_admin_database_v1 import ListBackupsRequest
|
|
31
|
+
from google.cloud.spanner_admin_database_v1 import ListBackupOperationsRequest
|
|
32
|
+
from google.cloud.spanner_admin_database_v1 import ListDatabasesRequest
|
|
33
|
+
from google.cloud.spanner_admin_database_v1 import ListDatabaseOperationsRequest
|
|
34
|
+
from google.cloud.spanner_v1._helpers import _metadata_with_prefix
|
|
35
|
+
from google.cloud.spanner_v1.backup import Backup
|
|
36
|
+
from google.cloud.spanner_v1.database import Database
|
|
37
|
+
from google.cloud.spanner_v1.testing.database_test import TestDatabase
|
|
38
|
+
|
|
39
|
+
_INSTANCE_NAME_RE = re.compile(
|
|
40
|
+
r"^projects/(?P<project>[^/]+)/" r"instances/(?P<instance_id>[a-z][-a-z0-9]*)$"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
DEFAULT_NODE_COUNT = 1
|
|
44
|
+
PROCESSING_UNITS_PER_NODE = 1000
|
|
45
|
+
|
|
46
|
+
_OPERATION_METADATA_MESSAGES: typing.Tuple = (
|
|
47
|
+
backup.Backup,
|
|
48
|
+
backup.CreateBackupMetadata,
|
|
49
|
+
backup.CopyBackupMetadata,
|
|
50
|
+
spanner_database_admin.CreateDatabaseMetadata,
|
|
51
|
+
spanner_database_admin.Database,
|
|
52
|
+
spanner_database_admin.OptimizeRestoredDatabaseMetadata,
|
|
53
|
+
spanner_database_admin.RestoreDatabaseMetadata,
|
|
54
|
+
spanner_database_admin.UpdateDatabaseDdlMetadata,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
_OPERATION_METADATA_TYPES = {
|
|
58
|
+
"type.googleapis.com/{}".format(message._meta.full_name): message
|
|
59
|
+
for message in _OPERATION_METADATA_MESSAGES
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
_OPERATION_RESPONSE_TYPES = {
|
|
63
|
+
backup.CreateBackupMetadata: backup.Backup,
|
|
64
|
+
backup.CopyBackupMetadata: backup.Backup,
|
|
65
|
+
spanner_database_admin.CreateDatabaseMetadata: spanner_database_admin.Database,
|
|
66
|
+
spanner_database_admin.OptimizeRestoredDatabaseMetadata: spanner_database_admin.Database,
|
|
67
|
+
spanner_database_admin.RestoreDatabaseMetadata: spanner_database_admin.Database,
|
|
68
|
+
spanner_database_admin.UpdateDatabaseDdlMetadata: Empty,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _type_string_to_type_pb(type_string):
|
|
73
|
+
return _OPERATION_METADATA_TYPES.get(type_string, Empty)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class Instance(object):
|
|
77
|
+
"""Representation of a Cloud Spanner Instance.
|
|
78
|
+
|
|
79
|
+
We can use a :class:`Instance` to:
|
|
80
|
+
|
|
81
|
+
* :meth:`reload` itself
|
|
82
|
+
* :meth:`create` itself
|
|
83
|
+
* :meth:`update` itself
|
|
84
|
+
* :meth:`delete` itself
|
|
85
|
+
|
|
86
|
+
:type instance_id: str
|
|
87
|
+
:param instance_id: The ID of the instance.
|
|
88
|
+
|
|
89
|
+
:type client: :class:`~google.cloud.spanner_v1.client.Client`
|
|
90
|
+
:param client: The client that owns the instance. Provides
|
|
91
|
+
authorization and a project ID.
|
|
92
|
+
|
|
93
|
+
:type configuration_name: str
|
|
94
|
+
:param configuration_name: Name of the instance configuration defining
|
|
95
|
+
how the instance will be created.
|
|
96
|
+
Required for instances which do not yet exist.
|
|
97
|
+
|
|
98
|
+
:type node_count: int
|
|
99
|
+
:param node_count: (Optional) Number of nodes allocated to the instance.
|
|
100
|
+
|
|
101
|
+
:type processing_units: int
|
|
102
|
+
:param processing_units: (Optional) The number of processing units
|
|
103
|
+
allocated to this instance.
|
|
104
|
+
|
|
105
|
+
:type display_name: str
|
|
106
|
+
:param display_name: (Optional) The display name for the instance in the
|
|
107
|
+
Cloud Console UI. (Must be between 4 and 30
|
|
108
|
+
characters.) If this value is not set in the
|
|
109
|
+
constructor, will fall back to the instance ID.
|
|
110
|
+
|
|
111
|
+
:type labels: dict (str -> str) or None
|
|
112
|
+
:param labels: (Optional) User-assigned labels for this instance.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
def __init__(
|
|
116
|
+
self,
|
|
117
|
+
instance_id,
|
|
118
|
+
client,
|
|
119
|
+
configuration_name=None,
|
|
120
|
+
node_count=None,
|
|
121
|
+
display_name=None,
|
|
122
|
+
emulator_host=None,
|
|
123
|
+
labels=None,
|
|
124
|
+
processing_units=None,
|
|
125
|
+
):
|
|
126
|
+
self.instance_id = instance_id
|
|
127
|
+
self._client = client
|
|
128
|
+
self.configuration_name = configuration_name
|
|
129
|
+
if node_count is not None and processing_units is not None:
|
|
130
|
+
if processing_units != node_count * PROCESSING_UNITS_PER_NODE:
|
|
131
|
+
raise InvalidArgument(
|
|
132
|
+
"Only one of node count and processing units can be set."
|
|
133
|
+
)
|
|
134
|
+
if node_count is None and processing_units is None:
|
|
135
|
+
self._node_count = DEFAULT_NODE_COUNT
|
|
136
|
+
self._processing_units = DEFAULT_NODE_COUNT * PROCESSING_UNITS_PER_NODE
|
|
137
|
+
elif node_count is not None:
|
|
138
|
+
self._node_count = node_count
|
|
139
|
+
self._processing_units = node_count * PROCESSING_UNITS_PER_NODE
|
|
140
|
+
else:
|
|
141
|
+
self._processing_units = processing_units
|
|
142
|
+
self._node_count = processing_units // PROCESSING_UNITS_PER_NODE
|
|
143
|
+
self.display_name = display_name or instance_id
|
|
144
|
+
self.emulator_host = emulator_host
|
|
145
|
+
if labels is None:
|
|
146
|
+
labels = {}
|
|
147
|
+
self.labels = labels
|
|
148
|
+
|
|
149
|
+
def _update_from_pb(self, instance_pb):
|
|
150
|
+
"""Refresh self from the server-provided protobuf.
|
|
151
|
+
|
|
152
|
+
Helper for :meth:`from_pb` and :meth:`reload`.
|
|
153
|
+
"""
|
|
154
|
+
if not instance_pb.display_name: # Simple field (string)
|
|
155
|
+
raise ValueError("Instance protobuf does not contain display_name")
|
|
156
|
+
self.display_name = instance_pb.display_name
|
|
157
|
+
self.configuration_name = instance_pb.config
|
|
158
|
+
self._node_count = instance_pb.node_count
|
|
159
|
+
self._processing_units = instance_pb.processing_units
|
|
160
|
+
self.labels = instance_pb.labels
|
|
161
|
+
|
|
162
|
+
@classmethod
|
|
163
|
+
def from_pb(cls, instance_pb, client):
|
|
164
|
+
"""Creates an instance from a protobuf.
|
|
165
|
+
|
|
166
|
+
:type instance_pb:
|
|
167
|
+
:class:`~google.spanner.v2.spanner_instance_admin_pb2.Instance`
|
|
168
|
+
:param instance_pb: A instance protobuf object.
|
|
169
|
+
|
|
170
|
+
:type client: :class:`~google.cloud.spanner_v1.client.Client`
|
|
171
|
+
:param client: The client that owns the instance.
|
|
172
|
+
|
|
173
|
+
:rtype: :class:`Instance`
|
|
174
|
+
:returns: The instance parsed from the protobuf response.
|
|
175
|
+
:raises ValueError:
|
|
176
|
+
if the instance name does not match
|
|
177
|
+
``projects/{project}/instances/{instance_id}`` or if the parsed
|
|
178
|
+
project ID does not match the project ID on the client.
|
|
179
|
+
"""
|
|
180
|
+
match = _INSTANCE_NAME_RE.match(instance_pb.name)
|
|
181
|
+
if match is None:
|
|
182
|
+
raise ValueError(
|
|
183
|
+
"Instance protobuf name was not in the " "expected format.",
|
|
184
|
+
instance_pb.name,
|
|
185
|
+
)
|
|
186
|
+
if match.group("project") != client.project:
|
|
187
|
+
raise ValueError(
|
|
188
|
+
"Project ID on instance does not match the " "project ID on the client"
|
|
189
|
+
)
|
|
190
|
+
instance_id = match.group("instance_id")
|
|
191
|
+
configuration_name = instance_pb.config
|
|
192
|
+
|
|
193
|
+
result = cls(instance_id, client, configuration_name)
|
|
194
|
+
result._update_from_pb(instance_pb)
|
|
195
|
+
return result
|
|
196
|
+
|
|
197
|
+
@property
|
|
198
|
+
def name(self):
|
|
199
|
+
"""Instance name used in requests.
|
|
200
|
+
|
|
201
|
+
.. note::
|
|
202
|
+
|
|
203
|
+
This property will not change if ``instance_id`` does not,
|
|
204
|
+
but the return value is not cached.
|
|
205
|
+
|
|
206
|
+
The instance name is of the form
|
|
207
|
+
|
|
208
|
+
``"projects/{project}/instances/{instance_id}"``
|
|
209
|
+
|
|
210
|
+
:rtype: str
|
|
211
|
+
:returns: The instance name.
|
|
212
|
+
"""
|
|
213
|
+
return self._client.project_name + "/instances/" + self.instance_id
|
|
214
|
+
|
|
215
|
+
@property
|
|
216
|
+
def processing_units(self):
|
|
217
|
+
"""Processing units used in requests.
|
|
218
|
+
|
|
219
|
+
:rtype: int
|
|
220
|
+
:returns: The number of processing units allocated to this instance.
|
|
221
|
+
"""
|
|
222
|
+
return self._processing_units
|
|
223
|
+
|
|
224
|
+
@processing_units.setter
|
|
225
|
+
def processing_units(self, value):
|
|
226
|
+
"""Sets the processing units for requests. Affects node_count.
|
|
227
|
+
|
|
228
|
+
:param value: The number of processing units allocated to this instance.
|
|
229
|
+
"""
|
|
230
|
+
self._processing_units = value
|
|
231
|
+
self._node_count = value // PROCESSING_UNITS_PER_NODE
|
|
232
|
+
|
|
233
|
+
@property
|
|
234
|
+
def node_count(self):
|
|
235
|
+
"""Node count used in requests.
|
|
236
|
+
|
|
237
|
+
:rtype: int
|
|
238
|
+
:returns:
|
|
239
|
+
The number of nodes in the instance's cluster;
|
|
240
|
+
used to set up the instance's cluster.
|
|
241
|
+
"""
|
|
242
|
+
return self._node_count
|
|
243
|
+
|
|
244
|
+
@node_count.setter
|
|
245
|
+
def node_count(self, value):
|
|
246
|
+
"""Sets the node count for requests. Affects processing_units.
|
|
247
|
+
|
|
248
|
+
:param value: The number of nodes in the instance's cluster.
|
|
249
|
+
"""
|
|
250
|
+
self._node_count = value
|
|
251
|
+
self._processing_units = value * PROCESSING_UNITS_PER_NODE
|
|
252
|
+
|
|
253
|
+
def __eq__(self, other):
|
|
254
|
+
if not isinstance(other, self.__class__):
|
|
255
|
+
return NotImplemented
|
|
256
|
+
# NOTE: This does not compare the configuration values, such as
|
|
257
|
+
# the display_name. Instead, it only compares
|
|
258
|
+
# identifying values instance ID and client. This is
|
|
259
|
+
# intentional, since the same instance can be in different states
|
|
260
|
+
# if not synchronized. Instances with similar instance
|
|
261
|
+
# settings but different clients can't be used in the same way.
|
|
262
|
+
return other.instance_id == self.instance_id and other._client == self._client
|
|
263
|
+
|
|
264
|
+
def __ne__(self, other):
|
|
265
|
+
return not self == other
|
|
266
|
+
|
|
267
|
+
def copy(self):
|
|
268
|
+
"""Make a copy of this instance.
|
|
269
|
+
|
|
270
|
+
Copies the local data stored as simple types and copies the client
|
|
271
|
+
attached to this instance.
|
|
272
|
+
|
|
273
|
+
:rtype: :class:`~google.cloud.spanner_v1.instance.Instance`
|
|
274
|
+
:returns: A copy of the current instance.
|
|
275
|
+
"""
|
|
276
|
+
new_client = self._client.copy()
|
|
277
|
+
return self.__class__(
|
|
278
|
+
self.instance_id,
|
|
279
|
+
new_client,
|
|
280
|
+
self.configuration_name,
|
|
281
|
+
node_count=self._node_count,
|
|
282
|
+
processing_units=self._processing_units,
|
|
283
|
+
display_name=self.display_name,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
def create(self):
|
|
287
|
+
"""Create this instance.
|
|
288
|
+
|
|
289
|
+
See
|
|
290
|
+
https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.CreateInstance
|
|
291
|
+
|
|
292
|
+
.. note::
|
|
293
|
+
|
|
294
|
+
Uses the ``project`` and ``instance_id`` on the current
|
|
295
|
+
:class:`Instance` in addition to the ``display_name``.
|
|
296
|
+
To change them before creating, reset the values via
|
|
297
|
+
|
|
298
|
+
.. code:: python
|
|
299
|
+
|
|
300
|
+
instance.display_name = 'New display name'
|
|
301
|
+
instance.instance_id = 'i-changed-my-mind'
|
|
302
|
+
|
|
303
|
+
before calling :meth:`create`.
|
|
304
|
+
|
|
305
|
+
:rtype: :class:`~google.api_core.operation.Operation`
|
|
306
|
+
:returns: an operation instance
|
|
307
|
+
:raises Conflict: if the instance already exists
|
|
308
|
+
"""
|
|
309
|
+
api = self._client.instance_admin_api
|
|
310
|
+
instance_pb = InstancePB(
|
|
311
|
+
name=self.name,
|
|
312
|
+
config=self.configuration_name,
|
|
313
|
+
display_name=self.display_name,
|
|
314
|
+
processing_units=self._processing_units,
|
|
315
|
+
labels=self.labels,
|
|
316
|
+
)
|
|
317
|
+
metadata = _metadata_with_prefix(self.name)
|
|
318
|
+
|
|
319
|
+
future = api.create_instance(
|
|
320
|
+
parent=self._client.project_name,
|
|
321
|
+
instance_id=self.instance_id,
|
|
322
|
+
instance=instance_pb,
|
|
323
|
+
metadata=metadata,
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
return future
|
|
327
|
+
|
|
328
|
+
def exists(self):
|
|
329
|
+
"""Test whether this instance exists.
|
|
330
|
+
|
|
331
|
+
See
|
|
332
|
+
https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.GetInstanceConfig
|
|
333
|
+
|
|
334
|
+
:rtype: bool
|
|
335
|
+
:returns: True if the instance exists, else false
|
|
336
|
+
"""
|
|
337
|
+
api = self._client.instance_admin_api
|
|
338
|
+
metadata = _metadata_with_prefix(self.name)
|
|
339
|
+
|
|
340
|
+
try:
|
|
341
|
+
api.get_instance(name=self.name, metadata=metadata)
|
|
342
|
+
except NotFound:
|
|
343
|
+
return False
|
|
344
|
+
|
|
345
|
+
return True
|
|
346
|
+
|
|
347
|
+
def reload(self):
|
|
348
|
+
"""Reload the metadata for this instance.
|
|
349
|
+
|
|
350
|
+
See
|
|
351
|
+
https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.GetInstanceConfig
|
|
352
|
+
|
|
353
|
+
:raises NotFound: if the instance does not exist
|
|
354
|
+
"""
|
|
355
|
+
api = self._client.instance_admin_api
|
|
356
|
+
metadata = _metadata_with_prefix(self.name)
|
|
357
|
+
|
|
358
|
+
instance_pb = api.get_instance(name=self.name, metadata=metadata)
|
|
359
|
+
|
|
360
|
+
self._update_from_pb(instance_pb)
|
|
361
|
+
|
|
362
|
+
def update(self):
|
|
363
|
+
"""Update this instance.
|
|
364
|
+
|
|
365
|
+
See
|
|
366
|
+
https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.UpdateInstance
|
|
367
|
+
|
|
368
|
+
.. note::
|
|
369
|
+
|
|
370
|
+
Updates the ``display_name``, ``node_count``, ``processing_units``
|
|
371
|
+
and ``labels``. To change those values before updating, set them via
|
|
372
|
+
|
|
373
|
+
.. code:: python
|
|
374
|
+
|
|
375
|
+
instance.display_name = 'New display name'
|
|
376
|
+
instance.node_count = 5
|
|
377
|
+
|
|
378
|
+
before calling :meth:`update`.
|
|
379
|
+
|
|
380
|
+
:rtype: :class:`google.api_core.operation.Operation`
|
|
381
|
+
:returns: an operation instance
|
|
382
|
+
:raises NotFound: if the instance does not exist
|
|
383
|
+
"""
|
|
384
|
+
api = self._client.instance_admin_api
|
|
385
|
+
instance_pb = InstancePB(
|
|
386
|
+
name=self.name,
|
|
387
|
+
config=self.configuration_name,
|
|
388
|
+
display_name=self.display_name,
|
|
389
|
+
node_count=self._node_count,
|
|
390
|
+
processing_units=self._processing_units,
|
|
391
|
+
labels=self.labels,
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
# Always update only processing_units, not nodes
|
|
395
|
+
field_mask = FieldMask(
|
|
396
|
+
paths=["config", "display_name", "processing_units", "labels"]
|
|
397
|
+
)
|
|
398
|
+
metadata = _metadata_with_prefix(self.name)
|
|
399
|
+
|
|
400
|
+
future = api.update_instance(
|
|
401
|
+
instance=instance_pb, field_mask=field_mask, metadata=metadata
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
return future
|
|
405
|
+
|
|
406
|
+
def delete(self):
|
|
407
|
+
"""Mark an instance and all of its databases for permanent deletion.
|
|
408
|
+
|
|
409
|
+
See
|
|
410
|
+
https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.DeleteInstance
|
|
411
|
+
|
|
412
|
+
Immediately upon completion of the request:
|
|
413
|
+
|
|
414
|
+
* Billing will cease for all of the instance's reserved resources.
|
|
415
|
+
|
|
416
|
+
Soon afterward:
|
|
417
|
+
|
|
418
|
+
* The instance and all databases within the instance will be deleted.
|
|
419
|
+
All data in the databases will be permanently deleted.
|
|
420
|
+
"""
|
|
421
|
+
api = self._client.instance_admin_api
|
|
422
|
+
metadata = _metadata_with_prefix(self.name)
|
|
423
|
+
|
|
424
|
+
api.delete_instance(name=self.name, metadata=metadata)
|
|
425
|
+
|
|
426
|
+
def database(
|
|
427
|
+
self,
|
|
428
|
+
database_id,
|
|
429
|
+
ddl_statements=(),
|
|
430
|
+
pool=None,
|
|
431
|
+
logger=None,
|
|
432
|
+
encryption_config=None,
|
|
433
|
+
database_dialect=DatabaseDialect.DATABASE_DIALECT_UNSPECIFIED,
|
|
434
|
+
database_role=None,
|
|
435
|
+
enable_drop_protection=False,
|
|
436
|
+
# should be only set for tests if tests want to use interceptors
|
|
437
|
+
enable_interceptors_in_tests=False,
|
|
438
|
+
proto_descriptors=None,
|
|
439
|
+
):
|
|
440
|
+
"""Factory to create a database within this instance.
|
|
441
|
+
|
|
442
|
+
:type database_id: str
|
|
443
|
+
:param database_id: The ID of the database.
|
|
444
|
+
|
|
445
|
+
:type ddl_statements: list of string
|
|
446
|
+
:param ddl_statements: (Optional) DDL statements, excluding the
|
|
447
|
+
'CREATE DATABASE' statement.
|
|
448
|
+
|
|
449
|
+
:type pool: concrete subclass of
|
|
450
|
+
:class:`~google.cloud.spanner_v1.pool.AbstractSessionPool`.
|
|
451
|
+
:param pool: (Optional) session pool to be used by database.
|
|
452
|
+
|
|
453
|
+
:type logger: :class:`logging.Logger`
|
|
454
|
+
:param logger: (Optional) a custom logger that is used if `log_commit_stats`
|
|
455
|
+
is `True` to log commit statistics. If not passed, a logger
|
|
456
|
+
will be created when needed that will log the commit statistics
|
|
457
|
+
to stdout.
|
|
458
|
+
|
|
459
|
+
:type encryption_config:
|
|
460
|
+
:class:`~google.cloud.spanner_admin_database_v1.types.EncryptionConfig`
|
|
461
|
+
or :class:`~google.cloud.spanner_admin_database_v1.types.RestoreDatabaseEncryptionConfig`
|
|
462
|
+
or :class:`dict`
|
|
463
|
+
:param encryption_config:
|
|
464
|
+
(Optional) Encryption configuration for the database.
|
|
465
|
+
If a dict is provided, it must be of the same form as either of the protobuf
|
|
466
|
+
messages :class:`~google.cloud.spanner_admin_database_v1.types.EncryptionConfig`
|
|
467
|
+
or :class:`~google.cloud.spanner_admin_database_v1.types.RestoreDatabaseEncryptionConfig`
|
|
468
|
+
|
|
469
|
+
:type database_dialect:
|
|
470
|
+
:class:`~google.cloud.spanner_admin_database_v1.types.DatabaseDialect`
|
|
471
|
+
:param database_dialect:
|
|
472
|
+
(Optional) database dialect for the database
|
|
473
|
+
|
|
474
|
+
:type enable_drop_protection: boolean
|
|
475
|
+
:param enable_drop_protection: (Optional) Represents whether the database
|
|
476
|
+
has drop protection enabled or not.
|
|
477
|
+
|
|
478
|
+
:type enable_interceptors_in_tests: boolean
|
|
479
|
+
:param enable_interceptors_in_tests: (Optional) should only be set to True
|
|
480
|
+
for tests if the tests want to use interceptors.
|
|
481
|
+
|
|
482
|
+
:type proto_descriptors: bytes
|
|
483
|
+
:param proto_descriptors: (Optional) Proto descriptors used by CREATE/ALTER PROTO BUNDLE
|
|
484
|
+
statements in 'ddl_statements' above.
|
|
485
|
+
|
|
486
|
+
:rtype: :class:`~google.cloud.spanner_v1.database.Database`
|
|
487
|
+
:returns: a database owned by this instance.
|
|
488
|
+
"""
|
|
489
|
+
|
|
490
|
+
if not enable_interceptors_in_tests:
|
|
491
|
+
return Database(
|
|
492
|
+
database_id,
|
|
493
|
+
self,
|
|
494
|
+
ddl_statements=ddl_statements,
|
|
495
|
+
pool=pool,
|
|
496
|
+
logger=logger,
|
|
497
|
+
encryption_config=encryption_config,
|
|
498
|
+
database_dialect=database_dialect,
|
|
499
|
+
database_role=database_role,
|
|
500
|
+
enable_drop_protection=enable_drop_protection,
|
|
501
|
+
proto_descriptors=proto_descriptors,
|
|
502
|
+
)
|
|
503
|
+
else:
|
|
504
|
+
return TestDatabase(
|
|
505
|
+
database_id,
|
|
506
|
+
self,
|
|
507
|
+
ddl_statements=ddl_statements,
|
|
508
|
+
pool=pool,
|
|
509
|
+
logger=logger,
|
|
510
|
+
encryption_config=encryption_config,
|
|
511
|
+
database_dialect=database_dialect,
|
|
512
|
+
database_role=database_role,
|
|
513
|
+
enable_drop_protection=enable_drop_protection,
|
|
514
|
+
)
|
|
515
|
+
|
|
516
|
+
def list_databases(self, page_size=None):
|
|
517
|
+
"""List databases for the instance.
|
|
518
|
+
|
|
519
|
+
See
|
|
520
|
+
https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.ListDatabases
|
|
521
|
+
|
|
522
|
+
:type page_size: int
|
|
523
|
+
:param page_size:
|
|
524
|
+
Optional. The maximum number of databases in each page of results
|
|
525
|
+
from this request. Non-positive values are ignored. Defaults
|
|
526
|
+
to a sensible value set by the API.
|
|
527
|
+
|
|
528
|
+
:rtype: :class:`~google.api._ore.page_iterator.Iterator`
|
|
529
|
+
:returns:
|
|
530
|
+
Iterator of :class:`~google.cloud.spanner_admin_database_v1.types.Database`
|
|
531
|
+
resources within the current instance.
|
|
532
|
+
"""
|
|
533
|
+
metadata = _metadata_with_prefix(self.name)
|
|
534
|
+
request = ListDatabasesRequest(parent=self.name, page_size=page_size)
|
|
535
|
+
page_iter = self._client.database_admin_api.list_databases(
|
|
536
|
+
request=request, metadata=metadata
|
|
537
|
+
)
|
|
538
|
+
return page_iter
|
|
539
|
+
|
|
540
|
+
def backup(
|
|
541
|
+
self,
|
|
542
|
+
backup_id,
|
|
543
|
+
database="",
|
|
544
|
+
expire_time=None,
|
|
545
|
+
version_time=None,
|
|
546
|
+
encryption_config=None,
|
|
547
|
+
):
|
|
548
|
+
"""Factory to create a backup within this instance.
|
|
549
|
+
|
|
550
|
+
:type backup_id: str
|
|
551
|
+
:param backup_id: The ID of the backup.
|
|
552
|
+
|
|
553
|
+
:type database: :class:`~google.cloud.spanner_v1.database.Database`
|
|
554
|
+
:param database:
|
|
555
|
+
Optional. The database that will be used when creating the backup.
|
|
556
|
+
Required if the create method needs to be called.
|
|
557
|
+
|
|
558
|
+
:type expire_time: :class:`datetime.datetime`
|
|
559
|
+
:param expire_time:
|
|
560
|
+
Optional. The expire time that will be used when creating the backup.
|
|
561
|
+
Required if the create method needs to be called.
|
|
562
|
+
|
|
563
|
+
:type version_time: :class:`datetime.datetime`
|
|
564
|
+
:param version_time:
|
|
565
|
+
Optional. The version time that will be used to create the externally
|
|
566
|
+
consistent copy of the database. If not present, it is the same as
|
|
567
|
+
the `create_time` of the backup.
|
|
568
|
+
|
|
569
|
+
:type encryption_config:
|
|
570
|
+
:class:`~google.cloud.spanner_admin_database_v1.types.CreateBackupEncryptionConfig`
|
|
571
|
+
or :class:`dict`
|
|
572
|
+
:param encryption_config:
|
|
573
|
+
(Optional) Encryption configuration for the backup.
|
|
574
|
+
If a dict is provided, it must be of the same form as the protobuf
|
|
575
|
+
message :class:`~google.cloud.spanner_admin_database_v1.types.CreateBackupEncryptionConfig`
|
|
576
|
+
|
|
577
|
+
:rtype: :class:`~google.cloud.spanner_v1.backup.Backup`
|
|
578
|
+
:returns: a backup owned by this instance.
|
|
579
|
+
"""
|
|
580
|
+
try:
|
|
581
|
+
return Backup(
|
|
582
|
+
backup_id,
|
|
583
|
+
self,
|
|
584
|
+
database=database.name,
|
|
585
|
+
expire_time=expire_time,
|
|
586
|
+
version_time=version_time,
|
|
587
|
+
encryption_config=encryption_config,
|
|
588
|
+
)
|
|
589
|
+
except AttributeError:
|
|
590
|
+
return Backup(
|
|
591
|
+
backup_id,
|
|
592
|
+
self,
|
|
593
|
+
database=database,
|
|
594
|
+
expire_time=expire_time,
|
|
595
|
+
version_time=version_time,
|
|
596
|
+
encryption_config=encryption_config,
|
|
597
|
+
)
|
|
598
|
+
|
|
599
|
+
def copy_backup(
|
|
600
|
+
self,
|
|
601
|
+
backup_id,
|
|
602
|
+
source_backup,
|
|
603
|
+
expire_time=None,
|
|
604
|
+
encryption_config=None,
|
|
605
|
+
):
|
|
606
|
+
"""Factory to create a copy backup within this instance.
|
|
607
|
+
|
|
608
|
+
:type backup_id: str
|
|
609
|
+
:param backup_id: The ID of the backup copy.
|
|
610
|
+
:type source_backup: str
|
|
611
|
+
:param source_backup_id: The full path of the source backup to be copied.
|
|
612
|
+
:type expire_time: :class:`datetime.datetime`
|
|
613
|
+
:param expire_time:
|
|
614
|
+
Optional. The expire time that will be used when creating the copy backup.
|
|
615
|
+
Required if the create method needs to be called.
|
|
616
|
+
:type encryption_config:
|
|
617
|
+
:class:`~google.cloud.spanner_admin_database_v1.types.CopyBackupEncryptionConfig`
|
|
618
|
+
or :class:`dict`
|
|
619
|
+
:param encryption_config:
|
|
620
|
+
(Optional) Encryption configuration for the backup.
|
|
621
|
+
If a dict is provided, it must be of the same form as the protobuf
|
|
622
|
+
message :class:`~google.cloud.spanner_admin_database_v1.types.CopyBackupEncryptionConfig`
|
|
623
|
+
:rtype: :class:`~google.cloud.spanner_v1.backup.Backup`
|
|
624
|
+
:returns: a copy backup owned by this instance.
|
|
625
|
+
"""
|
|
626
|
+
return Backup(
|
|
627
|
+
backup_id,
|
|
628
|
+
self,
|
|
629
|
+
source_backup=source_backup,
|
|
630
|
+
expire_time=expire_time,
|
|
631
|
+
encryption_config=encryption_config,
|
|
632
|
+
)
|
|
633
|
+
|
|
634
|
+
def list_backups(self, filter_="", page_size=None):
|
|
635
|
+
"""List backups for the instance.
|
|
636
|
+
|
|
637
|
+
:type filter_: str
|
|
638
|
+
:param filter_:
|
|
639
|
+
Optional. A string specifying a filter for which backups to list.
|
|
640
|
+
|
|
641
|
+
:type page_size: int
|
|
642
|
+
:param page_size:
|
|
643
|
+
Optional. The maximum number of databases in each page of results
|
|
644
|
+
from this request. Non-positive values are ignored. Defaults to a
|
|
645
|
+
sensible value set by the API.
|
|
646
|
+
|
|
647
|
+
:rtype: :class:`~google.api_core.page_iterator.Iterator`
|
|
648
|
+
:returns:
|
|
649
|
+
Iterator of :class:`~google.cloud.spanner_admin_database_v1.types.Backup`
|
|
650
|
+
resources within the current instance.
|
|
651
|
+
"""
|
|
652
|
+
metadata = _metadata_with_prefix(self.name)
|
|
653
|
+
request = ListBackupsRequest(
|
|
654
|
+
parent=self.name,
|
|
655
|
+
filter=filter_,
|
|
656
|
+
page_size=page_size,
|
|
657
|
+
)
|
|
658
|
+
page_iter = self._client.database_admin_api.list_backups(
|
|
659
|
+
request=request, metadata=metadata
|
|
660
|
+
)
|
|
661
|
+
return page_iter
|
|
662
|
+
|
|
663
|
+
def list_backup_operations(self, filter_="", page_size=None):
|
|
664
|
+
"""List backup operations for the instance.
|
|
665
|
+
|
|
666
|
+
:type filter_: str
|
|
667
|
+
:param filter_:
|
|
668
|
+
Optional. A string specifying a filter for which backup operations
|
|
669
|
+
to list.
|
|
670
|
+
|
|
671
|
+
:type page_size: int
|
|
672
|
+
:param page_size:
|
|
673
|
+
Optional. The maximum number of operations in each page of results
|
|
674
|
+
from this request. Non-positive values are ignored. Defaults to a
|
|
675
|
+
sensible value set by the API.
|
|
676
|
+
|
|
677
|
+
:rtype: :class:`~google.api_core.page_iterator.Iterator`
|
|
678
|
+
:returns:
|
|
679
|
+
Iterator of :class:`~google.api_core.operation.Operation`
|
|
680
|
+
resources within the current instance.
|
|
681
|
+
"""
|
|
682
|
+
metadata = _metadata_with_prefix(self.name)
|
|
683
|
+
request = ListBackupOperationsRequest(
|
|
684
|
+
parent=self.name,
|
|
685
|
+
filter=filter_,
|
|
686
|
+
page_size=page_size,
|
|
687
|
+
)
|
|
688
|
+
page_iter = self._client.database_admin_api.list_backup_operations(
|
|
689
|
+
request=request, metadata=metadata
|
|
690
|
+
)
|
|
691
|
+
return map(self._item_to_operation, page_iter)
|
|
692
|
+
|
|
693
|
+
def list_database_operations(self, filter_="", page_size=None):
|
|
694
|
+
"""List database operations for the instance.
|
|
695
|
+
|
|
696
|
+
:type filter_: str
|
|
697
|
+
:param filter_:
|
|
698
|
+
Optional. A string specifying a filter for which database operations
|
|
699
|
+
to list.
|
|
700
|
+
|
|
701
|
+
:type page_size: int
|
|
702
|
+
:param page_size:
|
|
703
|
+
Optional. The maximum number of operations in each page of results
|
|
704
|
+
from this request. Non-positive values are ignored. Defaults to a
|
|
705
|
+
sensible value set by the API.
|
|
706
|
+
|
|
707
|
+
:rtype: :class:`~google.api_core.page_iterator.Iterator`
|
|
708
|
+
:returns:
|
|
709
|
+
Iterator of :class:`~google.api_core.operation.Operation`
|
|
710
|
+
resources within the current instance.
|
|
711
|
+
"""
|
|
712
|
+
metadata = _metadata_with_prefix(self.name)
|
|
713
|
+
request = ListDatabaseOperationsRequest(
|
|
714
|
+
parent=self.name,
|
|
715
|
+
filter=filter_,
|
|
716
|
+
page_size=page_size,
|
|
717
|
+
)
|
|
718
|
+
page_iter = self._client.database_admin_api.list_database_operations(
|
|
719
|
+
request=request, metadata=metadata
|
|
720
|
+
)
|
|
721
|
+
return map(self._item_to_operation, page_iter)
|
|
722
|
+
|
|
723
|
+
def _item_to_operation(self, operation_pb):
|
|
724
|
+
"""Convert an operation protobuf to the native object.
|
|
725
|
+
:type operation_pb: :class:`~google.longrunning.operations.Operation`
|
|
726
|
+
:param operation_pb: An operation returned from the API.
|
|
727
|
+
:rtype: :class:`~google.api_core.operation.Operation`
|
|
728
|
+
:returns: The next operation in the page.
|
|
729
|
+
"""
|
|
730
|
+
operations_client = self._client.database_admin_api.transport.operations_client
|
|
731
|
+
metadata_type = _type_string_to_type_pb(operation_pb.metadata.type_url)
|
|
732
|
+
response_type = _OPERATION_RESPONSE_TYPES[metadata_type]
|
|
733
|
+
return google.api_core.operation.from_gapic(
|
|
734
|
+
operation_pb, operations_client, response_type, metadata_type=metadata_type
|
|
735
|
+
)
|