
    "                         S r SSKJr  SSKJr  SSKJr  SSKJr	  SSK
Jr  SSKJr  SSKJr  SSKJr   " S	 S
\	R$                  5      rS rS rS r SS\4S jjr  SS jrS rS rS rS rS rS rS rg)z!Cloud Spanner backups API helper.    )apis)
exceptions)CloudKmsKeyName)requests)times)http_client)urllibc                       \ rS rSrSrSrg)HttpRequestFailedError   z3Indicates that the http request failed in some way. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       -lib/googlecloudsdk/api_lib/spanner/backups.pyr   r      s    ;r   r   c                     [         R                  " U 5      R                  [         R                  " [         R                  5      5      n[         R
                  " US[         R                  S9nU$ )z5Parse flag value into Datetime format for expireTime.z%Y-%m-%dT%H:%M:%S.%6f%Ez)tzinfo)r   ParseDurationGetRelativeDateTimeNowUTCFormatDateTime)expiration_valuedatetimeparsed_datetimes      r   ParseExpireTimer    "   sW     *+??
))EII
   ((*599>/	r   c                 h   U R                  S5      (       a  U R                  S5      (       d,  U R                  S5      (       d-  U R                  S5      (       d  [        R                  " SS5      eU R                  (       a  U R                  $ U R                  (       a  [        U R                  5      $ g)zGCheck if fields for expireTime are correctly specified and parse value.expiration_dateretention_periodz'--expiration-date or --retention-periodz<Must specify either --expiration-date or --retention-period.N)IsSpecifiedc_exceptionsInvalidArgumentExceptionr"   r#   r    )argss    r   CheckAndGetExpireTimer(   -   s     ())
)**


,
-
-


-
.
.

/
/1FH H 
40011 r   c                     [         R                  " SS5      n[         R                  " SS5      nUR                  U R	                  5       S9nUR
                  R                  U5      $ )zGet a backup.spannerv1)name)r   GetClientInstanceGetMessagesModule)SpannerProjectsInstancesBackupsGetRequestRelativeNameprojects_instances_backupsGet)
backup_refclientmsgsreqs       r   	GetBackupr7   >   s^    !!)T2&				4	0$66""$ 	7 	&#		*	*	.	.s	33r   Nkms_keyc           	         [         R                  " SS5      n[         R                  " SS5      nSUR                  S.nU(       a  X&S'   U(       aA  UR                  (       a  UR                  US'   O UR
                  (       a  UR
                  US'   U R                  5       R                  5       nSR                  UR                  U[        R                  R                  US	S
95      nUR                  US-   UR                  -   [        U5      S9n	UR!                  S5      (       a  UR"                  U	l        [&        R(                  " 5       n
SU
l        U
R-                  SXR/                  U	5      S9n[1        UR2                  5      [4        R6                  :w  a  [9        SUR:                  -   5      e[=        US5      nUR?                  XR@                  5      $ )zCreate a new backup.r*   r+   json)altbackupIdzencryptionConfig.encryptionTypezencryptionConfig.kmsKeyNamezencryptionConfig.kmsKeyNamesz{}v1/{}/backups?{}T)doseq/databases/)database
expireTimeversion_timezutf-8POST)datazHTTP request failed. Response: 	Operation)!r   r-   r.   backupkms_key_namekms_key_namesParentr0   formaturlr	   parse	urlencodeBackupr?   r(   r$   rA   versionTimer   
GetSessionencodingrequestSerializeMessageintstatus_codehttplibOKr   textgetattrDeserializeMessagecontent)r3   r'   encryption_typer8   r4   r5   query_paramsparentrJ   rE   r   responsemessage_types                r   CreateBackupr`   G   s    !!)T2&				4	0$T[[9,6E234;4H4Hl01			5<5J5Jl12++-&##jj&&,,00T0J	# ;;%5&t,  .& 
n%%**F ##%+ +  c//7 ! ( 			'**,
 )HMM9  {+,		"	"<1A1A	BBr   c                    [         R                  " SS5      n[         R                  " SS5      nUR                  UR	                  5       U R                  5       S9n[        U5      Ul        U(       a+  UR                  UUR                  UR                  S9Ul        OU(       a  UR                  US9Ul        UR                  UR                  5       R                  5       US9nUR                  R                  U5      $ )zCopy a backup.r*   r+   )r<   sourceBackup)encryptionType
kmsKeyNamekmsKeyNames)rc   )r]   copyBackupRequest)r   r-   r.   CopyBackupRequestNamer0   r(   r@   CopyBackupEncryptionConfigrF   rG   encryptionConfig*SpannerProjectsInstancesBackupsCopyRequestrH   r1   Copy)	source_backup_refdestination_backup_refr'   r[   r8   r4   r5   copy_backup_requestr6   s	            r   
CopyBackuprp   w   s    !!)T2&				4	0$..%**,$113 / 5 $9#> +/+J+J&'')) ,K ,(
 +/+J+J& ,K ,( 	77#**,99;+ 	8 	-# 
	*	*	/	/	44r   c                     U R                  5       R                  5       S-   UR                  -   UR                  l        [	        U5      UR                  l        SUl        U$ )z4Parse arguments and construct update backup request.z	/backups/expire_time)rH   r0   rE   r,   r(   r@   
updateMask)r3   r'   r6   s      r   ModifyUpdateMetadataRequestrt      sR    %%'44 KK #**//5#** #.	*r   c                     U R                  5       Ul        UR                  (       a6  U R                  5       S-   UR                  -   nSR                  U5      Ul        U$ )z3Parse arguments and construct list backups request.r>   zdatabase="{}")r0   r]   r?   rI   filter)instance_refr'   r6   r?   s       r   ModifyListRequestrx      sL    ((*#*	]]((*]:T]]JH ''1CJ	*r   c                     [        U 5        U$ )z0Checks if backup exists, if so, returns request.)r7   )r3   _r6   s      r   CheckBackupExistsr{      s     J	*r   c                 D    U  Vs/ s H  n[        U5      PM     sn$ s  snf )zFormats existing fields for displaying them in the list response.

