
    }                     V   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ZddlmZm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ  ej$                  d
      Z ej$                  d      Z ej$                  d      Zej,                  ZdededdfdZdededdfdZdeddfdZdede	e   ddfdZy)z4Utility for the CMEK and user-provided AR use cases.    )absolute_import)division)print_function)unicode_literalsN)AnyOptional)
exceptions)http_clientz`^projects/[^/]+/locations/(?P<location>[^/]+)/keyRings/[a-zA-Z0-9_-]+/cryptoKeys/[a-zA-Z0-9_-]+$zc^projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/repositories/[a-z]([a-z0-9-]*[a-z0-9])?$zG^(?P<location>.*)-docker.pkg.dev\/(?P<project>[^\/]+)\/(?P<repo>[^\/]+)kms_keyfunction_refreturnc                     t         j                  |       }|rR|j                  d      }|dk(  rt        j                  dd      |j
                  |k7  rt        j                  dd      yy)a  Checks that the KMS key is compatible with the function.

  Args:
    kms_key: Fully qualified KMS key name.
    function_ref: Function resource reference.

  Raises:
    InvalidArgumentException: If the specified KMS key is not compatible with
      the function.
  locationglobalz	--kms-keyz$Global KMS keyrings are not allowed.zBKMS keyrings should be created in the same region as the function.N)_KMS_KEY_REsearchgroupbase_exceptionsInvalidArgumentExceptionlocationsId)r   r   kms_key_matchkms_keyring_locations       1lib/googlecloudsdk/api_lib/functions/cmek_util.pyValidateKMSKeyForFunctionr   0   s}     $$W--(..z:x'44
=  #7744

N  8     docker_repositoryc                    | y|j                   }|j                  }t        j                  |       }|r#|j	                  d      }|j	                  d      }n>t
        j                  |       }|r#|j	                  d      }|j	                  d      }nd}d}|r@||k7  r;|j                         |j                         k(  rt        j                  dd| d      |r ||k7  rt        j                  dd| d      yy)a<  Checks that the Docker repository is compatible with the function.

  Args:
    docker_repository: Fully qualified Docker repository resource name.
    function_ref: Function resource reference.

  Raises:
    InvalidArgumentException: If the specified Docker repository is not
      compatible with the function.
  Nprojectr   z--docker-repositoryzLCross-project repositories are not supported: the repository should be in `$z`.zMCross-location repositories are not supported: the repository should be in `$)	
projectsIdr   _DOCKER_REPOSITORY_REr   r   #_DOCKER_REPOSITORY_DOCKER_FORMAT_REisdigitr   r   )r   r   function_projectfunction_location
repo_matchrepo_projectrepo_locationrepo_match_docker_formats           r   #ValidateDockerRepositoryForFunctionr)   I   s'    
!,,"..$++,=>*##I.L$$Z0MBII   -33I>l.44Z@mml 
l
*

"
"
$(<(<(>
>

2
2	!""	&  (M9

2
2	%&b	*  :]r   c                     | | S t         j                  |       }|rF|j                  d      }|j                  d      }|j                  d      }dj                  |||      S | S )zNormalizes the docker repository name to the standard resource format.

  Args:
    docker_repository: Fully qualified Docker repository name.

  Returns:
    The name in a standard format supported by the API.
  r   r   repoz(projects/{}/locations/{}/repositories/{})r!   r   r   format)r   r(   r   r   names        r   NormalizeDockerRepositoryFormatr.   ~   s{     @GG &,,Y7G'--j9H#))&1D5<<4  
r   http_exceptionc                     |rC| j                   t        j                  k(  r%t        j                  dj                  |            y y )Na3  An error occurred. Ensure that the KMS key {kms_key} exists and the Cloud Functions service account has encrypter/decrypter permissions (roles/cloudkms.cryptoKeyEncrypterDecrypter) on the key. If you have recently made changes to the IAM config, wait a few minutes for the config to propagate and try again.)r   )status_coder
   INTERNAL_SERVER_ERRORr	   FunctionsErrorr,   )r/   r   s     r   ProcessExceptionr4      sN     

$
$(I(I
I 
#
#	5 6<VGV5L 	 J r   )__doc__
__future__r   r   r   r   retypingr   r   apitools.base.pyr	   http_exceptions#googlecloudsdk.api_lib.functions.v1googlecloudsdk.callioper   	six.movesr
   compiler   r    r!   	HttpError
_HttpErrorstrr   r)   r.   r4    r   r   <module>rC      s    ; &  % ' 	   : : A ! bjj" #

0  '1bjjN' #
 &&
s # $ 222*-2	2js t 4)1#	r   