Commit 528550d9 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Enrich/rename astakos client calls with endpoints

Refs: #3874

Kamaki.clients.astakos.AstakosClient is now a high-level client lib that
offers access to token-authenticated astakos-aquired cached information.
Users who need an astakos client should use the snf-astakosclient instead.

Usage:

 astakos_cache = AstakosClient(<astakos/base/url/with/identity/api>)
 astakos_cache.authenticate(<token>)
 user_info = astakos_cache.user_info()
 compute_endpoints = astakos_cache.get_service_endpoints('compute', 'v2')

Major changes:

Modify ReST call from /im/authenticate to /tokens

New methods as AstakosClient instance cache accessors:
 get_services
 get_service_details
 get_service_endpoints

Method renaiming for existing AstakosClient instance cache accessors:
 info --> user_info
 user --> list_users
parent 59794110
...@@ -55,8 +55,9 @@ Changes: ...@@ -55,8 +55,9 @@ Changes:
- Add a _format_image_headers method and use it in image_register and get_meta - Add a _format_image_headers method and use it in image_register and get_meta
for uniform image meta output [#3797] for uniform image meta output [#3797]
- Rename meta-->metadata and remove values @lib [#3633] - Rename meta-->metadata and remove values @lib [#3633]
- Adjust astakos authenticate to the new url shchem of synnefo >= 0.14 - Adjust astakos authenticate to the new url shchem of synnefo >= 0.14 [#3832, #3874]
as a side effect, some renamings in astakos.AstakosClient:
info --> user_info, user --> list_users
Features: Features:
...@@ -82,4 +83,6 @@ Features: ...@@ -82,4 +83,6 @@ Features:
- Add server-firewall-get command to get a VMs firewall profile - Add server-firewall-get command to get a VMs firewall profile
- Implement an optional astakosclient cli exposed as "astakos", with the following methods: - Implement an optional astakosclient cli exposed as "astakos", with the following methods:
authenticate, uuid, username, quotas, service uuid/username/quotas authenticate, uuid, username, quotas, service uuid/username/quotas
- Add some astakos/keystone kamaki-lib api calls [#3874], used to access astakos-calls cache:
get_services, get_service_details, get_service_endpoints
...@@ -315,6 +315,7 @@ class Client(object): ...@@ -315,6 +315,7 @@ class Client(object):
LOG_DATA = False LOG_DATA = False
def __init__(self, base_url, token): def __init__(self, base_url, token):
assert base_url, 'No base_url for client %s' % self
self.base_url = base_url self.base_url = base_url
self.token = token self.token = token
self.headers, self.params = dict(), dict() self.headers, self.params = dict(), dict()
......
...@@ -32,14 +32,18 @@ ...@@ -32,14 +32,18 @@
# or implied, of GRNET S.A. # or implied, of GRNET S.A.
from kamaki.clients import Client, ClientError from kamaki.clients import Client, ClientError
from logging import getLogger
class AstakosClient(Client): class AstakosClient(Client):
"""Synnefo Astakos API client""" """Synnefo Astakos API client"""
def __init__(self, base_url, token): def __init__(self, base_url, token=None):
super(AstakosClient, self).__init__(base_url, token) super(AstakosClient, self).__init__(base_url, token)
self._cache = {} self._cache = {}
self.log = getLogger('__name__')
def authenticate(self, token=None): def authenticate(self, token=None):
"""Get authentication information and store it in this client """Get authentication information and store it in this client
...@@ -51,28 +55,88 @@ class AstakosClient(Client): ...@@ -51,28 +55,88 @@ class AstakosClient(Client):
:returns: (dict) authentication information :returns: (dict) authentication information
""" """
self.token = token or self.token self.token = token or self.token
self._cache[self.token] = self.get('/astakos/api/authenticate').json self._cache[self.token] = self.get('/tokens').json
return self._cache[self.token] return self._cache[self.token]
def list(self): def get_services(self, token=None):
"""list cached user information""" """
:returns: (list) [{name:..., type:..., endpoints:[...]}, ...]
"""
token_bu = self.token or token
token = token or self.token
try:
r = self._cache[token]
except KeyError:
r = self.authenticate(token)
finally:
self.token = token_bu
return r['serviceCatalog']
def get_service_details(self, service_type, token=None):
"""
:param service_type: (str) compute, object-store, image, account, etc.
:returns: (dict) {name:..., type:..., endpoints:[...]}
:raises ClientError: (600) if service_type not in service catalog
"""
services = self.get_services(token)
for service in services:
try:
if service['type'].lower() == service_type.lower():
return service
except KeyError:
self.log.warning('Misformated service %s' % service)
raise ClientError(
'Service type "%s" not in service catalog' % service_type, 600)
def get_service_endpoints(self, service_type, version=None, token=None):
"""
:param service_type: (str) can be compute, object-store, etc.
:param version: (str) the version id of the service
:returns: (dict) {SNF:uiURL, adminURL, internalURL, publicURL, ...}
:raises ClientError: (600) if service_type not in service catalog
:raises ClientError: (601) if #matching endpoints != 1
"""
service = self.get_service_details(service_type, token)
matches = []
for endpoint in service['endpoints']:
if (not version) or (
endpoint['version_id'].lower() == version.lower()):
matches.append(endpoint)
if len(matches) != 1:
raise ClientError(
'%s endpoints match type %s %s' % (
len(matches), service_type,
('and version_id %s' % version) if version else ''),
601)
return matches[0]
def list_users(self):
"""list cached users information"""
r = [] r = []
for k, v in self._cache.items(): for k, v in self._cache.items():
r.append(dict(v)) r.append(dict(v['user']))
r[-1].update(dict(auth_token=k)) r[-1].update(dict(auth_token=k))
return r return r
def info(self, token=None): def user_info(self, token=None):
"""Get (cached) user information""" """Get (cached) user information"""
token_bu = self.token token_bu = self.token or token
token = token or self.token token = token or self.token
try: try:
r = self._cache[token] r = self._cache[token]
except KeyError: except KeyError:
r = self.authenticate(token) r = self.authenticate(token)
self.token = token_bu finally:
return r self.token = token_bu
return r['user']
def term(self, key, token=None): def term(self, key, token=None):
"""Get (cached) term, from user credentials""" """Get (cached) term, from user credentials"""
return self.info(token).get(key, None) return self.user_info(token).get(key, None)
...@@ -34,10 +34,19 @@ ...@@ -34,10 +34,19 @@
from mock import patch, call from mock import patch, call
from unittest import TestCase from unittest import TestCase
from kamaki.clients import astakos from kamaki.clients import astakos, ClientError
example = dict( example = dict(
serviceCatalog=[
dict(name='service name 1', type='compute', endpoints=[
dict(version_id='v1', publicUrl='http://1.1.1.1/v1'),
dict(version_id='v2', publicUrl='http://1.1.1.1/v2')]),
dict(name='service name 2', type='image', endpoints=[
dict(version_id='v2', publicUrl='http://1.1.1.1/v2'),
dict(version_id='v2.1', publicUrl='http://1.1.1.1/v2/xtra')])
],
user=dict(
name='Simple Name', name='Simple Name',
username='User Full Name', username='User Full Name',
auth_token_expires='1362583796000', auth_token_expires='1362583796000',
...@@ -45,15 +54,26 @@ example = dict( ...@@ -45,15 +54,26 @@ example = dict(
email=['user@example.gr'], email=['user@example.gr'],
id=42, id=42,
uuid='aus3r-uu1d-f0r-73s71ng-as7ak0s') uuid='aus3r-uu1d-f0r-73s71ng-as7ak0s')
)
example0 = dict( example0 = dict(
serviceCatalog=[
dict(name='service name 1', type='compute', endpoints=[
dict(version_id='v1', publicUrl='http://1.1.1.1/v1'),
dict(version_id='v2', publicUrl='http://1.1.1.1/v2')]),
dict(name='service name 3', type='object-storage', endpoints=[
dict(version_id='v2', publicUrl='http://1.1.1.1/v2'),
dict(version_id='v2.1', publicUrl='http://1.1.1.1/v2/xtra')])
],
user=dict(
name='Simple Name 0', name='Simple Name 0',
username='User Full Name 0', username='User Full Name 0',
auth_token_expires='1362583796001', auth_token_expires='1362585796000',
auth_token_created='1359991796001', auth_token_created='1359931796000',
email=['user0@example.gr'], email=['user0@example.gr'],
id=32, id=42,
uuid='an07h2r-us3r-uu1d-f0r-as7ak0s') uuid='aus3r-uu1d-507-73s71ng-as7ak0s')
)
class FR(object): class FR(object):
...@@ -70,6 +90,14 @@ class AstakosClient(TestCase): ...@@ -70,6 +90,14 @@ class AstakosClient(TestCase):
cached = False cached = False
def assert_dicts_are_equal(self, d1, d2):
for k, v in d1.items():
self.assertTrue(k in d2)
if isinstance(v, dict):
self.assert_dicts_are_equal(v, d2[k])
else:
self.assertEqual(unicode(v), unicode(d2[k]))
def setUp(self): def setUp(self):
self.url = 'https://astakos.example.com' self.url = 'https://astakos.example.com'
self.token = 'ast@k0sT0k3n==' self.token = 'ast@k0sT0k3n=='
...@@ -81,37 +109,62 @@ class AstakosClient(TestCase): ...@@ -81,37 +109,62 @@ class AstakosClient(TestCase):
@patch('%s.get' % astakos_pkg, return_value=FR()) @patch('%s.get' % astakos_pkg, return_value=FR())
def _authenticate(self, get): def _authenticate(self, get):
r = self.client.authenticate() r = self.client.authenticate()
self.assertEqual(get.mock_calls[-1], call('/astakos/api/authenticate')) self.assertEqual(get.mock_calls[-1], call('/tokens'))
self.cached = True self.cached = True
return r return r
def test_authenticate(self): def test_authenticate(self):
r = self._authenticate() r = self._authenticate()
for term, val in example.items(): self.assert_dicts_are_equal(r, example)
self.assertTrue(term in r)
self.assertEqual(val, r[term]) def test_get_services(self):
if not self.cached:
self._authenticate()
slist = self.client.get_services()
self.assertEqual(slist, example['serviceCatalog'])
def test_info(self): def test_get_service_details(self):
if not self.cached:
self._authenticate()
stype = '#FAIL'
self.assertRaises(ClientError, self.client.get_service_details, stype)
stype = 'compute'
expected = [s for s in example['serviceCatalog'] if s['type'] == stype]
self.assert_dicts_are_equal(
self.client.get_service_details(stype), expected[0])
def test_get_service_endpoints(self):
if not self.cached:
self._authenticate()
stype, version = 'compute', 'V2'
self.assertRaises(
ClientError, self.client.get_service_endpoints, stype)
expected = [s for s in example['serviceCatalog'] if s['type'] == stype]
expected = [e for e in expected[0]['endpoints'] if (
e['version_id'] == version.lower())]
self.assert_dicts_are_equal(
self.client.get_service_endpoints(stype, version), expected[0])
def test_user_info(self):
if not self.cached: if not self.cached:
self._authenticate() self._authenticate()
return self.test_info()
self.assertTrue(set( self.assertTrue(set(
example.keys()).issubset(self.client.info().keys())) example['user'].keys()).issubset(self.client.user_info().keys()))
def test_get(self): def test_item(self):
if not self.cached: if not self.cached:
self._authenticate() self._authenticate()
return self.test_get() for term, val in example['user'].items():
for term, val in example.items():
self.assertEqual(self.client.term(term, self.token), val) self.assertEqual(self.client.term(term, self.token), val)
self.assertTrue(example['email'][0] in self.client.term('email')) self.assertTrue(
example['user']['email'][0] in self.client.term('email'))
def test_list(self): def test_list_users(self):
if not self.cached: if not self.cached:
self._authenticate self._authenticate
FR.json = example0 FR.json = example0
self._authenticate() self._authenticate()
r = self.client.list() r = self.client.list_users()
self.assertTrue(len(r) == 1) self.assertTrue(len(r) == 1)
self.assertEqual(r[0]['auth_token'], self.token) self.assertEqual(r[0]['auth_token'], self.token)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment