From a587581e02d6f0c77cf1f38d1948d7fdb1424405 Mon Sep 17 00:00:00 2001 From: Ilya Tyaptin Date: Fri, 22 Nov 2013 13:53:16 +0400 Subject: 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 --- ceilometerclient/common/http.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/ceilometerclient/common/http.py b/ceilometerclient/common/http.py index 1a4b58c..cfd95cf 100644 --- a/ceilometerclient/common/http.py +++ b/ceilometerclient/common/http.py @@ -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 -- cgit v1.2.1