
    w<                        S r SSKJr  SSKJr  SSKJr  SSKrSSKJ	r	  SSK
r
SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SS
KJr  SSKJr  SSKJr  SSKJr  SS jr " S S\5      r g)z$API client library for Applications.    )annotations)Sequence)partialN)Any)
exceptions)projects_api)utils)util)base)iam_util)log)
propertiesc                    Ub  [        U5      O[        [        R                  " 5       5      nUR                  S5      tp4S[	        U5      -
  S-
  nU S[        SU5       R                  S5      nU SU 3$ )aF  Constructs a unique service account name with a UUID suffix.

The service account ID must be between 6 and 30 characters.

Args:
  application_id: str, The ID of the application.
  uuid_value: uuid.UUID, Optional. A UUID value to use instead of generating
    a new one.

Returns:
  str, The constructed service account name.
N-      )struuiduuid4splitlenmaxrstrip)application_id
uuid_valueuuid_struuid_suffix_max_app_id_lenapp_id_parts          8lib/googlecloudsdk/api_lib/design_center/applications.pyConstructServiceAccountNamer"   %   ss     !+ 6S_C

<M(NN3'/+K((1,.6A~ 67>>sC+=+	''    c                     \ rS rSrSr\R                  R                  4S jrSS\	R                  4       SS jjr  SS jr  SS jrS r  SS jrS
S	S	S
S.           SS jjrSS jr   S       SS jjrSrg	)ApplicationsClient<   z1Client for Applications in the Design Center API.c                    [         R                  " U5      U l        [         R                  " U5      U l        U R                  R
                  U l        g N)r	   GetClientInstanceclientGetMessagesModulemessages&projects_locations_spaces_applications_service)selfrelease_tracks     r!   __init__ApplicationsClient.__init__?   s7    ))-8DK++M:DMKKFFDMr#      c                P   Sn[        U5       H  n U" 5           g   X#-  n
