
                         z    d Z ddl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dd	Zd
 Zd Z	 ddZy)zBuild utils.    N)
exceptions)cloudbuild_util)util)iam_util)logz&^\d+@cloudbuild\.gserviceaccount\.com$c                     t        j                         }d|  d| d}|j                  j                  |j                  j                  |            j                  S )z5Gets the default build service account for a project.z	projects/z/locations/z/defaultServiceAccountname)r   GetClientInstanceprojects_locationsGetDefaultServiceAccountMESSAGES_MODULE:CloudbuildProjectsLocationsGetDefaultServiceAccountRequestserviceAccountEmail)
project_idregionclientr
   s       0lib/googlecloudsdk/command_lib/run/build_util.py_GetDefaultBuildServiceAccountr      sc    ,,.&ZLF83I	J$		"	"	;	;WW X 
 	    c                 V    t        j                  d|       }|r|j                  d      S y)zEExtracts the service account email from the service account resource.z/serviceAccounts/([^/]+)$   N)researchgroup)service_accountmatchs     r   _ExtractServiceAccountEmailr   '   s'    
))0/
B%
;;q>r   c                     t        j                         \  }}|j                  j                  |j	                  t        j                  |                   S )Nr	   )iam_api_utilGetClientAndMessagesprojects_serviceAccountsGet$IamProjectsServiceAccountsGetRequestr   EmailToAccountResourceName)service_account_emailr   messagess      r   _DescribeServiceAccountr(   0   sP    !668&(		(	(	,	,33223HI 4 
 r   c                    |t        | |      }t        |      }	 t        j                  t        |      s1t        |      }|j                  rt        j                  d| d      yy# t        j                  $ r t        j                  d| d| d       Y yt        j                  $ r3 t        j                  d| d       t        j                  d	| d
      w xY w)a  Util to validate the default build service account permission.

  Prompt a warning to users if cloudbuild.builds.builder is missing.

  Args:
    project_id: id of project.
    region: region to deploy the service.
    build_service_account: user provided build service account. It will be None,
      if user doesn't provide it.

  Raises:
    ServiceAccountError: if the build service account is disabled/not
    found/missing required permissions.
  Nz~Could not build the function due to disabled service account used by Cloud Build. Please make sure that the service account: [z] is active.zPYour account does not have permission to check details of build service account z). If the deployment fails, please ensure z is active.zThe build service account z does not exist. If you just created this account, or if this is your first time deploying with the default build service account, it may take a few minutes for the service account to become fully available. Please try again later.zBuild service account z does not exist.)r   r   r   r   _LEGACY_BUILD_SA_FORMATr(   disabledserverless_exceptionsServiceAccountErrorapitools_exceptionsHttpForbiddenErrorr   warningHttpNotFoundError)r   r   build_service_accountr&   !build_service_account_descriptions        r   +ValidateBuildServiceAccountAndPromptWarningr4   9   s   $ "::vN56KL88+-BC*A
+' 
+	3	3#77&'|5
 	
 
4	 D 
	/	/  KK	12 3/0	=
 
	.	. 
KK
$%:$; < 	  
3
3
 !6 77GH 
s   A
A' '/CAC)global)N)__doc__r   apitools.base.pyr   r.   !googlecloudsdk.api_lib.cloudbuildr   googlecloudsdk.api_lib.iamr   r    googlecloudsdk.command_lib.iamr   googlecloudsdk.command_lib.runr,   googlecloudsdk.corer   r*   r   r   r(   r4    r   r   <module>r>      sA     	 > = ; 3 N # D  /33r   