
    h                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZ dd
lmZ ddlZd Zd Zd Zd Z	 	 ddZy)z=A module for changing Cloud SDK proxy settings interactively.    )absolute_import)division)unicode_literals)
http_proxy)log)
properties)
console_io)encoding)http_proxy_typesNc                  
   	 t               \  } }t        | |       t        j                  j                  j                  j                         ry|rZg d}t        j                  |d      }|dk(  r
t               S |dk(  r*t                t        j
                  j                  d	       yyt        j                  d
      r
t               S y# t        j                  $ r$ t        j
                  j                  d       d}Y w xY w)zDisplays proxy information and helps user set up gcloud proxy properties.

  Returns:
    Whether properties were successfully changed.
  zCloud SDK network proxy settings appear to be invalid. Proxy type, address, and port must be specified. Run [gcloud info] for more details.
TF)z)Change Cloud SDK network proxy propertiesz!Clear all gcloud proxy propertiesExitzWhat would you like to do?messager      z$Cloud SDK proxy properties cleared.
z;Do you have a network proxy you would like to set in gcloud)prompt_string)EffectiveProxyInfo_DisplayGcloudProxyInfor   InvalidValueErrorr   statusPrintVALUEScoredisable_promptsGetBoolr	   PromptChoice_ProxySetupWalkthroughSetGcloudProxyPropertiesPromptContinue)
proxy_infois_existing_proxyoptionsexisting_proxy_idxs       7lib/googlecloudsdk/core/diagnostics/http_proxy_setup.pyChangeGcloudProxySettingsr$       s    	;$6$8!J! J(9:++335G $0057 Q#%%Q 	jj>?   0C D#%%A 
	%	% JJ	 s   C 4DDc                     t        d t        j                  D              } t        j                  | d      }|y| |   j                         }t        j                  d      }t        j                  j                          |syt        j                  d      }t        j                  j                          |sy	 dt        |      cxk  rdk  s"n t        j                  j                  d	       y	 d\  }}t        j                  dd      }|rlt        j                  d      }t        j                  j                          |syt        j                  d      }t        j                  j                          |syt        |||||       t        j                  j                  d       y# t        $ r" t        j                  j                  d
       Y yw xY w)z6Walks user through setting up gcloud proxy properties.c              3   <   K   | ]  }|j                           y w)N)upper).0ts     r#   	<genexpr>z)_ProxySetupWalkthrough.<locals>.<genexpr>M   s      :8Aaggi8s   zSelect the proxy type:r   FzEnter the proxy host address: zEnter the proxy port: r   i  z(Port number must be between 0 and 65535.z,Please enter an integer for the port number.)NNzIs your proxy authenticated)r   defaultzEnter the proxy username: zEnter the proxy password: 
proxy_typeaddressportusernamepasswordz Cloud SDK proxy properties set.
T)sortedr   PROXY_TYPE_MAPr	   r   lowerPromptResponser   r   r   int
ValueErrorr   r   )proxy_type_optionsproxy_type_idxr-   r.   r/   r0   r1   authenticateds           r#   r   r   K   s    :)88: :**":<.!.1779*%%&FG'**			"	"#;	<$**	D	"U"	jjAB # "(H++15B-(()EFHJJ(()EFHJJj'$,xA**67	) 
 JJCDs   ;5F, ,(GGc                      t        j                         } | syd}t        | t        j                        s
d} | d      } | |fS )a  Returns ProxyInfo effective in gcloud and if it is from gloud properties.

  Returns:
    A tuple of two elements in which the first element is an httplib2.ProxyInfo
      object and the second is a bool that is True if the proxy info came from
      previously set Cloud SDK proxy properties.

  Raises:
    properties.InvalidValueError: If the properties did not include a valid set.
      "Valid" means all three of these attributes are present: proxy type, host,
      and port.
  )NFTFhttps)r   GetHttpProxyInfo
isinstancehttplib2	ProxyInfo)r   from_gcloud_propertiess     r#   r   r   z   sL     **,*	
  	J 2 2	3"G$J	+	++    c                    | st         j                  j                          yt         j                  j                  d       |st         j                  j                  d       t        j                  j                  | j                  d      }t         j                  j                  dj                  |             t         j                  j                  dj                  | j                               t         j                  j                  dj                  | j                               t         j                  j                  dj                  t        j                  | j                                     t         j                  j                  d	j                  t        j                  | j                                     t         j                  j                          y)
z%Displays Cloud SDK proxy information.Nz3Current effective Cloud SDK network proxy settings:zL(These settings are from your machine's environment, not gcloud properties.)zUNKNOWN PROXY TYPEz    type = {0}z    host = {0}z    port = {0}z    username = {0}z    password = {0})r   r   r   r   REVERSE_PROXY_TYPE_MAPgetr-   format
proxy_host
proxy_portr
   Decode
proxy_user
proxy_pass)r   from_gcloudproxy_type_names      r#   r   r      sC   	JJ
**HI	JJ / 0$;;??13/**#**?;<**#**:+@+@AB**#**:+@+@AB **'..ooj++,. /**'..ooj++,. /**rB   c                 4   t        j                  t         j                  j                  j                  |        t        j                  t         j                  j                  j
                  |       t        j                  t         j                  j                  j                  |       t        j                  t         j                  j                  j                  |       t        j                  t         j                  j                  j                  |       y)zDSets proxy group properties; clears any property not explicitly set.N)	r   PersistPropertyr   proxyr-   r.   r/   r0   r1   r,   s        r#   r   r      s     Z..44??LZ..44<<gFZ..44994@Z..44==xHZ..44==xHrB   )NNNNN)__doc__
__future__r   r   r   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler	   googlecloudsdk.core.utilr
   r   r?   r$   r   r   r   r    rB   r#   <module>rW      sO     D &  ' * # * 2 - 5 (V,^,:2 BF59IrB   