Skip to content

Commit d2d7219

Browse files
Get "security_groups" when port list
Neutron API is accepting 'security_groups' field in order to return the list of security_groups attached to a port, but openstackclient is parsing the output over a Openstack Port object that has security_group_ids to map. This patch sends to the Neutron API the expected field value and replace the output key to allow the mapping just in case '--long' argument is passed. Closes-Bug: #2095414 Change-Id: I188edc3c620ce29d7b16497ca24fd7d972a06618
1 parent 146a181 commit d2d7219

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

openstackclient/network/v2/port.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -866,35 +866,30 @@ def take_action(self, parsed_args):
866866
network_client = self.app.client_manager.network
867867
identity_client = self.app.client_manager.identity
868868

869-
columns = (
869+
columns = [
870870
'id',
871871
'name',
872872
'mac_address',
873873
'fixed_ips',
874874
'status',
875-
)
876-
column_headers = (
875+
]
876+
column_headers = [
877877
'ID',
878878
'Name',
879879
'MAC Address',
880880
'Fixed IP Addresses',
881881
'Status',
882-
)
882+
]
883883

884884
filters = {}
885885
if parsed_args.long:
886-
columns += (
887-
'security_group_ids',
888-
'device_owner',
889-
'tags',
890-
'trunk_details',
886+
columns.extend(
887+
['security_groups', 'device_owner', 'tags', 'trunk_details']
891888
)
892-
column_headers += (
893-
'Security Groups',
894-
'Device Owner',
895-
'Tags',
896-
'Trunk subports',
889+
column_headers.extend(
890+
['Security Groups', 'Device Owner', 'Tags', 'Trunk subports']
897891
)
892+
898893
if parsed_args.device_owner is not None:
899894
filters['device_owner'] = parsed_args.device_owner
900895
if parsed_args.device_id is not None:
@@ -942,6 +937,12 @@ def take_action(self, parsed_args):
942937

943938
data = network_client.ports(fields=columns, **filters)
944939

940+
if parsed_args.long:
941+
columns = [
942+
'security_group_ids' if item == 'security_groups' else item
943+
for item in columns
944+
]
945+
945946
headers, attrs = utils.calculate_header_and_attrs(
946947
column_headers, columns, parsed_args
947948
)

openstackclient/tests/functional/network/v2/test_port.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,20 @@ def test_port_list(self):
8181
self.addCleanup(self.openstack, f'port delete {id1}')
8282
self.assertEqual(self.NAME, json_output.get('name'))
8383

84+
# sg for port2
85+
sg_name1 = uuid.uuid4().hex
8486
json_output = self.openstack(
85-
f'port create --network {self.NETWORK_NAME} {self.NAME}x',
87+
f'security group create {sg_name1}',
8688
parse_output=True,
8789
)
90+
sg_id1 = json_output.get('id')
91+
self.addCleanup(self.openstack, f'security group delete {sg_id1}')
92+
json_output = self.openstack(
93+
f'port create --network {self.NETWORK_NAME} '
94+
f'--security-group {sg_name1} {self.NAME}x',
95+
parse_output=True,
96+
)
97+
8898
id2 = json_output.get('id')
8999
self.assertIsNotNone(id2)
90100
mac2 = json_output.get('mac_address')
@@ -113,6 +123,12 @@ def test_port_list(self):
113123
id_list = [item.get('ID') for item in json_output]
114124
self.assertIn(id1, id_list)
115125
self.assertIn(id2, id_list)
126+
item_sg_map = {
127+
item.get('ID'): item.get('Security Groups') for item in json_output
128+
}
129+
self.assertIn(id1, item_sg_map.keys())
130+
self.assertIn(id2, item_sg_map.keys())
131+
self.assertIn([sg_id1], item_sg_map.values())
116132

117133
# Test list --mac-address
118134
json_output = self.openstack(
@@ -127,6 +143,17 @@ def test_port_list(self):
127143
self.assertNotIn(mac1, item_map.values())
128144
self.assertIn(mac2, item_map.values())
129145

146+
# Test list --security-group
147+
json_output = self.openstack(
148+
f'port list --security-group {sg_id1}',
149+
parse_output=True,
150+
)
151+
item_map = {
152+
item.get('ID'): item.get('Security Groups') for item in json_output
153+
}
154+
self.assertNotIn(id1, item_map.keys())
155+
self.assertIn(id2, item_map.keys())
156+
130157
# Test list with unknown fields
131158
json_output = self.openstack(
132159
'port list -c ID -c Name -c device_id',

openstackclient/tests/unit/network/v2/test_port.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
from openstackclient.tests.unit import utils as test_utils
2525

2626

27-
LIST_FIELDS_TO_RETRIEVE = ('id', 'name', 'mac_address', 'fixed_ips', 'status')
28-
LIST_FIELDS_TO_RETRIEVE_LONG = (
29-
'security_group_ids',
27+
LIST_FIELDS_TO_RETRIEVE = ['id', 'name', 'mac_address', 'fixed_ips', 'status']
28+
LIST_FIELDS_TO_RETRIEVE_LONG = [
29+
'security_groups',
3030
'device_owner',
3131
'tags',
3232
'trunk_details',
33-
)
33+
]
3434

3535

3636
class TestPort(network_fakes.TestNetworkV2):
@@ -1274,15 +1274,15 @@ class TestListPort(compute_fakes.FakeClientMixin, TestPort):
12741274
)
12751275
_ports = (_pport, _sport1, _sport2)
12761276

1277-
columns = (
1277+
columns = [
12781278
'ID',
12791279
'Name',
12801280
'MAC Address',
12811281
'Fixed IP Addresses',
12821282
'Status',
1283-
)
1283+
]
12841284

1285-
columns_long = (
1285+
columns_long = [
12861286
'ID',
12871287
'Name',
12881288
'MAC Address',
@@ -1292,7 +1292,7 @@ class TestListPort(compute_fakes.FakeClientMixin, TestPort):
12921292
'Device Owner',
12931293
'Tags',
12941294
'Trunk subports',
1295-
)
1295+
]
12961296

12971297
data = []
12981298
for prt in _ports:

0 commit comments

Comments
 (0)