From b5e36ced8fbae0b93cd713b1cf47c7cc877837b8 Mon Sep 17 00:00:00 2001 From: Matthew Gilliard Date: Thu, 18 Sep 2014 14:32:45 +0000 Subject: [PATCH] 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 --- novaclient/tests/v1_1/test_hypervisors.py | 8 ++++++++ novaclient/v1_1/client.py | 1 + novaclient/v1_1/hypervisors.py | 19 +++++++++++++++++++ novaclient/v1_1/shell.py | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/novaclient/tests/v1_1/test_hypervisors.py b/novaclient/tests/v1_1/test_hypervisors.py index 94bbb0838..1a6212015 100644 --- a/novaclient/tests/v1_1/test_hypervisors.py +++ b/novaclient/tests/v1_1/test_hypervisors.py @@ -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("", + result.__repr__()) diff --git a/novaclient/v1_1/client.py b/novaclient/v1_1/client.py index 5e26ddd64..83789aa6b 100644 --- a/novaclient/v1_1/client.py +++ b/novaclient/v1_1/client.py @@ -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) diff --git a/novaclient/v1_1/hypervisors.py b/novaclient/v1_1/hypervisors.py index 4a4f2b93a..b1bfcb4b9 100644 --- a/novaclient/v1_1/hypervisors.py +++ b/novaclient/v1_1/hypervisors.py @@ -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 ("" % + (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. diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index 1463f5fd4..9eaca1177 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -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())