
    ^                        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mZ ddlmZ ddlmZ dEdZdEdZdEdZdEdZdEdZdEdZdEdZdEdZdEdZdFdZdEdZdEdZdEdZ d Z!d Z"d Z#dEdZ$dEd Z%	 dEd!ejL                  d"e'd#dfd$Z(d% Z)d& Z*d' Z+d( Z,d) Z-d* Z.dEd+Z/d, Z0dGd-Z1dHd.Z2	 dId/Z3d0 Z4d1 Z5d2 Z6d3 Z7d4 Z8d5 Z9d6 Z:d7 Z;d8 Z<d9 Z=d: Z>d; Z?d< Z@d= ZAd> ZBd? ZCdJd@eDdAeDfdBZEdC ZFdKdDZGy)Lz$Shared resource arguments and flags.    )absolute_import)division)unicode_literals)arg_parsers)base)parser_arguments)concepts)	multitype)
completers)concept_parsers)presentation_specs)	resourcesc                 D     | j                   t        d|      fddd| y )Nz	data-filePATHz]File path from which to read secret data. Set this to "-" to read the secret data from stdin.metavarhelpadd_argument
_ArgOrFlagparser
positionalkwargss      .lib/googlecloudsdk/command_lib/secrets/args.pyAddDataFiler   #   s3    &j)*
 	    c                 D     | j                   t        d|      fddd| y )Nzout-filezOUT-FILE-PATHz~File path to which secret data is written. If this flag is not provided secret data will be written to stdout in UTF-8 format.r   r   r   s      r   
AddOutFiler   ,   s4    &Z(N
 	r   c                     t        j                  j                  dt        d|      t	               dd|j                  |        y )NprojectzThe project ID.nameresource_spec
group_help )r   ConceptParserForResourcer   GetProjectResourceSpecAddToParserr   s      r   
AddProjectr+   5   sD    ++ i,*," 		 F#r   c                     t        j                  j                  dt        d|      t	               dj                  |      d|j                  |        y )NlocationzThe location {}.r"   r&   )r   r'   r(   r   GetLocationResourceSpecformatr*   r   purposer   r   s       r   AddLocationr2   =   sO    ++ j*-+-#**73 		 F#r   c                 D     | j                   t        d|      fddd| y )Nreplication-policy-fileREPLICATION-POLICY-FILEJSON or YAML file to use to read the replication policy. The file must conform to https://cloud.google.com/secret-manager/docs/reference/rest/v1/projects.secrets#replication.Set this to "-" to read from stdin.r   r   r   s      r   AddReplicationPolicyFiler7   E   s4    &*J7'0	 	r   c                 D     | j                   t        d|      fddd| y )Nkms-key-nameKMS-KEY-NAMEyGlobal KMS key with which to encrypt and decrypt the secret. Only valid for secrets with an automatic replication policy.r   r   r   s      r   AddKmsKeyNamer<   Q   s4    &,F
 	r   c                 D     | j                   t        d|      fddd| y )Nset-kms-keySET-KMS-KEYENew KMS key with which to encrypt and decrypt future secret versions.r   r   r   s      r   AddSetKmsKeyNamerA   Z   s1    &
+
Q	 	r   c                 D     | j                   t        d|      fddd| y )Nremove-cmek
store_truetRemove customer managed encryption key so that future versions will be encrypted by a Google managed encryption key.actionr   r   r   s      r   AddRemoveCmekrH   d   s3    &
+=	 	r   c                 D     | j                   t        d|      fddd| y )Nr-   REPLICA-LOCATIONdLocation of replica to update. For secrets with automatic replication policies, this can be omitted.r   r   r   s      r   AddReplicaLocationrL   n   s3    &Z( 9
 	r   Nc                     |dj                  |      }t        j                  j                  dt	        d|      t               |d|j                  |        y)a7  Add secret resource argument to the parser.

  Args:
    parser: The parser to add the argument to.
    purpose: The purpose of the secret, used to generate the help text.
    positional: Whether the argument is positional.
    help_text: The help text to use for the argument.
    **kwargs: Extra arguments.
  NzThe secret {}.secretr"   r&   )r/   r   r'   r(   r   GetSecretResourceSpecr*   )r   r1   r   	help_textr   s        r   	AddSecretrQ   w   s\      ''0I++ h
