/
backup.py
446 lines (361 loc) · 15.7 KB
/
backup.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# 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.
"""A user-friendly wrapper for a Google Cloud Bigtable Backup."""
import re
from google.cloud._helpers import _datetime_to_pb_timestamp
from google.cloud.bigtable_admin_v2.gapic.bigtable_table_admin_client import (
BigtableTableAdminClient,
)
from google.cloud.bigtable_admin_v2.types import table_pb2
from google.cloud.bigtable.policy import Policy
from google.cloud.exceptions import NotFound
from google.protobuf import field_mask_pb2
_BACKUP_NAME_RE = re.compile(
r"^projects/(?P<project>[^/]+)/"
r"instances/(?P<instance_id>[a-z][-a-z0-9]*)/"
r"clusters/(?P<cluster_id>[a-z][-a-z0-9]*)/"
r"backups/(?P<backup_id>[a-z][a-z0-9_\-]*[a-z0-9])$"
)
_TABLE_NAME_RE = re.compile(
r"^projects/(?P<project>[^/]+)/"
r"instances/(?P<instance_id>[a-z][-a-z0-9]*)/"
r"tables/(?P<table_id>[_a-zA-Z0-9][-_.a-zA-Z0-9]*)$"
)
class Backup(object):
"""Representation of a Google Cloud Bigtable Backup.
A :class: `Backup` can be used to:
* :meth:`create` the backup
* :meth:`update` the backup
* :meth:`delete` the backup
:type backup_id: str
:param backup_id: The ID of the backup.
:type instance: :class:`~google.cloud.bigtable.instance.Instance`
:param instance: The Instance that owns this Backup.
:type cluster_id: str
:param cluster_id: (Optional) The ID of the Cluster that contains this Backup.
Required for calling 'delete', 'exists' etc. methods.
:type table_id: str
:param table_id: (Optional) The ID of the Table that the Backup is for.
Required if the 'create' method will be called.
:type expire_time: :class:`datetime.datetime`
:param expire_time: (Optional) The expiration time after which the Backup
will be automatically deleted. Required if the `create`
method will be called.
"""
def __init__(
self, backup_id, instance, cluster_id=None, table_id=None, expire_time=None
):
self.backup_id = backup_id
self._instance = instance
self._cluster = cluster_id
self.table_id = table_id
self._expire_time = expire_time
self._parent = None
self._source_table = None
self._start_time = None
self._end_time = None
self._size_bytes = None
self._state = None
@property
def name(self):
"""Backup name used in requests.
The Backup name is of the form
``"projects/../instances/../clusters/../backups/{backup_id}"``
:rtype: str
:returns: The Backup name.
:raises: ValueError: If the 'cluster' has not been set.
"""
if not self._cluster:
raise ValueError('"cluster" parameter must be set')
return BigtableTableAdminClient.backup_path(
project=self._instance._client.project,
instance=self._instance.instance_id,
cluster=self._cluster,
backup=self.backup_id,
)
@property
def cluster(self):
"""The ID of the [parent] cluster used in requests.
:rtype: str
:returns: The ID of the cluster containing the Backup.
"""
return self._cluster
@cluster.setter
def cluster(self, cluster_id):
self._cluster = cluster_id
@property
def parent(self):
"""Name of the parent cluster used in requests.
.. note::
This property will return None if ``cluster`` is not set.
The parent name is of the form
``"projects/{project}/instances/{instance_id}/clusters/{cluster}"``
:rtype: str
:returns: A full path to the parent cluster.
"""
if not self._parent and self._cluster:
self._parent = BigtableTableAdminClient.cluster_path(
project=self._instance._client.project,
instance=self._instance.instance_id,
cluster=self._cluster,
)
return self._parent
@property
def source_table(self):
"""The full name of the Table from which this Backup is created.
.. note::
This property will return None if ``table_id`` is not set.
The table name is of the form
``"projects/../instances/../tables/{source_table}"``
:rtype: str
:returns: The Table name.
"""
if not self._source_table and self.table_id:
self._source_table = BigtableTableAdminClient.table_path(
project=self._instance._client.project,
instance=self._instance.instance_id,
table=self.table_id,
)
return self._source_table
@property
def expire_time(self):
"""Expiration time used in the creation requests.
:rtype: :class:`datetime.datetime`
:returns: A 'datetime' object representing the expiration time of
this Backup.
"""
return self._expire_time
@expire_time.setter
def expire_time(self, new_expire_time):
self._expire_time = new_expire_time
@property
def start_time(self):
"""The time this Backup was started.
:rtype: :class:`datetime.datetime`
:returns: A 'datetime' object representing the time when the creation
of this Backup had started.
"""
return self._start_time
@property
def end_time(self):
"""The time this Backup was finished.
:rtype: :class:`datetime.datetime`
:returns: A 'datetime' object representing the time when the creation
of this Backup was finished.
"""
return self._end_time
@property
def size_bytes(self):
"""The size of this Backup, in bytes.
:rtype: int
:returns: The size of this Backup, in bytes.
"""
return self._size_bytes
@property
def state(self):
"""The current state of this Backup.
:rtype: :class:`~google.cloud.bigtable_admin_v2.gapic.enums.Backup.State`
:returns: The current state of this Backup.
"""
return self._state
@classmethod
def from_pb(cls, backup_pb, instance):
"""Creates a Backup instance from a protobuf message.
:type backup_pb: :class:`table_pb2.Backup`
:param backup_pb: A Backup protobuf object.
:type instance: :class:`Instance <google.cloud.bigtable.instance.Instance>`
:param instance: The Instance that owns the Backup.
:rtype: :class:`~google.cloud.bigtable.backup.Backup`
:returns: The backup parsed from the protobuf response.
:raises: ValueError: If the backup name does not match the expected
format or the parsed project ID does not match the
project ID on the Instance's client, or if the
parsed instance ID does not match the Instance ID.
"""
match = _BACKUP_NAME_RE.match(backup_pb.name)
if match is None:
raise ValueError(
"Backup protobuf name was not in the expected format.", backup_pb.name
)
if match.group("project") != instance._client.project:
raise ValueError(
"Project ID of the Backup does not match the Project ID "
"of the instance's client"
)
instance_id = match.group("instance_id")
if instance_id != instance.instance_id:
raise ValueError(
"Instance ID of the Backup does not match the Instance ID "
"of the instance"
)
backup_id = match.group("backup_id")
cluster_id = match.group("cluster_id")
match = _TABLE_NAME_RE.match(backup_pb.source_table)
table_id = match.group("table_id") if match else None
expire_time = backup_pb.expire_time
backup = cls(
backup_id,
instance,
cluster_id=cluster_id,
table_id=table_id,
expire_time=expire_time,
)
backup._start_time = backup_pb.start_time
backup._end_time = backup_pb.end_time
backup._size_bytes = backup_pb.size_bytes
backup._state = backup_pb.state
return backup
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return other.backup_id == self.backup_id and other._instance == self._instance
def __ne__(self, other):
return not self == other
def create(self, cluster_id=None):
"""Creates this backup within its instance.
:type cluster_id: str
:param cluster_id: (Optional) The ID of the Cluster for the newly
created Backup.
:rtype: :class:`~google.api_core.operation.Operation`
:returns: :class:`~google.cloud.bigtable_admin_v2.types._OperationFuture`
instance, to be used to poll the status of the 'create' request
:raises Conflict: if the Backup already exists
:raises NotFound: if the Instance owning the Backup does not exist
:raises BadRequest: if the `table` or `expire_time` values are invalid,
or `expire_time` is not set
"""
if not self._expire_time:
raise ValueError('"expire_time" parameter must be set')
# TODO: Consider implementing a method that sets a default value of
# `expire_time`, e.g. 1 week from the creation of the Backup.
if not self.table_id:
raise ValueError('"table" parameter must be set')
if cluster_id:
self._cluster = cluster_id
if not self._cluster:
raise ValueError('"cluster" parameter must be set')
backup = table_pb2.Backup(
source_table=self.source_table,
expire_time=_datetime_to_pb_timestamp(self.expire_time),
)
api = self._instance._client.table_admin_client
return api.create_backup(self.parent, self.backup_id, backup)
def get(self):
"""Retrieves metadata of a pending or completed Backup.
:returns: An instance of
:class:`~google.cloud.bigtable_admin_v2.types.Backup`
:raises google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
:raises google.api_core.exceptions.RetryError: If the request failed
due to a retryable error and retry attempts failed.
:raises ValueError: If the parameters are invalid.
"""
api = self._instance._client.table_admin_client
try:
return api.get_backup(self.name)
except NotFound:
return None
def reload(self):
"""Refreshes the stored backup properties."""
backup = self.get()
self._source_table = backup.source_table
self._expire_time = backup.expire_time
self._start_time = backup.start_time
self._end_time = backup.end_time
self._size_bytes = backup.size_bytes
self._state = backup.state
def exists(self):
"""Tests whether this Backup exists.
:rtype: bool
:returns: True if the Backup exists, else False.
"""
return self.get() is not None
def update_expire_time(self, new_expire_time):
"""Update the expire time of this Backup.
:type new_expire_time: :class:`datetime.datetime`
:param new_expire_time: the new expiration time timestamp
"""
backup_update = table_pb2.Backup(
name=self.name,
expire_time=_datetime_to_pb_timestamp(new_expire_time),
)
update_mask = field_mask_pb2.FieldMask(paths=["expire_time"])
api = self._instance._client.table_admin_client
api.update_backup(backup_update, update_mask)
self._expire_time = new_expire_time
def delete(self):
"""Delete this Backup."""
self._instance._client.table_admin_client.delete_backup(self.name)
def restore(self, table_id):
"""Creates a new Table by restoring from this Backup. The new Table
must be in the same Instance as the Instance containing the Backup.
The returned Table ``long-running operation`` can be used to track the
progress of the operation and to cancel it. The ``response`` type is
``Table``, if successful.
:param table_id: The ID of the Table to create and restore to.
This Table must not already exist.
:returns: An instance of
:class:`~google.cloud.bigtable_admin_v2.types._OperationFuture`.
:raises: google.api_core.exceptions.AlreadyExists: If the table
already exists.
:raises: google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
:raises: google.api_core.exceptions.RetryError: If the request failed
due to a retryable error and retry attempts failed.
:raises: ValueError: If the parameters are invalid.
"""
api = self._instance._client.table_admin_client
return api.restore_table(self._instance.name, table_id, self.name)
def get_iam_policy(self):
"""Gets the IAM access control policy for this backup.
:rtype: :class:`google.cloud.bigtable.policy.Policy`
:returns: The current IAM policy of this backup.
"""
table_api = self._instance._client.table_admin_client
args = {"resource": self.name}
response = table_api.get_iam_policy(**args)
return Policy.from_pb(response)
def set_iam_policy(self, policy):
"""Sets the IAM access control policy for this backup. Replaces any
existing policy.
For more information about policy, please see documentation of
class `google.cloud.bigtable.policy.Policy`
:type policy: :class:`google.cloud.bigtable.policy.Policy`
:param policy: A new IAM policy to replace the current IAM policy
of this backup.
:rtype: :class:`google.cloud.bigtable.policy.Policy`
:returns: The current IAM policy of this backup.
"""
table_api = self._instance._client.table_admin_client
response = table_api.set_iam_policy(resource=self.name, policy=policy.to_pb())
return Policy.from_pb(response)
def test_iam_permissions(self, permissions):
"""Tests whether the caller has the given permissions for this backup.
Returns the permissions that the caller has.
:type permissions: list
:param permissions: The set of permissions to check for
the ``resource``. Permissions with wildcards (such as '*'
or 'storage.*') are not allowed. For more information see
`IAM Overview
<https://cloud.google.com/iam/docs/overview#permissions>`_.
`Bigtable Permissions
<https://cloud.google.com/bigtable/docs/access-control>`_.
:rtype: list
:returns: A List(string) of permissions allowed on the backup.
"""
table_api = self._instance._client.table_admin_client
response = table_api.test_iam_permissions(
resource=self.name, permissions=permissions
)
return list(response.permissions)