
    "                         S r SSKJr  SSKJr  SSKJ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rSrSrSrSrSrS/rSrS r " S S\R<                  5      rg)z5Troubleshoot Network and firewall for ssh connection.    )absolute_import)division)unicode_literalsN)resolver)
enable_api)apis)ssh_troubleshooter)log)
console_io   networkmanagementcompute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   6   sY     +,.,! mmF**V]]:;,  . 
"	##.s   ?A$c                   Z    \ rS rSrSrS 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.c                 z   Xl         X l        X0l        [        R                  " [
        [        5      U l        [        R                  " [
        [        5      U l	        [        R                  " [        [        5      U l        [        R                  " [        [        5      U l        SU l        S[        5       -   U l        g )NFzssh-troubleshoot-)projectzoneinstancer   GetClientInstance"_API_NETWORKMANAGEMENT_CLIENT_NAME_API_CLIENT_VERSION_V1	nm_clientGetMessagesModule
nm_message_API_COMPUTE_CLIENT_NAMEcompute_clientcompute_messageskip_troubleshootr   test_id)selfr"   r#   r$   s       r   __init__NetworkTroubleshooter.__init__@   s    LIM++,N,BDDN,,-O-CEDO001I1GID112J2HJD"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   )r0   msgpromptcancelprompt_continuemissing_permissionss         r   check_prerequisite(NetworkTroubleshooter.check_prerequisiteO   s    JJ>?C	! .F$"11	 o
 $32d  &&t||'8'8+FAAC	jj;;A6hh*+<-.
  $d  -- $#d$s   #C( (DDc                     g )N r0   s    r   cleanup_resources'NetworkTroubleshooter.cleanup_resourcesu   s    
r3   c                    U R                   (       a  g U R                  5       U l        [        R                  R                  SR                  U R                  5      5        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 )NzYour source IP address is {0}
zSCould not resolve source external IP address, can't run network connectivity test.
T   )r.   _GetSourceIPAddress
ip_addressr
   r:   r;   rB   _RunConnectivityTest_IsConnectivityTestFinishtimesleep_GetConnectivityTestResult#_PrintConciseConnectivityTestResultCONNECTIVITY_TEST_MESSAGEr/   r"   r?   )r0   operation_nametest_results      r   troubleshoot"NetworkTroubleshooter.troubleshootx   s    ..0DOJJ6==dooNO??	jj : ;#d ..0N,,^<<
jjm ,,^<< 113K,,[9JJ.55dll'') *
r3   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*   FNetworkmanagementProjectsLocationsGlobalConnectivityTestsCreateRequestrB   r"   r?   r/   r(   +projects_locations_global_connectivityTestsCreate)r0   connectivity_testconnectivity_test_create_reqs      r   rS   *NetworkTroubleshooter._RunConnectivityTest   s}    446#'??#y#y7>>||(( ? *||*	 $z $, 
 >>EELL$&&*d+r3   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})r`   r/   r?   )	rB   r"   r?   r/   r*   CNetworkmanagementProjectsLocationsGlobalConnectivityTestsGetRequestr(   rf   Get)r0   r?   connectivity_test_get_reqs      r   rW   0NetworkTroubleshooter._GetConnectivityTestResult   sn    $,,*;*;'+||  5 	 !% s s !t !>>EEII!# #r3   c                     U R                   R                  US9nU R                  R                  R	                  U5      R
                  $ )Nrl   )r*   <NetworkmanagementProjectsLocationsGlobalOperationsGetRequestr(   $projects_locations_global_operationsrn   done)r0   r?   operation_get_reqs      r   rT   /NetworkTroubleshooter._IsConnectivityTestFinish   sE    dd e >>>>BB4 r3   c                    U R                   R                  SR                  U R                  R                  U R
                  S9SU R                   R                  U R                  U R                  R                  S9U R                   R                  SSR                  U R                  R                  U R                  U R                  R                  S9S9S	S
9$ )Nz;projects/{name}/locations/global/connectivityTests/{testId})r?   rb   zHThis connectivity test is created by 'gcloud compute ssh --troubleshoot')	ipAddress	projectId   z4projects/{project}/zones/{zone}/instances/{instance})r"   r#   r$   )portr$   TCP)r?   descriptionsourcedestinationprotocol)
r*   ConnectivityTestrB   r"   r?   r/   EndpointrR   r#   r$   rL   s    r   rd   -NetworkTroubleshooter._CreateConnectivityTest   s    ??++J	T\\&&t||	<.''oo1B1B ( DOO,,KV))YY++  - - .  ,  r3   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/*r_   )permissions)resourcetestIamPermissionsRequest)rB   r"   r?   r*   TestIamPermissionsRequestnetworkmanagement_permissionsRNetworkmanagementProjectsLocationsGlobalConnectivityTestsTestIamPermissionsRequestr(   rf   TestIamPermissionssetr   )r0   resource_urltest_permission_reqnm_testiampermission_reqresponses        r   rA   8NetworkTroubleshooter._CheckNetworkManagementPermissions   s    **0&DLL<M<M&*N //CC1 D 3  $   B   B  B  N~~II\\]uvH,-H4H4H0IIIr3   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)zgGet current external IP from Google DNS server.

Returns:
  str, an ipv4 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)r0   rerdatas      r   rQ   )NetworkTroubleshooter._GetSourceIPAddress   s^     
			B**+;<=BN 9%H]]5!'',, Ir3   c                     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&Network Connectivity Test Result: {0}
N)reachabilityDetailsr
   r:   r;   rB   result)r0   r   detailss      r   rX   9NetworkTroubleshooter._PrintConciseConnectivityTestResult   s>     **G	jj@GG
..  r3   )
r,   r-   r$   rR   r(   r*   r"   r.   r/   r#   N)__name__
__module____qualname____firstlineno____doc__r1   rH   rM   r\   rS   rW   rT   rd   rA   rQ   rX   __static_attributes__rK   r3   r   r   r   =   s>    P<$L.	+# "	Jr3   r   ) r   
__future__r   r   r   r   r   r   rU   dnsr   googlecloudsdk.api_lib.servicesr   googlecloudsdk.api_lib.utilr   "googlecloudsdk.command_lib.computer	   googlecloudsdk.corer
   googlecloudsdk.core.consoler   r   r   r&   r+   r'   r@   r   rY   r   SshTroubleshooterr   rK   r3   r   <module>r      s    < &  '      6 , A # 2 
 %8 "$  0 .! 
g $b.@@ br3   