Skip to content

Commit f1a3fc3

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "compute: Always use SDK client to display server"
2 parents 5898f13 + 628ac48 commit f1a3fc3

3 files changed

Lines changed: 287 additions & 86 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,26 @@ def _get_ip_address(addresses, address_type, ip_address_family):
128128
)
129129

130130

131-
def _prep_server_detail(compute_client, image_client, server, refresh=True):
131+
def _prep_server_detail(compute_client, image_client, server, *, refresh=True):
132132
"""Prepare the detailed server dict for printing
133133
134134
:param compute_client: a compute client instance
135135
:param image_client: an image client instance
136136
:param server: a Server resource
137137
:param refresh: Flag indicating if ``server`` is already the latest version
138-
or if it needs to be refreshed, for example when showing
139-
the latest details of a server after creating it.
138+
or if it needs to be refreshed, for example when showing the latest
139+
details of a server after creating it.
140140
:rtype: a dict of server details
141141
"""
142-
# Note: Some callers of this routine pass a novaclient server, and others
143-
# pass an SDK server. Column names may be different across those cases.
144142
info = server.to_dict()
143+
145144
if refresh:
146-
server = utils.find_resource(compute_client.servers, info['id'])
147-
info.update(server.to_dict())
145+
server = compute_client.get_server(info['id'])
146+
# we only update if the field is not empty, to avoid overwriting
147+
# existing values
148+
info.update(
149+
**{x: y for x, y in server.to_dict().items() if x not in info or y}
150+
)
148151

149152
# Some commands using this routine were originally implemented with the
150153
# nova python wrappers, and were later migrated to use the SDK. Map the
@@ -159,7 +162,6 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
159162
'compute_host': 'OS-EXT-SRV-ATTR:host',
160163
'created_at': 'created',
161164
'disk_config': 'OS-DCF:diskConfig',
162-
'flavor_id': 'flavorRef',
163165
'has_config_drive': 'config_drive',
164166
'host_id': 'hostId',
165167
'fault': 'fault',
@@ -194,10 +196,12 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
194196
'public_v6',
195197
# create-only columns
196198
'block_device_mapping',
199+
'flavor_id',
197200
'host',
198201
'image_id',
199202
'max_count',
200203
'min_count',
204+
'networks',
201205
'personality',
202206
'scheduler_hints',
203207
# aliases
@@ -208,11 +212,12 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
208212
# Some columns are only present in certain responses and should not be
209213
# shown otherwise.
210214
optional_columns = {
211-
'admin_password', # removed in 2.14
212-
'fault', # only present in errored servers
213-
'flavor_id', # removed in 2.47
214-
'networks', # only present in create responses
215-
'security_groups', # only present in create, detail responses
215+
# only in create responses if '[api] enable_instance_password' is set
216+
'admin_password',
217+
# only present in errored servers
218+
'fault',
219+
# only present in create, detail responses
220+
'security_groups',
216221
}
217222

218223
data = {}
@@ -252,7 +257,9 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
252257
if flavor_info.get('original_name') is None: # microversion < 2.47
253258
flavor_id = flavor_info.get('id', '')
254259
try:
255-
flavor = utils.find_resource(compute_client.flavors, flavor_id)
260+
flavor = compute_client.find_flavor(
261+
flavor_id, ignore_missing=False
262+
)
256263
info['flavor'] = f"{flavor.name} ({flavor_id})"
257264
except Exception:
258265
info['flavor'] = flavor_id
@@ -2056,8 +2063,10 @@ def _match_image(image_api, wanted_properties):
20562063
msg = _('Error creating server: %s') % parsed_args.server_name
20572064
raise exceptions.CommandError(msg)
20582065

2059-
details = _prep_server_detail(compute_client, image_client, server)
2060-
return zip(*sorted(details.items()))
2066+
# TODO(stephenfin): Remove when the whole command is using SDK
2067+
compute_client = self.app.client_manager.sdk_connection.compute
2068+
data = _prep_server_detail(compute_client, image_client, server)
2069+
return zip(*sorted(data.items()))
20612070

20622071

20632072
class CreateServerDump(command.Command):
@@ -3681,10 +3690,12 @@ def _show_progress(progress):
36813690
msg = _('Error rebuilding server: %s') % server.id
36823691
raise exceptions.CommandError(msg)
36833692

3684-
details = _prep_server_detail(
3693+
# TODO(stephenfin): Remove when the whole command is using SDK
3694+
compute_client = self.app.client_manager.sdk_connection.compute
3695+
data = _prep_server_detail(
36853696
compute_client, image_client, server, refresh=False
36863697
)
3687-
return zip(*sorted(details.items()))
3698+
return zip(*sorted(data.items()))
36883699

36893700

36903701
class EvacuateServer(command.ShowOne):
@@ -3803,10 +3814,10 @@ def _show_progress(progress):
38033814
msg = _('Error evacuating server: %s') % server.id
38043815
raise exceptions.CommandError(msg)
38053816

3806-
details = _prep_server_detail(
3807-
compute_client, image_client, server, refresh=True
3808-
)
3809-
return zip(*sorted(details.items()))
3817+
# TODO(stephenfin): Remove when the whole command is using SDK
3818+
compute_client = self.app.client_manager.sdk_connection.compute
3819+
data = _prep_server_detail(compute_client, image_client, server)
3820+
return zip(*sorted(data.items()))
38103821

38113822

38123823
class RemoveFixedIP(command.Command):
@@ -4629,6 +4640,7 @@ def get_parser(self, prog_name):
46294640

46304641
def take_action(self, parsed_args):
46314642
compute_client = self.app.client_manager.sdk_connection.compute
4643+
image_client = self.app.client_manager.image
46324644

46334645
# Find by name or ID, then get the full details of the server
46344646
server = compute_client.find_server(
@@ -4652,17 +4664,10 @@ def take_action(self, parsed_args):
46524664
topology = server.fetch_topology(compute_client)
46534665

46544666
data = _prep_server_detail(
4655-
# TODO(dannosliwcd): Replace these clients with SDK clients after
4656-
# all callers of _prep_server_detail() are using the SDK.
4657-
self.app.client_manager.compute,
4658-
self.app.client_manager.image,
4659-
server,
4660-
refresh=False,
4667+
compute_client, image_client, server, refresh=False
46614668
)
4662-
46634669
if topology:
46644670
data['topology'] = format_columns.DictColumn(topology)
4665-
46664671
return zip(*sorted(data.items()))
46674672

46684673

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ def test_server_set(self):
210210
self.flavor_name,
211211
flavor['name'],
212212
)
213-
self.assertEqual(
214-
'{} ({})'.format(flavor['name'], flavor['id']),
215-
cmd_output["flavor"],
216-
)
213+
# assume the v2.47+ output format
214+
self.assertIsInstance(cmd_output['flavor'], dict)
215+
self.assertIn('name', cmd_output['flavor'])
216+
self.assertEqual(flavor['name'], cmd_output['flavor']['name'])
217217
image = self.openstack(
218218
'image show ' + self.image_name,
219219
parse_output=True,

0 commit comments

Comments
 (0)