
    \)                     d   S r SSKJr  SSKJr  SSKJr  SSK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r(Sr)S/r*/ SQr+ " S S\RX                  \RZ                  5      r.S r/g)z7Utilities for interacting with the Connect Gateway API.    )absolute_import)division)unicode_literals)ListUnion)projects_api)util)client)
enable_api)apis)base)api_util)gwkubeconfig_util)	overrides)errors)log)
properties)	platformsz0connectgateway_{project}_{location}_{membership}zihttps://{service_name}/{version}/projects/{project_number}/locations/{location}/{collection}/{membership}gkehub.gateway.get)zgkehub.memberships.getr   zserviceusage.services.getc                       \ rS rSrSrSS jr  SS\S\S\S\\S4   4S	 jjr	SS
 jr
S\S\\   4S jrS r SS jr\S 5       rSrg)GetCredentialsCommand5   zeGetCredentialsCommand is a base class with util functions for Gateway credential generating commands.Nc                    [         R                  " 5         [        R                  R	                  5       n[
        R                  R                  S5        [
        R                  R                  SU-   5        U R                  U[        5         [        R                  R                  R                  S5      R                  5       n[!        U["        R$                  " US 5      5        U R'                  XBU5      nSnUS:X  a  OI[)        US5      (       a8  [)        UR*                  S5      (       a  UR*                  R,                  (       a  SnU R/                  ["        R$                  " XR5      UUUUU5        S	U R1                  XBX5      -   S
-   n[
        R                  R                  U5        g ! [        R                   a    S n Nf = f)Nz'Starting to build Gateway kubeconfig...zCurrent project_id: gkehubmembershipszgkeconnect-proberendpoint
gkeClustergkeMembershipsA new kubeconfig entry "4" has been generated and set as the current context.)container_utilCheckKubectlInstalledhub_base
HubCommandProjectr   statusPrintRunIamCheckREQUIRED_CLIENT_PERMISSIONSr   VALUESapi_endpoint_overridesPropertyGetNoSuchPropertyErrorCheckGatewayApiEnablementr	   GetConnectGatewayServiceNameReadClusterMembershiphasattrr   r   GenerateKubeconfigKubeContext)	selfmembership_idarg_locationarg_namespace
project_idhub_endpoint_override
membership
collectionmsgs	            9lib/googlecloudsdk/command_lib/container/fleet/gateway.pyRunGetCredentials'GetCredentialsCommand.RunGetCredentials9   s   ((*$$,,.JJJ>?JJ+j89Z!<=#(//FFOO
  ))*?F
 ++-J J ((

J''J''66**#j))*?N 	#


m
	
 A		A  JJSU )) #"#s   7F* *GGr6   r7   force_use_agentr8   c           	         [         R                  R                  S5        [        R                  " 5         [
        R                  R                  5       n[
        R                  R                  SS9nU R                  U[        5        Sn[        R                  R                  5       (       a$  [        R                  " U R                  5       5      n[         R"                  " U5         [$        R&                  " U R                  5       5      nUR)                  SU SU SU 3UUUS9n	SSS5        [*        R,                  R/                  W	R0                  5      n
[*        R,                  R3                  5       nUR5                  U
SS	9  UR7                  [9        U
R:                  R=                  5       5      S
   5        UR?                  5         SUR@                   S3n[         R                  R                  U5        g! , (       d  f       N= f)a  RunServerSide generates credentials using server-side kubeconfig generation.

Args:
  membership_id: The short name of the membership to generate credentials
    for.
  arg_location: The location of the membership to generate credentials for.
  force_use_agent: Whether to force the use of Connect Agent in generated
    credentials.
  arg_namespace: The namespace to use in the kubeconfig context.
zFetching Gateway kubeconfig...T)numberNz	projects/z/locations/z/memberships/)namerA   kubernetes_namespaceoperating_system)	overwriter   r   r    )!r   r&   r'   r!   r"   r#   r$   r%   r(   REQUIRED_SERVER_PERMISSIONSr   OperatingSystem	IsWindowsgateway_utilWindowsOperatingSystemReleaseTrackr   RegionalGatewayEndpointgateway_clientGatewayClientGenerateCredentialskconfig
KubeconfigLoadFromBytes
kubeconfigDefaultMergeSetCurrentContextlistcontextskeys
SaveToFilecurrent_context)r5   r6   r7   rA   r8   r9   project_numberrF   r
   respnewrU   r=   s                r>   RunServerSide#GetCredentialsCommand.RunServerSideq   s   " JJ56((*$$,,.J((000=N
 	Z!<=  **,,%<<



 
	*	*<	8++D,=,=,?@f''>*+l^=Q^P_`),+	 ( d 
9 


*
*4??
;C##++-JSD)  cll&7&7&9!:1!=> #:#=#="> ?5 	5  JJS) 
9	8s   %A G99
Hc                 L    [         R                  XUS9nU(       a  USU-   -  nU$ )N)projectlocationr;   z_ns-)KUBECONTEXT_FORMATformat)r5   r9   re   r;   	namespacekcs         r>   r4   !GetCredentialsCommand.KubeContext   s5    		"	"* 
# 
B FYbI    r9   permissionsc                     [         R                  " U5      n[        R                  " X25      nUR                  n[        U5      R                  [        U5      5      (       d  [        R                  " 5       eg)z^Run an IAM check, making sure the caller has the necessary permissions to use the Gateway API.N)	project_utilParseProjectr   TestIamPermissionsrl   setissubsetmemberships_errorsInsufficientPermissionsError)r5   r9   rl   project_refresultgranted_permissionss         r>   r(   !GetCredentialsCommand.RunIamCheck   s^    ++J7K,,[FF ,,{$$S)<%=>>;;== ?rk   c                 \    [         R                  " XU5      n[         R                  " U5      $ N)hubapi_utilMembershipRefGetMembership)r5   r9   re   r;   resource_names        r>   r1   +GetCredentialsCommand.ReadClusterMembership   s%    --jJOM$$]33rk   c                 :   [         R                  " U5      nUUU[        R                  UU R	                  5       UUUUS9SS.nSS0n	0 n
U R                  X#XV5      nU R                  X#U5      n[        R                  R                  5       n[        R                  " XX5      UR                  U'   [        R                  " U40 U	D6UR                  U'   [        R                  " XS   40 U
D6UR                  U'   UR                  U5        UR!                  5         U$ )N)service_nameversionr^   re   r<   r;   gcp)r;   re   r9   serverauth_providerr   r   )rn   GetProjectNumberSERVER_FORMATrg   
GetVersionr4   rR   rS   rV   ContextrZ   UserusersClusterclustersrX   r\   )r5   r   r9   re   r<   r;   rh   r^   kwargsuser_kwargscluster_kwargscontextclusterrU   s                 r>   r3   (GetCredentialsCommand.GenerateKubeconfig   s+    "22:>N  &&%OO%)!! ' 
 F 	K NzZKGzZ@G##++-J#*??'$J  !(W D DJW#*??!$%3$J    )rk   c                    U R                  5       [        R                   R                  L a  gU R                  5       [        R                   R                  L a  gU R                  5       [        R                   R                  L a  gg)Nv1alpha1v1beta1v1 )rM   r   ALPHABETAGA)clss    r>   r    GetCredentialsCommand.GetVersion   se    
T..444				t0055	5				t0033	3rk    rz   )FN)__name__
__module____qualname____firstlineno____doc__r?   strboolr   ra   r4   r   r(   r1   r3   classmethodr   __static_attributes__r   rk   r>   r   r   5   s    m6x $(,55 5 	5
 39%5n>C >d3i >4 *X  rk   r   c           	          [         R                  " X5      (       d/   [        R                  " U U[        R
                  " SX5      5        gg! [        R                  R                   a     gf = f)a  Checks if the Connect Gateway API is enabled for a given project.

Prompts the user to enable the API if the API is not enabled. Defaults to
"No". Throws an error if the user declines to enable the API.

Args:
  project_id: The ID of the project on which to check/enable the API.
  service_name: The name of the service to check/enable the API.

Raises:
  memberships_errors.ServiceNotEnabledError: if the user declines to attempt
    to enable the API.
  exceptions.GetServicesPermissionDeniedException: if a 403 or 404 error is
    returned by the Get request.
  apitools_exceptions.HttpError: Another miscellaneous error with the
    listing service.
  api_exceptions.HttpException: API not enabled error if the user chooses to
    not enable the API.
zConnect Gateway APIN)r   IsServiceEnabledr   PromptToEnableApirs   ServiceNotEnabledErrorapitools_exceptionsRequestError)r9   r   s     r>   r/   r/      sh    ( 
	$	$Z	>	>




3
3#\ 
? ##00  s   -A A-,A-N)0r   
__future__r   r   r   typingr   r   +googlecloudsdk.api_lib.cloudresourcemanagerr    googlecloudsdk.api_lib.containerr	   r!   5googlecloudsdk.api_lib.container.fleet.connectgatewayr
   rO   rK   googlecloudsdk.api_lib.servicesr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   *googlecloudsdk.command_lib.container.fleetr   r{   r#   r   rR   r   6googlecloudsdk.command_lib.container.fleet.membershipsr   rs   #googlecloudsdk.command_lib.projectsrn   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   rf   r   rH   r)   r$   Commandr   r/   r   rk   r>   <module>r      s    > &  '  D C Z V 6 , ( N G S @ _ G D # * .G {  ~H// ~B rk   