def GetProxyProperties(): """Get proxy information from cloud sdk properties in dictionary form.""" proxy_type_map = http_proxy_types.PROXY_TYPE_MAP proxy_type = properties.VALUES.proxy.proxy_type.Get() proxy_address = properties.VALUES.proxy.address.Get() proxy_port = properties.VALUES.proxy.port.GetInt() proxy_prop_set = len( [f for f in (proxy_type, proxy_address, proxy_port) if f]) if proxy_prop_set > 0 and proxy_prop_set != 3: raise properties.InvalidValueError( 'Please set all or none of the following properties: ' 'proxy/type, proxy/address and proxy/port') if not proxy_prop_set: return {} proxy_rdns = properties.VALUES.proxy.rdns.GetBool() proxy_user = properties.VALUES.proxy.username.Get() proxy_pass = properties.VALUES.proxy.password.Get() return { 'proxy_type': proxy_type_map[proxy_type], 'proxy_address': proxy_address, 'proxy_port': proxy_port, 'proxy_rdns': proxy_rdns, 'proxy_user': proxy_user, 'proxy_pass': proxy_pass, }
def MakeProxyFromProperties(): """Returns the proxy string for use by grpc from gcloud properties.""" proxy_type = properties.VALUES.proxy.proxy_type.Get() proxy_address = properties.VALUES.proxy.address.Get() proxy_port = properties.VALUES.proxy.port.GetInt() proxy_prop_set = len( [f for f in (proxy_type, proxy_address, proxy_port) if f]) if proxy_prop_set > 0 and proxy_prop_set != 3: raise properties.InvalidValueError( 'Please set all or none of the following properties: ' 'proxy/type, proxy/address and proxy/port') if not proxy_prop_set: return proxy_user = properties.VALUES.proxy.username.Get() proxy_pass = properties.VALUES.proxy.password.Get() http_proxy_type = http_proxy_types.PROXY_TYPE_MAP[proxy_type] if http_proxy_type != socks.PROXY_TYPE_HTTP: raise ValueError( 'Unsupported proxy type for gRPC: {}'.format(proxy_type)) if proxy_user or proxy_pass: proxy_auth = ':'.join( urllib.parse.quote(x) or '' for x in (proxy_user, proxy_pass)) proxy_auth += '@' else: proxy_auth = '' return 'http://{}{}:{}'.format(proxy_auth, proxy_address, proxy_port)
def RequireProjectID(args): """Prohibit specifying project as a project number. Most APIs accept both project number and project id, some of them accept only project ids. Args: args: argparse.namespace, the parsed arguments from the command line """ if args.project: if args.project.isdigit(): raise properties.InvalidValueError( "The value of ``--project'' flag was set to Project number." "To use this command, set it to PROJECT ID instead.") else: return else: proj = properties.VALUES.core.project.Get() if proj and proj.isdigit(): raise properties.InvalidValueError( "The value of ``core/project'' property is set to project number." "To use this command, set ``--project'' flag to PROJECT ID or " "set ``core/project'' property to PROJECT ID.")
def GetProxyInfo(): """Returns the proxy string for use by requests from gcloud properties. See https://requests.readthedocs.io/en/master/user/advanced/#proxies. """ proxy_type = properties.VALUES.proxy.proxy_type.Get() proxy_address = properties.VALUES.proxy.address.Get() proxy_port = properties.VALUES.proxy.port.GetInt() proxy_prop_set = len( [f for f in (proxy_type, proxy_address, proxy_port) if f]) if proxy_prop_set > 0 and proxy_prop_set != 3: raise properties.InvalidValueError( 'Please set all or none of the following properties: ' 'proxy/type, proxy/address and proxy/port') if not proxy_prop_set: return proxy_rdns = properties.VALUES.proxy.rdns.GetBool() proxy_user = properties.VALUES.proxy.username.Get() proxy_pass = properties.VALUES.proxy.password.Get() proxy_type = http_proxy_types.PROXY_TYPE_MAP[proxy_type] if proxy_type == socks.PROXY_TYPE_SOCKS4: proxy_scheme = 'socks4a' if proxy_rdns else 'socks4' elif proxy_type == socks.PROXY_TYPE_SOCKS5: proxy_scheme = 'socks5h' if proxy_rdns else 'socks5' elif proxy_type == socks.PROXY_TYPE_HTTP: proxy_scheme = 'https' elif proxy_type == socks.PROXY_TYPE_HTTP_NO_TUNNEL: proxy_scheme = 'http' else: raise ValueError('Unsupported proxy type: {}'.format(proxy_type)) if proxy_user or proxy_pass: proxy_auth = ':'.join(x or '' for x in (proxy_user, proxy_pass)) proxy_auth += '@' else: proxy_auth = '' return '{}://{}{}:{}'.format(proxy_scheme, proxy_auth, proxy_address, proxy_port)
def _GetHttpProxyInfo(): """Get ProxyInfo object to be passed to HTTP functions.""" proxy_type_map = config.GetProxyTypeMap() proxy_type = properties.VALUES.proxy.proxy_type.Get() proxy_address = properties.VALUES.proxy.address.Get() proxy_port = properties.VALUES.proxy.port.GetInt() proxy_prop_set = len(filter(None, (proxy_type, proxy_address, proxy_port))) if proxy_prop_set > 0 and proxy_prop_set < 3: raise properties.InvalidValueError( 'Please set all or none of the following properties: ' 'proxy/type, proxy/address and proxy/port') proxy_info = httplib2.proxy_info_from_environment if proxy_prop_set > 0: proxy_info = httplib2.ProxyInfo( proxy_type_map[proxy_type], proxy_address, proxy_port, proxy_user=properties.VALUES.proxy.username.Get(), proxy_pass=properties.VALUES.proxy.password.Get()) return proxy_info
def get_effect(*unused_args, **kwargs): if kwargs['validate'] is False: return 'foo' else: raise properties.InvalidValueError( 'Invalid value error message')