Skip to content

Commit 8387b11

Browse files
committed
Add "fields" parameter to ListPort query
This new query parameter will allow to send a query to the Neutron server filtering only by those parameters needed by the list command: ID, name, MAC address, fixed IPs and status. When using input parameter "long", security groups IDs, device owner and tags will be added to the fields filter. With 4500 ports, those are the execution times for the command "openstack port list" (average values in a development environment): Neutron API (seconds) CLI (seconds) Without filter: 3.05 10.15 With filter: 2.76 8.19 Depends-On: https://review.opendev.org/#/c/754113/ Change-Id: I1cccf0bc3533f8085e8dd61bf2fbe78c49b74b31 Closes-Bug: #1897100
1 parent 7146dee commit 8387b11

File tree

4 files changed

+58
-22
lines changed

4 files changed

+58
-22
lines changed

lower-constraints.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ msgpack-python==0.4.0
4545
munch==2.1.0
4646
netaddr==0.7.18
4747
netifaces==0.10.4
48-
openstacksdk==0.48.0
48+
openstacksdk==0.51.0
4949
os-service-types==1.7.0
5050
os-testr==1.0.0
5151
osc-lib==2.2.0

openstackclient/network/v2/port.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ def take_action(self, parsed_args):
665665

666666
_tag.get_tag_filtering_args(parsed_args, filters)
667667

668-
data = network_client.ports(**filters)
668+
data = network_client.ports(fields=columns, **filters)
669669

670670
headers, attrs = utils.calculate_header_and_attrs(
671671
column_headers, columns, parsed_args)

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

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
from openstackclient.tests.unit import utils as tests_utils
2727

2828

29+
LIST_FIELDS_TO_RETRIEVE = ('id', 'name', 'mac_address', 'fixed_ips', 'status')
30+
LIST_FIELDS_TO_RETRIEVE_LONG = ('security_group_ids', 'device_owner', 'tags')
31+
32+
2933
class TestPort(network_fakes.TestNetworkV2):
3034

3135
def setUp(self):
@@ -883,7 +887,8 @@ def test_port_list_no_options(self):
883887

884888
columns, data = self.cmd.take_action(parsed_args)
885889

886-
self.network.ports.assert_called_once_with()
890+
self.network.ports.assert_called_once_with(
891+
fields=LIST_FIELDS_TO_RETRIEVE)
887892
self.assertEqual(self.columns, columns)
888893
self.assertListItemEqual(self.data, list(data))
889894

@@ -901,7 +906,8 @@ def test_port_list_router_opt(self):
901906
columns, data = self.cmd.take_action(parsed_args)
902907

903908
self.network.ports.assert_called_once_with(**{
904-
'device_id': 'fake-router-id'
909+
'device_id': 'fake-router-id',
910+
'fields': LIST_FIELDS_TO_RETRIEVE,
905911
})
906912
self.assertEqual(self.columns, columns)
907913
self.assertListItemEqual(self.data, list(data))
@@ -921,7 +927,8 @@ def test_port_list_with_server_option(self, mock_find):
921927
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
922928
columns, data = self.cmd.take_action(parsed_args)
923929
self.network.ports.assert_called_once_with(
924-
device_id=fake_server.id)
930+
device_id=fake_server.id,
931+
fields=LIST_FIELDS_TO_RETRIEVE)
925932
mock_find.assert_called_once_with(mock.ANY, 'fake-server-name')
926933
self.assertEqual(self.columns, columns)
927934
self.assertListItemEqual(self.data, list(data))
@@ -940,7 +947,8 @@ def test_port_list_device_id_opt(self):
940947
columns, data = self.cmd.take_action(parsed_args)
941948

942949
self.network.ports.assert_called_once_with(**{
943-
'device_id': self._ports[0].device_id
950+
'device_id': self._ports[0].device_id,
951+
'fields': LIST_FIELDS_TO_RETRIEVE,
944952
})
945953
self.assertEqual(self.columns, columns)
946954
self.assertListItemEqual(self.data, list(data))
@@ -959,7 +967,8 @@ def test_port_list_device_owner_opt(self):
959967
columns, data = self.cmd.take_action(parsed_args)
960968

