
    2                        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 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dlmZ ddlmZ dZdZdZdZdZ dZ! ejD                  d      Z#d Z$dejJ                  dede	ejL                     fdZ'dejJ                  dedejP                  fdZ)	 d3dejJ                  dede	eejT                        de	ejV                     fdZ,dejJ                  ded ejL                  dejZ                  fd!Z.	 d3dejJ                  ded ejL                  d"e	e/   dejZ                  f
d#Z0	 d4dejJ                  ded$ejb                  d%e	e2   dejZ                  f
d&Z3dejJ                  d'e/de	ejb                     fd(Z4dejJ                  d)ejZ                  dejL                  fd*Z5dejJ                  d)ejZ                  dejb                  fd+Z6	 	 d5dejJ                  d)ejZ                  fd,Z7d)ejZ                  dejp                  fd-Z9dejJ                  d.e/dejt                  fd/Z;d0e/d1e<de<fd2Z=y)6z;Functionality related to Cloud Run Integration API clients.    )absolute_import)division)print_function)unicode_literalsN)ListOptional)encoding)
exceptions)apis)waiter)log)	resources)retry)runapps_v1alpha1_client)runapps_v1alpha1_messagesrunappsv1alpha1configr   i@w   zLocation [\w-]+ is not foundc                  >    t        j                  t        t              S )zReturns the messages module for the Runapps API.

  Returns:
    Module containing the definitions of messages for the Runapps API.
  )r   GetMessagesModuleAPI_NAMEAPI_VERSION     8lib/googlecloudsdk/api_lib/run/integrations/api_utils.pyGetMessagesr   7   s     
		+	66r   clientapp_refreturnc                 "   | j                   j                  |j                               }	 | j                  j	                  |      S # t
        j                  $ r Y yt
        j                  $ r }t        |j                  |       Y d}~yd}~ww xY w)ax  Calls GetApplication API of Runapps for the specified reference.

  Args:
    client: The api client to use.
    app_ref: The resource reference of the application.

  Raises:
    exceptions.UnsupportedIntegrationsLocationError: if the region does not
      exist for the user.

  Returns:
    The application.  If the application does not exist, then
    None is returned.
  nameN)
MESSAGES_MODULE.RunappsProjectsLocationsApplicationsGetRequestRelativeNameprojects_locations_applicationsGetapitools_exceptionsHttpNotFoundErrorHttpForbiddenError_HandleLocationErrorlocationsId)r   r   requestes       r   GetApplicationr0   @   s    $ KK##% L ' 
11155g>>		.	. 		/	/ 1,,a001s   A BB.B		Bc           	      4   | j                   j                  |j                               }| j                  j	                  |      }|j
                  rFt        j                  dj                  dj                  t        |j
                                           |S )zBCalls ListApplications API of Runapps for the specified reference.)parentzIThe following regions did not respond: {}. List results may be incompletez, )r$   /RunappsProjectsLocationsApplicationsListRequestr&   r'   r   unreachabler   warningformatjoinsorted)r   r   r.   responses       r   ListApplicationsr:   ]   s    
 LL%%' M  
 3388A(KK	))/IIfX1123*
 
/r   resource_idsc                 ,   |r*|D cg c]  }|j                   dz   |j                  z     }}ng }| j                  }|j                  |j	                         |      }	 | j
                  j                  |      S c c}w # t        j                  $ r Y yw xY w)aw  Calls GetApplicationStatus API of Runapps for the specified reference.

  Args:
    client: the api client to use.
    app_ref: the resource reference of the application.
    resource_ids: ResourceID of the resource to get status for. If not given,
      all resources in the application will be queried.

  Returns:
    The ApplicationStatus object. Or None if not found.
  /)r#   r   N)	typer#   r$   4RunappsProjectsLocationsApplicationsGetStatusRequestr&   r'   	GetStatusr)   r*   )r   r   r;   res_idres_filtersmoduler.   s          r   GetApplicationStatusrD   s   s    " 6B6BFcFKK'l   K!!&GG![ H '11;;GDD 
	.	. s   #A8A= =BBapplicationc                     | j                   j                  | j                  j                  ||j                  |j                         j                                     S )a  Calls CreateApplicaton API of Runapps for the specified reference.

  Args:
    client: the api client to use.
    app_ref: the resource reference of
      the application.
    application: the application to create

  Returns:
    the LRO of this request.
  )rE   applicationIdr2   )r'   Creater$   1RunappsProjectsLocationsApplicationsCreateRequestr#   Parentr&   )r   r   rE   s      r   CreateApplicationrK      sV      
	/	/	6	6NN!#((!..0 O 2
