diff options
author | Doug Hellmann <doug.hellmann@dreamhost.com> | 2013-07-02 17:12:35 -0400 |
---|---|---|
committer | Doug Hellmann <doug.hellmann@dreamhost.com> | 2013-07-02 20:58:40 -0400 |
commit | 4437fb5f0b1820031d396a9ba39d50725d81d1eb (patch) | |
tree | c52935e36f377fa7210e491696166d13ba7243cd | |
parent | a10773e41388140126c38baad7bf88f909736923 (diff) | |
download | ceilometer-4437fb5f0b1820031d396a9ba39d50725d81d1eb.tar.gz |
Update glance pollster to use cache
Save the images being scanned by the glance
pollster to the cache.
blueprint one-meter-per-plugin
Change-Id: I7fc808e7b6f0c0ca93a10319574cbb865a69a424
Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
-rw-r--r-- | ceilometer/image/glance.py | 11 | ||||
-rw-r--r-- | tests/image/test_glance.py | 13 |
2 files changed, 19 insertions, 5 deletions
diff --git a/ceilometer/image/glance.py b/ceilometer/image/glance.py index e96a9a23..9efe2559 100644 --- a/ceilometer/image/glance.py +++ b/ceilometer/image/glance.py @@ -40,8 +40,7 @@ class _Base(plugin.PollsterBase): return glanceclient.Client('1', endpoint, token=ksclient.auth_token) - def iter_images(self, ksclient): - """Iterate over all images.""" + def _get_images(self, ksclient): client = self.get_glance_client(ksclient) #TODO(eglynn): use pagination to protect against unbounded # memory usage @@ -70,6 +69,12 @@ class _Base(plugin.PollsterBase): imageIdSet -= set([image.id]) yield image + def _iter_images(self, ksclient, cache): + """Iterate over all images.""" + if 'images' not in cache: + cache['images'] = list(self._get_images(ksclient)) + return iter(cache['images']) + @staticmethod def extract_image_metadata(image): return dict((k, getattr(image, k)) @@ -100,7 +105,7 @@ class ImagePollster(_Base): return ['image', 'image.size'] def get_counters(self, manager, cache): - for image in self.iter_images(manager.keystone): + for image in self._iter_images(manager.keystone, cache): yield counter.Counter( name='image', type=counter.TYPE_GAUGE, diff --git a/tests/image/test_glance.py b/tests/image/test_glance.py index f1618b8b..6d00f03a 100644 --- a/tests/image/test_glance.py +++ b/tests/image/test_glance.py @@ -133,12 +133,21 @@ class TestImagePollster(base.TestCase): self.stubs.Set(glance._Base, 'get_glance_client', self.fake_get_glance_client) - # Tests whether the iter_images method returns an unique image list. def test_iter_images(self): + # Tests whether the iter_images method returns an unique image + # list when there is nothing in the cache images = list(glance.ImagePollster(). - iter_images(self.manager.keystone)) + _iter_images(self.manager.keystone, {})) self.assertEqual(len(images), len(set(image.id for image in images))) + def test_iter_images_cached(self): + # Tests whether the iter_images method returns the values from + # the cache + cache = {'images': []} + images = list(glance.ImagePollster(). + _iter_images(self.manager.keystone, cache)) + self.assertEqual(images, []) + def test_glance_image_counter(self): counters = list(glance.ImagePollster().get_counters(self.manager, {})) self.assertEqual(len(counters), 6) |