961969
self.network.ports.assert_called_once_with(**{
962-
'device_owner': self._ports[0].device_owner
970+
'device_owner': self._ports[0].device_owner,
971+
'fields': LIST_FIELDS_TO_RETRIEVE,
963972
})
964973
self.assertEqual(self.columns, columns)
965974
self.assertListItemEqual(self.data, list(data))
@@ -987,7 +996,8 @@ def test_port_list_all_opt(self):
987996
'device_owner': self._ports[0].device_owner,
988997
'device_id': 'fake-router-id',
989998
'network_id': 'fake-network-id',
990-
'mac_address': self._ports[0].mac_address
999+
'mac_address': self._ports[0].mac_address,
1000+
'fields': LIST_FIELDS_TO_RETRIEVE,
9911001
})
9921002
self.assertEqual(self.columns, columns)
9931003
self.assertListItemEqual(self.data, list(data))
@@ -1006,7 +1016,8 @@ def test_port_list_mac_address_opt(self):
10061016
columns, data = self.cmd.take_action(parsed_args)
10071017

10081018
self.network.ports.assert_called_once_with(**{
1009-
'mac_address': self._ports[0].mac_address
1019+
'mac_address': self._ports[0].mac_address,
1020+
'fields': LIST_FIELDS_TO_RETRIEVE,
10101021
})
10111022
self.assertEqual(self.columns, columns)
10121023
self.assertListItemEqual(self.data, list(data))
@@ -1025,7 +1036,9 @@ def test_port_list_fixed_ip_opt_ip_address(self):
10251036
columns, data = self.cmd.take_action(parsed_args)
10261037

10271038
self.network.ports.assert_called_once_with(**{
1028-
'fixed_ips': ['ip_address=%s' % ip_address]})
1039+
'fixed_ips': ['ip_address=%s' % ip_address],
1040+
'fields': LIST_FIELDS_TO_RETRIEVE,
1041+
})
10291042
self.assertEqual(self.columns, columns)
10301043
self.assertListItemEqual(self.data, list(data))
10311044

@@ -1043,7 +1056,9 @@ def test_port_list_fixed_ip_opt_ip_address_substr(self):
10431056
columns, data = self.cmd.take_action(parsed_args)
10441057

10451058
self.network.ports.assert_called_once_with(**{
1046-
'fixed_ips': ['ip_address_substr=%s' % ip_address_ss]})
1059+
'fixed_ips': ['ip_address_substr=%s' % ip_address_ss],
1060+
'fields': LIST_FIELDS_TO_RETRIEVE,
1061+
})
10471062
self.assertEqual(self.columns, columns)
10481063
self.assertListItemEqual(self.data, list(data))
10491064

@@ -1063,7 +1078,9 @@ def test_port_list_fixed_ip_opt_subnet_id(self):
10631078
columns, data = self.cmd.take_action(parsed_args)
10641079

10651080
self.network.ports.assert_called_once_with(**{
1066-
'fixed_ips': ['subnet_id=%s' % subnet_id]})
1081+
'fixed_ips': ['subnet_id=%s' % subnet_id],
1082+
'fields': LIST_FIELDS_TO_RETRIEVE,
1083+
})
10671084
self.assertEqual(self.columns, columns)
10681085
self.assertListItemEqual(self.data, list(data))
10691086

@@ -1087,7 +1104,9 @@ def test_port_list_fixed_ip_opts(self):
10871104

10881105
self.network.ports.assert_called_once_with(**{
10891106
'fixed_ips': ['subnet_id=%s' % subnet_id,
1090-
'ip_address=%s' % ip_address]})
1107+
'ip_address=%s' % ip_address],
1108+
'fields': LIST_FIELDS_TO_RETRIEVE,
1109+
})
10911110
self.assertEqual(self.columns, columns)
10921111
self.assertListItemEqual(self.data, list(data))
10931112

@@ -1103,15 +1122,19 @@ def test_port_list_fixed_ips(self):
11031122
{'ip-address': ip_address}])
11041123
]
11051124

