
    ,                         S r SSKrSSK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SKJr  SSKJr  SS	KJr  SSKrS
rSrSrSrS/rSrS r " S S\R4                  5      rg)z5Troubleshoot Network and firewall for ssh connection.    N)resolver)
enable_api)apis)ssh_troubleshooter)	ssh_utils)log)
console_io   networkmanagementv1z networkmanagement.googleapis.comz'networkmanagement.connectivitytests.geta  To view complete details of this test, see https://console.cloud.google.com/net-intelligence/connectivity/tests/details/{0}?project={1}

Help for connectivity tests:
https://cloud.google.com/network-intelligence-center/docs/connectivity-tests/concepts/overview
c                      [        [        5       V s/ s H8  n [        R                  " [        R
                  [        R                  -   5      PM:     nn SR                  U5      $ s  sn f )N )range_NUM_RANDOM_CHARACTERSrandomchoicestringascii_lowercasedigitsjoin)_random_characterss     @lib/googlecloudsdk/command_lib/compute/network_troubleshooter.py_GetRandomSuffixr   4   sY     +,,! mmF**V]]:;,   
"	##	s   ?A$c                   ^    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rS rS rS rSrg)NetworkTroubleshooter<   zHCheck network and firewall setting by running network connectivity test.Nc                     Xl         X l        X0l        X@l        [        R
                  " [        [        5      U l        [        R                  " [        [        5      U l
        SU l        S[        5       -   U l        g )NFzssh-troubleshoot-)projectzoneinstanceiap_tunnel_argsr   GetClientInstance"_API_NETWORKMANAGEMENT_CLIENT_NAME_API_CLIENT_VERSION_V1	nm_clientGetMessagesModule
nm_messageskip_troubleshootr   test_id)selfr   r    r!   r"   s        r   __init__NetworkTroubleshooter.__init__?   sc    LIM*++*,BDN ,,*,BDO #D&)9);;DL    c                    [         R                  R                  S5        SnSnSn [        R                  " UUSUS9nU(       + U l        U R
                  (       a  g [        R                  " U R                  R                  [        5        U R                  5       nU(       aE  [         R                  R                  SR                  SR                  U5      5      5        SU l        g g ! [        R                   a
    SU l         Nf = f)	Nz'---- Checking network connectivity ----zLThe Network Management API is needed to check the VM's network connectivity.zVIf not already enabled, is it OK to enable it and check the VM's network connectivity?zfTest skipped.
To manually test network connectivity, try reaching another device on the same network.
T)messageprompt_stringcancel_on_nocancel_stringzMissing the IAM permissions {0} necessary to perform the network connectivity test. To manually test network connectivity, try reaching another device on the same network.
 )r   statusPrintr	   PromptContinuer)   OperationCancelledErrorr   EnableServiceIfDisabledr   nameNETWORK_API"_CheckNetworkManagementPermissionsformatr   )r+   msgpromptcancelprompt_continuemissing_permissionss         r   check_prerequisite(NetworkTroubleshooter.check_prerequisiteM   s    JJ>?	 
	! 
	( 
	$"11	o $32d  &&t||'8'8+FAAC	jj;;A6hh*+<  $d  -- $#d$s   #C( (DDc                     g N r+   s    r   cleanup_resources'NetworkTroubleshooter.cleanup_resourcesz   s    
r.   c                 :   U R                   (       a  g SU l        SU l        U R                  (       GaX  [        R
                  " U R                  SS9U l        U R                  (       a  SU R                  ;  ay  SU l        [        R                  R                  SR                  U R                  5      5        [        R                  R                  SR                  U R                  5      5        GOU R                  (       a  SU R                  ;   aw  SU l        [        R                  R                  S	R                  U R                  5      5        [        R                  R                  S
R                  U R                  5      5        GOU R                  5       U l        U R                  (       a  SU R                  ;  a  [        R                  " U R                  5      U l        [        R                  R                  SR                  U R                  5      5        [        R                  R                  SR                  U R                  5      5        OU R                  (       a  SU R                  ;   a  [        R                  " U R                  5      U l        [        R                  R                  SR                  U R                  5      5        [        R                  R                  SR                  U R                  5      5        U R                  (       a  U R                  (       d'  [        R                  R                  S5        SU l         g U R                  5       nU R                  U5      (       d.  [         R"                  " S5        U R                  U5      (       d  M.  U R%                  5       nU R'                  U5        [        R                  R                  [(        R                  U R*                  U R,                  R.                  5      5        g )Nr   T)no_raise:z35.235.240.123zWSource IP address: {0} (a random IP address from the IAP IPv4 range - 35.235.240.0/20)
zLDestination IP address: {0} (primary internal IPv4 address of the instance)
z2600:2d00:1:7:1:2:3::zZSource IP address: {0} (a random IP address from the IAP IPv6 range - 2600:2d00:1:7::/64)
zLDestination IP address: {0} (primary internal IPv6 address of the instance)
z<Source IP address: {0} (your current external IPv4 address)
zLDestination IP address: {0} (primary external IPv4 address of the instance)
z<Source IP address: {0} (your current external IPv6 address)
zLDestination IP address: {0} (primary external IPv6 address of the instance)
zRCould not resolve source or destination IP address, can't run a Connectivity Test
   )r)   src_ip_addressdst_ip_addressr"   r   GetInternalIPAddressr!   r   r5   r6   r=   _GetSourceIPAddressGetPrimaryExternalIPv4AddressGetPrimaryExternalIPv6Address_RunConnectivityTest_IsConnectivityTestFinishtimesleep_GetConnectivityTestResult#_PrintConciseConnectivityTestResultCONNECTIVITY_TEST_MESSAGEr*   r   r:   )r+   operation_nametest_results      r   troubleshoot"NetworkTroubleshooter.troubleshoot}   s   DD%::
--$d 
		D,?,?!?.

**0&1D1D*E	
 	

!6$"5"56	
 3$*=*=#=5

--3VD4G4G-H	
 	

!6$"5"56	
 !446d			D,?,?!?'EEMM
 	

KVD''(	
 	

!6$"5"56	
 3$*=*=#='EEMM
 	

KVD''(	
 	

!6$"5"56	

 d&9&9	jj!  $d ..0N,,^<<
jjm ,,^<< 113K,,[9JJ!((t||7H7HI r.   c                    U R                  5       nU R                  R                  SR                  U R                  R
                  S9U R                  US9nU R                  R                  R                  U5      R
                  $ )Nz&projects/{project_id}/locations/global
project_id)parenttestIdconnectivityTest)
_CreateConnectivityTestr(   FNetworkmanagementProjectsLocationsGlobalConnectivityTestsCreateRequestr=   r   r:   r*   r&   +projects_locations_global_connectivityTestsCreate)r+   connectivity_testconnectivity_test_create_reqs      r   rU   *NetworkTroubleshooter._RunConnectivityTest   s    446#'??#y#y7>>||(( ? 
 ||* $z $  >>EELL$
dr.   c                     SR                  U R                  R                  U R                  S9nU R                  R                  US9nU R                  R                  R                  U5      $ )NzBprojects/{project_id}/locations/global/connectivityTests/{test_id})rb   r*   r:   )	r=   r   r:   r*   r(   CNetworkmanagementProjectsLocationsGlobalConnectivityTestsGetRequestr&   rh   Get)r+   r:   connectivity_test_get_reqs      r   rY   0NetworkTroubleshooter._GetConnectivityTestResult   sj    	Fdll&7&7FN 	 !% s s !t ! >>EEII! r.   c                     U R                   R                  US9nU R                  R                  R	                  U5      R
                  $ )Nrn   )r(   <NetworkmanagementProjectsLocationsGlobalOperationsGetRequestr&   $projects_locations_global_operationsrp   done)r+   r:   operation_get_reqs      r   rV   /NetworkTroubleshooter._IsConnectivityTestFinish   sG    dd e  >>>>BB
dr.   c                    U R                   R                  SR                  U R                  R                  U R
                  S9SU R                   R                  U R                  U R                   R                  R                  R                  S9U R                   R                  SSR                  U R                  R                  U R                  U R                  R                  S9U R                  S9S	S
S9$ )Nz;projects/{name}/locations/global/connectivityTests/{testId})r:   rd   zHThis connectivity test is created by 'gcloud compute ssh --troubleshoot')	ipAddressnetworkType   z4projects/{project}/zones/{zone}/instances/{instance})r   r    r!   )portr!   rz   TCPT)r:   descriptionsourcedestinationprotocol	roundTrip)r(   ConnectivityTestr=   r   r:   r*   EndpointrO   NetworkTypeValueValuesEnumINTERNETr    r!   rP   rH   s    r   rf   -NetworkTroubleshooter._CreateConnectivityTest   s    ??++IV**4<<V@2 ''))00KKTT ( 
 OO,,FMM LL--!]]// N  )) - 

 3 ,  r.   c                 H   SR                  U R                  R                  S9nU R                  R	                  [
        S9nU R                  R                  XS9nU R                  R                  R                  U5      n[        [
        5      [        UR                  5      -
  $ )Nz:projects/{project_id}/locations/global/connectivityTests/*ra   )permissions)resourcetestIamPermissionsRequest)r=   r   r:   r(   TestIamPermissionsRequestnetworkmanagement_permissionsRNetworkmanagementProjectsLocationsGlobalConnectivityTestsTestIamPermissionsRequestr&   rh   TestIamPermissionssetr   )r+   resource_urltest_permission_reqnm_testiampermission_reqresponses        r   r<   8NetworkTroubleshooter._CheckNetworkManagementPermissions  s    DKK||(( 	L 	
 
 //CC1 D   $   B   B  B   ~~II\\ H ,-H4H4H0IIIr.   c                     [         R                  " 5       n[        R                  " S5      /Ul        UR                  SSS9 H(  n[        R                  " U5      R                  S5      s  $    g)zbGet current external IP from Google DNS server.

Returns:
  str, an address represented by string
zns1.google.comzo-o.myaddr.l.google.comTXT)qnamerdtype"r   )	r   Resolversocketgethostbynamenameserversquerysix	text_typestrip)r+   rerdatas      r   rR   )NetworkTroubleshooter._GetSourceIPAddress%  s^     
			B**+;<=BN 9%H]]5!'',, Ir.   c                 2   UR                   nU(       a8  [        R                  R                  SR	                  UR
                  5      5        UR                  nU(       a9  [        R                  R                  SR	                  UR
                  5      5        gg)zPrint concise network connectivity test result from response.

Args:
  response: A response from projects_locations_global_connectivityTests Get

Returns:
z+Connectivity Test forward path result: {0}
z*Connectivity Test return path result: {0}
N)reachabilityDetailsr   r5   r6   r=   resultreturnReachabilityDetails)r+   r   detailsreturn_detailss       r   rZ   9NetworkTroubleshooter._PrintConciseConnectivityTestResult2  st     **G	jj
8
?
?
O 77N	jj
7
>
>## r.   )
rP   r"   r!   r&   r(   r   r)   rO   r*   r    rF   )__name__
__module____qualname____firstlineno____doc__r,   rC   rI   r^   rU   rY   rV   rf   r<   rR   rZ   __static_attributes__rG   r.   r   r   r   <   s@    P<+ZTl
:J$r.   r   )r   r   r   r   rW   dnsr   googlecloudsdk.api_lib.servicesr   googlecloudsdk.api_lib.utilr   "googlecloudsdk.command_lib.computer   r   googlecloudsdk.corer   googlecloudsdk.core.consoler	   r   r   r$   r%   r;   r   r[   r   SshTroubleshooterr   rG   r.   r   <module>r      sy    <      6 , A 8 # 2 
 %8 " 0 .! 
g $I.@@ Ir.   