Add HTTP proxy support to ceilometer client

There is a need for HTTP Proxy support in Ceilometer client
It's used in the case when Ceilometer API is not directly available
Proxy url is taken from environment variables http_proxy or https_proxy

Implements: blueprint proxy-support

Change-Id: I839d704a7fa16521292205166d7cbea56497842b
This commit is contained in:
Ilya Tyaptin
2013-11-22 13:53:16 +04:00
parent 936faeb332
commit a587581e02

View File

@@ -48,6 +48,7 @@ class HTTPClient(object):
self.endpoint = endpoint
self.auth_token = kwargs.get('token')
self.connection_params = self.get_connection_params(endpoint, **kwargs)
self.proxy_url = self.get_proxy_url()
@staticmethod
def get_connection_params(endpoint, **kwargs):
@@ -74,8 +75,13 @@ class HTTPClient(object):
def get_connection(self):
_class = self.connection_params[0]
try:
return _class(*self.connection_params[1][0:2],
**self.connection_params[2])
if self.proxy_url:
proxy_parts = urlutils.urlparse(self.proxy_url)
return _class(proxy_parts.hostname, proxy_parts.port,
**self.connection_params[2])
else:
return _class(*self.connection_params[1][0:2],
**self.connection_params[2])
except httplib.InvalidURL:
raise exc.InvalidEndpoint()
@@ -137,7 +143,10 @@ class HTTPClient(object):
conn = self.get_connection()
try:
conn_url = self._make_connection_url(url)
if self.proxy_url:
conn_url = self.endpoint + self._make_connection_url(url)
else:
conn_url = self._make_connection_url(url)
conn.request(method, conn_url, **kwargs)
resp = conn.getresponse()
except socket.gaierror as e:
@@ -202,6 +211,15 @@ class HTTPClient(object):
'application/octet-stream')
return self._http_request(url, method, **kwargs)
def get_proxy_url(self):
scheme = urlutils.urlparse(self.endpoint).scheme
if scheme == 'https':
return os.environ.get('https_proxy')
elif scheme == 'http':
return os.environ.get('http_proxy')
msg = 'Unsupported scheme: %s' % scheme
raise exc.InvalidEndpoint(msg)
class VerifiedHTTPSConnection(httplib.HTTPSConnection):
"""httplib-compatibile connection using client-side SSL authentication