
                             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Zg d	Zd
 Zd Zd Zd Z	 	 ddZ	 ddZy)zUtility functions for Cloud KMS integration with GCE.

Collection of methods to handle Cloud KMS (Key Management Service) resources
with Google Compute Engine (GCE).
    )absolute_import)division)unicode_literals)
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                     | syt               }t        D ]4  }t        | |j                  dd      d      s!|j	                  d|z          6 |S )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   '   sH    	e)gtW__S#.5mmD7N#  
    c                 b    | syt               }t        D ]  }|| v s|j                  |        |S )r   N)r   r   r   r   s      r   _GetSpecifiedKmsDictr   2   s4    	e)g$mmG  
r   c           
         | syd }t         j                  j                    || d             d| v r| d   n-t        j                  j
                  j                  j                   || d       || d       || d      dd	      S )
z<Returns the Cloud KMS crypto key name based on the KMS args.Nc                       fd}|S )Nc                  l    v r   nd } | r| S t        j                  ddj                              )Nz--create-diskzKKMS cryptokey resource was not fully specified. Key [{}] must be specified.)calliope_exceptionsInvalidArgumentExceptionformat)valr   keys    r   GetValueFuncz5_DictToKmsKey.<locals>.GetValue.<locals>.GetValueFuncD   s@    DI$c	
88
 &+' 'r    )r   r&   r'   s   `` r   GetValuez_DictToKmsKey.<locals>.GetValueB   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   _DictToKmsKeyr6   =   s    	 
			!	!htY! &3d%:d=!$$,,66t^,t]+tY'
 C 
" 
D Dr   c                 ^    t        |       }|sy|j                  |j                               S )zFReturns the Cloud KMS crypto key name based on the values in the dict.N
kmsKeyName)r6   CustomerEncryptionKeyRelativeName)r   messagesr&   s      r   _DictToMessager=   _   s/    d#			'	'33C3C3E	'	FFr   c                    |r| j                   j                  }d}n3|r| j                   j                  }d}n| j                   j                  }d}|j	                         }|t        |       v r|st        j                  |d      |rA|rt        j                  dgt        |        |j                  |j                               S |S )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-filer8   )CONCEPTSboot_disk_kms_keyinstance_kms_keykms_keyr1   r   r"   r#   ConflictingArgumentsExceptionr:   r;   )r   r<   current_valueboot_disk_prefixinstance_prefixkey_argflagr&   s           r   MaybeGetKmsKeyrJ   g   s    & mm--G Dmm,,GDmm##GD#	!$''

6
6?A A==
:248: :))S5E5E5G)HH	r   c                     t        t        |             r-|rt        j                  |gt	        |        t        | |      S |S )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"   rD   r   r=   )r   r<   rE   conflicting_args       r   MaybeGetKmsKeyFromDictrN      sK    " 

t
$%==
8068 8$))	r   N)FF)r?   )__doc__
__future__r   r   r   googlecloudsdk.callioper   r"   googlecloudsdk.corer   r   KMS_HELP_URLr   r   r   r6   r=   rJ   rN   r(   r   r   <module>rT      s[    '  ' E * ).	DDG %*#(	&X ,=r   