Skip to content

Commit f1345dc

Browse files
rubasovDean Troyer
authored andcommitted
Make MAC address of port updatable
openstackclient does not allow the update of a port's MAC address. However this is possible in neutron API (though by default policy it is an admin-only operation). Allow it in openstackclient too. Change-Id: Ibd9e0a6fbd1d0d461b8a8daee24dbb7c3f929df6 Closes-Bug: #1670707
1 parent b6f51cd commit f1345dc

5 files changed

Lines changed: 55 additions & 7 deletions

File tree

doc/source/command-objects/port.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ Set port properties
219219
[--host <host-id>]
220220
[--enable | --disable]
221221
[--name <name>]
222+
[--mac-address <mac-address>]
222223
[--security-group <security-group>]
223224
[--no-security-group]
224225
[--enable-port-security | --disable-port-security]
@@ -285,6 +286,10 @@ Set port properties
285286
286287
Set port name
287288
289+
.. option:: --mac-address
290+
291+
Set port's MAC address (admin only)
292+
288293
.. option:: --security-group <security-group>
289294
290295
Security group to associate with this port (name or ID)

openstackclient/network/v2/port.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ def _get_attrs(client_manager, parsed_args):
130130
attrs['binding:vnic_type'] = parsed_args.vnic_type
131131
if parsed_args.host:
132132
attrs['binding:host_id'] = parsed_args.host
133+
if parsed_args.mac_address is not None:
134+
attrs['mac_address'] = parsed_args.mac_address
133135

134136
if parsed_args.dns_name is not None:
135137
attrs['dns_name'] = parsed_args.dns_name
@@ -138,8 +140,6 @@ def _get_attrs(client_manager, parsed_args):
138140
attrs['name'] = str(parsed_args.name)
139141
# The remaining options do not support 'port set' command, so they require
140142
# additional check
141-
if 'mac_address' in parsed_args and parsed_args.mac_address is not None:
142-
attrs['mac_address'] = parsed_args.mac_address
143143
if 'network' in parsed_args and parsed_args.network is not None:
144144
attrs['network_id'] = parsed_args.network
145145
if 'project' in parsed_args and parsed_args.project is not None:
@@ -234,6 +234,11 @@ def _add_updatable_args(parser):
234234
metavar='<device-id>',
235235
help=argparse.SUPPRESS,
236236
)
237+
parser.add_argument(
238+
'--mac-address',
239+
metavar='<mac-address>',
240+
help=_("MAC address of this port (admin only)")
241+
)
237242
parser.add_argument(
238243
'--device-owner',
239244
metavar='<device-owner>',
@@ -324,11 +329,6 @@ def get_parser(self, prog_name):
324329
action='store_true',
325330
help=_("Disable port")
326331
)
327-
parser.add_argument(
328-
'--mac-address',
329-
metavar='<mac-address>',
330-
help=_("MAC address of this port")
331-
)
332332
parser.add_argument(
333333
'--project',
334334
metavar='<project>',

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,22 @@ def test_port_set(self):
147147
'port show -f json ' + self.NAME
148148
))
149149
self.assertEqual('', json_output.get('security_group_ids'))
150+
151+
def test_port_admin_set(self):
152+
"""Test create, set (as admin), show, delete"""
153+
json_output = json.loads(self.openstack(
154+
'port create -f json ' +
155+
'--network ' + self.NETWORK_NAME + ' ' + self.NAME
156+
))
157+
id_ = json_output.get('id')
158+
self.addCleanup(self.openstack, 'port delete ' + id_)
159+
160+
raw_output = self.openstack(
161+
'--os-username admin '
162+
+ 'port set --mac-address 11:22:33:44:55:66 '
163+
+ self.NAME)
164+
self.assertOutput('', raw_output)
165+
json_output = json.loads(self.openstack(
166+
'port show -f json ' + self.NAME
167+
))
168+
self.assertEqual(json_output.get('mac_address'), '11:22:33:44:55:66')

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,25 @@ def test_overwrite_fixed_ip(self):
987987
self.network.update_port.assert_called_once_with(_testport, **attrs)
988988
self.assertIsNone(result)
989989

990+
def test_overwrite_mac_address(self):
991+
_testport = network_fakes.FakePort.create_one_port(
992+
{'mac_address': '11:22:33:44:55:66'})
993+
self.network.find_port = mock.Mock(return_value=_testport)
994+
arglist = [
995+
'--mac-address', '66:55:44:33:22:11',
996+
_testport.name,
997+
]
998+
verifylist = [
999+
('mac_address', '66:55:44:33:22:11'),
1000+
]
1001+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1002+
result = self.cmd.take_action(parsed_args)
1003+
attrs = {
1004+
'mac_address': '66:55:44:33:22:11',
1005+
}
1006+
self.network.update_port.assert_called_once_with(_testport, **attrs)
1007+
self.assertIsNone(result)
1008+
9901009
def test_set_this(self):
9911010
arglist = [
9921011
'--disable',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Add ``--mac-address`` option to ``port set`` command.
5+
[Bug `1670707 <https://launchpad.net/bugs/1670707>`_]

0 commit comments

Comments
 (0)