Skip to content

Commit e0d5864

Browse files
author
Jas
committed
Add 'port set' command
Add CLI support for the 'port set' command Change-Id: I2bea508e11290284aa64b1ab548a0bb61e7290d3 Partial-bug: #1519909 Partially-implements: blueprint neutron-client
1 parent 9ab7740 commit e0d5864

5 files changed

Lines changed: 194 additions & 5 deletions

File tree

doc/source/command-objects/port.rst

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ Create new port
4545
4646
.. option:: --vnic-type <vnic-type>
4747
48-
VNIC type for this port (direct | direct-physical | macvtap | normal(default) | baremetal)
48+
VNIC type for this port (direct | direct-physical | macvtap | normal | baremetal).
49+
If unspecified during port creation, default value will be 'normal'.
4950
5051
.. option:: --binding-profile <binding-profile>
5152
@@ -108,6 +109,65 @@ List ports
108109
109110
os port list
110111
112+
port set
113+
--------
114+
115+
Set port properties
116+
117+
.. program:: port set
118+
.. code:: bash
119+
120+
os port set
121+
[--fixed-ip subnet=<subnet>,ip-address=<ip-address>]
122+
[--device-id <device-id>]
123+
[--device-owner <device-owner>]
124+
[--vnic-type <vnic-type>]
125+
[--binding-profile <binding-profile>]
126+
[--host-id <host-id>]
127+
[--enable | --disable]
128+
<port>
129+
130+
.. option:: --fixed-ip subnet=<subnet>,ip-address=<ip-address>
131+
132+
Desired IP and/or subnet for this port:
133+
subnet=<subnet>,ip-address=<ip-address>
134+
(you can repeat this option)
135+
136+
.. option:: --device-id <device-id>
137+
138+
Device ID of this port
139+
140+
.. option:: --device-owner <device-owner>
141+
142+
Device owner of this port
143+
144+
.. option:: --vnic-type <vnic-type>
145+
146+
VNIC type for this port (direct | direct-physical | macvtap | normal | baremetal).
147+
If unspecified during port creation, default value will be 'normal'.
148+
149+
.. option:: --binding-profile <binding-profile>
150+
151+
Custom data to be passed as binding:profile: <key>=<value>
152+
(this option can be repeated)
153+
154+
.. option:: --host-id <host-id>
155+
156+
The ID of the host where the port is allocated
157+
158+
.. option:: --enable
159+
160+
Enable port
161+
162+
.. option:: --disable
163+
164+
Disable port
165+
166+
.. _port_set-port:
167+
.. describe:: <port>
168+
169+
Port to modify (name or ID)
170+
111171
port show
112172
---------
113173

