Adds separate class for Hypervisor Stats

Hypervisor stats was being called from the Hypervisors class, which means that
the statistics were being modeled as if they were a single Hypervisor. This
mostly worked, except that the stats didn't have an id field so a call to
__repr__() (implicitly called by print, or in the REPL) would throw an
AttributeError.

This patch creates a new class HypervisorStats which models a collection of
statistics about hypervisors. So you can now call:

  nc.hypervisor_stats.statistics()

The old call of

  nc.hypervisors.statistics()

is left for backward compatibility but just calls into the new method.

Change-Id: Ia31aacb95b1d517dab3ad38763d6448715bab68e
Closes-bug: 1370415
This commit is contained in:
Matthew Gilliard 2014-09-18 14:32:45 +00:00
parent 7bced6673c
commit b5e36ced8f
4 changed files with 29 additions and 1 deletions

@ -168,3 +168,11 @@ class HypervisorsTest(utils.FixturedTestCase):
self.assert_called('GET', '/os-hypervisors/statistics')
self.compare_to_expected(expected, result)
def test_hypervisor_statistics_data_model(self):
result = self.cs.hypervisor_stats.statistics()
self.assert_called('GET', '/os-hypervisors/statistics')
# Test for Bug #1370415, the line below used to raise AttributeError
self.assertEqual("<HypervisorStats: 2 Hypervisors>",
result.__repr__())

@ -151,6 +151,7 @@ class Client(object):
self.aggregates = aggregates.AggregateManager(self)
self.hosts = hosts.HostManager(self)
self.hypervisors = hypervisors.HypervisorManager(self)
self.hypervisor_stats = hypervisors.HypervisorStatsManager(self)
self.services = services.ServiceManager(self)
self.fixed_ips = fixed_ips.FixedIPsManager(self)
self.floating_ips_bulk = floating_ips_bulk.FloatingIPBulkManager(self)

@ -66,6 +66,25 @@ class HypervisorManager(base.ManagerWithFind):
return self._get("/os-hypervisors/%s/uptime" % base.getid(hypervisor),
"hypervisor")
def statistics(self):
"""
Get hypervisor statistics over all compute nodes.
Kept for backwards compatibility, new code should call
hypervisor_stats.statistics() instead of hypervisors.statistics()
"""
return self.api.hypervisor_stats.statistics()
class HypervisorStats(base.Resource):
def __repr__(self):
return ("<HypervisorStats: %d Hypervisor%s>" %
(self.count, "s" if self.count != 1 else ""))
class HypervisorStatsManager(base.Manager):
resource_class = HypervisorStats
def statistics(self):
"""
Get hypervisor statistics over all compute nodes.

@ -3629,7 +3629,7 @@ def do_hypervisor_uptime(cs, args):
def do_hypervisor_stats(cs, args):
"""Get hypervisor statistics over all compute nodes."""
stats = cs.hypervisors.statistics()
stats = cs.hypervisor_stats.statistics()
utils.print_dict(stats._info.copy())