+)+ 		
 Kr   c                     t        j                  j                  dt        d|      t	               dj                  |      d|j                  |        y )NversionzNumeric secret version {}.r"   r&   r   r'   r(   r   GetVersionResourceSpecr/   r*   r0   s       r   
AddVersionrV      sO    ++ i,*,.66w? 		 F#r   c                     t        j                  j                  dt        d|      t	               dj                  |      d|j                  |        y )NrS   z_Numeric secret version {} or a configured alias (including 'latest' to use the latest version).r"   r&   rT   r0   s       r   AddVersionOrAliasrX      sM    ++ i,*,
mw 	 F#r   c                      | j                   t        d|      fdt        j                         t        j                  dd| y )NtopicsTOPICSz2List of Pub/Sub topics to configure on the secret.r   typerG   r   )r   r   r   ArgListUpdateActionr   s      r   	AddTopicsr`      sF    &:& %%@ 	r   c                 f   | j                  dd      }|j                  t        dd      dt        j                         t        j
                  d       |j                  t        d	d      d
t        j                         t        j
                  d       |j                  t        dd      dd       y)z2Add flags for specifying topics on secret updates.TzTopics.mutexr   z
add-topicsFz
ADD-TOPICSz,List of Pub/Sub topics to add to the secret.r\   zremove-topicszREMOVE-TOPICSz1List of Pub/Sub topics to remove from the secret.zclear-topicsrD   z)Clear all Pub/Sub topics from the secret.rF   N)	add_groupr   r   r   r^   r_   r   groups     r   AddUpdateTopicsGrouprg      s     

I

6%u% %%:  = %( %%?  B '7  :r   c                    | j                  dd      }|j                  t        dd      dd       |j                  d	
      }|j                  t        dd      dd       |j                  t        dd      dd       y)z4Add flags for specifying replication policy updates.TzReplication update.rb   rC   FrD   rE   rF   zCMEK Update.r   r>   r?   r@   r   r-   rJ   rK   Nrd   r   r   r   rf   subgroups      r   AddUpdateReplicationGrouprm      s    


,A

B%&=	  ? __._1(
&
Q	  	 U# 9  ;r   c                    | j                  dd      }|j                  t        dd      dd       |j                  d	
      }|j                  t        dd      dd       |j                  t        dd      dd       |j                  t        dd      t        j                  dt        j
                         d       y)z?Add flags for specifying replication policy on secret creation.TzReplication policy.rb   r4   Fr5   r6   r   zInline replication arguments.ri   zreplication-policyPOLICYzThe type of replication policy to apply to this secret. Allowed values are "automatic" and "user-managed". If user-managed then --locations must also be provided.r9   r:   r;   	locationsLOCATIONzKComma-separated list of locations in which the secret should be replicated.)rG   r   r]   r   N)rd   r   r   r   r_   r^   rk   s      r   AddCreateReplicationPolicyGrouprr      s     

,A

B%*E2'0	  2 __"A_B(
%u-1  3 'F  H e$%%   r   c                 j     | j                   t        d|      fdt        j                         dd| y)z?Add flags for specifying version destroy ttl on secret creates.version-destroy-ttlVERSION-DESTROY-TTLa8  Secret Version Time To Live (TTL) after destruction request. For secret with TTL>0, version destruction does not happen immediately on calling destroy; instead, the version goes to a disabled state and destruction happens after the TTL expires. See `$ gcloud topic datetimes` for information on duration formats.r   r]   r   N)r   r   r   Durationr   s      r   AddCreateVersionDestroyTTLrx      sA    &&
3#!P 	r   c                     | j                  dd      } |j                  t        d|      fdt        j                         dd|  |j                  t        dd	      fd
dd| y)z?Add flags for specifying version destroy ttl on secret updates.TzVersion destroy ttl.rb   rt   ru   a)  Secret Version TTL after destruction request. For secret with TTL>0, version destruction does not happen immediately on calling destroy; instead, the version goes to a disabled state and destruction happens after the TTL expires. See `$ gcloud topic datetimes` for information on duration formats.rv   zremove-version-destroy-ttlFrD   z8If set, removes the version destroy TTL from the secret.rF   N)rd   r   r   r   rw   r   r   r   rf   s       r   AddUpdateVersionDestroyTTLr{     s    


,B

C%%&
3#!P 	 %-u5E 		r   r   r   returnc                     | j                  dd      } |j                  t        d|      fddd|  |j                  t        dd	      fd
dd| y)zAdd flags for specifying regional cmek on secret updates.

  Args:
      parser: Given argument parser.
      positional : Whether the argument is positional.
      **kwargs: Extra arguments.
  Tzregional kms key.rb   regional-kms-key-namezREGIONAL-KMS-KEY-NAMEz*regional kms key name for regional secret.r   zremove-regional-kms-key-nameFrD   z%If set, removes the regional kms key.rF   Nrj   rz   s       r   AddUpdateRegionalKmsKeyr     s|     

