From df62b92191cd0eb51294cee8df9a731d8d92b4a6 Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Tue, 2 Jul 2013 17:13:26 -0400 Subject: [PATCH] Update floating-ip pollster to use cache Use the pollster cache to store the list of floating IPs. blueprint one-meter-per-plugin Change-Id: I05da484a3336a59f1ceab335028aa8149f636019 Signed-off-by: Doug Hellmann --- ceilometer/network/floatingip.py | 12 ++++++++++-- tests/network/test_floatingip.py | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ceilometer/network/floatingip.py b/ceilometer/network/floatingip.py index 41ca6a200..5e0cc0dbd 100644 --- a/ceilometer/network/floatingip.py +++ b/ceilometer/network/floatingip.py @@ -35,9 +35,17 @@ class FloatingIPPollster(plugin.CentralPollster): def get_counter_names(): return ['ip.floating'] - def get_counters(self, manager, cache): + def _get_floating_ips(self): nv = nova_client.Client() - for ip in nv.floating_ip_get_all(): + return nv.floating_ip_get_all() + + def _iter_floating_ips(self, cache): + if 'floating_ips' not in cache: + cache['floating_ips'] = list(self._get_floating_ips()) + return iter(cache['floating_ips']) + + def get_counters(self, manager, cache): + for ip in self._iter_floating_ips(cache): self.LOG.info("FLOATING IP USAGE: %s" % ip.ip) # FIXME (flwang) Now Nova API /os-floating-ips can't provide those # attributes were used by Ceilometer, such as project id, host. diff --git a/tests/network/test_floatingip.py b/tests/network/test_floatingip.py index e54a49437..e7d3c94f0 100644 --- a/tests/network/test_floatingip.py +++ b/tests/network/test_floatingip.py @@ -85,3 +85,9 @@ class TestFloatingIPPollster(base.TestCase): counters = list(self.pollster.get_counters(self.manager, {})) self.assertEqual(set([c.name for c in counters]), set(self.pollster.get_counter_names())) + + def test_get_counters_cached(self): + cache = {} + cache['floating_ips'] = self.faux_get_ips(None)[:2] + counters = list(self.pollster.get_counters(self.manager, cache)) + self.assertEqual(len(counters), 2)