
    K                     j    S r SSKJr  SSKJr  SSKJr  Sr/ SQrS r	S r
S	 rS
 r  SS jr SS jrg)zUtility functions for Cloud KMS integration with GCE.

Collection of methods to handle Cloud KMS (Key Management Service) resources
with Google Compute Engine (GCE).
    )
exceptions)
properties)	resourceszGhttps://cloud.google.com/compute/docs/disks/customer-managed-encryption)kms-keykms-keyringkms-locationkms-projectzboot-disk-kms-keyzboot-disk-kms-keyringzboot-disk-kms-locationzboot-disk-kms-projectzinstance-kms-keyzinstance-kms-keyringzinstance-kms-locationzinstance-kms-projectc                     U (       d  g[        5       n[         H:  n[        XR                  SS5      S5      (       d  M&  UR	                  SU-   5        M<     U$ )3Returns the first KMS related argument as a string.N-_z--)set	_KMS_ARGSgetattrreplaceaddargs	specifiedkeywords      /lib/googlecloudsdk/api_lib/compute/kms_utils.py_GetSpecifiedKmsArgsr   $   sI    	e)gt__S#.55mmD7N#  
    c                 t    U (       d  g[        5       n[         H  nX ;   d  M
  UR                  U5        M     U$ )r   N)r   r   r   r   s      r   _GetSpecifiedKmsDictr   /   s2    	e)gmmG  
r   c           
         U (       d  gS n[         R                  R                  U" U S5      " 5       SU ;   a  U S   O-[        R                  R
                  R                  R                  U" U S5      U" U S5      U" U S5      S.SS	9$ )
z<Returns the Cloud KMS crypto key name based on the KMS args.Nc                    ^ ^ U U4S jnU$ )Nc                  |   > TT;   a  TT   OS n U (       a  U $ [         R                  " SSR                  T5      5      e)Nz--create-diskzKKMS cryptokey resource was not fully specified. Key [{}] must be specified.)calliope_exceptionsInvalidArgumentExceptionformat)valr   keys    r   GetValueFunc5_DictToKmsKey.<locals>.GetValue.<locals>.GetValueFuncA   s@    DI$c	
88
 &+' 'r    )r   r#   r$   s   `` r   GetValue_DictToKmsKey.<locals>.GetValue?   s    ' r   r   r	   r   r   )
projectsIdlocationsId
keyRingsIdcryptoKeysIdz/cloudkms.projects.locations.keyRings.cryptoKeys)params
collection)r   REGISTRYParser   VALUEScoreproject	GetOrFail)r   r'   s     r   _DictToKmsKeyr5   :   s    	 
			!	!tY! &3d%:d=!$$,,66t^,t]+tY'
 C 
" 
D Dr   c                 b    [        U 5      nU(       d  gUR                  UR                  5       S9$ )zFReturns the Cloud KMS crypto key name based on the values in the dict.N
kmsKeyName)r5   CustomerEncryptionKeyRelativeName)r   messagesr#   s      r   _DictToMessager<   \   s/    d#			'	'33C3C3E	'	FFr   c                    U(       a  U R                   R                  nSnO8U(       a  U R                   R                  nSnOU R                   R                  nSnUR	                  5       nU[        U 5      ;   a  U(       d  [        R                  " US5      eU(       aD  U(       a   [        R                  " S/[        U 5      Q76 eUR                  UR                  5       S9$ U$ )a  Gets the Cloud KMS CryptoKey reference from command arguments.

Args:
  args: Namespaced command line arguments.
  messages: Compute API messages module.
  current_value: Current CustomerEncryptionKey value.
  boot_disk_prefix: If the key flags have the 'boot-disk' prefix.
  instance_prefix: If the key flags have the 'instance' prefix.

Returns:
  CustomerEncryptionKey message with the KMS key populated if args has a key.
Raises:
  ConflictingArgumentsException if an encryption key is already populated.
z--boot-disk-kms-keyz--instance-kms-keyz	--kms-keyz/KMS cryptokey resource was not fully specified.--csek-key-filer7   )CONCEPTSboot_disk_kms_keyinstance_kms_keykms_keyr0   r   r   r    ConflictingArgumentsExceptionr9   r:   )r   r;   current_valueboot_disk_prefixinstance_prefixkey_argflagr#   s           r   MaybeGetKmsKeyrI   d   s    & mm--G Dmm,,GDmm##GD#	!$''

6
6?A A==
:248: :))S5E5E5G)HH	r   c                     [        [        U 5      5      (       a2  U(       a   [        R                  " U/[	        U 5      Q76 e[        X5      $ U$ )a  Gets the Cloud KMS CryptoKey reference for a boot disk's initialize params.

Args:
  args: A dictionary of a boot disk's initialize params.
  messages: Compute API messages module.
  current_value: Current CustomerEncryptionKey value.
  conflicting_arg: name of conflicting argument

Returns:
  CustomerEncryptionKey message with the KMS key populated if args has a key.
Raises:
  ConflictingArgumentsException if an encryption key is already populated.
)boolr   r   rC   r   r<   )r   r;   rD   conflicting_args       r   MaybeGetKmsKeyFromDictrM      sL    " 

t
$%%==
8068 8$))	r   N)FF)r>   )__doc__googlecloudsdk.callioper   r   googlecloudsdk.corer   r   KMS_HELP_URLr   r   r   r5   r<   rI   rM   r&   r   r   <module>rR      sS    F * ).	DDG %*#(	&X ,=r   