Skip to content

Commit 752a2db

Browse files
committed
Optimize _prep_server_detail to avoid redundant find_resource
When showing a server or doing a rebuild, we already have the latest version of the server so _prep_server_detail getting the server again is an unnecessary performance hit. ShowServer is pretty obvious here. For RebuildServer, the compute API actually refreshes the server before returning it in the response, so the client already gets the latest when the rebuild call returns. The only other usage of _prep_server_detail that does require a refresh is CreateServer since the POST /servers response is a minimal version of the server object. This adds a new refresh kwarg, backward compatible by default, to _prep_server_detail but changes ShowServer and RebuildServer to no longer refresh. Change-Id: Ib1c9c424ed1cafc2dfd8be90af8de8a774bdfbf0
1 parent 56b3467 commit 752a2db

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,21 @@ def func(value):
120120
return func
121121

122122

123-
def _prep_server_detail(compute_client, image_client, server):
123+
def _prep_server_detail(compute_client, image_client, server, refresh=True):
124124
"""Prepare the detailed server dict for printing
125125
126126
:param compute_client: a compute client instance
127127
:param image_client: an image client instance
128128
:param server: a Server resource
129+
:param refresh: Flag indicating if ``server`` is already the latest version
130+
or if it needs to be refreshed, for example when showing
131+
the latest details of a server after creating it.
129132
:rtype: a dict of server details
130133
"""
131134
info = server.to_dict()
132-
133-
server = utils.find_resource(compute_client.servers, info['id'])
134-
info.update(server.to_dict())
135+
if refresh:
136+
server = utils.find_resource(compute_client.servers, info['id'])
137+
info.update(server.to_dict())
135138

136139
# Convert the image blob to a name
137140
image_info = info.get('image', {})
@@ -1540,7 +1543,8 @@ def _show_progress(progress):
15401543
self.app.stdout.write(_('Error rebuilding server\n'))
15411544
raise SystemExit
15421545

1543-
details = _prep_server_detail(compute_client, image_client, server)
1546+
details = _prep_server_detail(compute_client, image_client, server,
1547+
refresh=False)
15441548
return zip(*sorted(six.iteritems(details)))
15451549

15461550

@@ -2021,7 +2025,8 @@ def take_action(self, parsed_args):
20212025
return ({}, {})
20222026
else:
20232027
data = _prep_server_detail(compute_client,
2024-
self.app.client_manager.image, server)
2028+
self.app.client_manager.image, server,
2029+
refresh=False)
20252030

20262031
return zip(*sorted(six.iteritems(data)))
20272032

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,17 +2331,17 @@ def setUp(self):
23312331
self.images_mock.get.return_value = self.image
23322332

23332333
# Fake the rebuilt new server.
2334-
new_server = compute_fakes.FakeServer.create_one_server()
2335-
2336-
# Fake the server to be rebuilt. The IDs of them should be the same.
23372334
attrs = {
2338-
'id': new_server.id,
23392335
'image': {
23402336
'id': self.image.id
23412337
},
23422338
'networks': {},
23432339
'adminPass': 'passw0rd',
23442340
}
2341+
new_server = compute_fakes.FakeServer.create_one_server(attrs=attrs)
2342+
2343+
# Fake the server to be rebuilt. The IDs of them should be the same.
2344+
attrs['id'] = new_server.id
23452345
methods = {
23462346
'rebuild': new_server,
23472347
}

0 commit comments

Comments
 (0)