Args:
  backup_refs: A list of backups.

Returns:
  The list of backups with the new formatting.
)_FormatBackup)backup_refsrz   r3   s      r   FormatListBackupsr      s"     7B	Bk
-

#k	BB	Bs   c                     U nU R                    Vs/ s H  n[        U5      PM     snUl         U R                   Vs/ s H  n[        UR                  5      PM     snUl        U$ s  snf s  snf )zFormats a single backup for displaying it in the list response.

This function makes in-place modifications.

Args:
  backup_ref: The backup to format.

Returns:
  The backup with the new formatting.
)backupSchedules#_ExtractScheduleNameFromScheduleUriinstancePartitions5_ExtractInstancePartitionNameFromInstancePartitionUriinstancePartition)r3   formatted_backup_refschedule_uriinstance_partitions       r   r}   r}      s     $ %44*4, *,74*& !+ = =	- !>
 <

.
. !>	-) 
*-s
   A%A*c                 *    U R                  S5      S   $ )a-  Converts a schedule URI to an schedule name.

Args:
    schedule_uri: The URI of the schedule, e.g.,
    "projects/test-project/instances/test-instance/databases/test-database/backupSchedules/test-backup-schedule".

Returns:
    The name of the schedule ("test-backup-schedule" in the example above).
/split)r   s    r   r   r      s     
		C	 	$$r   c                 .    SU R                  S5      S   0$ )aV  Converts an instance partition URI to an instance partition name.

Args:
    instance_partition_uri: The URI of an instance partition, e.g.,
      "projects/test-project/instances/test-instance/instancePartitions/test-instance-partition".

Returns:
    The name of the instance partition ("test-instance-partition" in the
    example above).
r   r   r   r   )instance_partition_uris    r   r   r      s      5;;C@D	EEr   )NN) r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r%   0googlecloudsdk.command_lib.spanner.resource_argsr   googlecloudsdk.corecore_exceptionsgooglecloudsdk.core.credentialsr   googlecloudsdk.core.utilr   	six.movesr   rU   r	   Errorr   r    r(   r7   r`   rp   rt   rx   r{   r   r}   r   r   r   r   r   <module>r      s    ( - > L = 4 * , _22 2"4 HL-C5D-Cf  $	5:		C2
%Fr   