From 68809fce5a1073659001a87aee4f9407affd5d0e Mon Sep 17 00:00:00 2001 From: zhouhenglc Date: Tue, 7 May 2019 10:52:49 +0800 Subject: [PATCH] openstack port create support --extra-dhcp-option neutron create-port API has extra_dhcp_opts parameter, this parameter can set port with special extra dhcp options. Change-Id: I199f17e95c509a33f809ac85c65f685a37acd198 --- openstackclient/network/v2/port.py | 30 ++++++++++++- .../tests/unit/network/v2/test_port.py | 42 +++++++++++++++++++ ...h-extra-dhcp-options-c2c40e4002b52e2a.yaml | 6 +++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/allow-port-create-with-extra-dhcp-options-c2c40e4002b52e2a.yaml diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index d42fbb683..e6bfe8537 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -280,6 +280,19 @@ def _convert_address_pairs(parsed_args): return ops +def _convert_extra_dhcp_options(parsed_args): + dhcp_options = [] + for opt in parsed_args.extra_dhcp_options: + option = {} + option['opt_name'] = opt['name'] + if 'value' in opt: + option['opt_value'] = opt['value'] + if 'ip-version' in opt: + option['ip_version'] = opt['ip-version'] + dhcp_options.append(option) + return dhcp_options + + class CreatePort(command.ShowOne): _description = _("Create a new port") @@ -350,8 +363,18 @@ class CreatePort(command.ShowOne): metavar='', help=_("Name of this port") ) - # TODO(singhj): Add support for extended options: - # dhcp + parser.add_argument( + '--extra-dhcp-option', + metavar='name=[,value=,ip-version={4,6}]', + default=[], + action=parseractions.MultiKeyValueCommaAction, + dest='extra_dhcp_options', + required_keys=['name'], + optional_keys=['value', "ip-version"], + help=_('Extra DHCP options to be assigned to this port: ' + 'name=[,value=,ip-version={4,6}] ' + '(repeat option to set multiple extra DHCP options)')) + secgroups = parser.add_mutually_exclusive_group() secgroups.add_argument( '--security-group', @@ -425,6 +448,9 @@ class CreatePort(command.ShowOne): attrs['allowed_address_pairs'] = ( _convert_address_pairs(parsed_args)) + if parsed_args.extra_dhcp_options: + attrs["extra_dhcp_opts"] = _convert_extra_dhcp_options(parsed_args) + if parsed_args.qos_policy: attrs['qos_policy_id'] = client.find_qos_policy( parsed_args.qos_policy, ignore_missing=False).id diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index acf85f465..c30d682f5 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -611,6 +611,48 @@ class TestCreatePort(TestPort): def test_create_with_uplink_status_propagation_disabled(self): self._test_create_with_uplink_status_propagation(enable=False) + def test_create_port_with_extra_dhcp_option(self): + extra_dhcp_options = [{'opt_name': 'classless-static-route', + 'opt_value': '169.254.169.254/32,22.2.0.2,' + '0.0.0.0/0,22.2.0.1', + 'ip_version': '4'}, + {'opt_name': 'dns-server', + 'opt_value': '240C::6666', + 'ip_version': '6'}] + arglist = [ + '--network', self._port.network_id, + '--extra-dhcp-option', 'name=classless-static-route,' + 'value=169.254.169.254/32,22.2.0.2,' + '0.0.0.0/0,22.2.0.1,' + 'ip-version=4', + '--extra-dhcp-option', 'name=dns-server,value=240C::6666,' + 'ip-version=6', + 'test-port', + ] + + verifylist = [ + ('network', self._port.network_id,), + ('extra_dhcp_options', [{'name': 'classless-static-route', + 'value': '169.254.169.254/32,22.2.0.2,' + '0.0.0.0/0,22.2.0.1', + 'ip-version': '4'}, + {'name': 'dns-server', + 'value': '240C::6666', + 'ip-version': '6'}]), + ('name', 'test-port'), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.network.create_port.assert_called_once_with(**{ + 'admin_state_up': True, + 'network_id': self._port.network_id, + 'extra_dhcp_opts': extra_dhcp_options, + 'name': 'test-port', + }) + class TestDeletePort(TestPort): diff --git a/releasenotes/notes/allow-port-create-with-extra-dhcp-options-c2c40e4002b52e2a.yaml b/releasenotes/notes/allow-port-create-with-extra-dhcp-options-c2c40e4002b52e2a.yaml new file mode 100644 index 000000000..ed4fa1bca --- /dev/null +++ b/releasenotes/notes/allow-port-create-with-extra-dhcp-options-c2c40e4002b52e2a.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add ``--extra-dhcp-options`` parameter to the ``port create`` command. The + neutronclient ``port-create`` command can accept extra DHCP options, add + it to the openstackclient in order to be consistent. \ No newline at end of file