3 3r   update_maskc                     | j                   j                  | j                  j                  |||j	                                     S )aL  Calls ApplicationPatch API of Runapps for the specified reference.

  Args:
    client: the api client to use.
    app_ref: the resource reference of
      the application.
    application: the application to patch
    update_mask: comma separated string listing the fields to be updated.

  Returns:
    the LRO of this request.
  )rE   
updateMaskr#   )r'   Patchr$   0RunappsProjectsLocationsApplicationsPatchRequestr&   )r   r   rE   rL   s       r   PatchApplicationrQ      sI    " 
	/	/	5	5MM! ##% N '
( (r   
deploymentvalidate_onlyc                     | j                   j                  | j                  j                  |j	                         ||j
                  |            S )a0  Calls CreateDeployment API of Runapps.

  Args:
    client: the api client to use.
    app_ref: the resource reference of the application the deployment belongs to
    deployment: the deployment object
    validate_only: whether to only validate the deployment

  Returns:
    the LRO of this request.
  )r2   rR   deploymentIdvalidateOnly)+projects_locations_applications_deploymentsrH   r$   <RunappsProjectsLocationsApplicationsDeploymentsCreateRequestr&   r#   )r   r   rR   rS   s       r   CreateDeploymentrY      sP    " 
	;	;	B	BCC%%'!$	 D &
 r   deployment_namec                     	 | j                   j                  | j                  j                  |            S # t        j
                  $ r Y yw xY w)aH  Calls GetDeployment API of Runapps.

  Args:
    client: the api client to use.
    deployment_name: the canonical name of the deployment.  For example:
      projects/<project>/locations/<location>/applications/<app>/deployment/<id>

  Returns:
    the Deployment object.  None is returned if the deployment cannot be found.
  r"   N)rW   r(   r$   9RunappsProjectsLocationsApplicationsDeploymentsGetRequestr)   r*   )r   rZ   s     r   GetDeploymentr]      sW    ==AA	B	B  
C 
"
 

 
	.	. s   47 AA	operationc                 0    t        | || j                        S )zWaits for an operation to complete.

  Args:
    client:  client used to make requests.
    operation: object to wait for.

  Returns:
    the application from the operation.
  )_WaitForOperationr'   )r   r^   s     r   WaitForApplicationOperationra      s      
