Repository: libcloud Updated Branches: refs/heads/trunk 7132cb223 -> d7917ef5a
Moved shared OpenStackException and OpenStackResponse class from libcloud.compute.drivers.openstack to libcloud.common.openstack module. Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/d7917ef5 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/d7917ef5 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/d7917ef5 Branch: refs/heads/trunk Commit: d7917ef5a2d27ce459cd797c37505eae0b387b4c Parents: 7132cb2 Author: Tomaz Muraus <[email protected]> Authored: Tue Aug 5 12:20:23 2014 +0200 Committer: Tomaz Muraus <[email protected]> Committed: Tue Aug 5 12:20:23 2014 +0200 ---------------------------------------------------------------------- libcloud/common/openstack.py | 82 ++++++++++++++++++++++++++++ libcloud/compute/drivers/openstack.py | 86 +++--------------------------- 2 files changed, 89 insertions(+), 79 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/d7917ef5/libcloud/common/openstack.py ---------------------------------------------------------------------- diff --git a/libcloud/common/openstack.py b/libcloud/common/openstack.py index 73bd27e..768f88b 100644 --- a/libcloud/common/openstack.py +++ b/libcloud/common/openstack.py @@ -16,15 +16,23 @@ """ Common utilities for OpenStack """ + import sys import datetime +try: + from lxml import etree as ET +except ImportError: + from xml.etree import ElementTree as ET + from libcloud.utils.py3 import httplib from libcloud.utils.iso8601 import parse_date from libcloud.common.base import ConnectionUserAndKey, Response +from libcloud.common.types import ProviderError from libcloud.compute.types import (LibcloudError, InvalidCredsError, MalformedResponseError) +from libcloud.compute.types import KeyPairDoesNotExistError try: import simplejson as json @@ -51,6 +59,8 @@ __all__ = [ 'OpenStackBaseConnection', 'OpenStackAuthConnection', 'OpenStackServiceCatalog', + 'OpenStackResponse', + 'OpenStackException', 'OpenStackDriverMixin', 'AUTH_TOKEN_EXPIRES_GRACE_SECONDS' @@ -658,6 +668,78 @@ class OpenStackBaseConnection(ConnectionUserAndKey): self._set_up_connection_info(url=url) +class OpenStackException(ProviderError): + pass + + +class OpenStackResponse(Response): + node_driver = None + + def success(self): + i = int(self.status) + return i >= 200 and i <= 299 + + def has_content_type(self, content_type): + content_type_value = self.headers.get('content-type') or '' + content_type_value = content_type_value.lower() + return content_type_value.find(content_type.lower()) > -1 + + def parse_body(self): + if self.status == httplib.NO_CONTENT or not self.body: + return None + + if self.has_content_type('application/xml'): + try: + return ET.XML(self.body) + except: + raise MalformedResponseError( + 'Failed to parse XML', + body=self.body, + driver=self.node_driver) + + elif self.has_content_type('application/json'): + try: + return json.loads(self.body) + except: + raise MalformedResponseError( + 'Failed to parse JSON', + body=self.body, + driver=self.node_driver) + else: + return self.body + + def parse_error(self): + text = None + body = self.parse_body() + + if self.has_content_type('application/xml'): + text = '; '.join([err.text or '' for err in body.getiterator() + if err.text]) + elif self.has_content_type('application/json'): + values = list(body.values()) + + context = self.connection.context + driver = self.connection.driver + key_pair_name = context.get('key_pair_name', None) + + if len(values) > 0 and values[0]['code'] == 404 and key_pair_name: + raise KeyPairDoesNotExistError(name=key_pair_name, + driver=driver) + elif len(values) > 0 and 'message' in values[0]: + text = ';'.join([fault_data['message'] for fault_data + in values]) + else: + text = body + else: + # while we hope a response is always one of xml or json, we have + # seen html or text in the past, its not clear we can really do + # something to make it more readable here, so we will just pass + # it along as the whole response body in the text variable. + text = body + + return '%s %s %s' % (self.status, self.error, text) + + class OpenStackDriverMixin(object): def __init__(self, *args, **kwargs): http://git-wip-us.apache.org/repos/asf/libcloud/blob/d7917ef5/libcloud/compute/drivers/openstack.py ---------------------------------------------------------------------- diff --git a/libcloud/compute/drivers/openstack.py b/libcloud/compute/drivers/openstack.py index c09cee6..edad98a 100644 --- a/libcloud/compute/drivers/openstack.py +++ b/libcloud/compute/drivers/openstack.py @@ -21,6 +21,11 @@ try: except ImportError: import json +try: + from lxml import etree as ET +except ImportError: + from xml.etree import ElementTree as ET + import warnings import base64 @@ -29,23 +34,18 @@ from libcloud.utils.py3 import b from libcloud.utils.py3 import next from libcloud.utils.py3 import urlparse -try: - from lxml import etree as ET -except ImportError: - from xml.etree import ElementTree as ET from libcloud.common.openstack import OpenStackBaseConnection from libcloud.common.openstack import OpenStackDriverMixin -from libcloud.common.types import MalformedResponseError, ProviderError +from libcloud.common.openstack import OpenStackException +from libcloud.common.openstack import OpenStackResponse from libcloud.utils.networking import is_private_subnet from libcloud.compute.base import NodeSize, NodeImage from libcloud.compute.base import (NodeDriver, Node, NodeLocation, StorageVolume, VolumeSnapshot) from libcloud.compute.base import KeyPair from libcloud.compute.types import NodeState, Provider -from libcloud.compute.types import KeyPairDoesNotExistError from libcloud.pricing import get_size_price -from libcloud.common.base import Response from libcloud.utils.xml import findall __all__ = [ @@ -67,78 +67,6 @@ ATOM_NAMESPACE = "http://www.w3.org/2005/Atom" DEFAULT_API_VERSION = '1.1' -class OpenStackException(ProviderError): - pass - - -class OpenStackResponse(Response): - node_driver = None - - def success(self): - i = int(self.status) - return i >= 200 and i <= 299 - - def has_content_type(self, content_type): - content_type_value = self.headers.get('content-type') or '' - content_type_value = content_type_value.lower() - return content_type_value.find(content_type.lower()) > -1 - - def parse_body(self): - if self.status == httplib.NO_CONTENT or not self.body: - return None - - if self.has_content_type('application/xml'): - try: - return ET.XML(self.body) - except: - raise MalformedResponseError( - 'Failed to parse XML', - body=self.body, - driver=self.node_driver) - - elif self.has_content_type('application/json'): - try: - return json.loads(self.body) - except: - raise MalformedResponseError( - 'Failed to parse JSON', - body=self.body, - driver=self.node_driver) - else: - return self.body - - def parse_error(self): - text = None - body = self.parse_body() - - if self.has_content_type('application/xml'): - text = '; '.join([err.text or '' for err in body.getiterator() - if err.text]) - elif self.has_content_type('application/json'): - values = list(body.values()) - - context = self.connection.context - driver = self.connection.driver - key_pair_name = context.get('key_pair_name', None) - - if len(values) > 0 and values[0]['code'] == 404 and key_pair_name: - raise KeyPairDoesNotExistError(name=key_pair_name, - driver=driver) - elif len(values) > 0 and 'message' in values[0]: - text = ';'.join([fault_data['message'] for fault_data - in values]) - else: - text = body - else: - # while we hope a response is always one of xml or json, we have - # seen html or text in the past, its not clear we can really do - # something to make it more readable here, so we will just pass - # it along as the whole response body in the text variable. - text = body - - return '%s %s %s' % (self.status, self.error, text) - - class OpenStackComputeConnection(OpenStackBaseConnection): # default config for http://devstack.org/ service_type = 'compute'
