
    (                     @   S 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\RP                  \RR                  5      r*S r+g)z7Utilities for interacting with the Connect Gateway API.    )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)GetCredentialsCommand2   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.RunGetCredentials6   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r3   r4   force_use_agentr5   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/)namer>   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)r2   r3   r4   r>   r5   r6   project_numberrC   r   respnewrR   r:   s                r;   RunServerSide#GetCredentialsCommand.RunServerSiden   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locationr8   z_ns-)KUBECONTEXT_FORMATformat)r2   r6   rb   r8   	namespacekcs         r;   r1   !GetCredentialsCommand.KubeContext   s5    		"	"* 
# 
B FYbI    r6   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   TestIamPermissionsri   setissubsetmemberships_errorsInsufficientPermissionsError)r2   r6   ri   project_refresultgranted_permissionss         r;   r%   !GetCredentialsCommand.RunIamCheck   s^    ++J7K,,[FF ,,{$$S)<%=>>;;== ?rh   c                 \    [         R                  " XU5      n[         R                  " U5      $ N)hubapi_utilMembershipRefGetMembership)r2   r6   rb   r8   resource_names        r;   r.   +GetCredentialsCommand.ReadClusterMembership   s%    --jJOM$$]33rh   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[   rb   r9   r8   gcp)r8   rb   r6   serverauth_providerr   r   )rk   GetProjectNumberSERVER_FORMATrd   
GetVersionr1   rO   rP   rS   ContextrW   UserusersClusterclustersrU   rY   )r2   r~   r6   rb   r9   r8   re   r[   kwargsuser_kwargscluster_kwargscontextclusterrR   s                 r;   r0   (GetCredentialsCommand.GenerateKubeconfig   s+    "22:>N  &&%OO%)!! ' 
 F 	K NzZKGzZ@G##++-J#*??'$J  !(W D DJW#*??!$%3$J    )rh   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 )rJ   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rh    rw   )FN)__name__
__module____qualname____firstlineno____doc__r<   strboolr   r^   r1   r   r%   r.   r0   classmethodr   __static_attributes__r   rh   r;   r   r   2   s    m6x $(,55 5 	5
 39%5n>C >d3i >4 *X  rh   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	   PromptToEnableApirp   ServiceNotEnabledErrorapitools_exceptionsRequestError)r6   r~   s     r;   r,   r,      sh    ( 
	$	$Z	>	>




3
3#\ 
? ##00  s   -A A-,A-N),r   typingr   r   +googlecloudsdk.api_lib.cloudresourcemanagerr    googlecloudsdk.api_lib.containerr   r   5googlecloudsdk.api_lib.container.fleet.connectgatewayr   rL   rH   googlecloudsdk.api_lib.servicesr   googlecloudsdk.api_lib.utilr	   googlecloudsdk.callioper
   *googlecloudsdk.command_lib.container.fleetr   rx   r    r   rO   r   6googlecloudsdk.command_lib.container.fleet.membershipsr   rp   #googlecloudsdk.command_lib.projectsrk   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   rc   r   rE   r&   r!   Commandr   r,   r   rh   r;   <module>r      s    >  D C Z V 6 , ( N G S @ _ G D # * .G {  ~H// ~B rh   