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 <doug.hellmann@dreamhost.com>
This commit is contained in:
Doug Hellmann 2013-07-02 17:13:26 -04:00
parent 4437fb5f0b
commit df62b92191
2 changed files with 16 additions and 2 deletions

View File

@ -35,9 +35,17 @@ class FloatingIPPollster(plugin.CentralPollster):
def get_counter_names(): def get_counter_names():
return ['ip.floating'] return ['ip.floating']
def get_counters(self, manager, cache): def _get_floating_ips(self):
nv = nova_client.Client() 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) self.LOG.info("FLOATING IP USAGE: %s" % ip.ip)
# FIXME (flwang) Now Nova API /os-floating-ips can't provide those # FIXME (flwang) Now Nova API /os-floating-ips can't provide those
# attributes were used by Ceilometer, such as project id, host. # attributes were used by Ceilometer, such as project id, host.

View File

@ -85,3 +85,9 @@ class TestFloatingIPPollster(base.TestCase):
counters = list(self.pollster.get_counters(self.manager, {})) counters = list(self.pollster.get_counters(self.manager, {}))
self.assertEqual(set([c.name for c in counters]), self.assertEqual(set([c.name for c in counters]),
set(self.pollster.get_counter_names())) 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)