o
    ;                     @   sF  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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 ddlmZ dd Zdd Zdd Zdd Zdd Zdd Zdd Z		
	
	
d4ddZejjfddZd
ejjdd
d
fd d!Zd"d# Zd5d$d%Zd&d' Z d(d) Z!d*d+ Z"d5d,d-Z#d.d/ Z$d0d1 Z%d2d3 Z&d
S )6zLibrary for obtaining API clients and messages.

This should only be called by api_lib.util.apis, core.resources, gcloud meta
commands, and module tests.
    )absolute_import)division)unicode_literals)	apis_util)resource)
properties)	transport)apis_mapN)urljoin)urlparsec                 C   s   t j| | | fS N)r   _API_NAME_ALIASESget)api_name r   F/tmp/google-cloud-sdk/lib/googlecloudsdk/api_lib/util/apis_internal.py_GetApiNameAndAlias#   s   r   c                 C   s@   t | \} }tj| i }t|D ]\}}|jr|  S qd S r   )r   r	   MAPr   six	iteritemsdefault_version)r   _api_versverapi_defr   r   r   _GetDefaultVersion(   s   r   c                   C   s   t tj S )z6Returns list of avaiblable apis, ignoring the version.)sortedr	   r   keysr   r   r   r   _GetApiNames1   s   r   c                 C   s8   t | \} }tj| d}|du rt| t| S )zReturn available versions for given api.

  Args:
    api_name: str, The API name (or the command surface name, if different).

  Raises:
    apis_util.UnknownAPIError: If api_name does not exist in the APIs map.

  Returns:
    list, of version names.
  N)r   r	   r   r   r   UnknownAPIErrorlistr   )r   r   version_mapr   r   r   _GetVersions6   s
   
r"   c                 C   s   t | \} }| tjvrt| tjj }|	d
| |}|s'|	|d}|p*|}tj|  }|du s8||vr>t| ||| }|S )a  Returns the APIDef for the specified API and version.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    api_version: str, The version of the API.

  Raises:
    apis_util.UnknownAPIError: If api_name does not exist in the APIs map.
    apis_util.UnknownVersionError: If api_version does not exist for given
      api_name in the APIs map.

  Returns:
    APIDef, The APIDef for the specified API and version.
  z{}/{}N)r   r	   r   r   r   r   VALUESapi_client_overrides	AllValuesr   formatUnknownVersionError)r   api_versionapi_name_aliasversion_overridesversion_overrideapi_versionsr   r   r   r   	GetApiDefI   s   


r-   c                 C   s   t | |}t|S )a  Returns the client class for the API specified in the args.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    api_version: str, The version of the API.

  Returns:
    base_api.BaseApiClient, Client class for the specified API.
  )r-   _GetClientClassFromDefr   r(   r   r   r   r   _GetClientClasso   s   

r0   c                 C   s0   | j j}|dd\}}t||gd}t||S )zReturns the apitools client class for the API definition specified in args.

  Args:
    api_def: apis_map.APIDef, The definition of the API.

  Returns:
    base_api.BaseApiClient, Client class for the specified API.
  .   fromlist)apitoolsclient_full_classpathrsplit
__import__getattr)r   r6   module_pathclient_class_name
module_objr   r   r   r.   }   s   	
r.   Fc                 C   s   |r	|du sJ n|du rddl m} |jtj|r|ndd}t| |}|t| ||d|d}|dur6||_tj	j
j }	|	rL|d|	 d	}
d
|j|
< |S )aG  Returns an instance of the API client specified in the args.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    api_version: str, The version of the API.
    no_http: bool, True to not create an http object for this client.
    http_client: bring your own http client to use. Incompatible with
      no_http=True.
    check_response_func: error handling callback to give to apitools.
    http_timeout_sec: int, seconds of http timeout to set, defaults if None.

  Returns:
    base_api.BaseApiClient, An instance of the specified API client.
  Nr   )
transportsunset)response_encodingtimeoutF)urlget_credentialshttpkeyzX-Google-Project-Overrideapikey)googlecloudsdk.core.credentialsr=   GetApitoolsTransportr   ENCODINGr0   _GetEffectiveApiEndpointcheck_response_funcr   r#   coreapi_keyGetAddGlobalParamadditional_http_headers)r   r(   no_httphttp_clientrJ   http_timeout_secr=   client_classclient_instancerL   headerr   r   r   _GetClientInstance   s,   



