You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by an...@apache.org on 2016/12/27 08:35:17 UTC

[3/7] libcloud git commit: Enable to search for the tenant name if not specified

Enable to search for the tenant name if not specified


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

Branch: refs/heads/trunk
Commit: d551369367890477e37ec88a307cf1634b4be024
Parents: 80b54cb
Author: micafer <mi...@upv.es>
Authored: Fri Dec 16 09:04:26 2016 +0100
Committer: micafer <mi...@upv.es>
Committed: Fri Dec 16 09:04:26 2016 +0100

----------------------------------------------------------------------
 libcloud/common/openstack_identity.py           | 59 +++++++++++++++++++-
 libcloud/test/common/test_openstack_identity.py | 10 +++-
 2 files changed, 64 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/d5513693/libcloud/common/openstack_identity.py
----------------------------------------------------------------------
diff --git a/libcloud/common/openstack_identity.py b/libcloud/common/openstack_identity.py
index efd8ce8..da90f0b 100644
--- a/libcloud/common/openstack_identity.py
+++ b/libcloud/common/openstack_identity.py
@@ -1582,12 +1582,65 @@ class OpenStackIdentity_2_0_Connection_VOMS(OpenStackIdentityConnection,
         if not self._is_authentication_needed(force=force):
             return self
 
-        data = {'auth': {"voms": True}}
-        if self.tenant_name:
-            data['auth']['tenantName'] = self.tenant_name
+        tenant = self.tenant_name
+        if not tenant:
+            # if the tenant name is not specified look for it
+            token = self._get_unscoped_token()
+            tenant = self._get_tenant_name(token)
+
+        data = {'auth': {'voms': True, 'tenantName': tenant}}
+
         reqbody = json.dumps(data)
         return self._authenticate_2_0_with_body(reqbody)
 
+    def _get_unscoped_token(self):
+        """
+        Get unscoped token from VOMS proxy
+        """
+        data = {'auth': {'voms': True}}
+        reqbody = json.dumps(data)
+
+        response = self.request('/v2.0/tokens', data=reqbody,
+                                headers={'Content-Type': 'application/json'},
+                                method='POST')
+
+        if response.status == httplib.UNAUTHORIZED:
+            # Invalid credentials
+            raise InvalidCredsError()
+        elif response.status in [httplib.OK, httplib.CREATED]:
+            try:
+                body = json.loads(response.body)
+                return body['access']['token']['id']
+            except Exception:
+                e = sys.exc_info()[1]
+                raise MalformedResponseError('Failed to parse JSON', e)
+        else:
+            raise MalformedResponseError('Malformed response',
+                                         driver=self.driver)
+
+    def _get_tenant_name(self, token):
+        """
+        Get the first available tenant name (usually there are only one)
+        """
+        headers = {'Accept': 'application/json',
+                   'Content-Type': 'application/json',
+                   'X-Auth-Token': token}
+        response = self.request('/v2.0/tenants', headers=headers, method='GET')
+
+        if response.status == httplib.UNAUTHORIZED:
+            # Invalid credentials
+            raise InvalidCredsError()
+        elif response.status in [httplib.OK, httplib.CREATED]:
+            try:
+                body = json.loads(response.body)
+                return body["tenants"][0]["name"]
+            except Exception:
+                e = sys.exc_info()[1]
+                raise MalformedResponseError('Failed to parse JSON', e)
+        else:
+            raise MalformedResponseError('Malformed response',
+                                         driver=self.driver)
+
     def _authenticate_2_0_with_body(self, reqbody):
         resp = self.request('/v2.0/tokens', data=reqbody,
                             headers={'Content-Type': 'application/json'},

http://git-wip-us.apache.org/repos/asf/libcloud/blob/d5513693/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 186d21c..902cde2 100644
--- a/libcloud/test/common/test_openstack_identity.py
+++ b/libcloud/test/common/test_openstack_identity.py
@@ -741,8 +741,7 @@ class OpenStackIdentity_2_0_Connection_VOMSMockHttp(MockHttp):
             status = httplib.UNAUTHORIZED
             data = json.loads(body)
             if 'voms' in data['auth'] and data['auth']['voms'] is True:
-                if 'tenantName' in data['auth'] and data['auth']['tenantName'] == 'VO':
-                    status = httplib.OK
+                status = httplib.OK
 
             body = ComputeFileFixtures('openstack').load('_v2_0__auth.json')
             headers = self.json_content_headers.copy()
@@ -750,5 +749,12 @@ class OpenStackIdentity_2_0_Connection_VOMSMockHttp(MockHttp):
             return (status, body, headers, httplib.responses[httplib.OK])
         raise NotImplementedError()
 
+    def _v2_0_tenants(self, method, url, body, headers):
+        if method == 'GET':
+            # get user projects
+            body = json.dumps({"tenant": [{"name": "tenant_name"}]})
+            return (httplib.OK, body, self.json_content_headers, httplib.responses[httplib.OK])
+        raise NotImplementedError()
+
 if __name__ == '__main__':
     sys.exit(unittest.main())