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 <lyan.h@inwinstack.com>

Closes-Bug: #1434167

Change-Id: I5c339bdd1ab867d972759ade9a10b86bfda1e70a
This commit is contained in:
melanie witt 2015-11-21 00:15:21 +00:00
parent cafba0c61e
commit c3dda7636c
3 changed files with 19 additions and 2 deletions

View File

@ -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
}

View File

@ -85,3 +85,8 @@ class HostsTest(utils.FixturedTestCase):
def test_hosts_repr(self):
hs = self.cs.hosts.get('host')
self.assertEqual('<Host: dummy>', repr(hs[0]))
def test_hosts_list_repr(self):
hs = self.cs.hosts.list()
for h in hs:
self.assertEqual('<Host: %s>' % h.host_name, repr(h))

View File

@ -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