
    V#                        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	KJr  SS
K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.    )absolute_import)division)unicode_literals)apis)
exceptions)CloudKmsKeyName)http)timesN)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+   1   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   	GetBackupr:   B   s^    !!)T2&				4	0$66""$ 	7 	&#		*	*	.	.s	33r   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(                  (       a  SOSn
[*        R,                  " U
S9R/                  USUR1                  U	5      S9u  p[3        UR5                  S5      5      [6        R8                  :w  a  [;        SU-   5      e[=        US5      nUR?                  X5      $ )zCreate a new backup.r-   r.   json)altbackupIdzencryptionConfig.encryptionTypezencryptionConfig.kmsKeyNamezencryptionConfig.kmsKeyNamesz{}v1/{}/backups?{}T)doseq/databases/)database
expireTimeversion_timeNzutf-8)response_encodingPOST)urimethodbodystatuszHTTP request failed. Response: 	Operation) r   r0   r1   backupkms_key_namekms_key_namesParentr3   formaturlr   parse	urlencodeBackuprB   r+   r'   rD   versionTimesixPY2r	   HttprequestSerializeMessageintgethttplibOKr   getattrDeserializeMessage)r6   r*   encryption_typer;   r7   r8   query_paramsparentrQ   rL   rE   responseresponse_bodymessage_types                 r   CreateBackuprg   K   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 "ggd7 !II)++27&v'>'>v'F ,3 ,H ( 	h	 GJJ.
 !B!."/ 0 0{+,		"	"<	??r   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)rj   )rc   copyBackupRequest)r   r0   r1   CopyBackupRequestNamer3   r+   rC   CopyBackupEncryptionConfigrM   rN   encryptionConfig*SpannerProjectsInstancesBackupsCopyRequestrO   r4   Copy)	source_backup_refdestination_backup_refr*   ra   r;   r7   r8   copy_backup_requestr9   s	            r   
CopyBackuprw   z   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)rO   r3   rL   r/   r+   rC   
updateMask)r6   r*   r9   s      r   ModifyUpdateMetadataRequestr{      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.rA   zdatabase="{}")r3   rc   rB   rP   filter)instance_refr*   r9   rB   s       r   ModifyListRequestr      sL    ((*#*	]]((*]:T]]JH ''1CJ	*r   c                     [        U 5        U$ )z0Checks if backup exists, if so, returns request.)r:   )r6   _r9   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_refsr   r6   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)r6   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   
__future__r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r(   0googlecloudsdk.command_lib.spanner.resource_argsr   googlecloudsdk.corecore_exceptionsgooglecloudsdk.core.credentialsr	   googlecloudsdk.core.utilr
   rV   	six.movesr   r]   r   Errorr   r#   r+   r:   rg   rw   r{   r   r   r   r   r   r   r   r   r   <module>r      s    ( &  ' , > L = 0 * 
 , _22 2"4 HL,@5D,@d  $	5:		C2
%Fr   