forked from googleapis/python-bigtable
/
app_profile.py
363 lines (295 loc) · 12.9 KB
/
app_profile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# 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
#
# 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.
"""User-friendly container for Google Cloud Bigtable AppProfile."""
import re
from google.cloud.bigtable.enums import RoutingPolicyType
from google.cloud.bigtable_admin_v2.types import instance
from google.protobuf import field_mask_pb2
from google.api_core.exceptions import NotFound
_APP_PROFILE_NAME_RE = re.compile(
r"^projects/(?P<project>[^/]+)/"
r"instances/(?P<instance>[^/]+)/"
r"appProfiles/(?P<app_profile_id>[_a-zA-Z0-9][-_.a-zA-Z0-9]*)$"
)
class AppProfile(object):
"""Representation of a Google Cloud Bigtable AppProfile.
We can use a :class:`AppProfile` to:
* :meth:`reload` itself
* :meth:`create` itself
* :meth:`update` itself
* :meth:`delete` itself
:type app_profile_id: str
:param app_profile_id: The ID of the AppProfile. Must be of the form
``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
:type: routing_policy_type: int
:param: routing_policy_type: (Optional) The type of the routing policy.
Possible values are represented
by the following constants:
:data:`google.cloud.bigtable.enums.RoutingPolicyType.ANY`
:data:`google.cloud.bigtable.enums.RoutingPolicyType.SINGLE`
:type: description: str
:param: description: (Optional) Long form description of the use
case for this AppProfile.
:type: cluster_id: str
:param: cluster_id: (Optional) Unique cluster_id which is only required
when routing_policy_type is
ROUTING_POLICY_TYPE_SINGLE.
:type: allow_transactional_writes: bool
:param: allow_transactional_writes: (Optional) If true, allow
transactional writes for
ROUTING_POLICY_TYPE_SINGLE.
"""
def __init__(
self,
app_profile_id,
instance,
routing_policy_type=None,
description=None,
cluster_id=None,
allow_transactional_writes=None,
):
self.app_profile_id = app_profile_id
self._instance = instance
self.routing_policy_type = routing_policy_type
self.description = description
self.cluster_id = cluster_id
self.allow_transactional_writes = allow_transactional_writes
@property
def name(self):
"""AppProfile name used in requests.
.. note::
This property will not change if ``app_profile_id`` does not, but
the return value is not cached.
For example:
.. literalinclude:: snippets.py
:start-after: [START bigtable_api_app_profile_name]
:end-before: [END bigtable_api_app_profile_name]
:dedent: 4
The AppProfile name is of the form
``"projects/../instances/../app_profile/{app_profile_id}"``
:rtype: str
:returns: The AppProfile name.
"""
return self.instance_admin_client.app_profile_path(
self._instance._client.project,
self._instance.instance_id,
self.app_profile_id,
)
@property
def instance_admin_client(self):
"""Shortcut to instance_admin_client
:rtype: :class:`.bigtable_admin_pb2.BigtableInstanceAdmin`
:returns: A BigtableInstanceAdmin instance.
"""
return self._instance._client.instance_admin_client
def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
# NOTE: This does not compare the configuration values, such as
# the routing_policy_type. Instead, it only compares
# identifying values instance, AppProfile ID and client. This is
# intentional, since the same AppProfile can be in different
# states if not synchronized.
return (
other.app_profile_id == self.app_profile_id
and other._instance == self._instance
)
def __ne__(self, other):
return not self == other
@classmethod
def from_pb(cls, app_profile_pb, instance):
"""Creates an instance app_profile from a protobuf.
:type app_profile_pb: :class:`instance.app_profile_pb`
:param app_profile_pb: An instance protobuf object.
:type instance: :class:`google.cloud.bigtable.instance.Instance`
:param instance: The instance that owns the cluster.
:rtype: :class:`AppProfile`
:returns: The AppProfile parsed from the protobuf response.
:raises: :class:`ValueError <exceptions.ValueError>` if the AppProfile
name does not match
``projects/{project}/instances/{instance_id}/appProfiles/{app_profile_id}``
or if the parsed instance ID does not match the istance ID
on the client.
or if the parsed project ID does not match the project ID
on the client.
"""
match_app_profile_name = _APP_PROFILE_NAME_RE.match(app_profile_pb.name)
if match_app_profile_name is None:
raise ValueError(
"AppProfile protobuf name was not in the " "expected format.",
app_profile_pb.name,
)
if match_app_profile_name.group("instance") != instance.instance_id:
raise ValueError(
"Instance ID on app_profile does not match the "
"instance ID on the client"
)
if match_app_profile_name.group("project") != instance._client.project:
raise ValueError(
"Project ID on app_profile does not match the "
"project ID on the client"
)
app_profile_id = match_app_profile_name.group("app_profile_id")
result = cls(app_profile_id, instance)
result._update_from_pb(app_profile_pb)
return result
def _update_from_pb(self, app_profile_pb):
"""Refresh self from the server-provided protobuf.
Helper for :meth:`from_pb` and :meth:`reload`.
"""
self.routing_policy_type = None
self.allow_transactional_writes = None
self.cluster_id = None
self.description = app_profile_pb.description
routing_policy_type = None
if app_profile_pb._pb.HasField("multi_cluster_routing_use_any"):
routing_policy_type = RoutingPolicyType.ANY
self.allow_transactional_writes = False
else:
routing_policy_type = RoutingPolicyType.SINGLE
self.cluster_id = app_profile_pb.single_cluster_routing.cluster_id
self.allow_transactional_writes = (
app_profile_pb.single_cluster_routing.allow_transactional_writes
)
self.routing_policy_type = routing_policy_type
def _to_pb(self):
"""Create an AppProfile proto buff message for API calls
:rtype: :class:`.instance.AppProfile`
:returns: The converted current object.
:raises: :class:`ValueError <exceptions.ValueError>` if the AppProfile
routing_policy_type is not set
"""
if not self.routing_policy_type:
raise ValueError("AppProfile required routing policy.")
single_cluster_routing = None
multi_cluster_routing_use_any = None
if self.routing_policy_type == RoutingPolicyType.ANY:
multi_cluster_routing_use_any = (
instance.AppProfile.MultiClusterRoutingUseAny()
)
else:
single_cluster_routing = instance.AppProfile.SingleClusterRouting(
cluster_id=self.cluster_id,
allow_transactional_writes=self.allow_transactional_writes,
)
app_profile_pb = instance.AppProfile(
name=self.name,
description=self.description,
multi_cluster_routing_use_any=multi_cluster_routing_use_any,
single_cluster_routing=single_cluster_routing,
)
return app_profile_pb
def reload(self):
"""Reload the metadata for this cluster
For example:
.. literalinclude:: snippets.py
:start-after: [START bigtable_api_reload_app_profile]
:end-before: [END bigtable_api_reload_app_profile]
:dedent: 4
"""
app_profile_pb = self.instance_admin_client.get_app_profile(
request={"name": self.name}
)
# NOTE: _update_from_pb does not check that the project and
# app_profile ID on the response match the request.
self._update_from_pb(app_profile_pb)
def exists(self):
"""Check whether the AppProfile already exists.
For example:
.. literalinclude:: snippets.py
:start-after: [START bigtable_api_app_profile_exists]
:end-before: [END bigtable_api_app_profile_exists]
:dedent: 4
:rtype: bool
:returns: True if the AppProfile exists, else False.
"""
try:
self.instance_admin_client.get_app_profile(request={"name": self.name})
return True
# NOTE: There could be other exceptions that are returned to the user.
except NotFound:
return False
def create(self, ignore_warnings=None):
"""Create this AppProfile.
.. note::
Uses the ``instance`` and ``app_profile_id`` on the current
:class:`AppProfile` in addition to the ``routing_policy_type``,
``description``, ``cluster_id`` and ``allow_transactional_writes``.
To change them before creating, reset the values via
For example:
.. literalinclude:: snippets.py
:start-after: [START bigtable_api_create_app_profile]
:end-before: [END bigtable_api_create_app_profile]
:dedent: 4
:type: ignore_warnings: bool
:param: ignore_warnings: (Optional) If true, ignore safety checks when
creating the AppProfile.
"""
return self.from_pb(
self.instance_admin_client.create_app_profile(
request={
"parent": self._instance.name,
"app_profile_id": self.app_profile_id,
"app_profile": self._to_pb(),
"ignore_warnings": ignore_warnings,
}
),
self._instance,
)
def update(self, ignore_warnings=None):
"""Update this app_profile.
.. note::
Update any or all of the following values:
``routing_policy_type``
``description``
``cluster_id``
``allow_transactional_writes``
For example:
.. literalinclude:: snippets.py
:start-after: [START bigtable_api_update_app_profile]
:end-before: [END bigtable_api_update_app_profile]
:dedent: 4
"""
update_mask_pb = field_mask_pb2.FieldMask()
if self.description is not None:
update_mask_pb.paths.append("description")
if self.routing_policy_type == RoutingPolicyType.ANY:
update_mask_pb.paths.append("multi_cluster_routing_use_any")
else:
update_mask_pb.paths.append("single_cluster_routing")
return self.instance_admin_client.update_app_profile(
request={
"app_profile": self._to_pb(),
"update_mask": update_mask_pb,
"ignore_warnings": ignore_warnings,
}
)
def delete(self, ignore_warnings=None):
"""Delete this AppProfile.
For example:
.. literalinclude:: snippets.py
:start-after: [START bigtable_api_delete_app_profile]
:end-before: [END bigtable_api_delete_app_profile]
:dedent: 4
:type: ignore_warnings: bool
:param: ignore_warnings: If true, ignore safety checks when deleting
the AppProfile.
: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.
"""
self.instance_admin_client.delete_app_profile(
request={"name": self.name, "ignore_warnings": ignore_warnings}
)