
                         @   S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSK	J
r
Jr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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K)J*r*  SSK+J,r,  S\l-        Sr.Sr/Sr0Sr1\Rd                  S  5       r3\Rd                  S! 5       r4S" r5S# r6 " S$ S%\75      r8S&\
\,Rr                     S'\
\,Rr                     4S( jr:S)\;S'\<4S* jr=g)+zDAllows you to write surfaces in terms of logical RunApps operations.    )absolute_import)division)print_function)unicode_literalsN)ListMutableSequenceOptional)
exceptions)global_methods)	api_utils)types_utils)	validator)apis)connection_context)flags)serverless_operations)integration_list_printer)messages_util)stages)typekits_util)base)
properties)	resources)yaml)progress_tracker)runapps_v1alpha1_messagesservicedefault-runappsv1alpha1c              #   ~   #    [         R                  " U SS9n[        R                  " U 5      n[	        X#U5      v   g7f)zProvide a RunAppsOperations instance to use.

Arguments:
  args: Namespace, the args namespace.
  release_track: the release track of the command.

Yields:
  A RunAppsOperations instance.
T)promptN)	run_flags	GetRegionr   GetServiceAccount_GetRunAppsOperations)argsrelease_trackregionservice_accounts       Flib/googlecloudsdk/command_lib/run/integrations/run_apps_operations.pyConnectr-   <   s6      tD1&++D1/f}EEs   ;=c              #   (   #    [        XU5      v   g7f)zProvide a RunAppsOperations instance to use.

Arguments:
  region: Region to connect to.
  service_account: Service account to use.
  release_track: the release track of the command.

Yields:
  A RunAppsOperations instance.
N)r'   )r*   r+   r)   s      r,   ConnectWithRegionr/   M   s      	f}EEs   c                     [         R                  " [        [        5      nU (       d  [        R
                  " S5      e[        U[        U UU5      $ )NzYYou must specify a region. Either use the `--region` flag or set the run/region property.)r   GetClientInstance_RUN_APPS_API_NAME_RUN_APPS_API_VERSIONr
   ArgumentErrorRunAppsOperations)r*   r+   r)   clients       r,   r'   r'   ]   sO    !!"46KL&	

"
"	*  

     c                     [         R                  " U R                  5      nUS   S   nUS   S   nUS:X  a  US:X  a  [        R                  " S5      eU e)zReraises the error if with better message if it's a queueing error.

Args:
  err: the exception to be handled.

Raises:
  exceptions.IntegrationsOperationError: this is a queueing error.
errormessagecodezunable to queue the operationi  znAn integration is currently being configured.  Please wait until the current process is complete and try again)jsonloadscontentr
   IntegrationsOperationError)errr>   msgr;   s       r,   _HandleQueueingExceptionrB   m   sc     JJs{{#'##		&	!$++

/
/	@  	)r7   c            
          \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	S\
4S jr      S;S
\
S\R                  4S jjrS
\
S\R                  S\
4S jr   S<S jrS r\S 5       r S=S\
S\\
   S\\R*                     4S jjr S=S\
S\\
   S\\R*                     4S jjr S=S\R                  S\
S\\
   S\\R*                     4S jjrS\R2                  S\\R4                     4S jrS\R*                  S\\R8                     4S jrS=S jr S>S jrS r  S=S jr!S?S \"4S! jjr# S?S \"4S" jjr$  S>S#\
S$\
S%\
S&\\
   4S' jjr%S( r& S=S)\RN                  S#\
S$\
S*\\
   4S+ jjr(S\)\*RV                     4S, jr,S\RN                  4S- jr-S. r.S\/4S/ jr0S0 r1S\R                  S1\
S\
4S2 jr2S3 r3S4 r4S5 r5 S=S6\6\*Rn                     S\
S1\
S7\\"   4S8 jjr8S9 r9S:r:g	)@r5      zBClient used by Cloud Run Integrations to communicate with the API.c                 @    Xl         X l        X0l        X@l        XPl        g)a[  Inits RunAppsOperations with given API clients.

Args:
  client: The API client for interacting with RunApps APIs.
  api_version: Version of resources & clients (v1alpha1, v1beta1)
  region: str, The region of the control plane.
  release_track: the release track of the command.
  service_account: the service account to use for any deployments.
N)_client_api_version_region_release_track_service_account)selfr6   api_versionr*   r)   r+   s         r,   __init__RunAppsOperations.__init__   s      L#L'+r7   c                     U R                   $ N)rF   rK   s    r,   r6   RunAppsOperations.client       <<r7   c                 .    U R                   R                  $ rP   )rF   MESSAGES_MODULErQ   s    r,   messagesRunAppsOperations.messages   s    <<'''r7   c                     U R                   $ rP   )rH   rQ   s    r,   r*   RunAppsOperations.region   rS   r7   yaml_contentc           	      V  ^ ^ [        U5      n[        nSU;   a  UR                  S5      n[        R                  " [
        R                  " U5      R                  S5      S9n/ nSnUR                  S0 5       H^  nUR                  SS5      nUR                  S5      n	[        U	5      S	:w  a  M7  UR                  U	S
   U	S   S.5        U	S
   S:X  d  M\  SnM`     U(       a  UR                  SSS.5        UR                  S S9  [        S U5      n
[        R                  " U
5        [         R"                  " US9n[$        R&                  " U5      mU U4S jn[(        R*                  " STR-                  5       SS9 nT R/                  UUUUUS9  SSS5        g! , (       d  f       g= f)z`Applies the application config from yaml file.

