diff --git a/libcloud/common/base.py b/libcloud/common/base.py index 0ea9b3e..0d948c8 100644 --- a/libcloud/common/base.py +++ b/libcloud/common/base.py @@ -27,7 +27,7 @@ except: import libcloud -from libcloud.utils.py3 import PY3 +from libcloud.utils.py3 import PY3, PY25 from libcloud.utils.py3 import httplib from libcloud.utils.py3 import urlparse from libcloud.utils.py3 import urlencode @@ -249,6 +249,7 @@ class LoggingConnection(): return cls(b(self.s)) rr = r + original_data = body headers = lowercase_keys(dict(r.getheaders())) encoding = headers.get('content-encoding', None) @@ -357,11 +358,12 @@ class Connection(object): connection = None host = '127.0.0.1' port = 443 + timeout = None secure = 1 driver = None action = None - def __init__(self, secure=True, host=None, port=None, url=None): + def __init__(self, secure=True, host=None, port=None, url=None, timeout=None): self.secure = secure and 1 or 0 self.ua = [] self.context = {} @@ -383,6 +385,9 @@ class Connection(object): (self.host, self.port, self.secure, self.request_path) = self._tuple_from_url(url) + if timeout: + self.timeout = timeout + def set_context(self, context): self.context = context @@ -440,6 +445,10 @@ class Connection(object): kwargs = {'host': host, 'port': int(port)} + # Timeout is only supported in Python 2.6 and later + if self.timeout and not PY25: + kwargs.update({'timeout': self.timeout}) + connection = self.conn_classes[secure](**kwargs) # You can uncoment this line, if you setup a reverse proxy server # which proxies to your endpoint, and lets you easily capture @@ -735,13 +744,13 @@ class ConnectionKey(Connection): """ A Base Connection class to derive from, which includes a """ - def __init__(self, key, secure=True, host=None, port=None, url=None): + def __init__(self, key, secure=True, host=None, port=None, url=None, timeout=None): """ Initialize `user_id` and `key`; set `secure` to an C{int} based on passed value. """ super(ConnectionKey, self).__init__(secure=secure, host=host, - port=port, url=url) + port=port, url=url, timeout=timeout) self.key = key @@ -753,10 +762,10 @@ class ConnectionUserAndKey(ConnectionKey): user_id = None def __init__(self, user_id, key, secure=True, - host=None, port=None, url=None): + host=None, port=None, url=None, timeout=None): super(ConnectionUserAndKey, self).__init__(key, secure=secure, host=host, port=port, - url=url) + url=url, timeout=timeout) self.user_id = user_id diff --git a/libcloud/common/openstack.py b/libcloud/common/openstack.py index a93b1e5..744832b 100644 --- a/libcloud/common/openstack.py +++ b/libcloud/common/openstack.py @@ -21,6 +21,7 @@ import binascii import os from libcloud.utils.py3 import httplib +from libcloud.utils.py3 import urlparse from libcloud.common.base import ConnectionUserAndKey, Response from libcloud.compute.types import (LibcloudError, InvalidCredsError, @@ -79,14 +80,18 @@ class OpenStackAuthConnection(ConnectionUserAndKey): responseCls = OpenStackAuthResponse name = 'OpenStack Auth' + timeout = None - def __init__(self, parent_conn, auth_url, auth_version, user_id, key, tenant_name=None): + def __init__(self, parent_conn, auth_url, auth_version, user_id, key, tenant_name=None, timeout=None): self.parent_conn = parent_conn # enable tests to use the same mock connection classes. self.conn_classes = parent_conn.conn_classes + if timeout: + self.timeout = timeout + super(OpenStackAuthConnection, self).__init__( - user_id, key, url=auth_url) + user_id, key, url=auth_url, timeout=self.timeout) self.auth_version = auth_version self.auth_url = auth_url @@ -363,7 +368,7 @@ class OpenStackBaseConnection(ConnectionUserAndKey): service_region = None def __init__(self, user_id, key, secure=True, - host=None, port=None, + host=None, port=None, timeout=None, ex_force_base_url=None, ex_force_auth_url=None, ex_force_auth_version=None, @@ -392,7 +397,7 @@ class OpenStackBaseConnection(ConnectionUserAndKey): self._auth_version = AUTH_API_VERSION super(OpenStackBaseConnection, self).__init__( - user_id, key, secure=secure) + user_id, key, secure=secure, timeout=timeout) def get_endpoint(self): """ @@ -449,7 +454,9 @@ class OpenStackBaseConnection(ConnectionUserAndKey): 'have auth_url set') osa = OpenStackAuthConnection(self, aurl, self._auth_version, - self.user_id, self.key, self._ex_tenant_name) + self.user_id, self.key, + tenant_name=self._ex_tenant_name, + timeout=self.timeout) # may throw InvalidCreds, etc osa.authenticate()