new service catalog implementation.
This commit is contained in:
parent
2d5f6b2df6
commit
8491c76027
@ -1,6 +1,8 @@
|
||||
# Copyright 2010 Jacob Kaplan-Moss
|
||||
# Copyright 2011 OpenStack LLC.
|
||||
# Copyright 2011 Piston Cloud Computing, Inc.
|
||||
|
||||
# All Rights Reserved.
|
||||
"""
|
||||
OpenStack Client interface. Handles the REST calls and responses.
|
||||
"""
|
||||
@ -147,13 +149,14 @@ class HTTPClient(httplib2.Http):
|
||||
self.auth_url = url
|
||||
self.service_catalog = \
|
||||
service_catalog.ServiceCatalog(body)
|
||||
self.auth_token = self.service_catalog.token.id
|
||||
self.auth_token = self.service_catalog.get_token()
|
||||
|
||||
self.management_url = self.service_catalog.url_for(
|
||||
'nova', 'public', attr='region',
|
||||
attr='region',
|
||||
filter_value=self.region_name)
|
||||
return None
|
||||
except KeyError:
|
||||
except KeyError, e:
|
||||
print "Key ERROR", e
|
||||
raise exceptions.AuthorizationFailure()
|
||||
elif resp.status == 305:
|
||||
return resp['location']
|
||||
|
@ -18,6 +18,11 @@ class NoTokenLookupException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class EndpointNotFound(Exception):
|
||||
"""Could not find Service or Region in Service Catalog."""
|
||||
pass
|
||||
|
||||
|
||||
class ClientException(Exception):
|
||||
"""
|
||||
The base exception class for all exceptions this library raises.
|
||||
|
@ -1,4 +1,8 @@
|
||||
# Copyright 2011 OpenStack LLC.
|
||||
# Copyright 2011, Piston Cloud Computing, Inc.
|
||||
#
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
@ -11,96 +15,33 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import re
|
||||
|
||||
import novaclient.exceptions
|
||||
|
||||
|
||||
def get_resource(key):
|
||||
resource_classes = {
|
||||
"glance": GlanceCatalog,
|
||||
"identity": IdentityCatalog,
|
||||
"keystone": KeystoneCatalog,
|
||||
"nova": NovaCatalog,
|
||||
"nova_compat": NovaCompatCatalog,
|
||||
"swift": SwiftCatalog,
|
||||
"serviceCatalog": ServiceCatalog,
|
||||
}
|
||||
return resource_classes.get(key)
|
||||
|
||||
|
||||
def snake_case(string):
|
||||
return re.sub(r'([a-z])([A-Z])', r'\1_\2', string).lower()
|
||||
|
||||
|
||||
class CatalogResource(object):
|
||||
def __repr__(self):
|
||||
return "<%s: %s>" % (self.__class__.__name__, self.public_url)
|
||||
class ServiceCatalog(object):
|
||||
"""Helper methods for dealing with a Keystone Service Catalog."""
|
||||
|
||||
def __init__(self, resource_dict):
|
||||
for key, value in resource_dict.items():
|
||||
if self.__catalog_key__ in value:
|
||||
if not isinstance(value, dict):
|
||||
continue
|
||||
for res_key, res_value in value.items():
|
||||
for attr, val in res_value.items():
|
||||
res = get_resource(attr)
|
||||
if res:
|
||||
attribute = [res(x) for x in val]
|
||||
setattr(self, snake_case(attr), attribute)
|
||||
else:
|
||||
for key, attr in resource_dict.items():
|
||||
setattr(self, snake_case(key), attr)
|
||||
self.catalog = resource_dict
|
||||
|
||||
def get_token(self):
|
||||
return self.catalog['access']['token']['id']
|
||||
|
||||
class TokenCatalog(CatalogResource):
|
||||
__catalog_key__ = "token"
|
||||
def url_for(self, attr=None, filter_value=None):
|
||||
"""Fetch the public URL from the Compute service for
|
||||
a particular endpoint attribute. If none given, return
|
||||
the first. See tests for sample service catalog."""
|
||||
print "Looking for %s / %s in %s" % (attr, filter_value, self.catalog)
|
||||
catalog = self.catalog['access']['serviceCatalog']
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s: %s>" % (self.__class__.__name__, self.id)
|
||||
for service in catalog:
|
||||
if service['type'] != 'compute':
|
||||
continue
|
||||
|
||||
endpoints = service['endpoints']
|
||||
for endpoint in endpoints:
|
||||
if filter_value == None or endpoint[attr] == filter_value:
|
||||
return endpoint['publicURL']
|
||||
|
||||
class NovaCatalog(CatalogResource):
|
||||
__catalog_key__ = "nova"
|
||||
|
||||
|
||||
class KeystoneCatalog(CatalogResource):
|
||||
__catalog_key__ = 'keystone'
|
||||
|
||||
|
||||
class GlanceCatalog(CatalogResource):
|
||||
__catalog_key__ = 'glance'
|
||||
|
||||
|
||||
class SwiftCatalog(CatalogResource):
|
||||
__catalog_key__ = 'swift'
|
||||
|
||||
|
||||
class IdentityCatalog(CatalogResource):
|
||||
__catalog_key__ = 'identity'
|
||||
|
||||
|
||||
class NovaCompatCatalog(CatalogResource):
|
||||
__catalog_key__ = "nova_compat"
|
||||
|
||||
|
||||
class ServiceCatalog(CatalogResource):
|
||||
__catalog_key__ = "serviceCatalog"
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s: %s>" % (self.__class__.__name__, self.token.id)
|
||||
|
||||
def __init__(self, resource):
|
||||
super(ServiceCatalog, self).__init__(resource)
|
||||
|
||||
self.token = TokenCatalog(resource["auth"]["token"])
|
||||
|
||||
def url_for(self, catalog_class, url, attr=None, filter_value=None):
|
||||
catalog = getattr(self, catalog_class)
|
||||
if attr and filter_value:
|
||||
catalog = [item for item in catalog
|
||||
if hasattr(item, attr) and
|
||||
getattr(item, attr) == filter_value]
|
||||
if not catalog:
|
||||
raise ValueError("No catalog entries for %s=%s" %
|
||||
(attr, filter_value))
|
||||
if catalog:
|
||||
return getattr(catalog[0], url + "_url")
|
||||
raise novaclient.exceptions.EndpointNotFound()
|
||||
|
@ -1,31 +1,108 @@
|
||||
from novaclient import exceptions
|
||||
from novaclient import service_catalog
|
||||
from tests import utils
|
||||
|
||||
|
||||
SERVICE_CATALOG = {'auth': {'token': {'id': "FAKE_ID", },
|
||||
'serviceCatalog': {
|
||||
"nova": [{"publicURL": "http://fakeurl",
|
||||
"region": "north"},
|
||||
{"publicURL": "http://fakeurl2",
|
||||
"region": "south"}],
|
||||
"glance": [{"publicURL": "http://fakeurl"}],
|
||||
"swift": [{"publicURL": "http://fakeurl"}],
|
||||
"identity": [{"publicURL": "http://fakeurl"}],
|
||||
}}}
|
||||
# Taken directly from keystone/content/common/samples/auth.json
|
||||
# Do not edit this structure. Instead, grab the latest from there.
|
||||
|
||||
SERVICE_CATALOG = {
|
||||
"access":{
|
||||
"token":{
|
||||
"id":"ab48a9efdfedb23ty3494",
|
||||
"expires":"2010-11-01T03:32:15-05:00",
|
||||
"tenant":{
|
||||
"id": "345",
|
||||
"name": "My Project"
|
||||
}
|
||||
},
|
||||
"user":{
|
||||
"id":"123",
|
||||
"name":"jqsmith",
|
||||
"roles":[{
|
||||
"id":"234",
|
||||
"name":"compute:admin"
|
||||
},
|
||||
{
|
||||
"id":"235",
|
||||
"name":"object-store:admin",
|
||||
"tenantId":"1"
|
||||
}
|
||||
],
|
||||
"roles_links":[]
|
||||
},
|
||||
"serviceCatalog":[{
|
||||
"name":"Cloud Servers",
|
||||
"type":"compute",
|
||||
"endpoints":[{
|
||||
"tenantId":"1",
|
||||
"publicURL":"https://compute.north.host/v1/1234",
|
||||
"internalURL":"https://compute.north.host/v1/1234",
|
||||
"region":"North",
|
||||
"versionId":"1.0",
|
||||
"versionInfo":"https://compute.north.host/v1.0/",
|
||||
"versionList":"https://compute.north.host/"
|
||||
},
|
||||
{
|
||||
"tenantId":"2",
|
||||
"publicURL":"https://compute.north.host/v1.1/3456",
|
||||
"internalURL":"https://compute.north.host/v1.1/3456",
|
||||
"region":"North",
|
||||
"versionId":"1.1",
|
||||
"versionInfo":"https://compute.north.host/v1.1/",
|
||||
"versionList":"https://compute.north.host/"
|
||||
}
|
||||
],
|
||||
"endpoints_links":[]
|
||||
},
|
||||
{
|
||||
"name":"Cloud Files",
|
||||
"type":"object-store",
|
||||
"endpoints":[{
|
||||
"tenantId":"11",
|
||||
"publicURL":"https://compute.north.host/v1/blah-blah",
|
||||
"internalURL":"https://compute.north.host/v1/blah-blah",
|
||||
"region":"South",
|
||||
"versionId":"1.0",
|
||||
"versionInfo":"uri",
|
||||
"versionList":"uri"
|
||||
},
|
||||
{
|
||||
"tenantId":"2",
|
||||
"publicURL":"https://compute.north.host/v1.1/blah-blah",
|
||||
"internalURL":"https://compute.north.host/v1.1/blah-blah",
|
||||
"region":"South",
|
||||
"versionId":"1.1",
|
||||
"versionInfo":"https://compute.north.host/v1.1/",
|
||||
"versionList":"https://compute.north.host/"
|
||||
}
|
||||
],
|
||||
"endpoints_links":[{
|
||||
"rel":"next",
|
||||
"href":"https://identity.north.host/v2.0/endpoints?marker=2"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"serviceCatalog_links":[{
|
||||
"rel":"next",
|
||||
"href":"https://identity.host/v2.0/endpoints?session=2hfh8Ar&marker=2"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ServiceCatalogTest(utils.TestCase):
|
||||
def test_building_a_service_catalog(self):
|
||||
sc = service_catalog.ServiceCatalog(SERVICE_CATALOG)
|
||||
|
||||
self.assertEqual(sc.__repr__(), "<ServiceCatalog: FAKE_ID>")
|
||||
self.assertEqual(sc.token.__repr__(), "<TokenCatalog: FAKE_ID>")
|
||||
self.assertEqual(sc.nova.__repr__(),
|
||||
"[<NovaCatalog: http://fakeurl>, <NovaCatalog: http://fakeurl2>]")
|
||||
self.assertEquals(sc.url_for(),
|
||||
"https://compute.north.host/v1/1234")
|
||||
self.assertEquals(sc.url_for('tenantId', '1'),
|
||||
"https://compute.north.host/v1/1234")
|
||||
self.assertEquals(sc.url_for('tenantId', '2'),
|
||||
"https://compute.north.host/v1.1/3456")
|
||||
|
||||
self.assertEqual(sc.token.id, "FAKE_ID")
|
||||
self.assertEqual(sc.url_for('nova', 'public'),
|
||||
SERVICE_CATALOG['auth']['serviceCatalog']['nova'][0]['publicURL'])
|
||||
self.assertEqual(sc.url_for('nova', 'public',
|
||||
attr='region', filter_value='south'),
|
||||
SERVICE_CATALOG['auth']['serviceCatalog']['nova'][1]['publicURL'])
|
||||
self.assertRaises(exceptions.EndpointNotFound,
|
||||
sc.url_for, "region", "South")
|
||||
|
@ -20,14 +20,15 @@ def to_http_response(resp_dict):
|
||||
class AuthenticateAgainstKeystoneTests(utils.TestCase):
|
||||
def test_authenticate_success(self):
|
||||
cs = client.Client("username", "apikey", "project_id", "auth_url/v2.0")
|
||||
resp = {"auth":
|
||||
resp = {"access":
|
||||
{"token": {"expires": "12345", "id": "FAKE_ID"},
|
||||
"serviceCatalog": {
|
||||
"nova": [
|
||||
{"adminURL": "http://localhost:8774/v1.1",
|
||||
"serviceCatalog": [
|
||||
{"adminURL": "http://localhost:8774/v1.1",
|
||||
"type": "compute",
|
||||
"endpoints" : [{
|
||||
"region": "RegionOne",
|
||||
"internalURL": "http://localhost:8774/v1.1",
|
||||
"publicURL": "http://localhost:8774/v1.1/"}]}}}
|
||||
"publicURL": "http://localhost:8774/v1.1/"},]},]}}
|
||||
auth_response = httplib2.Response({
|
||||
"status": 200,
|
||||
"body": json.dumps(resp),
|
||||
@ -52,8 +53,9 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
|
||||
body=json.dumps(body))
|
||||
|
||||
self.assertEqual(cs.client.management_url,
|
||||
resp["auth"]["serviceCatalog"]["nova"][0]["publicURL"])
|
||||
self.assertEqual(cs.client.auth_token, resp["auth"]["token"]["id"])
|
||||
resp["access"]["serviceCatalog"][0]
|
||||
['endpoints'][0]["publicURL"])
|
||||
self.assertEqual(cs.client.auth_token, resp["access"]["token"]["id"])
|
||||
|
||||
test_auth_call()
|
||||
|
||||
@ -76,12 +78,13 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
|
||||
|
||||
def test_auth_redirect(self):
|
||||
cs = client.Client("username", "apikey", "project_id", "auth_url/v1.0")
|
||||
dict_correct_response = {"auth": {"token": {"expires": "12345", "id": "FAKE_ID"},
|
||||
"serviceCatalog": {
|
||||
"nova": [{"adminURL": "http://localhost:8774/v1.1",
|
||||
dict_correct_response = {"access": {"token": {"expires": "12345", "id": "FAKE_ID"},
|
||||
"serviceCatalog": [{
|
||||
"type": "compute",
|
||||
"endpoints": [{"adminURL": "http://localhost:8774/v1.1",
|
||||
"region": "RegionOne",
|
||||
"internalURL": "http://localhost:8774/v1.1",
|
||||
"publicURL": "http://localhost:8774/v1.1/"}]}}}
|
||||
"publicURL": "http://localhost:8774/v1.1/"},]},]}}
|
||||
correct_response = json.dumps(dict_correct_response)
|
||||
dict_responses = [
|
||||
{"headers": {'location':'http://127.0.0.1:5001'},
|
||||
@ -123,8 +126,9 @@ class AuthenticateAgainstKeystoneTests(utils.TestCase):
|
||||
|
||||
resp = dict_correct_response
|
||||
self.assertEqual(cs.client.management_url,
|
||||
resp["auth"]["serviceCatalog"]["nova"][0]["publicURL"])
|
||||
self.assertEqual(cs.client.auth_token, resp["auth"]["token"]["id"])
|
||||
resp["access"]["serviceCatalog"][0]
|
||||
['endpoints'][0]["publicURL"])
|
||||
self.assertEqual(cs.client.auth_token, resp["access"]["token"]["id"])
|
||||
|
||||
test_auth_call()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user