From 8549071363805a9eef815dd2429b6b860db11a2c Mon Sep 17 00:00:00 2001 From: David Rabel Date: Mon, 13 Mar 2017 15:16:48 +0100 Subject: [PATCH] Add --network and --port to server create --nic option is quite unhandy. It is better to have two seperate options --network and --port to add a network to a new server. Change-Id: I523abdc83ca2dd4c5dd3871f8f109c2bf57c2e02 Closes-Bug: #1612898 --- doc/source/command-objects/server.rst | 26 +++++++++++ openstackclient/compute/v2/server.py | 40 +++++++++++++++-- .../tests/unit/compute/v2/test_server.py | 44 ++++++++++++++----- .../notes/bug-1612898-bea3b68251d12d81.yaml | 6 +++ 4 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 releasenotes/notes/bug-1612898-bea3b68251d12d81.yaml diff --git a/doc/source/command-objects/server.rst b/doc/source/command-objects/server.rst index b2ae965a7b..3d2bbdf34c 100644 --- a/doc/source/command-objects/server.rst +++ b/doc/source/command-objects/server.rst @@ -108,6 +108,8 @@ Create a new server [--availability-zone ] [--block-device-mapping [...] ] [--nic [...] ] + [--network ] + [--port ] [--hint [...] ] [--config-drive |True ] [--min ] @@ -176,6 +178,20 @@ Create a new server Specifying a --nic of auto or none cannot be used with any other --nic value. +.. option:: --network + + Create a NIC on the server and connect it to network. + Specify option multiple times to create multiple NICs. + For more options on NICs see --nic parameter. + network: attach NIC to this network + +.. option:: --port + + Create a NIC on the server and connect it to port. + Specify option multiple times to create multiple NICs. + For more options on NICs see --nic parameter. + port: attach NIC to this port + .. option:: --hint Hints for the scheduler (optional extension) @@ -200,6 +216,16 @@ Create a new server New server name +.. + +The parameters ``--network `` and ``--port `` are actually +wrappers to ``--nic net-id=`` and ``--nic port-id=``. ``--nic`` +also provides additional options to specify an IP address, automatic network +assignment and NICs which are not assigned to any port. This functionality +is not part of ``--network`` and ``--port``, which aim to provide a simple +syntax for the standard use cases of connecting a new server to a given +network or port. + server delete ------------- diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index d33c631a17..c2482bb161 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -114,6 +114,16 @@ def _get_ip_address(addresses, address_type, ip_address_family): ) +def _prefix_checked_value(prefix): + def func(value): + if ',' in value or '=' in value: + msg = _("Invalid argument %s, " + "characters ',' and '=' are not allowed") % value + raise argparse.ArgumentTypeError(msg) + return prefix + value + return func + + def _prep_server_detail(compute_client, image_client, server): """Prepare the detailed server dict for printing @@ -404,7 +414,6 @@ class CreateServer(command.ShowOne): metavar="", action='append', - default=[], help=_("Create a NIC on the server. " "Specify option multiple times to create multiple NICs. " "Either net-id or port-id must be provided, but not both. " @@ -417,6 +426,28 @@ class CreateServer(command.ShowOne): "allocate a network. Specifying a --nic of auto or none " "cannot be used with any other --nic value."), ) + parser.add_argument( + '--network', + metavar="", + action='append', + dest='nic', + type=_prefix_checked_value('net-id='), + help=_("Create a NIC on the server and connect it to network. " + "Specify option multiple times to create multiple NICs. " + "For more options on NICs see --nic parameter. " + "network: attach NIC to this network "), + ) + parser.add_argument( + '--port', + metavar="", + action='append', + dest='nic', + type=_prefix_checked_value('port-id='), + help=_("Create a NIC on the server and connect it to port. " + "Specify option multiple times to create multiple NICs. " + "For more options on NICs see --nic parameter. " + "port: attach NIC this port "), + ) parser.add_argument( '--hint', metavar='', @@ -549,6 +580,8 @@ class CreateServer(command.ShowOne): nics = [] auto_or_none = False + if parsed_args.nic is None: + parsed_args.nic = [] for nic_str in parsed_args.nic: # Handle the special auto/none cases if nic_str in ('auto', 'none'): @@ -564,7 +597,7 @@ class CreateServer(command.ShowOne): msg = _('Invalid --nic argument %s.') % nic_str raise exceptions.CommandError(msg) if bool(nic_info["net-id"]) == bool(nic_info["port-id"]): - msg = _("either net-id or port-id should be specified " + msg = _("either network or port should be specified " "but not both") raise exceptions.CommandError(msg) if self.app.client_manager.is_network_endpoint_enabled(): @@ -593,7 +626,8 @@ class CreateServer(command.ShowOne): if auto_or_none: if len(nics) > 1: msg = _('Specifying a --nic of auto or none cannot ' - 'be used with any other --nic value.') + 'be used with any other --nic, --network ' + 'or --port value.') raise exceptions.CommandError(msg) nics = nics[0] else: diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index 249902bca4..600b872e54 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -384,14 +384,18 @@ class TestServerCreate(TestServer): arglist = [ '--image', 'image1', '--flavor', 'flavor1', - '--nic', 'net-id=net1', - '--nic', 'port-id=port1', + '--network', 'net1', + '--nic', 'net-id=net1,v4-fixed-ip=10.0.0.2', + '--port', 'port1', + '--network', 'net1', + '--nic', 'port-id=port2', self.new_server.name, ] verifylist = [ ('image', 'image1'), ('flavor', 'flavor1'), - ('nic', ['net-id=net1', 'port-id=port1']), + ('nic', ['net-id=net1', 'net-id=net1,v4-fixed-ip=10.0.0.2', + 'port-id=port1', 'net-id=net1', 'port-id=port2']), ('config_drive', False), ('server_name', self.new_server.name), ] @@ -411,20 +415,28 @@ class TestServerCreate(TestServer): network_client.find_port = find_port network_resource = mock.Mock() network_resource.id = 'net1_uuid' - port_resource = mock.Mock() - port_resource.id = 'port1_uuid' + port1_resource = mock.Mock() + port1_resource.id = 'port1_uuid' + port2_resource = mock.Mock() + port2_resource.id = 'port2_uuid' find_network.return_value = network_resource - find_port.return_value = port_resource + find_port.side_effect = (lambda port_id, ignore_missing: + {"port1": port1_resource, + "port2": port2_resource}[port_id]) # Mock sdk APIs. _network = mock.Mock() _network.id = 'net1_uuid' - _port = mock.Mock() - _port.id = 'port1_uuid' + _port1 = mock.Mock() + _port1.id = 'port1_uuid' + _port2 = mock.Mock() + _port2.id = 'port2_uuid' find_network = mock.Mock() find_port = mock.Mock() find_network.return_value = _network - find_port.return_value = _port + find_port.side_effect = (lambda port_id, ignore_missing: + {"port1": _port1, + "port2": _port2}[port_id]) self.app.client_manager.network.find_network = find_network self.app.client_manager.network.find_port = find_port @@ -449,10 +461,22 @@ class TestServerCreate(TestServer): 'v4-fixed-ip': '', 'v6-fixed-ip': '', 'port-id': ''}, + {'net-id': 'net1_uuid', + 'v4-fixed-ip': '10.0.0.2', + 'v6-fixed-ip': '', + 'port-id': ''}, {'net-id': '', 'v4-fixed-ip': '', 'v6-fixed-ip': '', - 'port-id': 'port1_uuid'}], + 'port-id': 'port1_uuid'}, + {'net-id': 'net1_uuid', + 'v4-fixed-ip': '', + 'v6-fixed-ip': '', + 'port-id': ''}, + {'net-id': '', + 'v4-fixed-ip': '', + 'v6-fixed-ip': '', + 'port-id': 'port2_uuid'}], scheduler_hints={}, config_drive=None, ) diff --git a/releasenotes/notes/bug-1612898-bea3b68251d12d81.yaml b/releasenotes/notes/bug-1612898-bea3b68251d12d81.yaml new file mode 100644 index 0000000000..e31b75d20a --- /dev/null +++ b/releasenotes/notes/bug-1612898-bea3b68251d12d81.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add ``--network`` and ``--port`` options to ``server create`` command + as alternatives to ``--nic`` option. + [Bug `1612898 `_]