,?

@%%(*5%7 		 %/72 		r   c                     | j                  dd      }|j                  t        dd      dd       |j                  t        d	d      d
d       y)z6Add flags for specifying expiration on secret creates.TExpiration.rb   expire-timeFEXPIRE-TIME6Timestamp at which to automatically delete the secret.r   ttlTTLhDuration of time (in seconds) from the running of the command until the secret is automatically deleted.Nrj   re   s     r   AddCreateExpirationGroupr   :  sd     

M

:%&D  G 1	  3r   c                     | j                  dd      }|j                  t        dd      dd       |j                  t        d	d      d
d       |j                  t        dd      dd       y)z7Add flags for specifying expiration on secret updates..Tr   rb   r   Fr   r   r   r   r   r   zremove-expirationrD   zAIf set, removes scheduled expiration from secret (if it had one).rF   Nrj   re   s     r   AddUpdateExpirationGroupr   J  s     

M

:%&D  G 1	  3 $e,
M	  Pr   c                     | j                  dd      }|j                  t        dd      d       |j                  t        dd      d       y	)
z4Add flags for specifying rotation on secret creates.F	Rotation.rb   next-rotation-time1Timestamp at which to send rotation notification.ri   rotation-period=Duration of time (in seconds) between rotation notifications.Nrj   re   s     r   AddCreateRotationGroupr   _  s_     

[

9%%u-?  B "E*K  Nr   c                 R   | j                  dd      }|j                  t        dd      d       |j                  t        dd      dd	
       |j                  t        dd      d       |j                  t        dd      dd
       |j                  t        dd      dd
       y)z5Add flags for specifying rotation on secret updates..Fr   rb   r   r   ri   zremove-next-rotation-timerD   z8Remove timestamp at which to send rotation notification.rF   r   r   zremove-rotation-periodzVIf set, removes the rotation period, cancelling all rotations except for the next one.zremove-rotation-schedulez.If set, removes rotation policy from a secret.Nrj   re   s     r   AddUpdateRotationGroupr   k  s     

[

9%%u-?  B ,e4F  I "E*K  N )51%	  	 +U3<  ?r   c                 `    | j                  t        dd      ddj                  |             y)z0Add flag for specifying the current secret etag.etagFETAGzCurrent entity tag (ETag) of the secret. If specified, the secret is {action} only if the ETag provided matches the current secret's ETag.rG   r   Nr   r   r/   r   rG   s     r   AddSecretEtagr     s5     	f  r   c                 `    | j                  t        dd      ddj                  |             y)z8Add flag for specifying the current secret version etag.r   Fr   zCurrent entity tag (ETag) of the secret version. If specified, the version is {action} only if the ETag provided matches the current version's ETag.r   r   Nr   r   s     r   AddVersionEtagr     s5     	f  r   c                 D     | j                   t        d|      fddd| y)z2Add flag for specifying the regional KMS key name.r~   r:   z_Regional KMS key with which to encrypt and decrypt the secret. Only valid for regional secrets.r   Nr   r   s      r   AddRegionalKmsKeyNamer     s4    &(*5(	 	r   c                 h    |r | j                         j                  dd      S dj                  |       S )zReturns the argument name in resource argument format or flag format.

  Args:
      name (str): name of the argument
      positional (bool): whether the argument is positional

  Returns:
      arg (str): the argument or flag
  -_z--{})upperreplacer/   )r#   r   s     r   r   r     s/     ::<S))	t	r   c           	          t        j                  dt               t               fddi|}t	        j
                  t        j                  d||dd      g      j                  |        y)a  Adds a secret resource.

  Secret resource can be global secret or regional secret. If command has
  "--location" then regional secret will be created or else global secret will
  be created.
  Regionl secret - projects/<project>/locations/<location>/secrets/<secret>
  Global secret - projects/<project>/secrets/<secret>

  Args:
      parser: given argument parser
      purpose: help text
      **kwargs: extra arguments
  zglobal or regional secretallow_inactiveTrN   requiredhiddenN)	r
   MultitypeResourceSpecrO   GetRegionalSecretResourceSpecr   r'   r   !MultitypeResourcePresentationSpecr*   )r   r1   r   secret_or_region_secret_specs       r   AddGlobalOrRegionalSecretr     sv     "+!@!@!#%" 	"
 	" ::

