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)