
    +                        S r SSKJr  SSKJr  SSKJ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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  \R.                  " S/ SQ5      rSrS r\R6                  \R8                  " \R:                  R<                  5       " S S\R>                  5      5       5       r \R6                  \R8                  " \R:                  RB                  5       " S S\ 5      5       5       r"\R6                  \R8                  " \R:                  RF                  5       " S S\"5      5       5       r$\" 5       \$l%        \" 5       \"l%        \" 5       \ l%        g)z<Implements the command for starting a tunnel with Cloud IAP.    )absolute_import)division)unicode_literalsN)base_classes)iap_tunnel_websocket)arg_parsers)base)
exceptions)
iap_tunnel)scope)	ssh_utils)flags)log)
properties_TargetArgs
projectzoneinstance	interfaceportregionnetworkhost
dest_groupsecurity_gatewayz

To increase the performance of the tunnel, consider installing NumPy. For instructions,
please see https://cloud.google.com/iap/docs/using-tcp-forwarding#increasing_the_tcp_upload_bandwidth
c                      SSSS.n U $ )z7Construct help text based on the command release track.$Starts an IAP TCP forwarding tunnel.a  Starts a tunnel to Cloud Identity-Aware Proxy for TCP forwarding through which
another process can create a connection (eg. SSH, RDP) to a Google Compute
Engine instance.

To learn more, see the
[IAP for TCP forwarding documentation](https://cloud.google.com/iap/docs/tcp-forwarding-overview).

If the `--region` and `--network` flags are provided, then an IP address or FQDN
must be supplied instead of an instance name. This is most useful for connecting
to on-prem resources.
a  To open a tunnel to the instances's RDP port on an arbitrary local port, run:

  $ {command} my-instance 3389

To open a tunnel to the instance's RDP port on a specific local port, run:

  $ {command} my-instance 3389 --local-host-port=localhost:3333

To use the IP address or FQDN of your remote VM (eg, for on-prem), you must also
specify the `--region` and `--network` flags:

  $ {command} 10.1.2.3 3389 --region=us-central1 --network=default
)briefDESCRIPTIONEXAMPLES )detailed_helps    'lib/surface/compute/start_iap_tunnel.py_DetailedHelpr%   /   s!     1#-B 
    c                   Z    \ rS rSrSrSrSr\S 5       rS r	S r
