Merge "Revert "Support truncated
flag returned by keystone""
This commit is contained in:
@@ -20,7 +20,6 @@ Base utilities to build API operation managers and objects on top of.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import abc
|
import abc
|
||||||
import collections
|
|
||||||
import copy
|
import copy
|
||||||
import functools
|
import functools
|
||||||
import warnings
|
import warnings
|
||||||
@@ -77,23 +76,6 @@ def filter_kwargs(f):
|
|||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
class KeystoneReturnedList(collections.Sequence):
|
|
||||||
"""A list of entities with additional attributes."""
|
|
||||||
|
|
||||||
def __init__(self, collection, truncated=False):
|
|
||||||
self.collection = collection
|
|
||||||
self.truncated = truncated
|
|
||||||
|
|
||||||
def __getitem__(self, i):
|
|
||||||
return self.collection[i]
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.collection)
|
|
||||||
|
|
||||||
def sort(self, *args, **kwargs):
|
|
||||||
return self.collection.sort(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
class Manager(object):
|
class Manager(object):
|
||||||
"""Basic manager type providing common operations.
|
"""Basic manager type providing common operations.
|
||||||
|
|
||||||
@@ -145,7 +127,6 @@ class Manager(object):
|
|||||||
obj_class = self.resource_class
|
obj_class = self.resource_class
|
||||||
|
|
||||||
data = body[response_key]
|
data = body[response_key]
|
||||||
truncated = body.get('truncated', False)
|
|
||||||
# NOTE(ja): keystone returns values as list as {'values': [ ... ]}
|
# NOTE(ja): keystone returns values as list as {'values': [ ... ]}
|
||||||
# unlike other services which just return the list...
|
# unlike other services which just return the list...
|
||||||
try:
|
try:
|
||||||
@@ -153,8 +134,7 @@ class Manager(object):
|
|||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
objects = [obj_class(self, res, loaded=True) for res in data if res]
|
return [obj_class(self, res, loaded=True) for res in data if res]
|
||||||
return KeystoneReturnedList(objects, truncated=truncated)
|
|
||||||
|
|
||||||
def _get(self, url, response_key, **kwargs):
|
def _get(self, url, response_key, **kwargs):
|
||||||
"""Get an object from collection.
|
"""Get an object from collection.
|
||||||
|
@@ -195,16 +195,11 @@ class CrudTests(object):
|
|||||||
kwargs.setdefault(uuid.uuid4().hex, uuid.uuid4().hex)
|
kwargs.setdefault(uuid.uuid4().hex, uuid.uuid4().hex)
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def encode(self, entity, truncated=None):
|
def encode(self, entity):
|
||||||
encoded = {}
|
|
||||||
if truncated is not None:
|
|
||||||
encoded['truncated'] = truncated
|
|
||||||
if isinstance(entity, dict):
|
if isinstance(entity, dict):
|
||||||
encoded[self.key] = entity
|
return {self.key: entity}
|
||||||
return encoded
|
|
||||||
if isinstance(entity, list):
|
if isinstance(entity, list):
|
||||||
encoded[self.collection_key] = entity
|
return {self.collection_key: entity}
|
||||||
return encoded
|
|
||||||
raise NotImplementedError('Are you sure you want to encode that?')
|
raise NotImplementedError('Are you sure you want to encode that?')
|
||||||
|
|
||||||
def stub_entity(self, method, parts=None, entity=None, id=None, **kwargs):
|
def stub_entity(self, method, parts=None, entity=None, id=None, **kwargs):
|
||||||
@@ -295,22 +290,14 @@ class CrudTests(object):
|
|||||||
|
|
||||||
self.assertRaises(TypeError, self.manager.list, **filter_kwargs)
|
self.assertRaises(TypeError, self.manager.list, **filter_kwargs)
|
||||||
|
|
||||||
def _test_list(self, ref_list=None, expected_path=None,
|
def test_list(self, ref_list=None, expected_path=None,
|
||||||
expected_query=None, truncated=None, **filter_kwargs):
|
expected_query=None, **filter_kwargs):
|
||||||
ref_list = ref_list or [self.new_ref(), self.new_ref()]
|
ref_list = ref_list or [self.new_ref(), self.new_ref()]
|
||||||
expected_path = self._get_expected_path(expected_path)
|
expected_path = self._get_expected_path(expected_path)
|
||||||
|
|
||||||
# We want to catch all cases: when `truncated` is not returned by the
|
|
||||||
# server, when it's False and when it's True.
|
|
||||||
# Attribute `truncated` of the returned list-like object should exist
|
|
||||||
# in all these cases. It should be False if the server returned a list
|
|
||||||
# without the flag.
|
|
||||||
expected_truncated = False
|
|
||||||
if truncated:
|
|
||||||
expected_truncated = truncated
|
|
||||||
|
|
||||||
self.requests_mock.get(urlparse.urljoin(self.TEST_URL, expected_path),
|
self.requests_mock.get(urlparse.urljoin(self.TEST_URL, expected_path),
|
||||||
json=self.encode(ref_list, truncated=truncated))
|
json=self.encode(ref_list))
|
||||||
|
|
||||||
returned_list = self.manager.list(**filter_kwargs)
|
returned_list = self.manager.list(**filter_kwargs)
|
||||||
self.assertEqual(len(ref_list), len(returned_list))
|
self.assertEqual(len(ref_list), len(returned_list))
|
||||||
[self.assertIsInstance(r, self.model) for r in returned_list]
|
[self.assertIsInstance(r, self.model) for r in returned_list]
|
||||||
@@ -329,20 +316,6 @@ class CrudTests(object):
|
|||||||
for key in qs_args:
|
for key in qs_args:
|
||||||
self.assertIn(key, qs_args_expected)
|
self.assertIn(key, qs_args_expected)
|
||||||
|
|
||||||
self.assertEqual(expected_truncated, returned_list.truncated)
|
|
||||||
|
|
||||||
def test_list(self, ref_list=None, expected_path=None,
|
|
||||||
expected_query=None, **filter_kwargs):
|
|
||||||
# test simple list, without any truncation
|
|
||||||
self._test_list(ref_list, expected_path, expected_query,
|
|
||||||
**filter_kwargs)
|
|
||||||
# test when a server returned a list with truncated=False
|
|
||||||
self._test_list(ref_list, expected_path, expected_query,
|
|
||||||
truncated=False, **filter_kwargs)
|
|
||||||
# test when a server returned a list with truncated=True
|
|
||||||
self._test_list(ref_list, expected_path, expected_query,
|
|
||||||
truncated=True, **filter_kwargs)
|
|
||||||
|
|
||||||
def test_list_params(self):
|
def test_list_params(self):
|
||||||
ref_list = [self.new_ref()]
|
ref_list = [self.new_ref()]
|
||||||
filter_kwargs = {uuid.uuid4().hex: uuid.uuid4().hex}
|
filter_kwargs = {uuid.uuid4().hex: uuid.uuid4().hex}
|
||||||
|
Reference in New Issue
Block a user