Args:
  yaml_content: content of the yaml file.
namezutf-8)configFr   id /   r      typer\   redisTvpc*c                     U S   $ Nrd    xs    r,   <lambda>-RunAppsOperations.ApplyYaml.<locals>.<lambda>   s    &	r7   keyc                     U S   $ ri   rj   rk   s    r,   rm   rn      s    air7   	selectorsc                 *   > TR                  XT5        g rP   _UpdateDeploymentTrackertracker	operationunused_statusrK   
stages_maps      r,   StatusUpdate1RunAppsOperations.ApplyYaml.<locals>.StatusUpdate       
##G
Cr7   zApplying Configuration...zFailed to apply configuration.failure_message)rx   tracker_update_funcappname	appconfigmatch_type_namesN)dict_DEFAULT_APP_NAMEpopr   Configr   dumpencodegetsplitlenappendsortmapr   CheckApiEnablementsr   GetComponentTypesFromSelectorsr   ApplyStagesr   StagedProgressTrackervaluesApplyAppConfig)rK   rZ   app_dictr\   r   r   rf   rres_idparts	all_typesresource_stagesr|   rx   r{   s   `             @r,   	ApplyYamlRunAppsOperations.ApplyYaml   s    L!HD\\&!d)00yy&--g6I 
C \\+r*uuT2fll3e	UquQxqBC	qW	 + uc:;12')9:I!!),99"O ##O4J 
	/	/#8
 

*+  
 
 
s   =F
F(Nr   r   c
                 p   UR                  [        R                  5        U(       a?  UR                  [        R                  [        R
                  " U R                  U5      5         U R                  X#U5        UR                  [        R                  5        Uc  SSS./nSU0nU(       d  UR                  S5        UR                  [        R                   5        U(       a   UR                  [        R                   U5         U R#                  UUU	US9  UR                  [        R                   S5        UR                  [        R                   5        UR                  S5        g! [        R                   a  n
[        U
5         Sn
A
NSn
A
f[        R                   a+  n
UR                  [        R                  U
5         Sn
A
GN0Sn
A
ff = f! [        R                   a  n
[        U
5         Sn
A
NSn
A
f[        R                   a*  n
UR                  [        R                   U
5         Sn
A
NSn
A
ff = f)	aB  Applies the application config.

Args:
  tracker: StagedProgressTracker, to report on the progress.
  appname:  name of the application.
  appconfig: config of the application.
  integration_name: name of the integration that's being updated.
  deploy_message: message to display when deployment in progress.
  match_type_names: array of type/name pairs used for create selector.
  intermediate_step: bool of whether this is an intermediate step.
  etag: the etag of the application if it's an incremental patch.
  tracker_update_func: optional custom fn to update the tracker.
Nrg   rc   matchTypeNamesz\Deployment started. This process will continue even if your terminal session is interrupted.)r   create_selectorr_   zDone.)
StartStager   UPDATE_APPLICATIONUpdateStager   CheckStatusMessagerI   _UpdateApplicationCompleteStageapi_exceptionsHttpConflictErrorrB   r
   r?   	FailStageUpdateHeaderMessageCREATE_DEPLOYMENT_CreateDeployment)rK   rx   r   r   integration_namedeploy_messager   intermediate_stepetagr   r@   r   s               r,   r    RunAppsOperations.ApplyAppConfig   s   2 v001

#
#

*
*!!#37
g$7 F556#&45')9:O!!2 v//0&22NC6


1)	   &22B7F445(E ++ $s##00 81137780 ++ $s##00 700#667sH   'E' G 'G;FG" GGH5$G44H5 H00H5r   c                    U R                  U5      nU R                  R                  XUS9nU=(       d!    [        R                  " U R
                  U5      nU(       a"  [        R                  " U R
                  XE5      nO![        R                  " U R
                  XE5      n[        R                  " U R
                  U5        g)zUpdate Application config, waits for operation to finish.

Args:
  appname:  name of the application.
  appconfig: config of the application.
  etag: the etag of the application if it's an incremental patch.
)r\   r]   r   N)		GetAppRefrV   Applicationr   GetApplicationrF   PatchApplicationCreateApplicationWaitForApplicationOperation)rK   r   r   r   app_refapplicationis_patchry   s           r,   r   $RunAppsOperations._UpdateApplication!  s     nnW%G--++T , K Fy//gFH,,T\\7Pi--
,,i ))$,,	Br7   c                    U R                  U5      nU R                  UR                  5       5      nU(       a  U(       a  [        R                  " S5      eU R
                  R                  UUUU R                  S9n[        R                  " U R                  Xh5      n	[        R                  " U R                  U	UUS9n
U R                  U
5        g)aQ  Create a deployment, waits for operation to finish.

Args:
  appname:  name of the application.
  tracker: The ProgressTracker to track the deployment operation.
  tracker_update_func: optional custom fn to update the tracker.
  create_selector: create selector for the deployment.
  delete_selector: delete selector for the deployment.
zIcreate_selector and delete_selector cannot be specified at the same time.)r\   createSelectordeleteSelectorserviceAccount)r   N)r   _GetDeploymentNameNamer
   r4   rV   
DeploymentrJ   r   CreateDeploymentrF   WaitForDeploymentOperationCheckDeploymentState)rK   r   rx   r   r   delete_selectorr   deployment_name
deploymentdeployment_opsdep_responses              r,   r   #RunAppsOperations._CreateDeployment8  s    " nnW%G--glln=O?$$2  ))&&,,	 * J //gN 77/	L 	l+r7   c                 |    SR                  U[        R                  R                  5       R                  S5      5      $ )Nz{}-{}z%Y%m%d%H%M%S)formatdatetimenowstrftime)rK   r   s     r,   r   $RunAppsOperations._GetDeploymentNamec  s2    >>""&&(11.A r7   c                    [         R                  " 5       n[         R                  " X15      n/ n/ nUR                  R                  nUR
                  b  UR
                   H  n[        R                  " UR                  R                  5      n	UR                  UR                  :X  a  UR                  U	5        UR                  UR                  :X  d  Mt  UR                  U	5        M     U H3  nX;   d  M
  U R                  U5      (       d  M"  U R                  U5        M5     U H3  nX;   d  M
  U R!                  U5      (       d  M"  U R#                  U5        M5     U R%                  5         g)a:  Updates deployment tracker with the current status of operation.

Args:
  tracker: The ProgressTracker to track the deployment operation.
  operation: run_apps.v1alpha1.operation object for the deployment.
  tracker_stages: map of stages with key as stage key (string) and value is
    the progress_tracker.Stage.
N)r   GetMessagesGetDeploymentOperationMetadataResourceDeploymentStatusStateValueValuesEnumresourceStatusr   StageKeyForResourceDeploymentr\   rd   stateRUNNINGr   FINISHED	IsWaitingr   	IsRunningr   Tick)
rx   ry   tracker_stagesrV   metadataresources_in_progressresources_completedresource_stateresource
stage_names
             r,   rv   *RunAppsOperations._UpdateDeploymentTrackerh  s     $$&H77LH66KKN*--(99(--:L:LM
>>^333

&
&z
2>>^444

$
$Z
0 . *		#(9(9((C(C8$ * (		#(9(9((C(Ch' ( LLNr7   r\   res_typereturnc                     U R                  5       R                  nU R                  X1U5      nU(       a  U$ [        R                  " SR                  U5      5      e)zGet an integration.

Args:
  name: the name of the resource.
  res_type: type of the resource. If empty, will match any type.

Raises:
  IntegrationNotFoundError: If the integration is not found.

Returns:
  The integration config.
 Integration [{}] cannot be found)GetDefaultAppr]   _FindResourcer
   IntegrationNotFoundErrorr   )rK   r\   r   r   ress        r,   GetIntegrationGeneric'RunAppsOperations.GetIntegrationGeneric  sS    " ""$++I


Yh
7C
j

-
-*11$7 r7   c                 Z    U R                  5       R                  nU R                  X1U5      $ )zGet an integration, or None if not found.

Args:
  name: the name of the resource.
  res_type: type of the resource. If empty, will match any type.

Returns:
  The integration config, if found.
)r   r]   r   )rK   r\   r   r   s       r,   MaybeGetIntegrationGeneric,RunAppsOperations.MaybeGetIntegrationGeneric  s+     ""$++Iix88r7   c                     UR                    HD  nUR                  R                  U:X  d  M  U(       a  UR                  R                  U:X  d  MB  Us  $    g rP   )resourceListr^   r\   rd   )rK   r   r\   r   r   s        r,   r   RunAppsOperations._FindResource  s?     %%		h#&&++2I
 & r7   r   c                     [         R                  " U R                  U R                  [        5      U/5      nU(       a'  UR
                   H  nUR                  U:X  d  M  Us  $    g! [        R                   a     gf = f)zGet status of an integration.

Args:
  res_id: ResourceID of the resource.

Returns:
  The ResourceStatus of the integration, or None if not found
N)	r   GetApplicationStatusrF   r   r   resourceStatusesr^   r   	HttpError)rK   r   
app_statusss       r,   GetIntegrationStatus&RunAppsOperations.GetIntegrationStatus  sp    
11
,,'89F8j 
,,ATTV^H - ## s   AA'  A' $A' 'A>=A>r   c                     UR                   (       d  g [        R                  " U R                  UR                   5      $ ! [        R
                   a     gf = f)aU  Fetches the deployment object given a resource config.

Args:
  resource: the resource object.

Returns:
  run_apps.v1alpha1.Deployment, the Deployment object.  This is None if
    the latest deployment name does not exist.  If the deployment itself
    cannot be found via the name or any http errors occur, then None will
    be returned.
N)latestDeploymentr   GetDeploymentrF   r   r   )rK   r   s     r,   GetLatestDeployment%RunAppsOperations.GetLatestDeployment  sJ     $$$$T\\83L3LMM## s   *? AAc                 *  ^ ^ T R                  5       n[        R                  " U5      nU(       a6  UR                  (       a%  [        R
                  " SR                  U5      5      eU(       d  UR                  UR                  5      nUR                  nT R                  UR                  XG5      (       a*  [        R
                  " [        R                  " U5      5      e[        R                  " [        R                  " XGS9S9nUR!                  X(5      n	UR                  R"                  R%                  U5        UR'                  U5      n
U(       a  U/O/ nUR)                  U	5        U H&  nU
R%                  [*        R,                  USS.5        M(     U H.  nT R/                  UR                  U[*        R,                  5        M0     T R1                  U5        U(       a;  T R                  UR                  U[*        R,                  5      nUR3                  UUS9  UR5                  U
S9nUR7                  SS9n[8        R:                  " SUS	9mU U4S
 jn[<        R>                  " STRA                  5       SS9 n T RC                  UU[D        UR                  UUU
URF                  S9   SSS5        U$ ! [        RH                   a7  nURK                  [        RL                  " T RN                  U5      5        UeSnAff = f! , (       d  f       U$ = f)a'  Create an integration.

Args:
  integration_type:  type of the integration.
  parameters: parameter dictionary from args.
  service: the service to attach to the new integration.
  name: name of the integration, if empty, a defalt one will be generated.

Returns:
  The name of the integration.
z0--name is not allowed for integration type [{}].r\   rd   r^   Trd   r\   ignoreResourceConfigintegrationworkloadrr   )creater  resource_typesc                 *   > TR                  XT5        g rP   ru   rw   s      r,   r|   9RunAppsOperations.CreateIntegration.<locals>.StatusUpdate1  r~   r7   zCreating new Integration...z!Failed to create new integration.r   rx   r   r   r   r   r   r   r   N)(r   r   
GetTypeKitis_singletonr
   r4   r   NewIntegrationNamer]   resource_typer   r   IntegrationAlreadyExistsr   Resource
ResourceIDUpdateResourceConfigr   r   GetCreateSelectorsextendr   SERVICE_TYPEEnsureWorkloadResourcesCheckCloudRunServicesExistenceBindServiceToIntegrationGetCreateComponentTypesGetDeployMessager   IntegrationStagesr   r   r   r   r   r   r?   
AddWarningRetryDeploymentMessagerI   )rK   integration_type
parametersr   r\   apptypekitr  r   services_in_paramsr   servicessvcr  r   r   r|   rx   r@   r{   s   `                  @r,   CreateIntegration#RunAppsOperations.CreateIntegration  s    


C&&'78G$$$$
<
C
C 
 ''

3d))M#**d::$$

