You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2014/08/13 16:38:16 UTC

[2/3] git commit: Add list_supported_versions to base OpenStackIdentityConnection class and parse roles which are assigned to the user which is used to authenticate.

Add list_supported_versions to base OpenStackIdentityConnection class and parse
roles which are assigned to the user which is used to authenticate.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/44945ed4
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/44945ed4
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/44945ed4

Branch: refs/heads/trunk
Commit: 44945ed403530af5df20dec9e43698db036153d8
Parents: 087da80
Author: Tomaz Muraus <to...@apache.org>
Authored: Wed Aug 13 16:22:50 2014 +0200
Committer: Tomaz Muraus <to...@apache.org>
Committed: Wed Aug 13 16:34:11 2014 +0200

----------------------------------------------------------------------
 libcloud/common/openstack.py                    |  3 +-
 libcloud/common/openstack_identity.py           | 59 ++++++++++++++++++++
 libcloud/test/common/test_openstack_identity.py | 19 +++++++
 .../openstack_identity/v3_versions.json         | 58 +++++++++++++++++++
 4 files changed, 137 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/44945ed4/libcloud/common/openstack.py
----------------------------------------------------------------------
diff --git a/libcloud/common/openstack.py b/libcloud/common/openstack.py
index 80ca08f..0a11d44 100644
--- a/libcloud/common/openstack.py
+++ b/libcloud/common/openstack.py
@@ -28,9 +28,9 @@ from libcloud.common.base import ConnectionUserAndKey, Response
 from libcloud.common.types import ProviderError
 from libcloud.compute.types import (LibcloudError, MalformedResponseError)
 from libcloud.compute.types import KeyPairDoesNotExistError
+from libcloud.common.openstack_identity import get_class_for_auth_version
 
 # Imports for backward compatibility reasons
-from libcloud.common.openstack_identity import get_class_for_auth_version
 from libcloud.common.openstack_identity import OpenStackServiceCatalog
 
 
@@ -246,7 +246,6 @@ class OpenStackBaseConnection(ConnectionUserAndKey):
                                                      name=service_name,
                                                      region=service_region)
 
-        # TODO: Normalize keys for different auth versions and use an object
         url = endpoint.url
 
         if not url:

