
    b:                     f   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 Zd Zd Zd Zd Zd Zd Z	 	 	 	 d!dZej4                  j6                  fdZd
ej4                  j6                  dd
fdZd Zd"dZd Z d Z!d Z"d"dZ#d Z$d Z%d  Z&y
)#zLibrary 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                 F    t         j                  j                  | |       | fS N)r   _API_NAME_ALIASESget)api_names    0lib/googlecloudsdk/api_lib/util/apis_internal.py_GetApiNameAndAliasr   #   s     

%
%
)
)(H
=x	HH    c                     t        |       \  } }t        j                  j                  | i       }t	        j
                  |      D ]  \  }}|j                  s|c S  y r   )r   r
   MAPr   six	iteritemsdefault_version)r   _api_versverapi_defs        r   _GetDefaultVersionr   (   sP    #H-+(A\\h+(mmH-lc7j . 
r   c                  P    t        t        j                  j                               S )z6Returns list of avaiblable apis, ignoring the version.)sortedr
   r   keys r   r   _GetApiNamesr#   1   s    	!!#	$$r   c                     t        |       \  } }t        j                  j                  | d      }|t	        j
                  |       t        |j                               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_maps      r   _GetVersionsr(   6   sS     $H-+(A  40+

#
#H
--	k 	!!r   c                    t        |       \  } }| t        j                  vrt        j                  |       t
        j                  j                  j                         }|j                  dj                  | |            }|s|j                  |d      }|xs |}t        j                  |    }|||vrt        j                  | |      ||   }|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   s          r   	GetApiDefr4   I   s     1:(NX\\!

#
#H
-- ''<<FFH '**7>>, 	(,,^TB /K+h',K|;

'
'+
>>;'G	.r   c                 0    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.
  )r4   _GetClientClassFromDefr   r/   r   s      r   _GetClientClassr8   o   s     h,'		((r   c                     | j                   j                  }|j                  dd      \  }}t        ||g      }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   r?   module_pathclient_class_name
module_objs        r   r6   r6   }   sL     "**@@#8#?#?Q#G + +1B0CD*	.	//r   Fc                 p   |r|/J |+ddl m} |j                  t        j                  |r|nd      }t        | |      } |t        | ||      d|      }|||_        t        j                  j                  j                  j                         }	|	r#|j                  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.
  r   )