0
0
6  )11$//TNH !55jKJJ""8,11$7#yHOO&'**"& 	  
""3::sK4L4LM ''1##
**g{77h && ' 
 55" 6 O --T-:N))OJ 
	/	/%;
 
 ,%jj!)- 	 		

, K 22 001D1DdK	
 		!
 
, Ks*   :L<.J55L 	2K;;L  L
Lc                   ^ ^ T R                  5       nT R                  UR                  U5      nUc*  [        R                  " [
        R                  " U5      5      e[        R                  " U5      n[        R                  " UR                  U5        / nUR                  X&5      n	U	(       a  UR                  U	5        U(       a  UR                  U5        UR                  U5      n
U HP  nT R!                  UR                  U["        R$                  5        T R'                  U
["        R$                  US5        MR     U(       a;  T R                  UR                  U["        R$                  5      nUR)                  UUS9  U(       a  T R                  UR                  U["        R$                  5      nU(       aH  UR+                  UUS9  T R-                  U5      (       a!  T R'                  U
["        R$                  U5        O%[        R.                  " SR1                  U5      5      eU(       a  T R3                  U5        UR4                  (       d  UR6                  (       a  Uc  Uc~  UR9                  XeR                  R:                  ["        R$                  5      nU HD  nX;  d  M
  T R-                  U5      (       d  M"  T R'                  U
["        R$                  US5        MF     UR=                  5       nUR?                  U
S9n[@        RB                  " SUS9mU U4S jn[D        RF                  " S	TRI                  5       S
S9 nT RK                  UU[L        UR                  UUU
URN                  S9sSSS5        $ ! , (       d  f       g= f)ad  Update an integration.

Args:
  name:  str, the name of the resource to update.
  parameters: dict, the parameters from args.
  add_service: the service to attach to the integration.
  remove_service: the service to remove from the integration.

Raises:
  IntegrationNotFoundError: If the integration is not found.

Returns:
  The name of the integration.
NTr  z,Service [{}] is not found among integrationsrr   Fr  c                 *   > TR                  XT5        g rP   ru   rw   s      r,   r|   9RunAppsOperations.UpdateIntegration.<locals>.StatusUpdate  r~   r7   zUpdating Integration...zFailed to update integration.r   r  )(r   r   r]   r
   r   r   IntegrationNotFoundr   GetTypeKitByResourcer   ValidateUpdateParametersr'  r  r  r   r  r  r   r  _AppendTypeMatcherr!  UnbindServiceFromIntegrationGetCloudRunServiceServiceNotFoundErrorr   r   is_ingress_serviceis_backing_serviceGetBindedWorkloadsr   r#  r"  r   r$  r   r   r   r   r   r   )rK   r\   r(  add_serviceremove_servicer)  existing_resourcer*  specified_servicesr+  r   r   r  workload_resref_svcsr   r   r|   rx   r{   s   `                  @r,   UpdateIntegration#RunAppsOperations.UpdateIntegrationM  s<   " 


C**3::t< //

+
+D
1  001BCG	""7#;#;ZH 55  23,11$7%
""
**g{77 


"
"

	 & ##
**k;#;#;h &&' ' 
 ''
**nk&>&>l 
,,)! 	- 	
 "">22 
!
! 8 8. --:AA
 	
 
))*<=!!"""++
ZZ44k6N6Nh ',1H1H2
 2
 
!
! 8 8'4  --/N55" 6 O ))_J 
	/	/!7
 
  *#JJ'+xx ! 	
 
 
s   .M::
Nc                   ^ ^ T R                  5       nT R                  UR                  U5      nUc%  [        R                  " SR                  U5      5      e [        R                  " U5      n[        R                  " UR                  R                  5      nUR                  UR                  5      n/ nU Hr  nUR                  [        R                   :X  d  M#  T R#                  UR$                  5      (       d  ME  UR'                  [        R                   UR$                  SS.5        Mt     U(       a!  UR)                  U5      n	UR+                  U	S9n
OEUR                  R                  UR                  R$                  S./n	UR                  R                  /n
[,        R.                  " U
[1        U5      S9mU U4S jn[2        R4                  " S	TR7                  5       S
S9 nU(       a  U HJ  nT R                  UR                  UR$                  UR                  5      n[        R8                  " X=5        ML     U(       a.  T R;                  UU[<        UR                  USUR>                  S9  O)T RA                  [<        UR                  UR>                  S9  SU	0nT RC                  XX5        SSS5        U(       a  URD                  $ UR                  R                  $ ! [        R                   a    Sn GNf = f! , (       d  f       NT= f)zDelete an integration.

Args:
  name:  str, the name of the resource to update.

Raises:
  IntegrationNotFoundError: If the integration is not found.

Returns:
  str, the type of the integration that is deleted.
Nr   Tr	  rr   rc   )destroy_resource_typesshould_configure_servicec                 *   > TR                  XT5        g rP   ru   rw   s      r,   r|   9RunAppsOperations.DeleteIntegration.<locals>.StatusUpdate  r~   r7   zDeleting Integration...zFailed to delete integration.r   )rx   r   r   r   r   r   r   r   r   r   r   )#r   r   r]   r
   r   r   r   r4  r4   r   BindingFinderr   GetIDsBindedTor^   rd   r   r  r8  r\   r   GetDeleteSelectorsGetDeleteComponentTypesr   IntegrationDeleteStagesboolr   r   r   RemoveBindingr   r   r   r   _UndeployResourcer'  )rK   r\   r)  r   r*  bindingsbinded_from_resourcesunbind_match_type_namesriddelete_match_type_namesr   r|   rx   
binded_resr   r{   s   `              @r,   DeleteIntegration#RunAppsOperations.DeleteIntegration  s    


C!!#**d3H//
,
3
3D
9 228<g !!#**"9"9:H$33HKK@ $	[--	-""388,,
!
(
(!..hh&** 	 %  ' : :4 @77+ 8 o
 ++""++""" !	 "))*o//.!%&=!>J
 
	/	/!7
 
	(C))jj#((CHH* 

X
2	 ) #


".'

6 $88   
!
!'

88 "  *+BCo
TGJ=
@ %%%[[O ## gH
 
s   K  #CK= K:9K:=
Lc                 *   UR                  [        R                  5        U R                  [        UUUS9  UR                  [        R                  5        U R                  5       nU R                  UR                  U5      nU(       a%  UR                  R                  R                  U5        UR                  [        R                  5        U R                  [        UR                  UR                  S9  UR                  [        R                  5        g)zUndeploy a resource.

Args:
  name: name of the resource
  delete_selector: The selector for the undeploy operation.
  tracker: StagedProgressTracker, to report on the progress.
  tracker_update_func: optional custom fn to update the tracker.
)r   rx   r   r   rJ  N)r   r   UNDEPLOY_RESOURCEr   r   r   r   r   r]   r   removeCLEANUP_CONFIGURATIONr   r   )rK   r\   r   rx   r   r)  r   s          r,   rR  #RunAppsOperations._UndeployResource-  s     v//0!/'	   &223 


C!!#**d3H	jj$$X.v334!**XX  
 &667r7   include_workloadc                     [         R                  " U R                  5       Vs/ s H6  nU(       d*  UR                  [         R                  R
                  :w  d  M4  UPM8     sn$ s  snf )zReturns the list of integration type definitions.

Args:
  include_workload: whether to include workload types

Returns:
  An array of integration type definitions.
)r   IntegrationTypesrF   service_typeServiceTypeWORKLOAD)rK   r`  r  s      r,   ListIntegrationTypes&RunAppsOperations.ListIntegrationTypesN  sU     '77EEK$$(?(?(H(HH 	E  s   3A#A#c                     [         R                  " U5      nU(       d+  Ub*  UR                  [         R                  R                  :w  a  U$ g)zReturns the integration type definition of the given name.

Args:
  type_name: name of the integration type
  include_workload: whether to include workload types

Returns:
  An integration type definition. None if no matching type.
N)r   GetTypeMetadatarc  rd  re  )rK   	type_namer`  type_metadatas       r,   GetIntegrationTypeDefinition.RunAppsOperations.GetIntegrationTypeDefinition^  s@      //	:M!""k&=&=&F&FFr7   integration_type_filterservice_name_filterr*   filter_for_typec           	      ,   [         R                  R                  R                  R	                  5       nU[
        :X  a`  [        U5      (       dP  [        R                  " U R                  U R                  5       5      nU(       a  UR                  O/ n[        U5      nO@[        R                  " U R                  U R                  [        5      5      nU(       a  U/O/ nU(       d  / $ / n	U H&  nU	R!                  U R#                  UUUU5      5        M(     U	$ )a  Returns the list of integrations from the default applications.

If a '-' is provided for the region, then list applications will be called.
This is for the global integrations list call.  Any other time
the default region (either from --region or from gcloud config) will be
used to fetch the default application.  If the global call is not needed,
then fetching from a single region will reduce latency and remove the need
of filtering out non default applications.

Args:
  integration_type_filter: if populated integration type to filter by.
  service_name_filter: if populated service name to filter by.
  region: GCP region. If not provided, then the region will be pulled from
    the flag or from the config.  Only '-', which is the global region has
    any effect here.
  filter_for_type: the type to filter the list on. if given, the resources
    of that type will not be included in the list, and will only show
    binding to or from that type. if not given, all resources and bindings
    will be shown. for example, for `run integrations list`, it would filter
    for `service`.

Returns:
  List of Dicts containing name, type, and services.
)r   VALUESapi_endpoint_overridesr    GetALL_REGIONS_IsLocalHostr   ListApplicationsrF   ListAppsRequestapplications_FilterForDefaultAppsr   r   r   r  _ParseResourcesForList)
rK   rn  ro  r*   rp  endpoint	list_appsappsr)  outputs
             r,   ListIntegrations"RunAppsOperations.ListIntegrationsr  s    D   77??CCEH\(%;%;,,
,,,,.i (1Y##bd"4(d$$
,,'89c cUrdiFmm

%
%%!	  Mr7   c                    [         R                  " U R                  U5      nU(       d  0 $ UR                   Vs0 s H:  nUR                  R
                  UR                  R                  4UR                  _M<     sn$ s  snf rP   )r   r   rF   r   r^   rd   r\   r   )rK   r   r   r   s       r,   _GetResourceStatusMap'RunAppsOperations._GetResourceStatusMap  so    //gFJi #333H 
		8;;++,hnn<3  s   AA=r)  focus_workloadc                    UR                   c  / $ UR                   R                  nU(       d  / $ [        R                  " U5      nU R	                  U R                  UR                  5      5      n/ n[        US S9 GH  n	 [        R                  " U	5      n
U
(       a  U
R                  OU	R                  R                  nX:X  a  MK  U(       a  X:w  a  MY  U(       aV  UR                  U	R                  5       Vs/ s H/  nUR                  [         R"                  :X  d  M#  UR                  PM1     nnOOUR                  U	R                  5       Vs/ s H)  nSR%                  UR                  UR                  5      PM+     nnU(       a  X=;  a  GM  UR                  R'                  S5      S   nUR)                  U	R                  R                  U	R                  R                  45      nUR+                  [,        R.                  " U	R                  R                  UUSR1                  [        U5      5      US95        GM     U$ ! [        R                   a    Sn
 GNf = fs  snf s  snf )	z>Helper function for ListIntegrations to parse relevant fields.Nc                 .    U R                   R                  $ rP   )r^   r\   rk   s    r,   rm   :RunAppsOperations._ParseResourcesForList.<locals>.<lambda>  s    		r7   ro   z{}/{}r`      ,)r   r*   r'  r,  latest_resource_status)r]   r   r   rK  r  r   r\   sortedr   r4  r
   r4   r'  r^   rd   GetBindingIDsr   r  r   r   r   r   r   Rowjoin)rK   r)  rn  ro  r  app_resourcesrS  resource_statusesr  r   r*  r'  r   r,  r*   resource_statuss                   r,   r{  (RunAppsOperations._ParseResourcesForList  s    zziJJ++Mi!!-0H224>>#((3KL F =.AB44X>
 '.'
"
"8;;3C3C  
	+
 "9	 #00=
={{k666 FKK= 	 
 #00=
= NN6;;4= 	 
 
!4!D xx~~c"1%f)--;;X[[--
.o mm
"
&
&'{{///xxx 01%4	] Cr Mm %% *

s$   I9"I I 0I%IIc                 4   [         R                  " U R                  U R                  [        5      5      nUb$  UR
                  b  UR
                  R                  c  / $ UR
                  R                  n[        R                  " U5      nUR                  5       $ )z]Return a list of bindings from the default application.

Returns:
  The list of BindingData.
)
r   r   rF   r   r   r]   r   r   rK  GetAllBindings)rK   r)  r  rS  s       r,   GetBindingData RunAppsOperations.GetBindingData	  sy     
"
"dnn%67C {cjj(CJJ,C,C,KiJJ++M!!-0H""$$r7   c                 0   [         R                  " U R                  U R                  [        5      5      nU(       d0  U R
                  R                  [        [        R                  " / S9S9nUR                  (       d  [        R                  " / S9Ul	        U$ )zEReturns the default application.

Returns:
  the application config.
)r   )r\   r]   )
r   r   rF   r   r   rV   r   r   r   r]   )rK   r)  s     r,   r   RunAppsOperations.GetDefaultApp  s~     
"
"dnn%67C MM%% *11 & c ::,33cj Jr7   c                     [         R                  R                  R                  R	                  SS9nU R
                  n[        R                  R                  UX#S.SS9nU$ )z~Returns the application resource object.

Args:
  name:  name of the application.

Returns:
  The application resource object
Trequired)
projectsIdlocationsIdz'runapps.projects.locations.applicationsparams
collection)	r   rr  coreprojectrt  rH   r   REGISTRYParse)rK   r\   r  locationr   s        r,   r   RunAppsOperations.GetAppRef1  s`     $$,,00$0?G||H  &&%?< ' G
 Nr7   c                     [         R                  R                  R                  R	                  SS9n[
        R                  R                  [        SU0SS9nU$ )zDCreates request object for calling ListApplications for all regions.Tr  r  zrunapps.projects.locationsr  )	r   rr  r  r  rt  r   r  r  ru  )rK   r  r   s      r,   rx  !RunAppsOperations.ListAppsRequestC  s[    $$,,00$0?G  &&'
 0 ' G Nr7   c                     [         R                  R                  R                  R	                  SS9n[
        R                  R                  UUUS.SS9nU$ )zReturns the Cloud Run service reference.

Args:
  name:  name of the Cloud Run service.

Returns:
  Cloud Run service reference
Tr  )namespacesId
servicesIdzrun.namespaces.servicesr  )r   rr  r  r  rt  r   r  r  )rK   r\   r  service_refs       r,   GetServiceRefRunAppsOperations.GetServiceRefO  s_     $$,,00$0?G$$**#
 - + K r7   res_namec           	          U R                  XU5      (       d@  UR                  R                  [        R                  " [        R
                  " X#S9S95        gg)zMake sure resources block for the Cloud Run services exists.

Args:
  appconfig: the application config
  res_name: name of the resource,
  res_type: type of the resource,
r  r  N)r   r   r   r   r  r  )rK   r   r  r   s       r,   r  )RunAppsOperations.EnsureWorkloadResourcesc  sO     i8<<##
#
,
,*55 =r7   c                 $   [         R                  " U R                  [        R                  [        R
                  5      n[        R                  " U5       nU R                  U5      nUR                  U5      sSSS5        $ ! , (       d  f       g= f)zCheck for existence of Cloud Run services.

Args:
  service_name: str, name of the service

Returns:
  the Cloud Run service object
N)
r   RegionalConnectionContextrH   r   SERVERLESS_API_NAMESERVERLESS_API_VERSIONr   r-   r  
GetService)rK   service_nameconn_contextr6   r  s        r,   r8  $RunAppsOperations.GetCloudRunServicey  si     &??**--L
 
	&	&|	4&&|4k{+ 
5	4	4s   "B
Bc                     U H@  nU R                  U5      nU(       a  M  [        R                  " SR                  U5      5      e   g)zCheck for existence of Cloud Run services.

Args:
  service_names: array, list of service to check.

Raises:
  exceptions.ServiceNotFoundError: when a Cloud Run service doesn't exist.
z Service [{}] could not be found.N)r8  r
   r9  r   )rK   service_namesr\   r   s       r,   r   0RunAppsOperations.CheckCloudRunServicesExistence  sD     ''-gW--.55d;
 	
 r7   c                 ~   U R                   R                  R                  nU R                   R                  R                  nUR                  R
                  UR                  :X  a  gUR                  R
                  UR                  :X  a  UR                  R                  (       d  [        R                  " S5      eSnUR                  R                  SSS2    H+  nUR
                  UR                  :X  d  M  UR                  n  O   SR                  SR                  UR                  R                  R                  S5      5      5      nU(       a  USR                  U5      -  n[        R                  " U5      e[        R                  " S	R                  UR                  R
                  R                   5      5      e)
zThrows any unexpected states contained within deployment reponse.

Args:
  response: run_apps.v1alpha1.deployment, response to check
NzConfiguration failed.r_   z%Configuration failed with error:
  {}z
  z; z
Logs are available at {}z0Configuration returned in unexpected state "{}".)rV   DeploymentStatusr   
JobDetailsstatusr   	SUCCEEDEDFAILEDerrorMessager
   r?   
jobDetailsjobUrir   r  r   r\   )rK   response	dep_state	job_stateurljob	error_msgs          r,   r   &RunAppsOperations.CheckDeploymentState  sW    ..CCI((==I	 3 33	 0 00__))334KLL c++DbD1#99	(((

#
 2
 ;AA
++hoo2288>
?i 
188==	11)<< 11
<
C
Coo##(( r7   type_matchersignore_resource_configc                 l    U H  nUS   U:X  d  M  US   U:X  d  M    g    UR                  UUUS.5        g )Nrd   r\   r	  )r   )rK   r  r   r  r  matchers         r,   r6  $RunAppsOperations._AppendTypeMatcher  sF     !	H	$H)D !  6 r7   c           
        ^  T R                  [        5      n [        R                  " T R                  UR
                  5      n[        U 4S jUR                   5       5      (       dX  [        R                  " SR                  SR                  UR                   Vs/ s H  o3R                  PM     sn5      5      5      eg ! [        R                   a     g f = fs  snf )Nc              3   T   >#    U  H  oR                   TR                  :H  v   M     g 7frP   )
locationIdrH   ).0lrK   s     r,   	<genexpr>3RunAppsOperations.VerifyLocation.<locals>.<genexpr>  s     H5G||t||+5Gs   %(z8Currently this feature is only available in regions {0}.z, )r   r   r   ListLocationsrF   r  r   r   any	locationsr
   $UnsupportedIntegrationsLocationErrorr   r  r  )rK   r   r  r  s   `   r,   VerifyLocation RunAppsOperations.VerifyLocation  s    nn./G((w7I7IJh HX5G5GHHH;;
D
K
Kiix/A/AB/A!/ABC  I ##  Cs   +C  C CC)rG   rF   rH   rI   rJ   )NNNFNN)NNNrP   )NN)F);__name__
__module____qualname____firstlineno____doc__rM   propertyr6   rV   r*   strr   r   r   r   r   r   r   staticmethodrv   r	   r  r   r   r   r  ResourceStatusr   r   r  r.  rC  rY  rR  rP  rf  rl  r  r  r   r{  r   r   BindingDatar  r   r   r   rx  r  r  r8  r   r   r   Selectorr6  r  __static_attributes__rj   r7   r,   r5   r5      sZ   J,&   ( (  5C 5x E) E) +11	E)NCC%>%E%ECMPC6 ),V
  H !%  )223	8 !%99 9 )223	9* !%		*11	 	 		
 )223	-88)889./88)445,_D @DAF[| AE8B4 " 16)-0 '+;"; ; 	;
  };z '+O$00O  #O 	O
 smOb%D%$6BB ,$
y 
(*11  	,,$
 &Z 04$T]]3  	
 'tn r7   r5   r~  r   c                     U  Vs/ s H-  oR                   R                  S[        -   5      (       d  M+  UPM/     sn$ s  snf )zReturns a dict with only default applications.