rV   c                 C   sf   t | |}|tjjkr|jj}n|tjjkr|jj}n|jj}|	dd\}}t
||gd}t||S )a*  Returns the GAPIC client class for the API def specified by the args.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    api_version: str, The version of the API.
    transport_choice: apis_util.GapicTransport, The transport to be used by the
      client.
  r1   r2   r3   )r-   r   GapicTransportGRPC_ASYNCIOgapicasync_client_full_classpathRESTrest_client_full_classpathr6   r7   r8   r9   )r   r(   transport_choicer   r6   r:   r;   r<   r   r   r   _GetGapicClientClass   s   



r^   c           
         s6    fdd}t ||d}	|	||t||||dS )a  Returns an instance of the GAPIC API client specified in the args.

  For apitools API clients, the API endpoint override is something like
  http://compute.googleapis.com/v1/. For GAPIC API clients, the DEFAULT_ENDPOINT
  is something like compute.googleapis.com. To use the same endpoint override
  property for both, we use the netloc of the API endpoint override.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    api_version: str, The version of the API.
    credentials: google.auth.credentials.Credentials, the credentials to use.
    address_override_func: function, function to call to override the client
      host. It takes a single argument which is the original host.
    transport_choice: apis_util.GapicTransport, The transport to be used by the
      client.
    attempt_direct_path: bool, True if we want to attempt direct path gRPC where
      possible.
    redact_request_body_reason: str, the reason why the request body must be
      redacted if --log-http is used. If None, the body is not redacted.
    custom_interceptors: list[grpc interceptor], a list of custom interceptors
    to add to the channel.

  Returns:
    An instance of the specified GAPIC API client.
  c                    s^   zt jj }W n t jy   d }Y nw |rt|j}  r% | } |d ur+| S t| S r   )	r   r#   api_endpoint_overridesPropertyrM   NoSuchPropertyErrorr   netlocUniversifyAddress)addressendpoint_overrideaddress_override_funcr   r   r   AddressOverride   s    
z0_GetGapicClientInstance.<locals>.AddressOverride)r]   )rg   mtls_enabledattempt_direct_pathredact_request_body_reasoncustom_interceptors)r^   _MtlsEnabled)
r   r(   credentialsrg   r]   rj   rk   rl   rh   rS   r   rf   r   _GetGapicClientInstance   s   #ro   c                 C   s8   t jjj}| }| dur|j|kr| |j|d} | S )z2Update a URL based on the current universe domain.Nr2   )r   r#   rK   universe_domainrM   defaultreplace)rd   universe_domain_propertyrp   r   r   r   rc     s   

rc   c                 C   s:   t | |}|jr|pt| |}n|pt| |}|jp|jS )zReturns mtls endpoint.)r-   r5   r0   r^   mtls_endpoint_overrideMTLS_BASE_URL)r   r(   rS   r   r   r   r   _GetMtlsEndpoint!  s
   
rv   c                 C   s4   t jjj r	dS t jjj sdS t| |}|jS )a  Checks if the API of the given version should use mTLS.

  If context_aware/always_use_mtls_endpoint is True, then mTLS will always be
  used.

  If context_aware/use_client_certificate is True, then mTLS will be used only
  if the API version is in the mTLS allowlist.

  gcloud maintains a client-side allowlist for the mTLS feature
  (go/gcloud-rollout-mtls).

  Args:
    api_name: str, The API name.
    api_version: str, The version of the API.

  Returns:
    True if the given service and version is in the mTLS allowlist.
  TF)r   r#   context_awarealways_use_mtls_endpointGetBooluse_client_certificater-   enable_mtlsr/   r   r   r   rm   +  s   
rm   c                 C   s@   t |}t | }|jdks|jdkr| S td|j|j|jS )zFConstructs a normalized endpoint URI depending on the client base_url./z{}://{})r   pathr
   r&   schemerb   )re   base_urlurl_baseurl_endpoint_overrider   r   r   _BuildEndpointOverrideH  s   r   c                 C   sv   t | |jrt| |}nt| |}t|dr|j}t	|S zt| |j}W t	|S  ty:   d| |}Y t	|S w )zReturns base url for given api.BASE_URLzhttps://{}.googleapis.com/{})
r-   r5   r0   r^   hasattrr   _GetResourceModuleAttributeErrorr&   rc   )r   r(   rS   client_base_urlr   r   r   _GetBaseUrlFromApiS  s   

r   c                 C   s   zt jj|  }W n t jy   d}Y nw t| |}|jr(|p&t| |}n|p.t	| |}t
| |}|r=t||}|S t| |rLtt| ||}|S |}|S )z)Returns effective endpoint for given api.N)r   r#   r_   r`   rM   ra   r-   r5   r0   r^   r   r   rm   rc   rv   )r   r(   rS   re   r   r   rd   r   r   r   rI   f  s0   




rI   c                 C   s   t | |}t|jjdgdS )a  Returns the messages module for the API specified in the args.

  Args:
    api_name: str, The API name (or the command surface name, if different).
    api_version: str, The version of the API.

  Returns:
    Module containing the definitions of messages for the specified API.
  	somethingr3   )r-   r8   r5   messages_full_modulepathr/   r   r   r   _GetMessagesModule  s   


r   c                 C   sD   t | |}|jrt|jjd d dgdS t|jjd d dgdS )z/Imports and returns given api resources module.r1   	resourcesr   r3   )r-   r5   r8   
class_pathrY   r/   r   r   r   r     s   
r   c                 c   s`    zt | |}W n
 ty   Y dS w |jD ]}t| ||j|j|j|j|j	|j
|j	V  qdS )z)Yields all collections for for given api.N)r   ImportErrorCollectionsresource_utilCollectionInfor   DOCS_URLcollection_namer}   
flat_pathsparamsenable_uri_parsing)r   r(   resources_module
collectionr   r   r   _GetApiCollections  s&   
r   )FNNNr   )'__doc__
__future__r   r   r   googlecloudsdk.api_lib.utilr   r   r   googlecloudsdk.corer   r   %googlecloudsdk.generated_clients.apisr	   r   six.moves.urllib.parser
   r   r   r   r   r"   r-   r0   r.   rV   rW   GRPCr^   ro   rc   rv   rm   r   r   rI   r   r   r   r   r   r   r   <module>   sR   	&
2

B


