
    V#                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lZddlmZ ddlmZ  G d dej.                        Zd Zd Zd Z	 ddefdZ	 	 ddZd Zd Zd Z d Z!d Z"d Z#d Z$y
)z!Cloud Spanner backups API helper.    )absolute_import)division)unicode_literals)apis)
exceptions)CloudKmsKeyName)http)timesN)http_client)urllibc                       e Zd ZdZy)HttpRequestFailedErrorz3Indicates that the http request failed in some way.N)__name__
__module____qualname____doc__     -lib/googlecloudsdk/api_lib/spanner/backups.pyr   r       s    ;r   r   c                     t        j                  |       j                  t        j                  t         j                              }t        j
                  |dt         j                        }|S )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                 *   | j                  d      r| j                  d      s"| j                  d      s'| j                  d      st        j                  dd      | j                  r| j                  S | j                  rt        | j                        S y)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                     t        j                  dd      }t        j                  dd      }|j                  | j	                               }|j
                  j                  |      S )zGet a backup.spannerv1)name)r   GetClientInstanceGetMessagesModule)SpannerProjectsInstancesBackupsGetRequestRelativeNameprojects_instances_backupsGet)
backup_refclientmsgsreqs       r   	GetBackupr7   B   s^    !!)T2&				4	0$66""$ 	7 	&#		*	*	.	.s	33r   kms_keyc                    t        j                  dd      }t        j                  dd      }d|j                  d}|r||d<   |r7|j                  r|j                  |d<   n|j
                  r|j
                  |d<   | j                         j                         }dj                  |j                  |t        j                  j                  |d	
            }|j                  |dz   |j                  z   t        |            }	|j!                  d      r|j"                  |	_        t&        j(                  rdnd}
t+        j,                  |
      j/                  |d|j1                  |	            \  }}t3        |j5                  d            t6        j8                  k7  rt;        d|z         t=        |d      }|j?                  ||      S )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   r-   r.   backupkms_key_namekms_key_namesParentr0   formaturlr   parse	urlencodeBackupr?   r(   r$   rA   versionTimesixPY2r	   HttprequestSerializeMessageintgethttplibOKr   getattrDeserializeMessage)r3   r'   encryption_typer8   r4   r5   query_paramsparentrN   rI   rB   responseresponse_bodymessage_types                 r   CreateBackuprd   K   s    !!)T2&				4	0$T[[9,6EL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 (M 	h	 GJJ.
 !B!."/ 0 0{+,		"	"<	??r   c                    t        j                  dd      }t        j                  dd      }|j                  |j	                         | j                               }t        |      |_        |r.|j                  ||j                  |j                        |_        n|r|j                  |      |_        |j                  |j                         j                         |      }|j                  j                  |      S )zCopy a backup.r*   r+   )r<   sourceBackup)encryptionType
kmsKeyNamekmsKeyNames)rg   )r`   copyBackupRequest)r   r-   r.   CopyBackupRequestNamer0   r(   r@   CopyBackupEncryptionConfigrJ   rK   encryptionConfig*SpannerProjectsInstancesBackupsCopyRequestrL   r1   Copy)	source_backup_refdestination_backup_refr'   r^   r8   r4   r5   copy_backup_requestr6   s	            r   
CopyBackuprt   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                     | j                         j                         dz   |j                  z   |j                  _        t	        |      |j                  _        d|_        |S )z4Parse arguments and construct update backup request.z	/backups/expire_time)rL   r0   rI   r,   r(   r@   
updateMask)r3   r'   r6   s      r   ModifyUpdateMetadataRequestrx      sR    %%'44 KK #**//5#** #.	*r   c                     | j                         |_        |j                  r6| j                         dz   |j                  z   }dj                  |      |_        |S )z3Parse arguments and construct list backups request.r>   zdatabase="{}")r0   r`   r?   rM   filter)instance_refr'   r6   r?   s       r   ModifyListRequestr|      sL    ((*#*	]]((*]:T]]JH ''1CJ	*r   c                     t        |        |S )z0Checks if backup exists, if so, returns request.)r7   )r3   _r6   s      r   CheckBackupExistsr      s     J	*r   c                 >    | D cg c]  }t        |       c}S c c}w )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~   r3   s      r   FormatListBackupsr      s"     7B	Bk
-

#k	BB	Bs   c                     | }| j                   D cg c]  }t        |       c}|_         | j                  D cg c]  }t        |j                         c}|_        |S c c}w c c}w )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                 *    | j                  d      d   S )a9  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                 .    d| j                  d      d   iS )ad  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
   rS   	six.movesr   rZ   r   Errorr   r    r(   r7   rd   rt   rx   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   