
    E                        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ZddZd Zd Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zd Zd Zy
)z9Shared utilities to access the Google Secret Manager API.    )absolute_import)division)unicode_literals)
exceptions)
list_pager)apis)base)iam_utilNc                 \    t        j                  d| xs t        j                  d            S )zGet the default client.secretmanager)r   GetClientInstanceResolveVersionversions    )lib/googlecloudsdk/api_lib/secrets/api.py	GetClientr      /    			 2E $ 3 3O D
F F    c                 \    t        j                  d| xs t        j                  d            S )z Get the default messages module.r   )r   GetMessagesModuler   r   s    r   GetMessagesr   "   r   r   c                 $    dj                  |       S )N,)join)update_masks    r   _FormatUpdateMaskr   (   s    	+	r   c           
         |sy| j                  | j                               }|dk(  r7|r5| j                  | j                  | j                  |d                     }|dk(  rg }t        |      D ]m  \  }}|t	        |      k  r6|j                  | j                  || j                  ||                      J|j                  | j                  ||   	             o | j                  | j                  |
            }|S )z1Create a replication message from its components.N)	automaticr   r   
kmsKeyName)customerManagedEncryptionzuser-managed)locationr!   )r"   )replicas)userManaged)Replication	AutomaticCustomerManagedEncryption	enumeratelenappendReplicaUserManaged)messagespolicy	locationskeysreplicationr#   ir"   s           r   _MakeReplicationMessager3   ,   s)   	$$x/A/A/C$D+{t&&$$&.&H&H7 'I '$ % % ' &K ~H +8	
SY!*2*L*L#Aw +M +(  )	* 	(()A,(?@ , &&((((; ' =K	r   c                       e Zd ZdZddZy)ClientzBase class for all clients.Nc                 l    |xs t        |      | _        |xs | j                  j                  | _        y Nr   )r   clientMESSAGES_MODULEr-   )selfr8   r-   api_versions       r   __init__zClient.__init__J   s)    :Ik:DK; ; ;DMr   NNN)__name__
__module____qualname____doc__r<    r   r   r5   r5   G   s
    #<r   r5   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )	Locationsz High-level client for locations.c                 f    t         t        |   |||       | j                  j                  | _        y N)superrD   r<   r8   projects_locationsservicer:   r8   r-   r;   	__class__s       r   r<   zLocations.__init__R   s'    	)T#FHkB;;11DLr   c                     | j                   j                  | j                  j                  |j	                                     S )z%Get the location with the given name.name)rI   Getr-   (SecretmanagerProjectsLocationsGetRequestRelativeName)r:   location_refs     r   rO   zLocations.GetV   s<    <<>>**, 	? 	./ /r   c                     | j                   j                  |j                               }t        j                  | j
                  |d|d      S )&List secrets returning a pager object.rM   r/   pageSizerI   requestfieldlimitbatch_size_attribute)r-   )SecretmanagerProjectsLocationsListRequestrQ   r   YieldFromListrI   )r:   project_refrY   rW   s       r   ListWithPagerzLocations.ListWithPager\   sP    mmEE%%' F )G ##') )r   r=   )r>   r?   r@   rA   r<   rO   r^   __classcell__rK   s   @r   rD   rD   O   s    (2/
)r   rD   c                        e Zd ZdZd fd	Z	 	 	 	 	 	 	 	 	 	 ddZddZddZddZddZ	ddZ
	 	 	 	 	 	 	 	 	 	 	 dd	Zd
 ZddZ	 ddZ	 ddZ	 ddZ xZS )SecretszHigh-level client for secrets.c                     |xs t        |      }t        t        |   ||       | j                  j
                  | _        y r7   )r   rG   rb   r<   r8   projects_secretsrI   rJ   s       r   r<   zSecrets.__init__l   s4    5y5F	'4!&(3;;//DLr   c                    |xs g }t        | j                  |||      }g }|r2|D ]-  }|j                  | j                  j                  |             / | j                  j                  j                  g       }|r"|D ]  }|j                  j                  |        d}|	s|
r| j                  j                  |	|
      }d}|r| j                  j                  |      }d}| j                  j                  | j                  j                  t        ||      |j                         | j                  j	                  ||||||||||
                  S )zCreate a secret.rM   additionalPropertiesNnextRotationTimerotationPeriodr   )