1106-
self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet(
1107-
{'id': subnet_id})
1125+
self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet({
1126+
'id': subnet_id,
1127+
'fields': LIST_FIELDS_TO_RETRIEVE,
1128+
})
11081129
self.network.find_subnet = mock.Mock(return_value=self.fake_subnet)
11091130
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
11101131
columns, data = self.cmd.take_action(parsed_args)
11111132

11121133
self.network.ports.assert_called_once_with(**{
11131134
'fixed_ips': ['subnet_id=%s' % subnet_id,
1114-
'ip_address=%s' % ip_address]})
1135+
'ip_address=%s' % ip_address],
1136+
'fields': LIST_FIELDS_TO_RETRIEVE,
1137+
})
11151138
self.assertEqual(self.columns, columns)
11161139
self.assertListItemEqual(self.data, list(data))
11171140

@@ -1128,7 +1151,8 @@ def test_list_port_with_long(self):
11281151

11291152
columns, data = self.cmd.take_action(parsed_args)
11301153

1131-
self.network.ports.assert_called_once_with()
1154+
self.network.ports.assert_called_once_with(
1155+
fields=LIST_FIELDS_TO_RETRIEVE + LIST_FIELDS_TO_RETRIEVE_LONG)
11321156
self.assertEqual(self.columns_long, columns)
11331157
self.assertListItemEqual(self.data_long, list(data))
11341158

@@ -1142,7 +1166,10 @@ def test_port_list_host(self):
11421166
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
11431167

11441168
columns, data = self.cmd.take_action(parsed_args)
1145-
filters = {'binding:host_id': 'foobar'}
1169+
filters = {
1170+
'binding:host_id': 'foobar',
1171+
'fields': LIST_FIELDS_TO_RETRIEVE,
1172+
}
11461173

11471174
self.network.ports.assert_called_once_with(**filters)
11481175
self.assertEqual(self.columns, columns)
@@ -1160,7 +1187,11 @@ def test_port_list_project(self):
11601187
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
11611188

11621189
columns, data = self.cmd.take_action(parsed_args)
1163-
filters = {'tenant_id': project.id, 'project_id': project.id}
1190+
filters = {
1191+
'tenant_id': project.id,
1192+
'project_id': project.id,
1193+
'fields': LIST_FIELDS_TO_RETRIEVE,
1194+
}
11641195

11651196
self.network.ports.assert_called_once_with(**filters)
11661197
self.assertEqual(self.columns, columns)
@@ -1180,7 +1211,11 @@ def test_port_list_project_domain(self):
11801211
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
11811212

11821213
columns, data = self.cmd.take_action(parsed_args)
1183-
filters = {'tenant_id': project.id, 'project_id': project.id}
1214+
filters = {
1215+
'tenant_id': project.id,
1216+
'project_id': project.id,
1217+
'fields': LIST_FIELDS_TO_RETRIEVE,
1218+
}
11841219

11851220
self.network.ports.assert_called_once_with(**filters)
11861221
self.assertEqual(self.columns, columns)
@@ -1206,7 +1241,8 @@ def test_list_with_tag_options(self):
12061241
**{'tags': 'red,blue',
12071242
'any_tags': 'red,green',
12081243
'not_tags': 'orange,yellow',
1209-
'not_any_tags': 'black,white'}
1244+
'not_any_tags': 'black,white',
1245+
'fields': LIST_FIELDS_TO_RETRIEVE}
12101246
)
12111247
self.assertEqual(self.columns, columns)
12121248
self.assertListItemEqual(self.data, list(data))

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pbr!=2.1.0,>=2.0.0 # Apache-2.0
55

66
cliff>=3.4.0 # Apache-2.0
77
iso8601>=0.1.11 # MIT
8-
openstacksdk>=0.48.0 # Apache-2.0
8+
openstacksdk>=0.51.0 # Apache-2.0
99
osc-lib>=2.2.0 # Apache-2.0
1010
oslo.i18n>=3.15.3 # Apache-2.0
1111
python-keystoneclient>=3.22.0 # Apache-2.0

0 commit comments

Comments
 (0)