
    h                     r    S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  S r
   SS jr " S	 S
5      rg)z+Library to SSH into a Cloud Run Deployment.    N)apis)
iap_tunnel)	ssh_utils)sshc                     [         R                  " SS5      n[         R                  " SS5      nUR                  U S9nUR                  R                  U5      nUR                  $ )zBReturns the Cloud project number associated with the `project_id`.cloudresourcemanagerv1)	projectId)r   GetMessagesModuleGetClientInstance&CloudresourcemanagerProjectsGetRequestprojectsGetprojectNumber)
project_idcrm_message_moduleresource_managerreqprojects        %lib/googlecloudsdk/api_lib/run/ssh.pyProjectIdToProjectNumberr      sg    --.DdK++,BDIAA 	B 	# %%))#.'			    c	                     0 n	X9S'   XIS'   XS'   XiS'   XyS'   [         R                  " 5       n
U R                  U
l        Xl        XZl        X*l        U(       a  U
R                  R                  SU-   5        U
$ )aD  Construct an SshTunnelArgs from command line args and values.

Args:
  track: ReleaseTrack, The currently running release track.
  project_number: str, the project number (string with digits).
  project: str, the project id.
  deployment_name: str, the name of the deployment.
  workload_type: Ssh.WorkloadType, the type of the workload.
  region: str, the region of the deployment.
  instance_id: str, the instance id (optional).
  container_id: str, the container id (optional).
  iap_tunnel_url_override: str, the IAP tunnel URL override (optional).

Returns:
  SshTunnelArgs.
deployment_nameworkload_typeproject_numberinstance_idcontainer_idz--iap-tunnel-url-override=)	r   SshTunnelArgsprefixtrackcloud_run_argsregionr   pass_through_argsappend)r!   r   r   r   r   r#   r   r   iap_tunnel_url_overrider"   ress              r   CreateSshTunnelArgsr(   $   s    8 .&5"#$1!%3!""-#/   "#ll#)%*+  $'>>@ 
*r   c                   t    \ rS rSrSr " S S\R                  5      rS\R                  S\4S jr
S rS	 rS
rg)SshT   z SSH into a Cloud Run Deployment.c                   $    \ rS rSrSrSrSrSrSrg)Ssh.WorkloadTypeW   zThe type of the deployment.worker_pooljobservice N)	__name__
__module____qualname____firstlineno____doc__WORKER_POOLJOBSERVICE__static_attributes__r2   r   r   WorkloadTyper-   W   s    %K
CGr   r<   argsr   c                 8   UR                   U l         X l        UR                  U l        [        UR                  5      U l        [        USS5      U l        [        USS5      U l        UR                  U l        UR                  U l	        [        USS5      U l
        g)zInitialize the SSH library.instanceN	containerr&   )r   r   r   r   r   getattrr?   r@   r#   release_trackr&   )selfr=   r   s      r   __init__Ssh.__init__^   s    //D&<<DL24<<@DD*d3DMT;5DN++DK++D#*'$D r   c                     U R                   (       a7  U R                  (       a&  SR                  U R                   U R                  5      $ g)z2Returns the host key alias for the SSH connection.zcloud-run-{}-{}zcloud-run-default)r?   r@   format)rC   s    r   HostKeyAliasSsh.HostKeyAliasl   s-    }}%%dmmT^^DD r   c                    [         R                  R                  5       nUR                  5         [         R                  R                  5       nUR                  SS9  Sn[         R                  " SSUUR                  5       R                  5       SU R                  U R                  U R                  U R                  S.S9  [         R                  " U R                  U R                  U R                  S9nU R                  5       n[         R                   " XS5      n[#        U R                  U R$                  U R                  U R                  U R&                  U R                  U R(                  U R*                  U R,                  5	      n[.        R0                  " 5       nUR3                  USS	9n	[         R4                  " UUUU	UR6                  S
9R9                  U5      $ )zRun the SSH command.F)	overwriterootN)r   r   r#   )cloud_run_params)r   r#   
deploymentno)host_key_aliasstrict_host_key_checking)remote	cert_fileiap_tunnel_argsoptionsidentity_file)r   EnvironmentCurrent
RequireSSHKeysFromFilenameEnsureKeysExistGetOsloginStateGetPublicKeyToEntryrB   r   r   r#   CertFileFromCloudRunDeploymentrH   Remoter(   r   r   r?   r@   r&   r   BaseSSHCLIHelper	GetConfig
SSHCommandkey_fileRun)
rC   envkeysuserrS   	dest_addrrR   rT   
ssh_helperssh_optionss
             r   rf   Ssh.Runs   s   
//
!
!
#CNN88  "D5)D ##%#33,,kk
 22{{''I
 !!#IZZ	(F)$$
O ++-J&& !% ' K
 >>'mm 
c#hr   )	r@   r   r&   r?   r   r   r#   rB   r   N)r3   r4   r5   r6   r7   enumEnumr<   argparse	NamespacerD   rH   rf   r;   r2   r   r   r*   r*   T   s8    (TYY 8-- l !6r   r*   )NNN)r7   rp   rn   googlecloudsdk.api_lib.utilr   "googlecloudsdk.command_lib.computer   r   #googlecloudsdk.command_lib.util.sshr   r   r(   r*   r2   r   r   <module>ru      s>    2   , 9 8 3$  -`U Ur   