Skip to content

Commit 03b9216

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Output correct json for security groups in 'openstack server show'"
2 parents 51aee43 + bae89b3 commit 03b9216

4 files changed

Lines changed: 42 additions & 21 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from novaclient import api_versions
2525
from novaclient.v2 import servers
2626
from openstack import exceptions as sdk_exceptions
27+
from osc_lib.cli import format_columns
2728
from osc_lib.cli import parseractions
2829
from osc_lib.command import command
2930
from osc_lib import exceptions
@@ -166,14 +167,14 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
166167
if 'os-extended-volumes:volumes_attached' in info:
167168
info.update(
168169
{
169-
'volumes_attached': utils.format_list_of_dicts(
170+
'volumes_attached': format_columns.ListDictColumn(
170171
info.pop('os-extended-volumes:volumes_attached'))
171172
}
172173
)
173174
if 'security_groups' in info:
174175
info.update(
175176
{
176-
'security_groups': utils.format_list_of_dicts(
177+
'security_groups': format_columns.ListDictColumn(
177178
info.pop('security_groups'))
178179
}
179180
)
@@ -182,9 +183,14 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
182183
info['addresses'] = _format_servers_list_networks(server.networks)
183184

184185
# Map 'metadata' field to 'properties'
185-
info.update(
186-
{'properties': utils.format_dict(info.pop('metadata'))}
187-
)
186+
if not info['metadata']:
187+
info.update(
188+
{'properties': utils.format_dict(info.pop('metadata'))}
189+
)
190+
else:
191+
info.update(
192+
{'properties': format_columns.DictColumn(info.pop('metadata'))}
193+
)
188194

189195
# Migrate tenant_id to project_id naming
190196
if 'tenant_id' in info:
@@ -2530,7 +2536,6 @@ def take_action(self, parsed_args):
25302536
data = _prep_server_detail(compute_client,
25312537
self.app.client_manager.image, server,
25322538
refresh=False)
2533-
25342539
return zip(*sorted(data.items()))
25352540

25362541

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def test_server_set(self):
230230
))
231231
# Really, shouldn't this be a list?
232232
self.assertEqual(
233-
"a='b', c='d'",
233+
{'a': 'b', 'c': 'd'},
234234
cmd_output['properties'],
235235
)
236236

@@ -244,7 +244,7 @@ def test_server_set(self):
244244
name
245245
))
246246
self.assertEqual(
247-
"c='d'",
247+
{'c': 'd'},
248248
cmd_output['properties'],
249249
)
250250

@@ -619,8 +619,8 @@ def test_server_boot_with_bdm_snapshot(self):
619619
server_name
620620
))
621621
volumes_attached = cmd_output['volumes_attached']
622-
self.assertTrue(volumes_attached.startswith('id='))
623-
attached_volume_id = volumes_attached.replace('id=', '')
622+
self.assertIsNotNone(volumes_attached)
623+
attached_volume_id = volumes_attached[0]["id"]
624624

625625
# check the volume that attached on server
626626
cmd_output = json.loads(self.openstack(
@@ -699,8 +699,8 @@ def test_server_boot_with_bdm_image(self):
699699
server_name
700700
))
701701
volumes_attached = cmd_output['volumes_attached']
702-
self.assertTrue(volumes_attached.startswith('id='))
703-
attached_volume_id = volumes_attached.replace('id=', '')
702+
self.assertIsNotNone(volumes_attached)
703+
attached_volume_id = volumes_attached[0]["id"]
704704

705705
# check the volume that attached on server
706706
cmd_output = json.loads(self.openstack(
@@ -773,10 +773,12 @@ def test_boot_from_volume(self):
773773
server_name
774774
))
775775
volumes_attached = cmd_output['volumes_attached']
776-
self.assertTrue(volumes_attached.startswith('id='))
777-
attached_volume_id = volumes_attached.replace('id=', '')
778-
# Don't leak the volume when the test exits.
779-
self.addCleanup(self.openstack, 'volume delete ' + attached_volume_id)
776+
self.assertIsNotNone(volumes_attached)
777+
attached_volume_id = volumes_attached[0]["id"]
778+
for vol in volumes_attached:
779+
self.assertIsNotNone(vol['id'])
780+
# Don't leak the volume when the test exits.
781+
self.addCleanup(self.openstack, 'volume delete ' + vol['id'])
780782

781783
# Since the server is volume-backed the GET /servers/{server_id}
782784
# response will have image=''.
@@ -785,7 +787,7 @@ def test_boot_from_volume(self):
785787
# check the volume that attached on server
786788
cmd_output = json.loads(self.openstack(
787789
'volume show -f json ' +
788-
attached_volume_id
790+
volumes_attached[0]["id"]
789791
))
790792
# The volume size should be what we specified on the command line.
791793
self.assertEqual(1, int(cmd_output['size']))
@@ -879,14 +881,21 @@ def test_server_create_with_security_group(self):
879881

880882
self.assertIsNotNone(server['id'])
881883
self.assertEqual(server_name, server['name'])
882-
self.assertIn(str(security_group1['id']), server['security_groups'])
883-
self.assertIn(str(security_group2['id']), server['security_groups'])
884+
sec_grp = ""
885+
for sec in server['security_groups']:
886+
sec_grp += sec['name']
887+
self.assertIn(str(security_group1['id']), sec_grp)
888+
self.assertIn(str(security_group2['id']), sec_grp)
884889
self.wait_for_status(server_name, 'ACTIVE')
885890
server = json.loads(self.openstack(
886891
'server show -f json ' + server_name
887892
))
888-
self.assertIn(sg_name1, server['security_groups'])
889-
self.assertIn(sg_name2, server['security_groups'])
893+
# check if security group exists in list
894+
sec_grp = ""
895+
for sec in server['security_groups']:
896+
sec_grp += sec['name']
897+
self.assertIn(sg_name1, sec_grp)
898+
self.assertIn(sg_name2, sec_grp)
890899

891900
def test_server_create_with_empty_network_option_latest(self):
892901
"""Test server create with empty network option in nova 2.latest."""

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5166,6 +5166,8 @@ def test_prep_server_detail(self, find_resource):
51665166
'tenant_id': u'tenant-id-xxx',
51675167
'networks': {u'public': [u'10.20.30.40', u'2001:db8::f']},
51685168
'links': u'http://xxx.yyy.com',
5169+
'properties': '',
5170+
'volumes_attached': [{"id": "6344fe9d-ef20-45b2-91a6"}],
51695171
}
51705172
_server = compute_fakes.FakeServer.create_one_server(attrs=server_info)
51715173
find_resource.side_effect = [_server, _flavor]
@@ -5182,6 +5184,7 @@ def test_prep_server_detail(self, find_resource):
51825184
'properties': '',
51835185
'OS-EXT-STS:power_state': server._format_servers_list_power_state(
51845186
getattr(_server, 'OS-EXT-STS:power_state')),
5187+
'volumes_attached': [{"id": "6344fe9d-ef20-45b2-91a6"}],
51855188
}
51865189

51875190
# Call _prep_server_detail().
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- The ``openstack server show -f json`` command was not outputting
4+
json for security groups, volumes and properties properly.

0 commit comments

Comments
 (0)