http://git-wip-us.apache.org/repos/asf/libcloud/blob/44945ed4/libcloud/common/openstack_identity.py
----------------------------------------------------------------------
diff --git a/libcloud/common/openstack_identity.py b/libcloud/common/openstack_identity.py
index 5fdd186..0e11a0b 100644
--- a/libcloud/common/openstack_identity.py
+++ b/libcloud/common/openstack_identity.py
@@ -53,6 +53,7 @@ AUTH_TOKEN_EXPIRES_GRACE_SECONDS = 5
 
 
 __all__ = [
+    'OpenStackIdentityVersion',
     'OpenStackIdentityDomain',
     'OpenStackIdentityProject',
     'OpenStackIdentityUser',
@@ -72,6 +73,19 @@ __all__ = [
 ]
 
 
+class OpenStackIdentityVersion(object):
+    def __init__(self, version, status, updated, url):
+        self.version = version
+        self.status = status
+        self.updated = updated
+        self.url = url
+
+    def __repr__(self):
+        return (('<OpenStackIdentityVersion version=%s, status=%s, '
+                 'updated=%s, url=%s' %
+                 (self.version, self.status, self.updated, self.url)))
+
+
 class OpenStackIdentityDomain(object):
     def __init__(self, id, name, enabled):
         self.id = id
@@ -479,6 +493,7 @@ class OpenStackAuthResponse(Response):
     def success(self):
         return self.status in [httplib.OK, httplib.CREATED,
                                httplib.ACCEPTED, httplib.NO_CONTENT,
+                               httplib.MULTIPLE_CHOICES,
                                httplib.UNAUTHORIZED,
                                httplib.INTERNAL_SERVER_ERROR]
 
@@ -614,6 +629,43 @@ class OpenStackIdentityConnection(ConnectionUserAndKey):
         """
         raise NotImplementedError('authenticate not implemented')
 
+    def list_supported_versions(self):
+        """
+        Retrieve a list of all the identity versions which are supported by
+        this installation.
+
+        :rtype: ``list`` of :class:`.OpenStackIdentityVersion`
+        """
+        response = self.request('/', method='GET')
+        result = self._to_versions(data=response.object['versions']['values'])
+        result = sorted(result, key=lambda x: x.version)
+        return result
+
+    def _to_versions(self, data):
+        result = []
+        for item in data:
+            version = self._to_version(data=item)
+            result.append(version)
+
+        return result
+
+    def _to_version(self, data):
+        try:
+            updated = parse_date(data['updated'])
+        except Exception:
+            updated = None
+
+        try:
+            url = data['links'][0]['href']
+        except IndexError:
+            url = None
+
+        version = OpenStackIdentityVersion(version=data['id'],
+                                           status=data['status'],
+                                           updated=updated,
+                                           url=url)
+        return version
+
     def _is_authentication_needed(self, force=False):
         """
         Determine if the authentication is needed or if the existing token (if
@@ -964,12 +1016,19 @@ class OpenStackIdentity_3_0_Connection(OpenStackIdentityConnection):
                 raise MalformedResponseError('Failed to parse JSON', e)
 
             try:
+                roles = self._to_roles(body['token']['roles'])
+            except Exception:
+                e = sys.exc_info()[1]
+                roles = []
+
+            try:
                 expires = body['token']['expires_at']
 
                 self.auth_token = headers['x-subject-token']
                 self.auth_token_expires = parse_date(expires)
                 self.urls = body['token']['catalog']
                 self.auth_user_info = None
+                self.auth_user_roles = roles
             except KeyError:
                 e = sys.exc_info()[1]
                 raise MalformedResponseError('Auth JSON response is \

http://git-wip-us.apache.org/repos/asf/libcloud/blob/44945ed4/libcloud/test/common/test_openstack_identity.py
----------------------------------------------------------------------
diff --git a/libcloud/test/common/test_openstack_identity.py b/libcloud/test/common/test_openstack_identity.py
index 0353bc1..e84222b 100644
--- a/libcloud/test/common/test_openstack_identity.py
+++ b/libcloud/test/common/test_openstack_identity.py
@@ -224,6 +224,7 @@ class OpenStackIdentityConnectionTestCase(unittest.TestCase):
 class OpenStackIdentity_3_0_ConnectionTests(unittest.TestCase):
     def setUp(self):
         mock_cls = OpenStackIdentity_3_0_MockHttp
+        mock_cls.type = None
         OpenStackIdentity_3_0_Connection.conn_classes = (mock_cls, mock_cls)
 
         self.auth_instance = OpenStackIdentity_3_0_Connection(auth_url='http://none',
@@ -231,6 +232,18 @@ class OpenStackIdentity_3_0_ConnectionTests(unittest.TestCase):
                                                               key='test')
         self.auth_instance.auth_token = 'mock'
 
+    def test_list_supported_versions(self):
+        OpenStackIdentity_3_0_MockHttp.type = 'v3'
+
+        versions = self.auth_instance.list_supported_versions()
+        self.assertEqual(len(versions), 2)
+        self.assertEqual(versions[0].version, 'v2.0')
+        self.assertEqual(versions[0].url,
+                         'http://192.168.18.100:5000/v2.0/')
+        self.assertEqual(versions[1].version, 'v3.0')
+        self.assertEqual(versions[1].url,
+                         'http://192.168.18.100:5000/v3/')
+
     def test_list_domains(self):
         domains = self.auth_instance.list_domains()
         self.assertEqual(len(domains), 1)
@@ -435,6 +448,12 @@ class OpenStackIdentity_3_0_MockHttp(MockHttp):
     fixtures = ComputeFileFixtures('openstack_identity')
     json_content_headers = {'content-type': 'application/json; charset=UTF-8'}
 
+    def _v3(self, method, url, body, headers):
+        if method == 'GET':
+            body = self.fixtures.load('v3_versions.json')
+            return (httplib.OK, body, self.json_content_headers, httplib.responses[httplib.OK])
+        raise NotImplementedError()
+
     def _v3_domains(self, method, url, body, headers):
         if method == 'GET':
             body = self.fixtures.load('v3_domains.json')

http://git-wip-us.apache.org/repos/asf/libcloud/blob/44945ed4/libcloud/test/compute/fixtures/openstack_identity/v3_versions.json
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/fixtures/openstack_identity/v3_versions.json b/libcloud/test/compute/fixtures/openstack_identity/v3_versions.json
new file mode 100644
index 0000000..c63b938
--- /dev/null
+++ b/libcloud/test/compute/fixtures/openstack_identity/v3_versions.json
@@ -0,0 +1,58 @@
+{
+    "versions": {
+        "values": [
+            {
+                "status": "stable",
+                "updated": "2013-03-06T00:00:00Z",
+                "media-types": [
+                    {
+                        "base": "application/json",
+                        "type": "application/vnd.openstack.identity-v3+json"
+                    },
+                    {
+                        "base": "application/xml",
+                        "type": "application/vnd.openstack.identity-v3+xml"
+                    }
+                ],
+                "id": "v3.0",
+                "links": [
+                    {
+                        "href": "http://192.168.18.100:5000/v3/",
+                        "rel": "self"
+                    }
+                ]
+            },
+            {
+                "status": "stable",
+                "updated": "2014-04-17T00:00:00Z",
+                "media-types": [
+                    {
+                        "base": "application/json",
+                        "type": "application/vnd.openstack.identity-v2.0+json"
+                    },
+                    {
+                        "base": "application/xml",
+                        "type": "application/vnd.openstack.identity-v2.0+xml"
+                    }
+                ],
+                "id": "v2.0",
+                "links": [
+                    {
+                        "href": "http://192.168.18.100:5000/v2.0/",
+                        "rel": "self"
+                    },
+                    {
+                        "href": "http://docs.openstack.org/api/openstack-identity-service/2.0/content/",
+                        "type": "text/html",
+                        "rel": "describedby"
+                    },
+                    {
+                        "href": "http://docs.openstack.org/api/openstack-identity-service/2.0/identity-dev-guide-2.0.pdf",
+                        "type": "application/pdf",
+                        "rel": "describedby"
+                    }
+                ]
+            }
+        ]
+    }
+}