&
!  [r   c           	          t        j                  dt               t               fddi|}t	        j
                  t        j                  d||dd      g      j                  |        y)zAdds a version resource.

  Args:
      parser: given argument parser
      purpose: help text
      **kwargs: extra arguments
  !global or regional secret versionr   TrS   r   N	r
   r   rU   GetRegionalVersionResourceSpecr   r'   r   r   r*   r   r1   r   global_or_region_version_specs       r   AddGlobalOrRegionalVersionr     sv     #,"A"A)$&# 	#
 	# ::

'
!  [r   c           	          t        j                  dt               t               fddi|}t	        j
                  t        j                  d||dd      g      j                  |        y)zAdds a version resource or alias.

  Args:
      parser: given argument parser
      purpose: help text
      **kwargs: extra arguments
  r   r   TrS   r   Nr   r   s       r   !AddGlobalOrRegionalVersionOrAliasr     sv     #,"A"A)$&# 	#
 	# ::

'
!  [r   c                  "    t         j                  S )N)r	    DEFAULT_PROJECT_ATTRIBUTE_CONFIGr&   r   r   GetProjectAttributeConfigr     s    		2	22r   c                  8    t        j                  ddddid      S )Nr-   zThe location of the {resource}.	fieldMaskr#   r#   rP   completion_request_paramscompletion_id_fieldr	    ResourceParameterAttributeConfigr&   r   r   GetLocationAttributeConfigr     s&    		2	21!,f 5 	
" "r   c                  8    t        j                  ddddid      S )z3Returns the attribute config for location resource.r-   z.[EXPERIMENTAL] The location of the {resource}.r   r#   r   r   r&   r   r   "GetLocationResourceAttributeConfigr   "  s&    		2	2
:!,f 5 
 r   c                  N    t        j                  ddt        j                        S )NrN   The secret of the {resource}.r#   rP   	completerr	   r   secrets_completersSecretsCompleterr&   r   r   GetSecretAttributeConfigr   .  s$    		2	2/"33
5 5r   c                  N    t        j                  ddt        j                        S )z1Returns the attribute config for regional secret.rN   r   r   r   r&   r   r    GetRegionalSecretAttributeConfigr   5  s$    		2	2/"33
 r   c                  8    t        j                  ddddid      S )NrS   The version of the {resource}.r   r#   r   r   r&   r   r   GetVersionAttributeConfigr   >  s&    		2	20!,f 5 	
" "r   c                  8    t        j                  ddddid      S )z9Returns the attribute config for regional secret version.rS   r   r   r#   r   r   r&   r   r   !GetRegionalVersionAttributeConfigr   F  s&    		2	20!,f 5 	
 r   c                  F    t        j                  ddddt                     S )Nzsecretmanager.projectsr!   projectsF)resource_collectionresource_nameplural_namedisable_auto_completers
projectsId)r	   ResourceSpecr   r&   r   r   r)   r)   S  s'    			2#*,
. .r   c            	      X    t        j                  ddddt               t                     S )Nz secretmanager.projects.locationsr-   rp   F)r   r   r   r   locationsIdr   )r	   r   r   r   r&   r   r   r.   r.   \  s-    			<#,.*,
. .r   c            	      X    t        j                  ddddt               t                     S )Nsecretmanager.projects.secretsrN   secretsF)r   r   r   r   	secretsIdr   )r	   r   r   r   r&   r   r   rO   rO   f  s-    			:#(**,
. .r   c            
      j    t        j                  ddddt               t               t	                     S )N'secretmanager.projects.secrets.versionsrS   F)r   r   r   
