From c3dda7636cb7cf1d613c027703b88b41144dec32 Mon Sep 17 00:00:00 2001 From: melanie witt Date: Sat, 21 Nov 2015 00:15:21 +0000 Subject: [PATCH] Fix repr of a host from a hosts.list() The os-hosts API uses different attribute names depending on whether the host was returned as part of a list or not. A host returned from 'show host' has the attribute "host" whereas a host returned from 'list hosts' has the attribute "host_name". This adds a host_name property to the Host class that will set the "host" attribute if necessary. Although this doesn't exactly mirror the responses coming back from nova api, it will make it easier for users to use the objects interchangeably for hosts.list() operations and hosts.update() operations, for example. Co-Authored-By: Chung Chih, Hung Closes-Bug: #1434167 Change-Id: I5c339bdd1ab867d972759ade9a10b86bfda1e70a --- novaclient/tests/unit/fixture_data/hosts.py | 4 ++-- novaclient/tests/unit/v2/test_hosts.py | 5 +++++ novaclient/v2/hosts.py | 12 ++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/novaclient/tests/unit/fixture_data/hosts.py b/novaclient/tests/unit/fixture_data/hosts.py index 28ff17ef1..160eb3989 100644 --- a/novaclient/tests/unit/fixture_data/hosts.py +++ b/novaclient/tests/unit/fixture_data/hosts.py @@ -62,12 +62,12 @@ class BaseFixture(base.Fixture): return { 'hosts': [ { - 'host': 'host1', + 'host_name': 'host1', 'service': service or 'nova-compute', 'zone': zone }, { - 'host': 'host1', + 'host_name': 'host1', 'service': service or 'nova-cert', 'zone': zone } diff --git a/novaclient/tests/unit/v2/test_hosts.py b/novaclient/tests/unit/v2/test_hosts.py index d99627b09..8f77e01b4 100644 --- a/novaclient/tests/unit/v2/test_hosts.py +++ b/novaclient/tests/unit/v2/test_hosts.py @@ -85,3 +85,8 @@ class HostsTest(utils.FixturedTestCase): def test_hosts_repr(self): hs = self.cs.hosts.get('host') self.assertEqual('', repr(hs[0])) + + def test_hosts_list_repr(self): + hs = self.cs.hosts.list() + for h in hs: + self.assertEqual('' % h.host_name, repr(h)) diff --git a/novaclient/v2/hosts.py b/novaclient/v2/hosts.py index af1756ad2..0f979973b 100644 --- a/novaclient/v2/hosts.py +++ b/novaclient/v2/hosts.py @@ -40,6 +40,18 @@ class Host(base.Resource): def reboot(self): return self.manager.host_action(self.host, 'reboot') + @property + def host_name(self): + return self.host + + @host_name.setter + def host_name(self, value): + # A host from hosts.list() has the attribute "host_name" instead of + # "host." This sets "host" if that's the case. Even though it doesn't + # exactly mirror the response format, it enables users to work with + # host objects from list and non-list operations interchangeably. + self.host = value + class HostManager(base.ManagerWithFind): resource_class = Host