69!AA
C Cr   c                 4    t        | || j                  ||      S )a9  Waits for an operation to complete.

  Args:
    client: client used to make requests.
    operation: object to wait for.
    tracker: The ProgressTracker that tracks the operation progress.
    tracker_update_func: function to update the tracker on polling.

  Returns:
    the deployment from thex operation.
  )r`   rW   )r   r^   trackertracker_update_funcs       r   WaitForDeploymentOperationre     s$      
69!MM"$7
9 9r   c           	         t        j                  || j                        }t        j                  j                  |j                  dj                  t                    }fd}	 |j                  t        j                  ||t        t        |            S # t         j                  $ rr |j                  |      }t        j                   dj                  |j"                  j$                  t'        j(                  |j"                  j*                                    t,        j.                  $ r, t        j0                  dj                  t        dz              w xY w)a  Waits for an operation to complete.

  Args:
    client:  client used to make requests.
    operation: object to wait for.
    resource_type: type, the expected type of resource response
    tracker: The ProgressTracker that tracks the operation progress.
    tracker_update_func: function to update the tracker on polling.

  Returns:
    The resulting resource of input paramater resource_type.
  z {}.projects.locations.operations)
collectionc                 F    y r | |       y j                          y N)Tick)resultstatusrc   rd   s     r   _StatusUpdatez(_WaitForOperation.<locals>._StatusUpdate3  s#    '662llnr   )max_wait_mswait_ceiling_msstatus_updatez%OperationError: code={0}, message={1}zkOperation timed out after {0} seconds. The operations may still be underway remotely and may still succeed.r   )r   CloudOperationPollerprojects_locations_operationsr   REGISTRYParseRelativeNamer#   r6   r   	GetResultPollUntilDone_POLLING_TIMEOUT_MS_RETRY_TIMEOUT_MSOperationErrorPollr
   IntegrationsOperationErrorerrorcoder	   Decodemessager   WaitExceptionTimeoutError)r   r^   resource_typerc   rd   polleroperation_refrm   s      ``   r   r`   r`     s0   " &&}'-'K'KM&$$66nn3::8D 7 F--+-'	)* * 
		 MM*I

/
//66OO  (//)//2I2I"J	LM M 
		 -


	6	#d*	+- --s   (0B CEc                 |    t        j                  | j                  t        j                  |j                              S )zGet the metadata message for the deployment operation.

  Args:
    messages: Module containing the definitions of messages for the Runapps
      API.
    operation: The LRO

  Returns:
    The DeploymentOperationMetadata object.
  )apitools_encodingPyValueToMessageDeploymentOperationMetadataMessageToPyValuemetadata)messagesr^   s     r   GetDeploymentOperationMetadatar   P  s4     
	+	+**((););<
> >r   proj_idc                     | j                   j                  dj                  |            }| j                  j	                  |      S )zGet the list of all available regions from control plane.

  Args:
    client: instance of a client to use for the list request.
    proj_id: project id of the project to query.

  Returns:
    A list of location resources.
  zprojects/{0}r"   )r$   #RunappsProjectsLocationsListRequestr6   projects_locationsr   )r   r   r.   s      r   ListLocationsr   d  sG     ""FF  ) G ' 
	"	"	'	'	00r   regionr|   c                     t        j                  |      }t        j                  |j                  j
                        r$t        j                  dj                  |             |)zGet the metadata message for the deployment operation.

  Args:
    region: target region of the request.
    error: original HttpError.

  Raises:
    UnsupportedIntegrationsLocationError if it's location error. Otherwise
    raise the original error.
  z3Location {} is not found or access is unauthorized.)	api_lib_exceptionsHttpException_LOCATION_ERROR_REGEXmatchpayloadstatus_messager
   $UnsupportedIntegrationsLocationErrorr6   )r   r|   
parsed_errs      r   r,   r,   v  sV     "//6*  !3!3!B!BC

9
9=DDVL  	+r   ri   )F)NN)>__doc__
__future__r   r   r   r   retypingr   r   apitools.base.pyr	   r   r
   r)   googlecloudsdk.api_lib.utilr   r   r   "googlecloudsdk.command_lib.runappsgooglecloudsdk.corer   r   googlecloudsdk.core.utilr   6googlecloudsdk.generated_clients.apis.runapps.v1alpha1r   r   r   r   APP_DICT_CONFIG_KEYAPP_CONFIG_DICT_RESOURCES_KEYrw   rx   compiler   r   RunappsV1alpha1Applicationr0   ListApplicationsResponser:   
ResourceIDApplicationStatusrD   	OperationrK   strrQ   
DeploymentboolrY   r]   ra   re   r`   r   r   ListLocationsResponser   	Exceptionr,   r   r   r   <module>r      s)   B &  % ' 	 ! : > , H . 9 # ) - * Z \    +  %  "

#BC 71#3311#$=$I$IJ1:#33>G772 JN#33 4 9 D DEF '99:	B3#3333 +663 
#	,	,	36 "&	(#33(( +66( #	( +D*M*M	(8 %*	#33 *44 D>	
 
#	,	,6#33%&?&J&JK.C#33C(22C 
#	.	.C$9#339(229 &?%I%I90 #*.	1-5EE 1-!:!D!D1-h>(22> 
#	>	>>(1#3311.DD1$ Y 9 r   