Skip to content

Commit 8549071

Browse files
committed
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
1 parent 4a19f67 commit 8549071

4 files changed

Lines changed: 103 additions & 13 deletions

File tree

doc/source/command-objects/server.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ Create a new server
108108
[--availability-zone <zone-name>]
109109
[--block-device-mapping <dev-name=mapping> [...] ]
110110
[--nic <net-id=net-uuid,v4-fixed-ip=ip-addr,v6-fixed-ip=ip-addr,port-id=port-uuid,auto,none> [...] ]
111+
[--network <network>]
112+
[--port <port>]
111113
[--hint <key=value> [...] ]
112114
[--config-drive <value>|True ]
113115
[--min <count>]
@@ -176,6 +178,20 @@ Create a new server
176178
Specifying a --nic of auto or none cannot be used with any other
177179
--nic value.
178180

181+
.. option:: --network <network>
182+
183+
Create a NIC on the server and connect it to network.
184+
Specify option multiple times to create multiple NICs.
185+
For more options on NICs see --nic parameter.
186+
network: attach NIC to this network
187+
188+
.. option:: --port <port>
189+
190+
Create a NIC on the server and connect it to port.
191+
Specify option multiple times to create multiple NICs.
192+
For more options on NICs see --nic parameter.
193+
port: attach NIC to this port
194+
179195
.. option:: --hint <key=value>
180196

181197
Hints for the scheduler (optional extension)
@@ -200,6 +216,16 @@ Create a new server
200216

201217
New server name
202218

219+
..
220+
221+
The parameters ``--network <network>`` and ``--port <port>`` are actually
222+
wrappers to ``--nic net-id=<network>`` and ``--nic port-id=<port>``. ``--nic``
223+
also provides additional options to specify an IP address, automatic network
224+
assignment and NICs which are not assigned to any port. This functionality
225+
is not part of ``--network`` and ``--port``, which aim to provide a simple
226+
syntax for the standard use cases of connecting a new server to a given
227+
network or port.
228+
203229
server delete
204230
-------------
205231

openstackclient/compute/v2/server.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ def _get_ip_address(addresses, address_type, ip_address_family):
114114
)
115115

116116

