Commit 6fdb3576 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Merge commit '528550d9' into develop

Conflicts:
	Changelog
	version
parents ed46d009 528550d9
CHANGELOG for version 0.10
Bug Fixes:
Changes:
- 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:
- Implement an optional astakosclient cli exposed as "astakos", with the following methods:
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):
LOG_DATA = False
def __init__(self, base_url, token):
assert base_url, 'No base_url for client %s' % self
self.base_url = base_url
self.token = token
self.headers, self.params = dict(), dict()
......
......@@ -32,14 +32,18 @@
# or implied, of GRNET S.A.
from kamaki.clients import Client, ClientError
from logging import getLogger
class AstakosClient(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)
self._cache = {}
self.log = getLogger('__name__')
def authenticate(self, token=None):
"""Get authentication information and store it in this client
......@@ -51,28 +55,88 @@ class AstakosClient(Client):
:returns: (dict) authentication information
"""
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]
def list(self):
"""list cached user information"""
def get_services(self, token=None):
"""
: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 = []
for k, v in self._cache.items():
r.append(dict(v))
r.append(dict(v['user']))
r[-1].update(dict(auth_token=k))
return r
def info(self, token=None):
def user_info(self, token=None):
"""Get (cached) user information"""
token_bu = self.token
token_bu = self.token or token
token = token or self.token
try:
r = self._cache[token]
except KeyError:
r = self.authenticate(token)
self.token = token_bu
return r
finally:
self.token = token_bu
return r['user']
def term(self, key, token=None):
"""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 @@
from mock import patch, call
from unittest import TestCase
from kamaki.clients import astakos
from kamaki.clients import astakos, ClientError
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',
username='User Full Name',
auth_token_expires='1362583796000',
......@@ -45,15 +54,26 @@ example = dict(
email=['user@example.gr'],
id=42,
uuid='aus3r-uu1d-f0r-73s71ng-as7ak0s')
)
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',
username='User Full Name 0',
auth_token_expires='1362583796001',
auth_token_created='1359991796001',
auth_token_expires='1362585796000',
auth_token_created='1359931796000',
email=['user0@example.gr'],
id=32,
uuid='an07h2r-us3r-uu1d-f0r-as7ak0s')
id=42,
uuid='aus3r-uu1d-507-73s71ng-as7ak0s')
)
class FR(object):
......@@ -70,6 +90,14 @@ class AstakosClient(TestCase):
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):
self.url = 'https://astakos.example.com'
self.token = 'ast@k0sT0k3n=='
......@@ -81,37 +109,62 @@ class AstakosClient(TestCase):
@patch('%s.get' % astakos_pkg, return_value=FR())
def _authenticate(self, get):
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
return r
def test_authenticate(self):
r = self._authenticate()
for term, val in example.items():
self.assertTrue(term in r)
self.assertEqual(val, r[term])
self.assert_dicts_are_equal(r, example)
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:
self._authenticate()
return self.test_info()
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:
self._authenticate()
return self.test_get()
for term, val in example.items():
for term, val in example['user'].items():
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:
self._authenticate
FR.json = example0
self._authenticate()
r = self.client.list()
r = self.client.list_users()
self.assertTrue(len(r) == 1)
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