21
21
from google .cloud .spanner_admin_database_v1 import Backup as BackupPB
22
22
from google .cloud .spanner_admin_database_v1 import CreateBackupEncryptionConfig
23
23
from google .cloud .spanner_admin_database_v1 import CreateBackupRequest
24
+ from google .cloud .spanner_admin_database_v1 import CopyBackupEncryptionConfig
25
+ from google .cloud .spanner_admin_database_v1 import CopyBackupRequest
24
26
from google .cloud .spanner_v1 ._helpers import _metadata_with_prefix
25
27
26
28
_BACKUP_NAME_RE = re .compile (
@@ -77,19 +79,30 @@ def __init__(
77
79
expire_time = None ,
78
80
version_time = None ,
79
81
encryption_config = None ,
82
+ source_backup = None ,
80
83
):
81
84
self .backup_id = backup_id
82
85
self ._instance = instance
83
86
self ._database = database
87
+ self ._source_backup = source_backup
84
88
self ._expire_time = expire_time
85
89
self ._create_time = None
86
90
self ._version_time = version_time
87
91
self ._size_bytes = None
88
92
self ._state = None
89
93
self ._referencing_databases = None
90
94
self ._encryption_info = None
95
+ self ._max_expire_time = None
96
+ self ._referencing_backups = None
91
97
if type (encryption_config ) == dict :
92
- self ._encryption_config = CreateBackupEncryptionConfig (** encryption_config )
98
+ if source_backup :
99
+ self ._encryption_config = CopyBackupEncryptionConfig (
100
+ ** encryption_config
101
+ )
102
+ else :
103
+ self ._encryption_config = CreateBackupEncryptionConfig (
104
+ ** encryption_config
105
+ )
93
106
else :
94
107
self ._encryption_config = encryption_config
95
108
@@ -185,6 +198,24 @@ def encryption_info(self):
185
198
"""
186
199
return self ._encryption_info
187
200
201
+ @property
202
+ def max_expire_time (self ):
203
+ """The max allowed expiration time of the backup.
204
+ :rtype: :class:`datetime.datetime`
205
+ :returns: a datetime object representing the max expire time of
206
+ this backup
207
+ """
208
+ return self ._max_expire_time
209
+
210
+ @property
211
+ def referencing_backups (self ):
212
+ """The names of the destination backups being created by copying this source backup.
213
+ :rtype: list of strings
214
+ :returns: a list of backup path strings which specify the backups that are
215
+ referencing this copy backup
216
+ """
217
+ return self ._referencing_backups
218
+
188
219
@classmethod
189
220
def from_pb (cls , backup_pb , instance ):
190
221
"""Create an instance of this class from a protobuf message.
@@ -223,7 +254,7 @@ def from_pb(cls, backup_pb, instance):
223
254
return cls (backup_id , instance )
224
255
225
256
def create (self ):
226
- """Create this backup within its instance.
257
+ """Create this backup or backup copy within its instance.
227
258
228
259
:rtype: :class:`~google.api_core.operation.Operation`
229
260
:returns: a future used to poll the status of the create request
@@ -234,17 +265,39 @@ def create(self):
234
265
"""
235
266
if not self ._expire_time :
236
267
raise ValueError ("expire_time not set" )
237
- if not self ._database :
238
- raise ValueError ("database not set" )
268
+
269
+ if not self ._database and not self ._source_backup :
270
+ raise ValueError ("database and source backup both not set" )
271
+
239
272
if (
240
- self ._encryption_config
273
+ (
274
+ self ._encryption_config
275
+ and self ._encryption_config .kms_key_name
276
+ and self ._encryption_config .encryption_type
277
+ != CreateBackupEncryptionConfig .EncryptionType .CUSTOMER_MANAGED_ENCRYPTION
278
+ )
279
+ and self ._encryption_config
241
280
and self ._encryption_config .kms_key_name
242
281
and self ._encryption_config .encryption_type
243
- != CreateBackupEncryptionConfig .EncryptionType .CUSTOMER_MANAGED_ENCRYPTION
282
+ != CopyBackupEncryptionConfig .EncryptionType .CUSTOMER_MANAGED_ENCRYPTION
244
283
):
245
284
raise ValueError ("kms_key_name only used with CUSTOMER_MANAGED_ENCRYPTION" )
285
+
246
286
api = self ._instance ._client .database_admin_api
247
287
metadata = _metadata_with_prefix (self .name )
288
+
289
+ if self ._source_backup :
290
+ request = CopyBackupRequest (
291
+ parent = self ._instance .name ,
292
+ backup_id = self .backup_id ,
293
+ source_backup = self ._source_backup ,
294
+ expire_time = self ._expire_time ,
295
+ encryption_config = self ._encryption_config ,
296
+ )
297
+
298
+ future = api .copy_backup (request = request , metadata = metadata ,)
299
+ return future
300
+
248
301
backup = BackupPB (
249
302
database = self ._database ,
250
303
expire_time = self .expire_time ,
@@ -294,6 +347,8 @@ def reload(self):
294
347
self ._state = BackupPB .State (pb .state )
295
348
self ._referencing_databases = pb .referencing_databases
296
349
self ._encryption_info = pb .encryption_info
350
+ self ._max_expire_time = pb .max_expire_time
351
+ self ._referencing_backups = pb .referencing_backups
297
352
298
353
def update_expire_time (self , new_expire_time ):
299
354
"""Update the expire time of this backup.
0 commit comments