versionsIdr   r   )r	   r   r   r   r   r&   r   r   rU   rU   p  s3    			/#*,(**,
. .r   c            
      j    t        j                  ddddt               t               t	                     S )z.Returns the resource spec for regional secret.z(secretmanager.projects.locations.secretszregional secretr   F)r   r   r   r   r   r   r   )r	   r   r   r   r   r&   r   r   r   r   {  s3    			D%#02*,46
 r   c                  |    t        j                  ddddt               t               t	               t                     S )z6Returns the resource spec for regional secret version.1secretmanager.projects.locations.secrets.versionszregional versionrS   F)r   r   r   r   r   r   r   r   )r	   r   r   r   r   r   r&   r   r   r   r     s9    			M&#2402*,46	
 	r   c                 D    t         j                  j                  | d      S )Nr   
collectionr   REGISTRYParserefs    r   ParseSecretRefr     s&    				!	!	6 
" 
 r   c                 D    t         j                  j                  | d      S )Nr   r   r   r   s    r   ParseVersionRefr     s&    				!	!	? 
" 
 r   c                 D    t         j                  j                  | d      S )zParses regional section version into 'secretmanager.projects.locations.secrets.versions' format .

  Args:
    ref: resource name of regional secret version.
  Returns:
    Parsed secret version.
  r   r   r   r   s    r   ParseRegionalVersionRefr     s(     
			!	!	I 
" 
 r   r   api_versionc                       fd}|S )aA  Returns a function which turns a resource into a uri.

  Example:
    class List(base.ListCommand):
      def GetUriFunc(self):
        return MakeGetUriFunc(self)

  Args:
    collection: A command instance.
    api_version: api_version to be displayed.

  Returns:
    A function which can be returned in GetUriFunc.
  c                     t         j                  j                         }|j                  d       |j	                  | j
                        }|j                         S )Nsecretmanagerr   )r   r   CloneRegisterApiByNamer   r#   SelfLink)resourceregistryparsedr   r   s      r   _GetUrizMakeGetUriFunc.<locals>._GetUri  sJ    !!'')H<^^HMMj^AF??r   r&   )r   r   r   s   `` r   MakeGetUriFuncr     s      
.r   c            	          g d} t        j                  ddt        j                         t        j                  dj                  |             S )z(Makes the base.Argument for --tags flag.)z%List of tags KEY=VALUE pairs to bind.zEach item must be expressed asz4`<tag-key-namespaced-name>=<tag-value-short-name>`.
z?Example: `123/environment=production,123/costCenter=marketing`
z--tagsz	KEY=VALUE
r\   )r   Argumentr   ArgDictr_   join)
help_partss    r   
GetTagsArgr    sC    * 
 %%99Z 
 r   c           
          t        | |      }|sy |t        |j                               D cg c]  \  }}|j                  ||       c}}      S c c}}w )zMakes the tags message object.N)keyvalue)additionalProperties)getattrsorteditemsAdditionalProperty)argstags_messagetags_arg_nametagsr  r	  s         r   GetTagsFromArgsr    s`    	}	%$		tzz|,,.,*#u %%#U%;,,. 
/ / ,.s   A
)F) FN)zcreate a secret)zcreate a version)zcreate a version alias)v1)r  )H__doc__
__future__r   r   r   googlecloudsdk.callioper   r   r    googlecloudsdk.calliope.conceptsr	   r
   "googlecloudsdk.command_lib.secretsr   r   (googlecloudsdk.command_lib.util.conceptsr   r   googlecloudsdk.corer   r   r   r+   r2   r7   r<   rA   rH   rL   rQ   rV   rX   r`   rg   rm   rr   rx   r{   ArgumentInterceptorboolr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r.   rO   rU   r   r   r   r   r   strr   r  r  r&   r   r   <module>r      s`   + &  ' / ( 4 5 6 O D G )$$	($$:,;.D"4 00 		63 P*	N?6		
B8 -@3"	5"....
"
s  2"/r   