diff options
| author | Boris Bobrov <bbobrov@mirantis.com> | 2016-02-15 14:14:49 +0300 |
|---|---|---|
| committer | Steve Martinelli <stevemar@ca.ibm.com> | 2016-02-26 00:06:09 +0000 |
| commit | 870be44c0ed5564101f9cf9d53e8085fb01680c8 (patch) | |
| tree | 981c230626ca7c4393a5b9283f0819934ddbb136 /keystoneclient/base.py | |
| parent | aeb69f3b6d8f35cd4956e1fe03d1ef9b2cd69bf0 (diff) | |
| download | python-keystoneclient-870be44c0ed5564101f9cf9d53e8085fb01680c8.tar.gz | |
Support `truncated` flag returned by identity service
Create a custom list with flag `truncated` to support corresponding
response from the identity service.
This is wanted by Horizon, that wants to know that the list returned
from keystone is not full and that more strict filters need to be
applied.
The previous attempt in commit c28d40814962b3a8ccb81e5e7d7f832c8f0a3c9a
was reverted by d20b300589863bcf165945beb129ebcc3621a14f because it
broke other code. This commit changes the way the flag is added and
verifies that existing code will not break.
Change-Id: Ia86cfd91110adae6d7ab86ff1f152a8f9be27837
Closes-Bug: 1520244
Diffstat (limited to 'keystoneclient/base.py')
| -rw-r--r-- | keystoneclient/base.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/keystoneclient/base.py b/keystoneclient/base.py index 3273ecb..d4fdc9e 100644 --- a/keystoneclient/base.py +++ b/keystoneclient/base.py @@ -76,6 +76,36 @@ def filter_kwargs(f): return func +class TruncatedList(list): + """List with attribute `truncated`. + + The main purpose of this class is to handle flag `truncated` returned + by Identity Service. It subclasses standard Python list and overrides + only equality operators. + + :param bool truncated: whether the list is truncated or not. + """ + def __init__(self, collection, truncated=False): + super(TruncatedList, self).__init__(collection) + self.truncated = truncated + + def __eq__(self, other): + """Compare this list with another one. + + Two TruncatedLists are equal if the lists they carry are equal + and their attributes `truncated` are equal. + + If another value has not attribute `truncated`, it is assumed to + be False. + """ + values_eq = super(TruncatedList, self).__eq__(other) + truncated_eq = self.truncated == getattr(other, 'truncated', False) + return values_eq and truncated_eq + + def __ne__(self, other): + return not self.__eq__(other) + + class Manager(object): """Basic manager type providing common operations. @@ -117,6 +147,8 @@ class Manager(object): :param body: data that will be encoded as JSON and passed in POST request (GET will be sent by default) :param kwargs: Additional arguments will be passed to the request. + :returns: list of objects with indication of truncation + :rtype: :py:class:`keystoneclient.base.TruncatedList` """ if body: resp, body = self.client.post(url, body=body, **kwargs) @@ -127,6 +159,7 @@ class Manager(object): obj_class = self.resource_class data = body[response_key] + truncated = body.get('truncated', False) # NOTE(ja): keystone returns values as list as {'values': [ ... ]} # unlike other services which just return the list... try: @@ -134,7 +167,8 @@ class Manager(object): except (KeyError, TypeError): pass - return [obj_class(self, res, loaded=True) for res in data if res] + objects = [obj_class(self, res, loaded=True) for res in data if res] + return TruncatedList(objects, truncated=truncated) def _get(self, url, response_key, **kwargs): """Get an object from collection. |
