From 429b43a331cd0370f35bcc63cdb54889f72e2a1b Mon Sep 17 00:00:00 2001 From: Jose Castro Leon Date: Mon, 13 Mar 2017 15:18:30 +0100 Subject: [PATCH] Adds missing flavor information in the server list long command Closes-Bug: #1672396 Change-Id: Ie2a664fd1c3db1b8269ea079df181f87afc702a7 --- openstackclient/compute/v2/server.py | 27 +++++++++++++++++-- .../tests/unit/compute/v2/test_server.py | 10 +++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index d33c631a17..cd045a4050 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -917,6 +917,8 @@ class ListServer(command.Lister): 'Networks', 'Image Name', 'Image ID', + 'Flavor Name', + 'Flavor ID', 'OS-EXT-AZ:availability_zone', 'OS-EXT-SRV-ATTR:host', 'Metadata', @@ -930,6 +932,8 @@ class ListServer(command.Lister): 'Networks', 'Image Name', 'Image ID', + 'Flavor Name', + 'Flavor ID', 'Availability Zone', 'Host', 'Properties', @@ -977,8 +981,19 @@ class ListServer(command.Lister): except Exception: pass - # Populate image_name and image_id attributes of server objects - # so that we can display "Image Name" and "Image ID" columns. + flavors = {} + # Create a dict that maps flavor_id to flavor object. + # Needed so that we can display the "Flavor Name" column. + # "Flavor Name" is not crucial, so we swallow any exceptions. + try: + flavors_list = compute_client.flavors.list() + for i in flavors_list: + flavors[i.id] = i + except Exception: + pass + + # Populate image_name, image_id, flavor_name and flavor_id attributes + # of server objects so that we can display those columns. for s in data: if 'id' in s.image: image = images.get(s.image['id']) @@ -988,6 +1003,14 @@ class ListServer(command.Lister): else: s.image_name = '' s.image_id = '' + if 'id' in s.flavor: + flavor = flavors.get(s.flavor['id']) + if flavor: + s.flavor_name = flavor.name + s.flavor_id = s.flavor['id'] + else: + s.flavor_name = '' + s.flavor_id = '' table = (column_headers, (utils.get_item_properties( diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 249902bca4..8aba177e2b 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -957,6 +957,8 @@ class TestServerList(TestServer): 'Networks', 'Image Name', 'Image ID', + 'Flavor Name', + 'Flavor ID', 'Availability Zone', 'Host', 'Properties', @@ -1027,6 +1029,12 @@ class TestServerList(TestServer): for s in self.servers ] + Flavor = collections.namedtuple('Flavor', 'id name') + self.flavors_mock.list.return_value = [ + Flavor(id=s.flavor['id'], name=self.flavor.name) + for s in self.servers + ] + for s in self.servers: self.data.append(( s.id, @@ -1046,6 +1054,8 @@ class TestServerList(TestServer): server._format_servers_list_networks(s.networks), self.image.name, s.image['id'], + self.flavor.name, + s.flavor['id'], getattr(s, 'OS-EXT-AZ:availability_zone'), getattr(s, 'OS-EXT-SRV-ATTR:host'), s.Metadata,