transportsunset)response_encodingtimeoutF)urlget_credentialshttpkeyzX-Google-Project-Overrideapikey)googlecloudsdk.core.credentialsrG   GetApitoolsTransportr	   ENCODINGr8   _GetEffectiveApiEndpointcheck_response_funcr   r*   coreapi_keyGetAddGlobalParamadditional_http_headers)r   r/   no_httphttp_clientrT   http_timeout_secrG   client_classclient_instancerV   headers              r   _GetClientInstancer`      s    ,  ;11#,,$4 ' 2 CK !;7, 
"8[,
G/ $*=O'""**..0'""5'2(F6>O++F3	r   c                 t   t        | |      }|t        j                  j                  k(  r|j                  j
                  }nJ|t        j                  j                  k(  r|j                  j                  }n|j                  j                  }|j                  dd      \  }}t        ||g      }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.
  r:   r;   r<   )r4   r   GapicTransportGRPC_ASYNCIOgapicasync_client_full_classpathRESTrest_client_full_classpathr?   r@   rA   rB   )r   r/   transport_choicer   r?   rC   rD   rE   s           r   _GetGapicClientClassri      s     h,'11>>>#MMEE933888#MMDD#MM??#8#?#?Q#G + +1B0CD*	.	//r   c                 \      fd}t         ||      } |||t         |      ||      S )ah  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.

  Returns:
    An instance of the specified GAPIC API client.
  c                 
   	 t         j                  j                  j                        j	                         }|rt        |      j                  } r |       } || S t        |       S # t         j
                  $ r d }Y Gw xY wr   )	r   r*   api_endpoint_overridesPropertyrW   NoSuchPropertyErrorr   netlocUniversifyAddress)addressendpoint_overrideaddress_override_funcr   s     r   AddressOverridez0_GetGapicClientInstance.<locals>.AddressOverride   s    $++BBKK
CE 
 *+22g%g.g$nW%% )) s   7A* *BB)rh   )rs   mtls_enabledattempt_direct_pathredact_request_body_reason)ri   _MtlsEnabled)	r   r/   credentialsrs   rh   rv   rw   rt   r]   s	   `  `     r   _GetGapicClientInstancerz      sC    @&$ &.>@, 
++6-!;
 r   c                     t         j                  j                  j                  }|j	                         }| ,|j
                  |k7  r| j                  |j
                  |d      } | S )z2Update a URL based on the current universe domain.r;   )r   r*   rU   universe_domainrW   defaultreplace)rq   universe_domain_propertyr|   s      r   rp   rp     s]    '..33CC,002/&&/9oo6>>-q2G	.r   c                     t        | |      }|j                  r|xs t        | |      }n|xs t        | |      }|j                  xs |j
                  S )zReturns mtls endpoint.)r4   r>   r8   ri   mtls_endpoint_overrideMTLS_BASE_URL)r   r/   r]   r   s       r   _GetMtlsEndpointr     sQ    h,'I?8[#ILN#7+#NL		'	'	E<+E+EEr   c                     t         j                  j                  j                  j	                         ryt         j                  j                  j
                  j	                         sy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_certificater4   enable_mtlsr7   s      r   rx   rx   '  s^    & $$==EEG				(	(	?	?	G	G	Ih,'			r   c                     t        |      }t        |       }|j                  dk(  s|j                  dk7  r| S t        dj                  |j                  |j
                        |j                        S )zFConstructs a normalized endpoint URI depending on the client base_url./z{}://{})r   pathr   r-   schemero   )rr   base_urlurl_baseurl_endpoint_overrides       r   _BuildEndpointOverrider   D  sn    h("#45]]c2773>	,33,3356>mm
E Er   c                 @   t        | |      j                  rt        | |      }nt        | |      }t	        |d      r|j
                  }t        |      S 	 t        | |      j
                  }t        |      S # t        $ r dj                  | |      }Y t        |      S w xY w)zReturns base url for given api.BASE_URLzhttps://{}.googleapis.com/{})
r4   r>   r8   ri   hasattrr   _GetResourceModuleAttributeErrorr-   rp   )r   r/   r]   client_base_urls       r   _GetBaseUrlFromApir   O  s    x%.."8[9L'+>L\:&"++O 
?	++*8[AJJo
 
?	++	  6==
Ko 
?	++	s   A5 5BBc                    	 t         j                  j                  j                  |       j	                         }t        | |      }|j                  r|xs t        | |      }n|xs t        | |      }t        | |      }|rt        ||      }|S t        | |      rt        t        | ||            }|S |}|S # t         j
                  $ r d}Y w xY w)z)Returns effective endpoint for given api.N)r   r*   rl   rm   rW   rn   r4   r>   r8   ri   r   r   rx   rp   r   )r   r/   r]   rr   r   r   rq   s          r   rS   rS   b  s    "))@@II#% 
 h,'I?8[#ILN#7+#NL&x=/$%6HG 
. Hk*;=G 
. G	.% 
	'	' s   7B6 6CCc                 ^    t        | |      }t        |j                  j                  dg      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.
  	somethingr<   )r4   rA   r>   messages_full_modulepathr7   s      r   _GetMessagesModuler   |  s4     h,' 
//;-
I Ir   c                     t        | |      }|j                  r(t        |j                  j                  dz   dz   dg      S t        |j                  j                  dz   dz   dg      S )z/Imports and returns given api resources module.r:   	resourcesr   r<   )r4   r>   rA   
class_pathrd   r7   s      r   r   r     so     h,' ##c)K7;-  
mm${2k]
 r   c              #   0  K   	 t        | |      }|j                  D ]g  }t        j                  | ||j                  |j
                  |j                  |j                  |j                  |j                  |j                  	       i y# t        $ r Y yw xY ww)z)Yields all collections for for given api.N)r   Collectionsresource_utilCollectionInfor   DOCS_URLcollection_namer   
flat_pathsparamsenable_uri_parsingImportError)r   r/   resources_module
collections       r   _GetApiCollectionsr     s     )(K@ '22
((



#
#

#
#

$
$
//







'
'
 
 3 
 		s(   BB A7B	BBBB)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(   r4   r8   r6   r`   rb   GRPCri   rz   rp   r   rx   r   r   rS   r   r   r   r"   r   r   <module>r      s    '  ' 1 A * ) : 
 * +I
%
"&#L)0"  %#'+/(,-d +4*B*B*G*G08 --22#;|F:E,&4I" r   