[        R
                  " SU
U5        [        R                  " SX4-  5      Ue! [        R                   a=  n	X#-  n
[        R                  " SU
US-   UU	SS9  XS-
  :  a  U" U5        U	n Sn	A	M  Sn	A	ff = f)z&Retries an IAM operation on HttpError.NTz%s (Attempt %d/%d): %rr   )exc_infoz%s (Failed after %d attempts).)rangeapitools_exceptions	HttpErrorr   warningerrorr   Error)r/   operation_fnerror_message_fmterror_message_argsmax_retriesdelay_secondssleeperlast_exceptionieformatted_base_messages              r!   _RetryIamOperation%ApplicationsClient._RetryIamOperationD   s     N;  " /CII(
 

(,B+PP' !** !2!G$"E	
 Q
-
 s   AB%(2B  B%c               H    U H  nU R                  XU4S jSXT45        M     g)ai  Adds a list of roles to a service account on a specific project.

Args:
  project_ref: The project resource reference.
  service_account_email: str, The email address of the service account.
  roles: Sequence[str], A sequence of role names to grant.
  project_id: str, The ID of the project.

Raises:
  exceptions.Error: If adding any role fails after retries.
c                8    [         R                  " U SU 3U5      $ )NserviceAccount:)r   AddIamPolicyBinding)prsaeroles      r!   <lambda>GApplicationsClient._AddRolesToServiceAccountOnProject.<locals>.<lambda>   s     ..#C5)r#   z6Failed to add role %s to service account on project %sN)rF   )r/   project_refservice_account_emailroles
project_idrole_to_grants         r!   "_AddRolesToServiceAccountOnProject5ApplicationsClient._AddRolesToServiceAccountOnProjectj   s2    & 
  C
%
 r#   c                    U Hc  nUR                   n[        R                  " U5      nU R                  UUUR                  US9  [
        R                  R                  SU-  5        Me     g)a  Adds required roles to the service account for each project parameter.

Args:
  project_parameters: A sequence of project parameter objects.
  service_account: str, The email address of the service account.

Raises:
  exceptions.Error: If adding any role fails.
)rR   rS   rT   zLSuccessfully added required project roles to service account for project %s.N)	projectIdcrm_utilParseProjectrV   rS   r   statusPrint)r/   project_parametersservice_accountppcurrent_project_idcurrent_project_refs         r!   _AddServiceAccountRoles*ApplicationsClient._AddServiceAccountRoles   sn     !<<$112DE
--
 /'	 .  
jj/0 !r#   c                  ^^^ [         R                  " U5      n[        R                  " US/5      n	U	R                  =(       d    / n
SU
;  a  [
        R                  " SU S35      eUnUb  UR                  S5      S   nO[        U5      nU SU S3nU R                  R                  U R                  R                  US95      nUR                  n[        R                  R!                  S5        [        R                  R!                  S	U-  5        TR"                  R%                  TR'                  [(        R*                  " U5      TR-                  UTR/                  S
U 3S9S9S95        [        R                  R!                  SU-  5        U R1                  X5        UU4S jmU4S jnU4S jn[3        XU5      nU R5                  USX45        [        R                  R!                  SU-  5        [        R                  " [         R                  " U5      5      R6                  n[8        R:                  " 5       nSU SU 3n[3        UUU5      nU R5                  USUU45        [        R                  R!                  S5        SU SU 3$ )a  Creates and configures a service account for application deployment.

Args:
  application_id: str, The ID of the application.
  name: str, The full resource name of the Application.
  project: str, The project ID.
  service_account: str, The email address of the service account, or None.
  user_account: str, The email address of the user.
  iam_client: The API client for IAM service accounts.
  iam_messages: The API messages module for IAM service accounts.

Returns:
  str, The fully qualified service account resource name on success, or None
  on failure.
Raises:
  exceptions.Error: If service account creation fails for reasons other than
  409.
ziam.serviceAccounts.createzDUser does not have permission to create service accounts in project z1. Required permission: iam.serviceAccounts.create@r   z.iam.gserviceaccount.comnamez@Successfully described application and retrieved required roles.z,Creating service account for application: %szService account for )displayName)	accountIdserviceAccount)rh   createServiceAccountRequestz(Successfully created service account: %sc           	     4  > [         R                  " U 5      nTR                  R                  TR	                  US95      n[         R
                  " TR                  UUS5        TR                  R                  TR                  UTR                  US9S95        g)z<Helper function to grant actAs permission to a given member.)resourcezroles/iam.serviceAccountUser)policy)rn   setIamPolicyRequestN)
r   EmailToAccountResourceNameprojects_serviceAccountsGetIamPolicy-IamProjectsServiceAccountsGetIamPolicyRequestAddBindingToIamPolicyBindingSetIamPolicy-IamProjectsServiceAccountsSetIamPolicyRequestSetIamPolicyRequest)sa_emailmembersa_resource_name	sa_policy
iam_clientiam_messagess       r!   _GrantActAsToPrincipalTApplicationsClient._CreateAndConfigureServiceAccount.<locals>._GrantActAsToPrincipal   s     "<<XF55BB

D
D' E i $$





(	 ))66

D
D'"."B"B" #C # E r#   c                    > SU 3nT" X5        g)z%Grant `actAs` permission to the user.zuser:N )rz   user_accountr{   r   s      r!   _GrantActAsToUserOApplicationsClient._CreateAndConfigureServiceAccount.<locals>._GrantActAsToUser  s    |n%fX.r#   c                    > SU 3nT" X5        g)z2Grant `actAs` permission to the ADC service agent.rJ   Nr   )rz   adc_service_agentr{   r   s      r!   _GrantActAsToAdcAgentSApplicationsClient._CreateAndConfigureServiceAccount.<locals>._GrantActAsToAdcAgent  s     !2 34fX.r#   z+Failed to grant actAs permission to user %sz1Successfully granted actAs permission to user %s.zservice-z5Failed to grant actAs permission to ADC service agentz;Successfully granted actAs permission to ADC service agent.z	projects/z/serviceAccounts/)rZ   r[   r   TestIamPermissionspermissionsr   r;   r   r"   r.   Getr,   9DesigncenterProjectsLocationsSpacesApplicationsGetRequestprojectParametersr   r\   r]   rr   Create'IamProjectsServiceAccountsCreateRequestr   ProjectToProjectResourceNameCreateServiceAccountRequestServiceAccountrc   r   rF   projectNumberr	   GetP4saHost)r/   r   rh   projectr_   r   r~   r   rQ   responseallowed_permissionsrz   sa_nameapp_detailsr^   r   r   grant_user_funcproject_number	p4sa_hostr   grant_agent_funcr   s         ``              @r!   !_CreateAndConfigureServiceAccount4ApplicationsClient._CreateAndConfigureServiceAccount   s   < ''0K..23H $//52#+>>YGI 
 H"%%c*1-g+N;g9AgY&>?h --##OO 	P 	
K
 %66JJJ
 JJ6G ''..<<66w?(4(P(P!+::"6xj A  ;   )Q ) 	= 	

 JJ?(JK  !3>6/
/
 /<HO5	 
 JJ;\I "%%g&m  !!#I">"2!I;?x): 	?	$%
 JJE wi0
;;r#   NFc                    U(       d  [        S5      eU R                  R                  UUS9nU(       a  X&l        OU(       a  X6l        U R                  R                  UUS9nU R                  R                  U5      $ )an  Calls the ImportApplicationIaC RPC.

Args:
  name: str, The full resource name of the Application.
  gcs_uri: str, The GCS URI of the IaC source.
  iac_module: messages.IaCModule, The IaCModule object.
  allow_partial_import: bool, Whether to allow partial imports.
  validate_iac: bool, Whether to only validate the IaC.

Returns:
  The response from the API call.
)Application name cannot be empty or None.)allowPartialImportvalidateIac)rh   importApplicationIaCRequest)
ValueErrorr,   ImportApplicationIaCRequestgcsUri	iacModule?DesigncenterProjectsLocationsSpacesApplicationsImportIaCRequestr.   	ImportIaC)r/   rh   gcs_uri
iac_moduleallow_partial_importvalidate_iacimport_iac_requestrequests           r!   	ImportIacApplicationsClient.ImportIac7  s     BCCBB/  C " ")	%/" 	UU(: 	V 	< 
 ==""7++r#   )replaceworker_poolr_   	create_sac                  U(       d  [        S5      eUR                  S5      S   n[        R                  R                  R
                  R                  5       nUnU(       a`  [        R                  R                  R                  R                  5       n	[        R                  " 5       u  pU R                  UUUUU	U
US9nUnU R                  R                  U R                  R                  UU R                  R                  UUUS9S95      $ )a  Calls the DeployApplication RPC.

Args:
  name: str, The full resource name of the Application.
  replace: bool, Flag to update the existing deployment.
  worker_pool: str, The user-specified Worker Pool resource.
  service_account: str | None, The email address of the service account.
  create_sa: bool, Whether to create a new service account.

Returns:
  The response from the API call.
r   /r   rh   r   r_   r   r~   r   )r   
workerPoolrk   )rh   deployApplicationRequest)r   r   r   VALUEScorer   r   accountiam_apiGetClientAndMessagesr   r.   Deployr,   <DesigncenterProjectsLocationsSpacesApplicationsDeployRequestDeployApplicationRequest)r/   rh   r   r   r_   r   r   r   deploy_service_accountr   r~   r   configured_sas                r!   DeployApplication$ApplicationsClient.DeployApplicationX  s   * BCCZZ_R(N$$,,002G,&&++33779l!(!=!=!?j<<')## = m  -==RR%)]]%K%K&5 &L & 	S 	
	 	r#   c                    U(       d  [        S5      eU R                  R                  U R                  R	                  US95      $ )zCalls the GetApplication RPC.r   rg   )r   r.   r   r,   r   )r/   rh   s     r!   GetApplication!ApplicationsClient.GetApplication  sC    BCC==OO 	P 	
 r#   c           
        U(       d  [        S5      eUR                  S5      S   n[        R                  R                  R
                  R                  5       nUnU(       a`  [        R                  R                  R                  R                  5       n[        R                  " 5       u  pU R                  UUUUUU	U
S9nUnU R                  R                  U R                  R                  UU R                  R                  UUS9S95      $ )aH  Calls the PreviewApplication RPC.

Args:
  name: str, The full resource name of the Application.
  worker_pool: str, The user-specified Worker Pool resource.
  service_account: str, The email address of the service account.
  create_sa: bool, Whether to create a new service account.

Returns:
  The response from the API call.
r   r   r   r   )r   rk   )rh   previewApplicationRequest)r   r   r   r   r   r   r   r   r   r   r   r.   Previewr,   =DesigncenterProjectsLocationsSpacesApplicationsPreviewRequestPreviewApplicationRequest)r/   rh   r   r_   r   r   r   preview_service_accountr   r~   r   r   s               r!   PreviewApplication%ApplicationsClient.PreviewApplication  s   $ BCCZZ_R(N$$,,002G-&&++33779l!(!=!=!?j<<')## = m !.==  SS&*mm&M&M&6 'N ' 	T 	
 r#   )r.   r*   r,   )r=   r   r>   Sequence[Any]r?   intr@   r   )rS   zSequence[str])r^   r   )NNFF)rh   r   r   boolr   
str | Noner_   r   r   r   return
Any | None)rh   r   r   r   )NNF)rh   r   r   r   r_   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   ReleaseTrackALPHAr1   timesleeprF   rV   rc   r   r   r   r   r   __static_attributes__r   r#   r!   r%   r%   <   s   9#'#4#4#:#: G jj$ $ (	$
 $ $L
 @'8O<b 6:9>,J  $$(11 	1
 1 "1 1 1f !%$(-- - "	-
 - -r#   r%   r(   )!r   
__future__r   collections.abcr   	functoolsr   r   typingr   r   apitools.base.pyr   r7   +googlecloudsdk.api_lib.cloudresourcemanagerr   $googlecloudsdk.api_lib.design_centerr	   googlecloudsdk.api_lib.iamr
   r   googlecloudsdk.callioper   googlecloudsdk.command_lib.iamr   #googlecloudsdk.command_lib.projectsrZ   googlecloudsdk.corer   r   r"   objectr%   r   r#   r!   <module>r      sM    + " $     > D 6 6 ( 3 @ * # *(.F Fr#   