Display token and service catalog for user

* Adds commands 'token', 'catalog' and 'endpoint-get' to keystone CLI
* Fixes bug 930421

Change-Id: I9eceea3bf98a5c87b122fa663c96f7119ef8d3cc
This commit is contained in:
Brian Waldon
2012-02-16 09:05:17 -08:00
parent 0414cf1ef2
commit 0cc939c904
4 changed files with 91 additions and 3 deletions

View File

@@ -27,7 +27,15 @@ class ServiceCatalog(object):
self.catalog = resource_dict
def get_token(self):
return self.catalog['token']['id']
"""Fetch token details fron service catalog"""
token = {'id': self.catalog['token']['id'],
'expires': self.catalog['token']['expires']}
try:
token['tenant'] = self.catalog['token']['tenant']['id']
except:
# just leave the tenant out if it doesn't exist
pass
return token
def url_for(self, attr=None, filter_value=None,
service_type='identity', endpoint_type='publicURL'):
@@ -47,7 +55,24 @@ class ServiceCatalog(object):
endpoints = service['endpoints']
for endpoint in endpoints:
if not filter_value or endpoint[attr] == filter_value:
if not filter_value or endpoint.get(attr) == filter_value:
return endpoint[endpoint_type]
raise exceptions.EndpointNotFound('Endpoint not found.')
def get_endpoints(self, service_type=None, endpoint_type=None):
"""Fetch and filter endpoints for the specified service(s)
Returns endpoints for the specified service (or all) and
that contain the specified type (or all).
"""
sc = {}
for service in self.catalog.get('serviceCatalog', []):
if service_type and service_type != service['type']:
continue
sc[service['type']] = []
for endpoint in service['endpoints']:
if endpoint_type and endpoint_type not in endpoint.keys():
continue
sc[service['type']].append(endpoint)
return sc

View File

@@ -111,7 +111,7 @@ class Client(client.HTTPClient):
""" Set the client's service catalog from the response data. """
self.service_catalog = service_catalog.ServiceCatalog(body)
try:
self.auth_token = self.service_catalog.get_token()
self.auth_token = self.service_catalog.get_token()['id']
except KeyError:
raise exceptions.AuthorizationFailure()

View File

@@ -244,3 +244,45 @@ def do_ec2_delete_credentials(kc, args):
print 'Deleted EC2 Credentials.'
except:
print 'Unable to delete EC2 Credentials.'
@utils.arg('--service', metavar='<service_type>',
help='Service type to return', nargs='?', default=None)
def do_catalog(kc, args):
"""List service catalog, possibly filtered by service"""
endpoints = kc.service_catalog.get_endpoints(service_type=args.service)
for (service, service_endpoints) in endpoints.iteritems():
if len(service_endpoints) > 0:
print "Service: %s" % service
for ep in service_endpoints:
utils.print_dict(ep)
@utils.arg('--endpoint_type', metavar='<endpoint_type>',
help='Endpoint type to select', nargs='?', default='publicURL')
@utils.arg('--service', metavar='<service_type>',
help='Service type to select', nargs='?', required=True)
@utils.arg('--attr', metavar='<attribute>',
help='Attribute to match', nargs='?')
@utils.arg('--value', metavar='<value>',
help='Value of attribute to match', nargs='?')
def do_endpoint_get(kc, args):
"""Find endpoint filtered by a specific attribute or service type"""
kwargs = {
'service_type': args.service,
'endpoint_type': args.endpoint_type,
}
if args.attr and args.value:
kwargs.update({'attr': args.attr, 'filter_value': args.value})
elif args.attr or args.value:
print 'Both --attr and --value required.'
return
url = kc.service_catalog.url_for(**kwargs)
utils.print_dict({'%s.%s' % (args.service, args.endpoint_type): url})
def do_token(kc, args):
"""Fetch the current user's token"""
utils.print_dict(kc.service_catalog.get_token())

View File

@@ -107,3 +107,24 @@ class ServiceCatalogTest(utils.TestCase):
self.assertRaises(exceptions.EndpointNotFound,
sc.url_for, "region", "South", service_type='compute')
def test_service_catalog_endpoints(self):
sc = service_catalog.ServiceCatalog(SERVICE_CATALOG['access'])
public_ep = sc.get_endpoints(service_type='compute',
endpoint_type='publicURL')
self.assertEquals(public_ep['compute'][1]['tenantId'], '2')
self.assertEquals(public_ep['compute'][1]['versionId'], '1.1')
self.assertEquals(public_ep['compute'][1]['internalURL'],
"https://compute.north.host/v1.1/3456")
def test_token(self):
sc = service_catalog.ServiceCatalog(SERVICE_CATALOG['access'])
self.assertEquals(sc.get_token(),
{'id': 'ab48a9efdfedb23ty3494',
'tenant': '345',
'expires': '2010-11-01T03:32:15-05:00'})
self.assertEquals(sc.catalog['token']['expires'],
"2010-11-01T03:32:15-05:00")
self.assertEquals(sc.catalog['token']['tenant']['id'],
'345')