Skip to content

Commit f66f989

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Append existing information during port set"
2 parents 881c836 + c92ac9d commit f66f989

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

openstackclient/network/v2/port.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
def _format_admin_state(state):
3131
return 'UP' if state else 'DOWN'
3232

33+
3334
_formatters = {
3435
'admin_state_up': _format_admin_state,
3536
'allowed_address_pairs': utils.format_list_of_dicts,
@@ -383,17 +384,24 @@ def take_action(self, parsed_args):
383384

384385
_prepare_fixed_ips(self.app.client_manager, parsed_args)
385386
attrs = _get_attrs(self.app.client_manager, parsed_args)
386-
387-
if parsed_args.no_fixed_ip:
388-
attrs['fixed_ips'] = []
389-
if parsed_args.no_binding_profile:
387+
obj = client.find_port(parsed_args.port, ignore_missing=False)
388+
if 'binding:profile' in attrs:
389+
attrs['binding:profile'].update(obj.binding_profile)
390+
elif parsed_args.no_binding_profile:
390391
attrs['binding:profile'] = {}
392+
if 'fixed_ips' in attrs:
393+
# When user unsets the fixed_ips, obj.fixed_ips = [{}].
394+
# Adding the obj.fixed_ips list to attrs['fixed_ips']
395+
# would therefore add an empty dictionary, while we need
396+
# to append the attrs['fixed_ips'] iff there is some info
397+
# in the obj.fixed_ips. Therefore I have opted for this `for` loop
398+
attrs['fixed_ips'] += [ip for ip in obj.fixed_ips if ip]
399+
elif parsed_args.no_fixed_ip:
400+
attrs['fixed_ips'] = []
391401

392402
if attrs == {}:
393403
msg = "Nothing specified to be set"
394404
raise exceptions.CommandError(msg)
395-
396-
obj = client.find_port(parsed_args.port, ignore_missing=False)
397405
client.update_port(obj, **attrs)
398406

399407

openstackclient/tests/network/v2/test_port.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ class TestSetPort(TestPort):
268268

269269
def setUp(self):
270270
super(TestSetPort, self).setUp()
271-
272271
self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet()
273272
self.network.find_subnet = mock.Mock(return_value=self.fake_subnet)
274273
self.network.find_port = mock.Mock(return_value=self._port)
@@ -295,6 +294,26 @@ def test_set_fixed_ip(self):
295294
self.network.update_port.assert_called_once_with(self._port, **attrs)
296295
self.assertIsNone(result)
297296

297+
def test_append_fixed_ip(self):
298+
_testport = network_fakes.FakePort.create_one_port(
299+
{'fixed_ips': [{'ip_address': '0.0.0.1'}]})
300+
self.network.find_port = mock.Mock(return_value=_testport)
301+
arglist = [
302+
'--fixed-ip', 'ip-address=10.0.0.12',
303+
_testport.name,
304+
]
305+
verifylist = [
306+
('fixed_ip', [{'ip-address': '10.0.0.12'}]),
307+
]
308+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
309+
result = self.cmd.take_action(parsed_args)
310+
attrs = {
311+
'fixed_ips': [
312+
{'ip_address': '10.0.0.12'}, {'ip_address': '0.0.0.1'}],
313+
}
314+
self.network.update_port.assert_called_once_with(_testport, **attrs)
315+
self.assertIsNone(result)
316+
298317
def test_set_this(self):
299318
arglist = [
300319
'--disable',

0 commit comments

Comments
 (0)