openstackclient/network/v2/port.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""Port action implementations"""
1515

1616
from openstackclient.common import command
17+
from openstackclient.common import exceptions
1718
from openstackclient.common import parseractions
1819
from openstackclient.common import utils
1920
from openstackclient.identity import common as identity_common
@@ -56,8 +57,6 @@ def _get_columns(item):
5657
def _get_attrs(client_manager, parsed_args):
5758
attrs = {}
5859

59-
if parsed_args.name is not None:
60-
attrs['name'] = str(parsed_args.name)
6160
if parsed_args.fixed_ip is not None:
6261
attrs['fixed_ips'] = parsed_args.fixed_ip
6362
if parsed_args.device_id is not None:
@@ -75,6 +74,8 @@ def _get_attrs(client_manager, parsed_args):
7574

7675
# The remaining options do not support 'port set' command, so they require
7776
# additional check
77+
if 'name' in parsed_args and parsed_args.name is not None:
78+
attrs['name'] = str(parsed_args.name)
7879
if 'mac_address' in parsed_args and parsed_args.mac_address is not None:
7980
attrs['mac_address'] = parsed_args.mac_address
8081
if 'network' in parsed_args and parsed_args.network is not None:
@@ -145,8 +146,9 @@ def _add_updatable_args(parser):
145146
metavar='<vnic-type>',
146147
choices=['direct', 'direct-physical', 'macvtap',
147148
'normal', 'baremetal'],
148-
help='VNIC type for this port (direct | direct-physical |'
149-
' macvtap | normal(default) | baremetal)')
149+
help="VNIC type for this port (direct | direct-physical |"
150+
" macvtap | normal | baremetal). If unspecified during"
151+
" port creation, default value will be 'normal'.")
150152
parser.add_argument(
151153
'--binding-profile',
152154
metavar='<binding-profile>',
@@ -265,6 +267,48 @@ def take_action(self, parsed_args):
265267
) for s in data))
266268

267269

270+
class SetPort(command.Command):
271+
"""Set port properties"""
272+
273+
def get_parser(self, prog_name):
274+
parser = super(SetPort, self).get_parser(prog_name)
275+
_add_updatable_args(parser)
276+
admin_group = parser.add_mutually_exclusive_group()
277+
admin_group.add_argument(
278+
'--enable',
279+
dest='admin_state',
280+
action='store_true',
281+
default=None,
282+
help='Enable port',
283+
)
284+
admin_group.add_argument(
285+
'--disable',
286+
dest='admin_state',
287+
action='store_false',
288+
help='Disable port',
289+
)
290+
parser.add_argument(
291+
'port',
292+
metavar="<port>",
293+
help=("Port to modify (name or ID)")
294+
)
295+
296+
return parser
297+
298+
def take_action(self, parsed_args):
299+
client = self.app.client_manager.network
300+
301+
_prepare_fixed_ips(self.app.client_manager, parsed_args)
302+
attrs = _get_attrs(self.app.client_manager, parsed_args)
303+
304+
if attrs == {}:
305+
msg = "Nothing specified to be set"
306+
raise exceptions.CommandError(msg)
307+
308+
obj = client.find_port(parsed_args.port, ignore_missing=False)
309+
client.update_port(obj, **attrs)
310+
311+
268312
class ShowPort(command.ShowOne):
269313
"""Display port details"""
270314

openstackclient/tests/network/v2/test_port.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,85 @@ def test_port_list_no_options(self):
240240
self.assertEqual(self.data, list(data))
241241

242242

243+
class TestSetPort(TestPort):
244+
245+
_port = network_fakes.FakePort.create_one_port()
246+
247+
def setUp(self):
248+
super(TestSetPort, self).setUp()
249+
250+
self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet()
251+
self.network.find_subnet = mock.Mock(return_value=self.fake_subnet)
252+
self.network.find_port = mock.Mock(return_value=self._port)
253+
self.network.update_port = mock.Mock(return_value=None)
254+
255+
# Get the command object to test
256+
self.cmd = port.SetPort(self.app, self.namespace)
257+
258+
def test_set_fixed_ip(self):
259+
arglist = [
260+
'--fixed-ip', 'ip-address=10.0.0.11',
261+
self._port.name,
262+
]
263+
verifylist = [
264+
('fixed_ip', [{'ip-address': '10.0.0.11'}]),
265+
]
266+
267+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
268+
result = self.cmd.take_action(parsed_args)
269+
270+
attrs = {
271+
'fixed_ips': [{'ip_address': '10.0.0.11'}],
272+
}
273+
self.network.update_port.assert_called_with(self._port, **attrs)
274+
self.assertIsNone(result)
275+
276+
def test_set_this(self):
277+
arglist = [
278+
'--disable',
279+
self._port.name,
280+
]
281+
verifylist = [
282+
('admin_state', False),
283+
]
284+
285+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
286+
result = self.cmd.take_action(parsed_args)
287+
288+
attrs = {
289+
'admin_state_up': False,
290+
}
291+
self.network.update_port.assert_called_with(self._port, **attrs)
292+
self.assertIsNone(result)
293+
294+
def test_set_that(self):
295+
arglist = [
296+
'--enable',
297+
'--vnic-type', 'macvtap',
298+
'--binding-profile', 'foo=bar',
299+
'--host-id', 'binding-host-id-xxxx',
300+
self._port.name,
301+
]
302+
verifylist = [
303+
('admin_state', True),
304+
('vnic_type', 'macvtap'),
305+
('binding_profile', {'foo': 'bar'}),
306+
('host_id', 'binding-host-id-xxxx'),
307+
]
308+
309+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
310+
result = self.cmd.take_action(parsed_args)
311+
312+
attrs = {
313+
'admin_state_up': True,
314+
'binding:vnic_type': 'macvtap',
315+
'binding:profile': {'foo': 'bar'},
316+
'binding:host_id': 'binding-host-id-xxxx',
317+
}
318+
self.network.update_port.assert_called_with(self._port, **attrs)
319+
self.assertIsNone(result)
320+
321+
243322
class TestShowPort(TestPort):
244323

245324
# The port to show.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Add support for the ``port set`` command.
5+
[Bug `1519909 <https://bugs.launchpad.net/python-openstackclient/+bug/1519909>`_]

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ openstack.network.v2 =
336336
port_create = openstackclient.network.v2.port:CreatePort
337337
port_delete = openstackclient.network.v2.port:DeletePort
338338
port_list = openstackclient.network.v2.port:ListPort
339+
port_set = openstackclient.network.v2.port:SetPort
339340
port_show = openstackclient.network.v2.port:ShowPort
340341

341342
router_create = openstackclient.network.v2.router:CreateRouter

0 commit comments

Comments
 (0)