labelstagsr1   
expireTimettltopicsannotationsrotationr!   versionDestroyTtl)parentsecretIdsecret)r3   r-   r*   TopicSecretAnnotationsValuerg   Rotationr'   rI   Create)SecretmanagerProjectsSecretsCreateRequestGetParentRelativeNameForSecretName)r:   
secret_refr.   r/   rk   rl   expire_timern   r0   next_rotation_timerotation_periodro   rp   regional_kms_key_nameversion_destroy_ttlsecret_locationr1   topics_message_listtopicnew_annotationsannotation_pairrq   customer_managed_encryptions                          r   rz   zSecrets.Createq   sl   & :2D)$--*.0K%""4==#6#6E#6#BC mm**;; < !O(/,,33OD ) H_''-o ( h #'$(MM$K$K* %L %!
 k<<??1*oN__&=='''&*+!*E"5 (  	@ 	
 r   c           	          | j                   j                  | j                  j                  |t	        ||                  S )zDelete a secret.)etagrN   )rI   Deleter-   )SecretmanagerProjectsSecretsDeleteRequestGetRelativeName)r:   r~   r   r   s       r   r   zSecrets.Delete   s=    <<??OJH 	@ 	
 r   c                     | j                   j                  | j                  j                  t	        ||                  S )z#Get the secret with the given name.rM   )rI   rO   r-   &SecretmanagerProjectsSecretsGetRequestr   r:   r~   r   s      r   rO   zSecrets.Get   s:    <<<< _= 	= 	
 r   c                 \    	 | j                  ||      S # t        j                  $ r Y yw xY w)zGAttempt to get the secret, returning None if the secret does not exist.)r~   r   N)rO   apitools_exceptionsHttpNotFoundErrorr   s      r   	GetOrNonezSecrets.GetOrNone   s2    XX_XMM00 s    ++c                     | j                   j                  |j                         |      }t        j                  | j
                  |d|d      S )rT   )rs   filtersecretsrU   rV   )r-   'SecretmanagerProjectsSecretsListRequestrQ   r   r\   rI   )r:   r]   rY   request_filterrW   s        r   r^   zSecrets.ListWithPager   sS    mmCC''). D BG ##') )r   c           	          | j                   j                  t        ||      | j                   j                  | j                   j	                  ||                  }| j
                  j                  |      S )z)Adds a new version of an existing secret.)data
dataCrc32c)payload)rs   addSecretVersionRequest)r-   -SecretmanagerProjectsSecretsAddVersionRequestr   AddSecretVersionRequestSecretPayloadrI   
AddVersion)r:   r~   r   data_crc32cr   rW   s         r   r   zSecrets.AddVersion   sq    mmIIz?; $ E EMM//k 0  !F !
 J G <<""7++r   c                    d}|
s|r| j                   j                  |
|      }g }|r2|D ]-  }|j                  | j                   j                  |             / | j                   j                  j                  g       }|r"|D ]  }|j                  j                  |        | j                   j                  j                  g       }|	r"|	D ]  }|j                  j                  |        d}|r| j                   j                  |      }| j                  j                  | j                   j                  t        ||      | j                   j	                  ||||||||||
      t        |                  S )zUpdate a secret.Nrh   rM   rf   r   )
rk   versionAliasesrp   r   rm   rn   ro   rq   rr   r!   rN   ru   
updateMask)r-   ry   r*   rv   rw   VersionAliasesValuerg   rx   r'   rI   Patch(SecretmanagerProjectsSecretsPatchRequestr   r   )r:   r~   rk   r   r   r   rn   ro   version_aliasesrp   r   r   r   r   r   rq   r   r   new_version_aliasesversion_alias_pairr   r   r   s                          r   UpdatezSecrets.Update   s   & H_''-o ( Oh %""4==#6#6E#6#BC --..BB C ! /
00778JK !0mm**;; < !O(/,,33OD ) #'$(MM$K$K* %L %! <<>> _===''2+&*!"5*E (  )5 	? 	
 r   c           	         t        | j                  |||      }| j                  j                  | j                  j	                  |j                         | j                  j                  |      t        dg                  S )z2Set the replication policy on an existing secret..)r1   r1   r   )r3   r-   rI   r   r   rQ   rw   r   )r:   r~   r.   r/   r0   r1   s         r   SetReplicationzSecrets.SetReplication!  st    )vy$K <<>>((*==''K'@(-9 	? 	
 r   c                 ~   |rQ| j                   j                  | _        | j                  j	                  t        ||      t        j                        }nP| j                   j                  | _        | j                  j                  t        j                  t        ||            }| j                  j                  |      S )zGet iam policy request.

    Args:
      resource_ref: Multitype resource (regional or global secret resource)
      secret_location: location of the secret, None if global

    Returns:
      Operation response
    )resourceoptions_requestedPolicyVersion)r   r   )r8   projects_locations_secretsrI   r-   8SecretmanagerProjectsLocationsSecretsGetIamPolicyRequestr   r
   !MAX_LIBRARY_IAM_SUPPORTED_VERSIONrd   /SecretmanagerProjectsSecretsGetIamPolicyRequestGetIamPolicy)r:   resource_refr   reqs       r   r   zSecrets.GetIamPolicy.  s     [[;;dlMMRR"<A)1)S)S S c
 [[11dlMMII)1)S)S"<A J c <<$$S))r   c                    |r^| j                   j                  | _        | j                  j	                  t        ||      | j                  j                  ||            }n]| j                   j                  | _        | j                  j                  t        ||      | j                  j                  ||            }| j                  j                  |      S )zSet iam policy request.

    Args:
      secret_ref: secret resource
      policy: policy to be set
      update_mask: update mask
      secret_location: location of the secret, None if global

    Returns:
      Operation response
    )r.   r   )r   setIamPolicyRequest)
r8   r   rI   r-   8SecretmanagerProjectsLocationsSecretsSetIamPolicyRequestr   SetIamPolicyRequestrd   /SecretmanagerProjectsSecretsSetIamPolicyRequestSetIamPolicy)r:   r~   r.   r   r   r   s         r   r   zSecrets.SetIamPolicyG  s     [[;;dlMMRR":?"mm?? @  S c [[11dlMMII":?"mm?? @  J c <<$$S))r   c                     | j                  ||      }t        j                  |_        t        j                  | j
                  j                  | j
                  j                  ||||       | j                  |||      S )zAdd iam policy binding request.r   	condition)	r   r
   r   r   "AddBindingToIamPolicyWithConditionr-   BindingExprr   r:   resorce_refmemberroler   r   r.   s          r   AddIamPolicyBindingzSecrets.AddIamPolicyBindingh  s{     {OLF??FN// V_   r   c                     | j                  ||      }t        j                  |_        t        j                  ||||       | j                  |||      S )z"Remove iam policy binding request.r   r   )r   r
   r   r   'RemoveBindingFromIamPolicyWithConditionr   r   s          r   RemoveIamPolicyBindingzSecrets.RemoveIamPolicyBindingz  sa     {OLF??FN44	 V_   r   r=   )
NNNNNNNNNNNNrF   )NNNNNNNNNNN)r>   r?   r@   rA   r<   rz   r   rO   r   r^   r   r   r   r   r   r   r   r_   r`   s   @r   rb   rb   i   s    &0 
 !?B
)
," 
 >@*4 CG*D HL& HLr   rb   c                   ,     e Zd ZdZd fd	ZddZ xZS )SecretsLatestz%High-level client for latest secrets.c                 f    t         t        |   |||       | j                  j                  | _        y rF   )rG   r   r<   r8   projects_secrets_latestrI   )r:   r8   r-   api_versionsrK   s       r   r<   zSecretsLatest.__init__  s'    	-',G;;66DLr   c                     | j                   j                  | j                  j                  t	        ||                  S )z&Access the latest version of a secret.rM   )rI   Accessr-   /SecretmanagerProjectsSecretsLatestAccessRequestr   r   s      r   r   zSecretsLatest.Access  s;    <<EE _= 	F 	
 r   r=   rF   )r>   r?   r@   rA   r<   r   r_   r`   s   @r   r   r     s    -7r   r   c                   ^     e Zd ZdZd
 fd	ZddZddZddZddZddZ	ddZ
	 dd	Z xZS )Versionsz&High-level client for secret versions.c                 f    t         t        |   |||       | j                  j                  | _        y rF   )rG   r   r<   r8   projects_secrets_versionsrI   rJ   s       r   r<   zVersions.__init__  s'    	(D"68[A;;88DLr   c                     | j                   j                  | j                  j                  t	        ||                  S )z&Access a specific version of a secret.rM   )rI   r   r-   1SecretmanagerProjectsSecretsVersionsAccessRequestr   r:   version_refr   s      r   r   zVersions.Access  s;    <<GG o> 	H 	
 r   c           	          | j                   j                  |      }| j                  j                  | j                   j	                  |t        ||                  S )zDestroy a secret version.r   )destroySecretVersionRequestrN   )r-   DestroySecretVersionRequestrI   Destroy2SecretmanagerProjectsSecretsVersionsDestroyRequestr   )r:   r   r   r   destroy_secret_version_requests        r   r   zVersions.Destroy  \    %)]]%N%N &O &"<<HH(F o> 	I 	
 r   c           	          | j                   j                  |      }| j                  j                  | j                   j	                  |t        ||                  S )zDisable a secret version.r   )disableSecretVersionRequestrN   )r-   DisableSecretVersionRequestrI   Disable2SecretmanagerProjectsSecretsVersionsDisableRequestr   )r:   r   r   r   disable_secret_version_requests        r   r   zVersions.Disable  r   r   c           	          | j                   j                  |      }| j                  j                  | j                   j	                  |t        ||                  S )zEnable a secret version.r   )enableSecretVersionRequestrN   )r-   EnableSecretVersionRequestrI   Enable1SecretmanagerProjectsSecretsVersionsEnableRequestr   )r:   r   r   r   enable_secret_version_requests        r   r   zVersions.Enable  s\    $(MM$L$L %M %!<<GG'D o> 	H 	
 r   c                     | j                   j                  | j                  j                  t	        ||                  S )z+Get the secret version with the given name.rM   )rI   rO   r-   .SecretmanagerProjectsSecretsVersionsGetRequestr   r   s      r   rO   zVersions.Get  s;    <<DD o> 	E 	
 r   c                     | j                   j                  t        ||      |      }| j                  j	                  |      S )z!List secrets and return an array.)rs   rU   )r-   /SecretmanagerProjectsSecretsVersionsListRequestr   rI   List)r:   r~   rY   r   rW   s        r   r   zVersions.List  s?    mmKKz?;e L G <<W%%r   c                     | j                   j                  t        ||      |d      }t        j                  | j
                  |d|dd      S )rT   r   )rs   r   rU   versionsrU   )rI   rW   rX   rY   
batch_sizerZ   )r-   r   r   r   r\   rI   )r:   r~   rY   r   r   rW   s         r   r^   zVersions.ListWithPager  sY     mmKKz?; L G
 ##') )r   r=   rF   r   )r>   r?   r@   rA   r<   r   r   r   r   rO   r   r^   r_   r`   s   @r   r   r     s5    .9			& EI)r   r   c                 |    | t         j                  j                  k(  ry| t         j                  j                  k(  ryy)z'Returns api version based on the track.v1beta2v1N)r	   ReleaseTrackBETAGA)tracks    r   GetApiFromTrackr    s5    
d$$$!!$$$ %r   c                     |rQ| j                         }|j                  d      }dj                  |d d       d| dz   dj                  |dd        z   S | j                         S )N/   /locations/)rQ   splitr   )r   r"   resource_urir
  s       r   r   r     si    ((*Ls#E88E"1I;xj!::SXXeABi=PPP				  r   c                     |r&| j                         j                         }|d| z   S | j                         j                         S )Nr	  )ParentrQ   )r   r"   r  s      r   r|   r|     sB    ??$113LKz222			'	'	))r   rF   )rA   
__future__r   r   r   apitools.base.pyr   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper	   googlecloudsdk.command_lib.iamr
   r   r   r   r3   objectr5   rD   rb   r   r   r  r   r|   rB   r   r   <module>r     s    @ &  ' > ' , ( 3FF6<V <) )4_f _D	F  N)v N)b!*r   