117+
def _prefix_checked_value(prefix):
118+
def func(value):
119+
if ',' in value or '=' in value:
120+
msg = _("Invalid argument %s, "
121+
"characters ',' and '=' are not allowed") % value
122+
raise argparse.ArgumentTypeError(msg)
123+
return prefix + value
124+
return func
125+
126+
117127
def _prep_server_detail(compute_client, image_client, server):
118128
"""Prepare the detailed server dict for printing
119129
@@ -404,7 +414,6 @@ def get_parser(self, prog_name):
404414
metavar="<net-id=net-uuid,v4-fixed-ip=ip-addr,v6-fixed-ip=ip-addr,"
405415
"port-id=port-uuid,auto,none>",
406416
action='append',
407-
default=[],
408417
help=_("Create a NIC on the server. "
409418
"Specify option multiple times to create multiple NICs. "
410419
"Either net-id or port-id must be provided, but not both. "
@@ -417,6 +426,28 @@ def get_parser(self, prog_name):
417426
"allocate a network. Specifying a --nic of auto or none "
418427
"cannot be used with any other --nic value."),
419428
)
429+
parser.add_argument(
430+
'--network',
431+
metavar="<network>",
432+
action='append',
433+
dest='nic',
434+
type=_prefix_checked_value('net-id='),
435+
help=_("Create a NIC on the server and connect it to network. "
436+
"Specify option multiple times to create multiple NICs. "
437+
"For more options on NICs see --nic parameter. "
438+
"network: attach NIC to this network "),
439+
)
440+
parser.add_argument(
441+
'--port',
442+
metavar="<port>",
443+
action='append',
444+
dest='nic',
445+
type=_prefix_checked_value('port-id='),
446+
help=_("Create a NIC on the server and connect it to port. "
447+
"Specify option multiple times to create multiple NICs. "
448+
"For more options on NICs see --nic parameter. "
449+
"port: attach NIC this port "),
450+
)
420451
parser.add_argument(
421452
'--hint',
422453
metavar='<key=value>',
@@ -549,6 +580,8 @@ def take_action(self, parsed_args):
549580

550581
nics = []
551582
auto_or_none = False
583+
if parsed_args.nic is None:
584+
parsed_args.nic = []
552585
for nic_str in parsed_args.nic:
553586
# Handle the special auto/none cases
554587
if nic_str in ('auto', 'none'):
@@ -564,7 +597,7 @@ def take_action(self, parsed_args):
564597
msg = _('Invalid --nic argument %s.') % nic_str
565598
raise exceptions.CommandError(msg)
566599
if bool(nic_info["net-id"]) == bool(nic_info["port-id"]):
567-
msg = _("either net-id or port-id should be specified "
600+
msg = _("either network or port should be specified "
568601
"but not both")
569602
raise exceptions.CommandError(msg)
570603
if self.app.client_manager.is_network_endpoint_enabled():
@@ -593,7 +626,8 @@ def take_action(self, parsed_args):
593626
if auto_or_none:
594627
if len(nics) > 1:
595628
msg = _('Specifying a --nic of auto or none cannot '
596-
'be used with any other --nic value.')
629+
'be used with any other --nic, --network '
630+
'or --port value.')
597631
raise exceptions.CommandError(msg)
598632
nics = nics[0]
599633
else:

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,18 @@ def test_server_create_with_network(self):
384384
arglist = [
385385
'--image', 'image1',
386386
'--flavor', 'flavor1',
387-
'--nic', 'net-id=net1',
388-
'--nic', 'port-id=port1',
387+
'--network', 'net1',
388+
'--nic', 'net-id=net1,v4-fixed-ip=10.0.0.2',
389+
'--port', 'port1',
390+
'--network', 'net1',
391+
'--nic', 'port-id=port2',
389392
self.new_server.name,
390393
]
391394
verifylist = [
392395
('image', 'image1'),
393396
('flavor', 'flavor1'),
394-
('nic', ['net-id=net1', 'port-id=port1']),
397+
('nic', ['net-id=net1', 'net-id=net1,v4-fixed-ip=10.0.0.2',
398+
'port-id=port1', 'net-id=net1', 'port-id=port2']),
395399
('config_drive', False),
396400
('server_name', self.new_server.name),
397401
]
@@ -411,20 +415,28 @@ def test_server_create_with_network(self):
411415
network_client.find_port = find_port
412416
network_resource = mock.Mock()
413417
network_resource.id = 'net1_uuid'
414-
port_resource = mock.Mock()
415-
port_resource.id = 'port1_uuid'
418+
port1_resource = mock.Mock()
419+
port1_resource.id = 'port1_uuid'
420+
port2_resource = mock.Mock()
421+
port2_resource.id = 'port2_uuid'
416422
find_network.return_value = network_resource
417-
find_port.return_value = port_resource
423+
find_port.side_effect = (lambda port_id, ignore_missing:
424+
{"port1": port1_resource,
425+
"port2": port2_resource}[port_id])
418426

419427
# Mock sdk APIs.
420428
_network = mock.Mock()
421429
_network.id = 'net1_uuid'
422-
_port = mock.Mock()
423-
_port.id = 'port1_uuid'
430+
_port1 = mock.Mock()
431+
_port1.id = 'port1_uuid'
432+
_port2 = mock.Mock()
433+
_port2.id = 'port2_uuid'
424434
find_network = mock.Mock()
425435
find_port = mock.Mock()
426436
find_network.return_value = _network
427-
find_port.return_value = _port
437+
find_port.side_effect = (lambda port_id, ignore_missing:
438+
{"port1": _port1,
439+
"port2": _port2}[port_id])
428440
self.app.client_manager.network.find_network = find_network
429441
self.app.client_manager.network.find_port = find_port
430442

@@ -449,10 +461,22 @@ def test_server_create_with_network(self):
449461
'v4-fixed-ip': '',
450462
'v6-fixed-ip': '',
451463
'port-id': ''},
464+
{'net-id': 'net1_uuid',
465+
'v4-fixed-ip': '10.0.0.2',
466+
'v6-fixed-ip': '',
467+
'port-id': ''},
468+
{'net-id': '',
469+
'v4-fixed-ip': '',
470+
'v6-fixed-ip': '',
471+
'port-id': 'port1_uuid'},
472+
{'net-id': 'net1_uuid',
473+
'v4-fixed-ip': '',
474+
'v6-fixed-ip': '',
475+
'port-id': ''},
452476
{'net-id': '',
453477
'v4-fixed-ip': '',
454478
'v6-fixed-ip': '',
455-
'port-id': 'port1_uuid'}],
479+
'port-id': 'port2_uuid'}],
456480
scheduler_hints={},
457481
config_drive=None,
458482
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add ``--network`` and ``--port`` options to ``server create`` command
5+
as alternatives to ``--nic`` option.
6+
[Bug `1612898 <https://bugs.launchpad.net/bugs/1612898>`_]

0 commit comments

Comments
 (0)