Skip to content

Commit 6f60f83

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "openstack port create support --extra-dhcp-option"
2 parents 005a247 + 68809fc commit 6f60f83

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

openstackclient/network/v2/port.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,19 @@ def _convert_address_pairs(parsed_args):
280280
return ops
281281

282282

283+
def _convert_extra_dhcp_options(parsed_args):
284+
dhcp_options = []
285+
for opt in parsed_args.extra_dhcp_options:
286+
option = {}
287+
option['opt_name'] = opt['name']
288+
if 'value' in opt:
289+
option['opt_value'] = opt['value']
290+
if 'ip-version' in opt:
291+
option['ip_version'] = opt['ip-version']
292+
dhcp_options.append(option)
293+
return dhcp_options
294+
295+
283296
class CreatePort(command.ShowOne):
284297
_description = _("Create a new port")
285298

@@ -350,8 +363,18 @@ def get_parser(self, prog_name):
350363
metavar='<name>',
351364
help=_("Name of this port")
352365
)
353-
# TODO(singhj): Add support for extended options:
354-
# dhcp
366+
parser.add_argument(
367+
'--extra-dhcp-option',
368+
metavar='name=<name>[,value=<value>,ip-version={4,6}]',
369+
default=[],
370+
action=parseractions.MultiKeyValueCommaAction,
371+
dest='extra_dhcp_options',
372+
required_keys=['name'],
373+
optional_keys=['value', "ip-version"],
374+
help=_('Extra DHCP options to be assigned to this port: '
375+
'name=<name>[,value=<value>,ip-version={4,6}] '
376+
'(repeat option to set multiple extra DHCP options)'))
377+
355378
secgroups = parser.add_mutually_exclusive_group()
356379
secgroups.add_argument(
357380
'--security-group',
@@ -425,6 +448,9 @@ def take_action(self, parsed_args):
425448
attrs['allowed_address_pairs'] = (
426449
_convert_address_pairs(parsed_args))
427450

451+
if parsed_args.extra_dhcp_options:
452+
attrs["extra_dhcp_opts"] = _convert_extra_dhcp_options(parsed_args)
453+
428454
if parsed_args.qos_policy:
429455
attrs['qos_policy_id'] = client.find_qos_policy(
430456
parsed_args.qos_policy, ignore_missing=False).id

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,48 @@ def test_create_with_uplink_status_propagation_enabled(self):
611611
def test_create_with_uplink_status_propagation_disabled(self):
612612
self._test_create_with_uplink_status_propagation(enable=False)
613613

614+
def test_create_port_with_extra_dhcp_option(self):
615+
extra_dhcp_options = [{'opt_name': 'classless-static-route',
616+
'opt_value': '169.254.169.254/32,22.2.0.2,'
617+
'0.0.0.0/0,22.2.0.1',
618+
'ip_version': '4'},
619+
{'opt_name': 'dns-server',
620+
'opt_value': '240C::6666',
621+
'ip_version': '6'}]
622+
arglist = [
623+
'--network', self._port.network_id,
624+
'--extra-dhcp-option', 'name=classless-static-route,'
625+
'value=169.254.169.254/32,22.2.0.2,'
626+
'0.0.0.0/0,22.2.0.1,'
627+
'ip-version=4',
628+
'--extra-dhcp-option', 'name=dns-server,value=240C::6666,'
629+
'ip-version=6',
630+
'test-port',
631+
]
632+
633+
verifylist = [
634+
('network', self._port.network_id,),
635+
('extra_dhcp_options', [{'name': 'classless-static-route',
636+
'value': '169.254.169.254/32,22.2.0.2,'
637+
'0.0.0.0/0,22.2.0.1',
638+
'ip-version': '4'},
639+
{'name': 'dns-server',
640+
'value': '240C::6666',
641+
'ip-version': '6'}]),
642+
('name', 'test-port'),
643+
]
644+
645+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
646+
647+
self.cmd.take_action(parsed_args)
648+
649+
self.network.create_port.assert_called_once_with(**{
650+
'admin_state_up': True,
651+
'network_id': self._port.network_id,
652+
'extra_dhcp_opts': extra_dhcp_options,
653+
'name': 'test-port',
654+
})
655+
614656

615657
class TestDeletePort(TestPort):
616658

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add ``--extra-dhcp-options`` parameter to the ``port create`` command. The
5+
neutronclient ``port-create`` command can accept extra DHCP options, add
6+
it to the openstackclient in order to be consistent.

0 commit comments

Comments
 (0)