
    ]                         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  e	j"                  e	j$                  j&                  e	j$                  j(                         G d
 de	j*                               Zy)zUpdate an existing secret.    )absolute_import)division)unicode_literals)api)base)
exceptions)args)logc                   V    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
Zed        Zd Zd Zd Zy)Updatea  Update a secret replica's metadata.

      Update a secret replica's metadata (e.g. cmek policy). This command will
      return an error if given a secret that does not exist or if given a
      location that the given secret doesn't exist in.

      The --remove-kms-key flag is only valid for Secrets that have an
      automatic replication policy or exist in a single location. To remove
      keys from a Secret with multiple user managed replicas, please use the
      set-replication command.

      ## EXAMPLES

      To remove CMEK from a secret called 'my-secret', run:

        $ {command} my-secret --remove-cmek

      To set the CMEK key on an automatic secret called my-secret to a specified
      KMS key, run:

        ${command} my-secret
        --set-kms-key=projects/my-project/locations/global/keyRings/my-keyring/cryptoKeys/my-key

      To set the CMEK key on a secret called my-secret to a specified KMS key in
      a specified location in its replication, run:

        ${command} my-secret
        --set-kms-key=projects/my-project/locations/us-central1/keyRings/my-keyring/cryptoKeys/my-key
        --location=us-central1


  z9There are no changes to the secret [{secret}] for update.zxThe secret [{secret}] cannot be updated because it does not exist. Please use the create command to create a new secret.zThis secret has a user managed replication polciy. The location in which to set the customer managed encryption key must be set with --location.zxThere was a problem updating replication for this secret. Please use the replication set command to perform this update.zuThis secret has an automatic replication policy. To set its customer managed encryption key, please omit --locations.z4The secret does not have a replica in this location.zEither all replicas must use customer managed encryption or all replicas must use Google managed encryption. To add customer managed encryption to all replicas, please use the replication set command.zGCannot simultaneously set and remove a customer managed encryption key.zCannot remove customer managed encryption keys for just one location. To use Google managed encryption keys for all locations, please remove --locations.c                 `    t        j                  | ddd       t        j                  |        y )Nz	to updateT)purpose
positionalrequired)secrets_args	AddSecretAddUpdateReplicationGroup)parsers    )lib/surface/secrets/replication/update.pyArgszUpdate.Args\   s(    tE**62    c                 ,   t        j                  | j                               }|j                  j                  rMt        j
                  |      j                  |dg g       }t        j
                         j                  |       |S |j                  j                  r|j                  j                  j                  rg }|j                  j                  j                  D ]H  }|j                  st        j                  | j                        |j                  |j                         J t        j
                  |      j                  |d|g       }t        j
                         j                  |       |S t        j                  | j                        )Napi_version	automaticuser-managed)secrets_apiGetApiFromTrackReleaseTrackreplicationr   SecretsSetReplicationsecrets_logUpdatedReplicationuserManagedreplicaslocationr   MisconfiguredReplicationError!MISCONFIGURED_REPLICATION_MESSAGEappend)self
secret_refsecretr   updated_secret	locationsreplicas          r   _RemoveCmekzUpdate._RemoveCmekb   sO   --d.?.?.ABK##"**!z;B7  ..z:%%&*<*<*H*H*Q*Qi''33<<'88446 6))*	 =
 #**!z>9bA  ..z:

2
2..0 0r   c                 J   t        j                  | j                               }|j                  j                  rp|r t        j                  d| j                        t        j                  |      j                  |dg |g      }t        j                         j                  |       |S |j                  j                  r|j                  j                  j                  r|s t        j                  d| j                        g }g }d}	|j                  j                  j                  D ]  }
|
j                   st#        j$                  | j&                        |j)                  |
j                          ||
j                   k(  rd}	|j)                  |       l|
j*                  sy|
j*                  j,                  s|j)                  |
j*                  j,                          |	s t        j.                  d| j0                        t3        |      t3        |      k7  rt#        j4                  | j6                        t        j                  |      j                  |d||      }t        j                         j                  |       |S t#        j$                  | j&                        )Nr'   r   r   FTr   )r   r   r   r    r   calliope_exceptionsBadArgumentExceptionLOCATION_AND_AUTOMATIC_MESSAGEr!   r"   r#   r$   r%   r&   RequiredArgumentExceptionLOCATION_REQUIRED_MESSAGEr'   r   r(   r)   r*   customerManagedEncryption
kmsKeyNameInvalidArgumentExceptionLOCATION_NOT_IN_POLICY_MESSAGElenMisconfiguredEncryptionErrorPARTIALLY_CMEK_MESSAGE)r+   r,   r-   kms_keyr'   r   r.   r/   keysfound_locationr0   s              r   
_SetKmsKeyzUpdate._SetKmsKeyy   s8   --d.?.?.ABK##	!66;;= 	="**!z;WI>  ..z:%%&*<*<*H*H*Q*Q!;;668 	8idn''33<<'88446 6))*w'''.
++g
..73T3T3_3_
++g77BB
C = !::;;= 	=	Y3t9	$55'') 	)"**!z>9dC  ..z:

2
2..0 0r   c                     t        j                  | j                               }|j                  j                  j                         }|j                  s#|j                  st        j                  ddg      |j                  r+|j                  rt        j                  | j                        |j                  r+|j                  rt        j                  | j                        t        j                  |      j                  |      }|>t        j                   d| j"                  j%                  |j'                                     |j                  r| j)                  ||      S | j+                  |||j                  |j                        S )Nz--remove-cmekz--set-kms-keyr   r-   )r-   )r   r   r   CONCEPTSr-   Parseremove_cmekset_kms_keyr3   MinimumArgumentExceptionConflictingArgumentsExceptionREMOVE_AND_SET_CMEK_MESSAGEr'    REMOVE_CMEK_AND_LOCATION_MESSAGEr!   	GetOrNoner:   SECRET_MISSING_MESSAGEformatNamer1   rB   )r+   r	   r   r,   r-   s        r   Runz
Update.Run   sL   --d.?.?.ABK%%++-JD$4$488O
,. .D,,==

*
*, ,DMM==

/
/1 1   [9CCJOF~88


%
%
,
,JOO4E
,
FH H j&11??:vt/?/?OOr   N)__name__
__module____qualname____doc__NO_CHANGES_MESSAGErM   r7   r)   r5   r;   r>   rJ   rK   staticmethodr   r1   rB   rP    r   r   r   r      s    D B >  
< $9 ! = !L 
 P  #
 3 3
0.(0TPr   r   N)rT   
__future__r   r   r   googlecloudsdk.api_lib.secretsr   r   googlecloudsdk.callioper   r   r3   "googlecloudsdk.command_lib.secretsr	   r   r
   r#   ReleaseTracksr   BETAGAUpdateCommandr   rW   r   r   <module>r`      st    ! &  ' = ( E C 9 A D%%**D,=,=,@,@AaPT aP BaPr   