Args:
  apps: the list of applications to filter.

Returns:
  A list of default applications.
r`   )r\   endswithr   )r~  r)  s     r,   rz  rz    s1     	L#!2!239J3J!K#	LL	Ls   *<<r|  c                     U S:H  $ )Nzhttp://localhost:8088/rj   )r|  s    r,   rv  rv    s    	-	--r7   )>r  
__future__r   r   r   r   
contextlibr   r<   typingr   r   r	   apitools.base.pyr
   r   googlecloudsdk.api_lib.runr   'googlecloudsdk.api_lib.run.integrationsr   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.runr   r   r$   r   +googlecloudsdk.command_lib.run.integrationsr   r   r   r   4googlecloudsdk.command_lib.run.integrations.typekitsr   "googlecloudsdk.command_lib.runappsgooglecloudsdk.corer   r   r   googlecloudsdk.core.consoler   6googlecloudsdk.generated_clients.apis.runapps.v1alpha1r   r  r   ru  r2   r3   contextmanagerr-   r/   r'   rB   objectr5   r   rz  r  rP  rv  rj   r7   r,   <module>r      s   K &  % '    2 2 9 5 = ? = , = = @ = P E > E E 9 * ) $ 8 \ %   "  F F  F F (` `F#M
(44
5M	
#
/
/0M.3 .4 .r7   