
                             d Z ddlmZ ddlmZ ddlm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Z G d de      Zy)z%Cloud Functions (2nd gen) API Client.    )absolute_import)division)unicode_literals)	GeneratorOptional)
exceptions)
list_pager)util)types)
propertiesNc            	       :   e Zd ZdZd Zdeej                  ddf   fdZdde	de
e	   fdZej                  	 dd	e	d
ede
ej                     fd       Zej                  d	e	dej"                  fd       Zej                  d	e	dej"                  fd       Zej                  d	e	dej"                  fd       Zej                  d	e	dej"                  fd       Zej                  d	e	de	dej"                  fd       Zej                  d	e	dej"                  fd       Zy)FunctionsClientz)Client for Cloud Functions (2nd gen) API.c                 l    t        j                  |      | _        t        j                  |      | _        y N)r
   GetClientInstanceclientGetMessagesModulemessages)selfrelease_tracks     1lib/googlecloudsdk/api_lib/functions/v2/client.py__init__zFunctionsClient.__init__#   s&    ((7DK**=9DM    returnNc                     t         j                  j                  j                  j	                         }| j
                  j                  d|z         }t        j                  | j                  j                  |dd      S )zLists GCF gen2 regions.

    Returns:
      Iterable[cloudfunctions_v2alpha.Location], Generator of available GCF gen2
        regions.
    z	projects/name	locationspageSize)servicerequestfieldbatch_size_attribute)r   VALUEScoreproject	GetOrFailr   *CloudfunctionsProjectsLocationsListRequestr	   YieldFromListr   projects_locations)r   r&   r!   s      r   ListRegionszFunctionsClient.ListRegions'   sq     $$,,668GmmFF7" G G ##..'	 r   regionquery_filterc                    t         j                  j                  j                  j	                         }| j
                  j                  dj                  ||      |      }| j                  j                  j                  |      S )a  Lists available GCF Gen 2 Runtimes in a region.

    Args:
      region: str, The region targeted to list runtimes in.
      query_filter: str, Filters to apply to the list runtimes request.

    Returns:
      v2alpha|v2beta.ListRuntimesResponse, The list runtimes request
    z%projects/{project}/locations/{region})r&   r,   )parentfilter)r   r$   r%   r&   r'   r   2CloudfunctionsProjectsLocationsRuntimesListRequestformatr   projects_locations_runtimesList)r   r,   r-   r&   r!   s        r   ListRuntimeszFunctionsClient.ListRuntimes9   sz     $$,,668G mmNN6==F > 
 	 O G ;;2277@@r   r   raise_if_not_foundc                 *   	 | j                   j                  j                  | j                  j	                  |            S # t
        j                  $ r>}|s1|j                  t        j                  j                  j                  k7  r Y d}~yd}~ww xY w)a=  Gets the function with the given name or None if not found.

    Args:
      name: GCFv2 function resource relative name.
      raise_if_not_found: If set, raises NOT_FOUND http errors instead of
        returning None.

    Returns:
      cloudfunctions_v2_messages.Function, the fetched GCFv2 function or None.
    r   N)r   projects_locations_functionsGetr   2CloudfunctionsProjectsLocationsFunctionsGetRequestapitools_exceptions	HttpErrorstatus_codesixmoveshttp_client	NOT_FOUND)r   r   r6   errors       r   GetFunctionzFunctionsClient.GetFunctionO   s~    [[5599
--
J
J K  
 (( 	


syy44>>
>s   >A B4BBc                     | j                   j                  j                  | j                  j	                  |            S )zAborts the function upgrade for the given function.

    Args:
      name: str, GCFv2 function resource relative name.

    Returns:
      A long-running operation.
    r   )r   r8   AbortFunctionUpgrader   CCloudfunctionsProjectsLocationsFunctionsAbortFunctionUpgradeRequestr   r   s     r   rE   z$FunctionsClient.AbortFunctionUpgradek   s<     ;;33HHYY 	Z 	
 r   c                     | j                   j                  j                  | j                  j	                  |            S )zCommits the function upgrade for the given function.

    Args:
      name: str, GCFv2 function resource relative name.

    Returns:
      A long-running operation.
    r   )r   r8   CommitFunctionUpgrader   DCloudfunctionsProjectsLocationsFunctionsCommitFunctionUpgradeRequestrG   s     r   rI   z%FunctionsClient.CommitFunctionUpgrade{   s<     ;;33IIZZ 	[ 	
 r   c                     | j                   j                  j                  | j                  j	                  |            S )zRedirects function upgrade traffic for the given function.

    Args:
      name: str, GCFv2 function resource relative name.

    Returns:
      A long-running operation.
    r   )r   r8   RedirectFunctionUpgradeTrafficr   MCloudfunctionsProjectsLocationsFunctionsRedirectFunctionUpgradeTrafficRequestrG   s     r   rL   z.FunctionsClient.RedirectFunctionUpgradeTraffic   <     ;;33RRcc 	d 	
 r   c                     | j                   j                  j                  | j                  j	                  |            S )zRolls back function upgrade traffic for the given function.

    Args:
      name: str, GCFv2 function resource relative name.

    Returns:
      A long-running operation.
    r   )r   r8   RollbackFunctionUpgradeTrafficr   MCloudfunctionsProjectsLocationsFunctionsRollbackFunctionUpgradeTrafficRequestrG   s     r   rP   z.FunctionsClient.RollbackFunctionUpgradeTraffic   rN   r   trigger_service_accountc                     | j                   j                  j                  | j                  j	                  || j                  j                  |                  S )a  Sets up the function upgrade config for the given function.

    Args:
      name: str, GCFv2 function resource relative name.
      trigger_service_account: str, The service account to use for the trigger.

    Returns:
      A long-running operation.
    )triggerServiceAccount)r   !setupFunctionUpgradeConfigRequest)r   r8   SetupFunctionUpgradeConfigr   ICloudfunctionsProjectsLocationsFunctionsSetupFunctionUpgradeConfigRequest!SetupFunctionUpgradeConfigRequest)r   r   rR   s      r   rV   z*FunctionsClient.SetupFunctionUpgradeConfig   sW     ;;33NN__.2mm.].]&= /^ / 	` 	
 r   c                     | j                   j                  j                  | j                  j	                  |            S )a&  Detach a GCF 2nd gen function from GCF and make it a native Cloud Run function.

    Args:
      name: str, GCFv2 function resource relative name which follows the format
        of `projects/{project}/locations/{region}/functions/{function}`.

    Returns:
      A long-running operation.
    r   )r   r8   DetachFunctionr   =CloudfunctionsProjectsLocationsFunctionsDetachFunctionRequestrG   s     r   rZ   zFunctionsClient.DetachFunction   s<     ;;33BBSS 	T 	
 r   r   )F)__name__
__module____qualname____doc__r   r   r   Locationr+   strr   r5   util_v1 CatchHTTPErrorRaiseHTTPExceptionboolFunctionrC   	OperationrE   rI   rL   rP   rV   rZ    r   r   r   r       s   1:9U^^T4%?@ $A AHSM A, ++27+/ ,6 ++s u  , ++   , ++   , ++   , ++03 ,* ++   ,r   r   )r_   
__future__r   r   r   typingr   r   apitools.base.pyr   r;   r	   #googlecloudsdk.api_lib.functions.v1r
   rb   #googlecloudsdk.api_lib.functions.v2r   googlecloudsdk.corer   r>   objectr   rg   r   r   <module>ro      s9    , &  ' & > ' ? 5 4 * 
pf pr   