Skip to content

Commit 2c57f7b

Browse files
boris-42Boris Pavlovic
authored andcommitted
Add server list -n and --no-name-lookup arguments
Remove translation of Image ID and Flavor ID to Image and Flavor names In large environments amount of images can be very large (thousands) Which requires ~hundreds of requests to Glance to get all images (by default client request only 20 images) As a result listing even few servers is going to take minutes This patch allows to avoid these queries by not doing translation, which allows one to get information about servers in seconds. Change-Id: I4ae00e6324a41c4c79bf5b620179dae99aea5431
1 parent ca4b9be commit 2c57f7b

File tree

3 files changed

+84
-16
lines changed

3 files changed

+84
-16
lines changed

doc/source/cli/command-objects/server.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ List servers
330330
[--all-projects]
331331
[--project <project> [--project-domain <project-domain>]]
332332
[--long]
333+
[-n | --no-name-lookup]
333334
[--marker <server>]
334335
[--limit <num-servers>]
335336
[--deleted]
@@ -397,6 +398,10 @@ List servers
397398
398399
List additional fields in output
399400
401+
.. option:: --no-name-lookup
402+
403+
Skips image and flavor names lookup
404+
400405
.. option:: --marker <server>
401406
402407
The last server of the previous page. Display list of servers

openstackclient/compute/v2/server.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,12 @@ def get_parser(self, prog_name):
921921
default=False,
922922
help=_('List additional fields in output'),
923923
)
924+
parser.add_argument(
925+
'-n', '--no-name-lookup',
926+
action='store_true',
927+
default=False,
928+
help=_('Skip flavor and image name lookup.'),
929+
)
924930
parser.add_argument(
925931
'--marker',
926932
metavar='<server>',
@@ -1054,21 +1060,26 @@ def take_action(self, parsed_args):
10541060
'OS-EXT-AZ:availability_zone',
10551061
'OS-EXT-SRV-ATTR:host',
10561062
]
1057-
else:
1063+
elif parsed_args.no_name_lookup:
10581064
columns = (
10591065
'ID',
10601066
'Name',
10611067
'Status',
1062-
'Networks',
1063-
'Image Name',
1068+
'Image ID',
1069+
'Flavor ID',
10641070
)
1065-
column_headers = (
1071+
column_headers = tuple(columns)
1072+
mixed_case_fields = []
1073+
1074+
else:
1075+
columns = (
10661076
'ID',
10671077
'Name',
10681078
'Status',
10691079
'Networks',
10701080
'Image Name',
10711081
)
1082+
column_headers = tuple(columns)
10721083
mixed_case_fields = []
10731084

10741085
marker_id = None
@@ -1084,23 +1095,25 @@ def take_action(self, parsed_args):
10841095
# Create a dict that maps image_id to image object.
10851096
# Needed so that we can display the "Image Name" column.
10861097
# "Image Name" is not crucial, so we swallow any exceptions.
1087-
try:
1088-
images_list = self.app.client_manager.image.images.list()
1089-
for i in images_list:
1090-
images[i.id] = i
1091-
except Exception:
1092-
pass
1098+
if not parsed_args.no_name_lookup:
1099+
try:
1100+
images_list = self.app.client_manager.image.images.list()
1101+
for i in images_list:
1102+
images[i.id] = i
1103+
except Exception:
1104+
pass
10931105

10941106
flavors = {}
10951107
# Create a dict that maps flavor_id to flavor object.
10961108
# Needed so that we can display the "Flavor Name" column.
10971109
# "Flavor Name" is not crucial, so we swallow any exceptions.
1098-
try:
1099-
flavors_list = compute_client.flavors.list()
1100-
for i in flavors_list:
1101-
flavors[i.id] = i
1102-
except Exception:
1103-
pass
1110+
if not parsed_args.no_name_lookup:
1111+
try:
1112+
flavors_list = compute_client.flavors.list()
1113+
for i in flavors_list:
1114+
flavors[i.id] = i
1115+
except Exception:
1116+
pass
11041117

11051118
# Populate image_name, image_id, flavor_name and flavor_id attributes
11061119
# of server objects so that we can display those columns.

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ def test_server_create_minimal(self):
395395

396396
self.assertEqual(self.columns, columns)
397397
self.assertEqual(self.datalist(), data)
398+
self.assertFalse(self.images_mock.called)
399+
self.assertFalse(self.flavors_mock.called)
398400

399401
def test_server_create_with_options(self):
400402
arglist = [
@@ -1457,6 +1459,14 @@ class TestServerList(TestServer):
14571459
'Properties',
14581460
)
14591461

1462+
columns_no_name_lookup = (
1463+
'ID',
1464+
'Name',
1465+
'Status',
1466+
'Image ID',
1467+
'Flavor ID',
1468+
)
1469+
14601470
def setUp(self):
14611471
super(TestServerList, self).setUp()
14621472

@@ -1515,6 +1525,7 @@ def setUp(self):
15151525
# Prepare data returned by fake Nova API.
15161526
self.data = []
15171527
self.data_long = []
1528+
self.data_no_name_lookup = []
15181529

15191530
Image = collections.namedtuple('Image', 'id name')
15201531
self.images_mock.list.return_value = [
@@ -1553,6 +1564,13 @@ def setUp(self):
15531564
getattr(s, 'OS-EXT-SRV-ATTR:host'),
15541565
s.Metadata,
15551566
))
1567+
self.data_no_name_lookup.append((
1568+
s.id,
1569+
s.name,
1570+
s.status,
1571+
s.image['id'],
1572+
s.flavor['id']
1573+
))
15561574

15571575
def test_server_list_no_option(self):
15581576
arglist = []
@@ -1585,6 +1603,38 @@ def test_server_list_long_option(self):
15851603
self.assertEqual(self.columns_long, columns)
15861604
self.assertEqual(tuple(self.data_long), tuple(data))
15871605

1606+
def test_server_list_no_name_lookup_option(self):
1607+
arglist = [
1608+
'--no-name-lookup',
1609+
]
1610+
verifylist = [
1611+
('all_projects', False),
1612+
('no_name_lookup', True),
1613+
]
1614+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1615+
1616+
columns, data = self.cmd.take_action(parsed_args)
1617+
1618+
self.servers_mock.list.assert_called_with(**self.kwargs)
1619+
self.assertEqual(self.columns_no_name_lookup, columns)
1620+
self.assertEqual(tuple(self.data_no_name_lookup), tuple(data))
1621+
1622+
def test_server_list_n_option(self):
1623+
arglist = [
1624+
'-n',
1625+
]
1626+
verifylist = [
1627+
('all_projects', False),
1628+
('no_name_lookup', True),
1629+
]
1630+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1631+
1632+
columns, data = self.cmd.take_action(parsed_args)
1633+
1634+
self.servers_mock.list.assert_called_with(**self.kwargs)
1635+
self.assertEqual(self.columns_no_name_lookup, columns)
1636+
self.assertEqual(tuple(self.data_no_name_lookup), tuple(data))
1637+
15881638
def test_server_list_with_image(self):
15891639

15901640
arglist = [

0 commit comments

Comments
 (0)