diff options
author | Andrey Kurilin <akurilin@mirantis.com> | 2014-01-08 11:38:47 +0200 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2014-04-04 07:36:06 +0000 |
commit | 14e244f0e90a839857ca6b8c25f950463ac75f1f (patch) | |
tree | 6ed72ddf90862b8b796ac295690b64dc24f74d42 | |
parent | 6e073020784f054e4a0558353f20bad2688c1da5 (diff) | |
download | python-cinderclient-14e244f0e90a839857ca6b8c25f950463ac75f1f.tar.gz |
Reuse Resource from oslo
Class Resource from oslo is equal to code from cinderclient.
In the process of unification of the clients code we should
reuse common functionality from Oslo.
Related to blueprint common-client-library-2
Change-Id: If39e49ff739d659d145725966192acf81975c90c
-rw-r--r-- | cinderclient/base.py | 89 |
1 files changed, 4 insertions, 85 deletions
diff --git a/cinderclient/base.py b/cinderclient/base.py index 280286c..5274f24 100644 --- a/cinderclient/base.py +++ b/cinderclient/base.py @@ -26,9 +26,13 @@ import os import six from cinderclient import exceptions +from cinderclient.openstack.common.apiclient import base as common_base from cinderclient import utils +Resource = common_base.Resource + + # Python 2.4 compat try: all @@ -215,88 +219,3 @@ class ManagerWithFind(six.with_metaclass(abc.ABCMeta, Manager)): continue return found - - -class Resource(object): - """ - A resource represents a particular instance of an object (server, flavor, - etc). This is pretty much just a bag for attributes. - - :param manager: Manager object - :param info: dictionary representing resource attributes - :param loaded: prevent lazy-loading if set to True - """ - HUMAN_ID = False - - def __init__(self, manager, info, loaded=False): - self.manager = manager - self._info = info - self._add_details(info) - self._loaded = loaded - - # NOTE(sirp): ensure `id` is already present because if it isn't we'll - # enter an infinite loop of __getattr__ -> get -> __init__ -> - # __getattr__ -> ... - if 'id' in self.__dict__ and len(str(self.id)) == 36: - self.manager.write_to_completion_cache('uuid', self.id) - - human_id = self.human_id - if human_id: - self.manager.write_to_completion_cache('human_id', human_id) - - @property - def human_id(self): - """Subclasses may override this provide a pretty ID which can be used - for bash completion. - """ - if 'name' in self.__dict__ and self.HUMAN_ID: - return utils.slugify(self.name) - return None - - def _add_details(self, info): - for (k, v) in six.iteritems(info): - try: - setattr(self, k, v) - except AttributeError: - # In this case we already defined the attribute on the class - pass - - def __getattr__(self, k): - if k not in self.__dict__: - #NOTE(bcwaldon): disallow lazy-loading if already loaded once - if not self.is_loaded(): - self.get() - return self.__getattr__(k) - - raise AttributeError(k) - else: - return self.__dict__[k] - - def __repr__(self): - reprkeys = sorted(k for k in self.__dict__ if k[0] != '_' - and k != 'manager') - info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys) - return "<%s %s>" % (self.__class__.__name__, info) - - def get(self): - # set_loaded() first ... so if we have to bail, we know we tried. - self.set_loaded(True) - if not hasattr(self.manager, 'get'): - return - - new = self.manager.get(self.id) - if new: - self._add_details(new._info) - - def __eq__(self, other): - if not isinstance(other, self.__class__): - return False - if hasattr(self, 'id') and hasattr(other, 'id'): - return self.id == other.id - return self._info == other._info - - def is_loaded(self): - return self._loaded - - def set_loaded(self, val): - self._loaded = val |