S rS	 rS
 rS rS rSrg)StartIapTunnelU   r   TFc                 h   [         R                  " U5        [        R                  R	                  U5        UR                  S[        R                  " SSS9SS9  SnUR                  SS	 S
US9  UR                  SSSSS9  UR                  SSSSS9  [         R                  " XR                  5        g )Ninstance_port   i  )lower_boundupper_boundz8The name or number of the instance's port to connect to.)typehelpaA  `LOCAL_HOST:LOCAL_PORT` on which gcloud should bind and listen for connections
that should be tunneled.

`LOCAL_PORT` may be omitted, in which case it is treated as 0 and an arbitrary
unused local port is chosen. The colon also may be omitted in that case.

If `LOCAL_PORT` is 0, an arbitrary unused local port is chosen.--local-host-portc                 >    [         R                  R                  U SS9$ )NT)ipv6_enabled)r   HostPortParse)args    r$   <lambda>%StartIapTunnel.Args.<locals>.<lambda>p   s    --33Cd3Kr&   zlocalhost:0)r/   defaultr0   --listen-on-stdin
store_trueTzWhether to get/put local data on stdin/stdout instead of listening on a socket.  It is an error to specify --local-host-port with this, because that flag has no meaning with this.)actionhiddenr0   z%--iap-tunnel-disable-connection-checkFz/Disables the immediate check of the connection.)r9   r<   r0   )
r   AddProxyServerHelperArgsr   INSTANCE_ARGAddArgumentadd_argumentr   
BoundedIntAddHostBasedTunnelArgssupport_security_gateway)clsparserlocal_host_port_help_texts      r$   ArgsStartIapTunnel.Args]   s    ''/	""6*
##uEG  I
!C K&	  ( 	   />	  @ %%f.J.JKr&   c                    UR                   (       a-  UR                  S5      (       a  [        R                  " SS5      eU R	                  U5      nU R                  X5      nU R                  5          UR                  5         g ! [        R                   aI  nU R                  UR                  5      (       a"  UR                  (       d  U R                  U5        UeS nAff = f)Nlocal_host_portr:   r1   )listen_on_stdinIsSpecifiedcalliope_exceptionsConflictingArgumentsException_GetTargetArgs_CreateIapTunnelHelper_CheckNumpyInstalledRunr   ConnectionCreationError%_ShouldFetchInstanceAfterConnectErrorr   r   _FetchInstance)selfargstargetiap_tunnel_helperes        r$   rS   StartIapTunnel.Run   s     0 01B C C==
24 4   &F33DA
77 

4
4TYY
?
?kk 	D!gs   2B C ACC c                 ,    U R                   =(       a    U$ )N)"fetch_instance_after_connect_error)rW   r   s     r$   rU   4StartIapTunnel._ShouldFetchInstanceAfterConnectError   s    22;t;r&   c           
      F   U R                   (       ag  UR                  (       aV  [        R                  " XR                  UR
                  UR                  UR                  UR                  UR                  S9nOUR                  (       aV  [        R                  " XR                  UR
                  UR                  UR                  UR                  UR                  S9nOJ[        R                  " XR                  UR                  UR                  UR                  UR                  S9nUR                  (       a  [        R                  " U5      nU$ U R!                  U5      u  pVSn[#        US5      (       a  UR$                  (       + n[        R&                  " XVXs5      nU$ )N)r   r   r   r   r   use_dest_group)r   r   r   r   r   )r   r   r   r   T#iap_tunnel_disable_connection_check)rD   r   r   SecurityGatewayTunnelHelperr   r   r   r   r   IAPWebsocketTunnelHelperr   r   r   r   rL   IapTunnelStdinHelper_GetLocalHostPorthasattrrb   IapTunnelProxyServerHelper)rW   rX   rY   tunnelerrZ   
local_host
local_portcheck_connections           r$   rQ   %StartIapTunnel._CreateIapTunnelHelper   sF   $$)>)>77
v}}!22{{V=N=NPh 
44
..{{{{&&(h 44
{{??$${{h $99(C   $55d;j	<	=	=#GGG$??
"2> r&   c                 D   UR                  S5      (       a  UR                  S5      (       at  [        [        R                  R                  R
                  R                  5       UR                  UR                  UR                  UR                  UR                  S S S S S9
$ U R                  (       a  UR                  (       at  [        [        R                  R                  R
                  R                  5       UR                  UR                  UR                  UR                  S UR                  S S S S9
$ U R                  UR                   5      (       a`  [        [        R                  R                  R
                  R                  5       UR                   UR                  SUR                  S S S S S S9
$ U R#                  U5      u  p#[        UR
                  UR                   UR$                  [&        R(                  " U5      R$                  UR                  S S S S S S9
$ )Nr   r   )
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   nic0r   )rM   _CreateTargetArgsr   VALUEScorer   	GetOrFailr   r   instance_namer+   r   rD   r   ra   rU   r   rV   namer   GetInternalInterface)rW   rX   instance_refinstance_objs       r$   rP   StartIapTunnel._GetTargetArgs   s   	""t'7'7'A'A##((00::<,,!!!!__
! 
! $$)>)>##((00::<!!!!00((
 
 11$))<<##((00::<yy%%!!
! 
! "&!4!4T!:L$$""00>CC
 
r&   c           	         [         R                  " U R                  5       5      nUR                  n[        R
                  " 5       n[        R                  R                  UR                  /[        R                  R                  UR                  UR                  [        R                  " U5      S9S   nXTR!                  X55      4$ )N)scope_listerr   )r   ComputeApiHolderReleaseTrackclientr   BaseSSHCLIHelperr   SSH_INSTANCE_RESOLVERResolveResourcesrt   r   	ScopeEnumZONEr   	resourcesGetInstanceZoneScopeListerGetInstance)rW   rX   holderr~   
ssh_helperrw   s         r$   rV   StartIapTunnel._FetchInstance   s    **4+<+<+>?F]]F++-J..??					55f= @ ?
 @ABL //EEEr&   c                 8   UR                   R                  =(       d    SnUR                   R                  (       a  [        UR                   R                  5      OSn[        R
                  " US9nU(       d"  [        R                  R                  SU-  5        X$4$ )N	localhostr   )port_argzPicking local unused port [%d].)	rK   r   r   intr   DetermineLocalPortr   statusPrint)rW   rX   local_host_argr   rk   s        r$   rf    StartIapTunnel._GetLocalHostPort  su    ))..=+N*.*>*>*C*CD  %%& ..AJ	jj8:EF%%r&   c                 b     SS K ng ! [         a    [        R                  " [        5         g f = f)Nr   )numpyImportErrorr   warning_NUMPY_HELP_TEXT)rW   r   s     r$   rR   #StartIapTunnel._CheckNumpyInstalled  s&    
$ $	kk"#$s    $..r"   N)__name__
__module____qualname____firstlineno____doc__r^   rD   classmethodrH   rS   rU   rQ   rP   rV   rf   rR   __static_attributes__r"   r&   r$   r(   r(   U   sN     -'+$"(L (LT*<!F5nF&$r&   r(   c                       \ rS rSrSrSrSrg)StartIapTunnelBetai  +Starts an IAP TCP forwarding tunnel (Beta).Tr"   N)r   r   r   r   r   r^   r   r"   r&   r$   r   r     s     4'+$r&   r   c                       \ rS rSrSrSrSrg)StartIapTunnelAlphai$  r   Tr"   N)r   r   r   r   r   rD   r   r"   r&   r$   r   r   $  s     4!r&   r   )&r   
__future__r   r   r   collectionsgooglecloudsdk.api_lib.computer   r   googlecloudsdk.callioper   r	   r
   rN   "googlecloudsdk.command_lib.computer   r   r   ,googlecloudsdk.command_lib.compute.instancesr   googlecloudsdk.corer   r   
namedtuplerp   r   r%   UniverseCompatibleReleaseTracksr}   GACommandr(   BETAr   ALPHAr   r#   r"   r&   r$   <module>r      sI   C &  '  7 ? / ( E 9 4 8 > # ***= ;  
 #L D%%(()B$T\\ B$ * B$J D%%**+, , , , D%%++,", " - "
 %2O  !#0?   , r&   