Merge pull request #96 from bcwaldon/lazyload-limit
Limit resource lazyloading
This commit is contained in:
commit
597ef2f5b6
@ -47,7 +47,7 @@ copyright = u'Rackspace, based on work by Jacob Kaplan-Moss'
|
||||
# The short X.Y version.
|
||||
version = '2.6'
|
||||
# The full version, including alpha/beta/rc tags.
|
||||
release = '2.6.2'
|
||||
release = '2.6.3'
|
||||
|
||||
# The language for content autogenerated by Sphinx. Refer to documentation
|
||||
# for a list of supported languages.
|
||||
|
@ -67,12 +67,13 @@ class Manager(object):
|
||||
|
||||
if obj_class is None:
|
||||
obj_class = self.resource_class
|
||||
|
||||
data = body[response_key]
|
||||
# NOTE(ja): keystone returns values as list as {'values': [ ... ]}
|
||||
# unlike other services which just return the list...
|
||||
if type(data) is dict:
|
||||
data = data['values']
|
||||
return [obj_class(self, res) for res in data if res]
|
||||
return [obj_class(self, res, loaded=True) for res in data if res]
|
||||
|
||||
def _get(self, url, response_key):
|
||||
resp, body = self.api.client.get(url)
|
||||
@ -203,19 +204,28 @@ 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
|
||||
"""
|
||||
def __init__(self, manager, info):
|
||||
def __init__(self, manager, info, loaded=False):
|
||||
self.manager = manager
|
||||
self._info = info
|
||||
self._add_details(info)
|
||||
self._loaded = loaded
|
||||
|
||||
def _add_details(self, info):
|
||||
for (k, v) in info.iteritems():
|
||||
setattr(self, k, v)
|
||||
|
||||
def __getattr__(self, k):
|
||||
self.get()
|
||||
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]
|
||||
@ -229,6 +239,9 @@ class Resource(object):
|
||||
def get(self):
|
||||
if not hasattr(self.manager, 'get'):
|
||||
return
|
||||
|
||||
self.set_loaded(True)
|
||||
|
||||
new = self.manager.get(self.id)
|
||||
if new:
|
||||
self._add_details(new._info)
|
||||
@ -239,3 +252,9 @@ class Resource(object):
|
||||
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
|
||||
|
@ -35,11 +35,11 @@ class Weighting(base.Resource):
|
||||
|
||||
|
||||
class Zone(base.Resource):
|
||||
def __init__(self, manager, info):
|
||||
def __init__(self, manager, info, loaded=False):
|
||||
self.name = "n/a"
|
||||
self.is_active = "n/a"
|
||||
self.capabilities = "n/a"
|
||||
super(Zone, self).__init__(manager, info)
|
||||
super(Zone, self).__init__(manager, info, loaded)
|
||||
|
||||
def __repr__(self):
|
||||
return "<Zone: %s>" % self.api_url
|
||||
|
@ -15,117 +15,11 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
"""
|
||||
Base utilities to build API operation managers and objects on top of.
|
||||
"""
|
||||
|
||||
from novaclient import base
|
||||
from novaclient import exceptions
|
||||
|
||||
# Python 2.4 compat
|
||||
try:
|
||||
all
|
||||
except NameError:
|
||||
def all(iterable):
|
||||
return True not in (not x for x in iterable)
|
||||
|
||||
|
||||
def getid(obj):
|
||||
"""
|
||||
Abstracts the common pattern of allowing both an object or an object's ID
|
||||
(UUID) as a parameter when dealing with relationships.
|
||||
"""
|
||||
|
||||
# Try to return the object's UUID first, if we have a UUID.
|
||||
try:
|
||||
if obj.uuid:
|
||||
return obj.uuid
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
return obj.id
|
||||
except AttributeError:
|
||||
return obj
|
||||
|
||||
|
||||
class Manager(object):
|
||||
"""
|
||||
Managers interact with a particular type of API (servers, flavors, images,
|
||||
etc.) and provide CRUD operations for them.
|
||||
"""
|
||||
resource_class = None
|
||||
|
||||
def __init__(self, api):
|
||||
self.api = api
|
||||
|
||||
def _list(self, url, response_key, obj_class=None, body=None):
|
||||
resp = None
|
||||
if body:
|
||||
resp, body = self.api.client.post(url, body=body)
|
||||
else:
|
||||
resp, body = self.api.client.get(url)
|
||||
|
||||
if obj_class is None:
|
||||
obj_class = self.resource_class
|
||||
return [obj_class(self, res)
|
||||
for res in body[response_key] if res]
|
||||
|
||||
def _get(self, url, response_key):
|
||||
resp, body = self.api.client.get(url)
|
||||
return self.resource_class(self, body[response_key])
|
||||
|
||||
def _create(self, url, body, response_key, return_raw=False):
|
||||
resp, body = self.api.client.post(url, body=body)
|
||||
if return_raw:
|
||||
return body[response_key]
|
||||
return self.resource_class(self, body[response_key])
|
||||
|
||||
def _delete(self, url):
|
||||
resp, body = self.api.client.delete(url)
|
||||
|
||||
def _update(self, url, body):
|
||||
resp, body = self.api.client.put(url, body=body)
|
||||
|
||||
|
||||
class ManagerWithFind(Manager):
|
||||
"""
|
||||
Like a `Manager`, but with additional `find()`/`findall()` methods.
|
||||
"""
|
||||
def find(self, **kwargs):
|
||||
"""
|
||||
Find a single item with attributes matching ``**kwargs``.
|
||||
|
||||
This isn't very efficient: it loads the entire list then filters on
|
||||
the Python side.
|
||||
"""
|
||||
rl = self.findall(**kwargs)
|
||||
try:
|
||||
return rl[0]
|
||||
except IndexError:
|
||||
raise exceptions.NotFound(404, "No %s matching %s." %
|
||||
(self.resource_class.__name__, kwargs))
|
||||
|
||||
def findall(self, **kwargs):
|
||||
"""
|
||||
Find all items with attributes matching ``**kwargs``.
|
||||
|
||||
This isn't very efficient: it loads the entire list then filters on
|
||||
the Python side.
|
||||
"""
|
||||
found = []
|
||||
searches = kwargs.items()
|
||||
|
||||
for obj in self.list():
|
||||
try:
|
||||
if all(getattr(obj, attr) == value
|
||||
for (attr, value) in searches):
|
||||
found.append(obj)
|
||||
except AttributeError:
|
||||
continue
|
||||
|
||||
return found
|
||||
|
||||
|
||||
class BootingManagerWithFind(ManagerWithFind):
|
||||
class BootingManagerWithFind(base.ManagerWithFind):
|
||||
"""Like a `ManagerWithFind`, but has the ability to boot servers."""
|
||||
def _boot(self, resource_url, response_key, name, image, flavor,
|
||||
meta=None, files=None, zone_blob=None,
|
||||
@ -155,8 +49,8 @@ class BootingManagerWithFind(ManagerWithFind):
|
||||
"""
|
||||
body = {"server": {
|
||||
"name": name,
|
||||
"imageRef": getid(image),
|
||||
"flavorRef": getid(flavor),
|
||||
"imageRef": base.getid(image),
|
||||
"flavorRef": base.getid(flavor),
|
||||
}}
|
||||
if meta:
|
||||
body["server"]["metadata"] = meta
|
||||
@ -194,43 +88,3 @@ class BootingManagerWithFind(ManagerWithFind):
|
||||
|
||||
return self._create(resource_url, body, response_key,
|
||||
return_raw=return_raw)
|
||||
|
||||
|
||||
class Resource(object):
|
||||
"""
|
||||
A resource represents a particular instance of an object (server, flavor,
|
||||
etc). This is pretty much just a bag for attributes.
|
||||
"""
|
||||
def __init__(self, manager, info):
|
||||
self.manager = manager
|
||||
self._info = info
|
||||
self._add_details(info)
|
||||
|
||||
def _add_details(self, info):
|
||||
for (k, v) in info.iteritems():
|
||||
setattr(self, k, v)
|
||||
|
||||
def __getattr__(self, k):
|
||||
self.get()
|
||||
if k not in self.__dict__:
|
||||
raise AttributeError(k)
|
||||
else:
|
||||
return self.__dict__[k]
|
||||
|
||||
def __repr__(self):
|
||||
reprkeys = sorted(k for k in self.__dict__.keys() 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):
|
||||
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
|
||||
|
@ -27,7 +27,7 @@ class Client(object):
|
||||
|
||||
"""
|
||||
|
||||
# FIXME(jesse): project_id isn't required to autenticate
|
||||
# FIXME(jesse): project_id isn't required to authenticate
|
||||
def __init__(self, username, api_key, project_id, auth_url, timeout=None):
|
||||
self.flavors = flavors.FlavorManager(self)
|
||||
self.floating_ips = floating_ips.FloatingIPManager(self)
|
||||
|
@ -13,7 +13,7 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from novaclient.v1_1 import base
|
||||
from novaclient import base
|
||||
|
||||
|
||||
class FloatingIP(base.Resource):
|
||||
|
@ -17,7 +17,8 @@
|
||||
Zone interface.
|
||||
"""
|
||||
|
||||
from novaclient.v1_1 import base
|
||||
from novaclient import base
|
||||
from novaclient.v1_1 import base as local_base
|
||||
|
||||
|
||||
class Weighting(base.Resource):
|
||||
@ -34,11 +35,11 @@ class Weighting(base.Resource):
|
||||
|
||||
|
||||
class Zone(base.Resource):
|
||||
def __init__(self, manager, info):
|
||||
def __init__(self, manager, info, loaded=False):
|
||||
self.name = "n/a"
|
||||
self.is_active = "n/a"
|
||||
self.capabilities = "n/a"
|
||||
super(Zone, self).__init__(manager, info)
|
||||
super(Zone, self).__init__(manager, info, loaded)
|
||||
|
||||
def __repr__(self):
|
||||
return "<Zone: %s>" % self.api_url
|
||||
@ -64,7 +65,7 @@ class Zone(base.Resource):
|
||||
weight_offset, weight_scale)
|
||||
|
||||
|
||||
class ZoneManager(base.BootingManagerWithFind):
|
||||
class ZoneManager(local_base.BootingManagerWithFind):
|
||||
resource_class = Zone
|
||||
|
||||
def info(self):
|
||||
|
2
setup.py
2
setup.py
@ -12,7 +12,7 @@ if sys.version_info < (2, 6):
|
||||
|
||||
setup(
|
||||
name = "python-novaclient",
|
||||
version = "2.6.2",
|
||||
version = "2.6.3",
|
||||
description = "Client library for OpenStack Nova API",
|
||||
long_description = read('README.rst'),
|
||||
url = 'https://github.com/rackspace/python-novaclient',
|
||||
|
@ -30,7 +30,6 @@ class BaseTest(utils.TestCase):
|
||||
|
||||
# Missing stuff still fails after a second get
|
||||
self.assertRaises(AttributeError, getattr, f, 'blahblah')
|
||||
cs.assert_called('GET', '/flavors/1')
|
||||
|
||||
def test_eq(self):
|
||||
# Two resources of the same type with the same id: equal
|
||||
|
Loading…
x
Reference in New Issue
Block a user