Skip to content

Commit 7977f7d

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add --network and --port to server create"
2 parents ef99f44 + 8549071 commit 7977f7d

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
@@ -138,6 +138,8 @@ Create a new server
138138
[--availability-zone <zone-name>]
139139
[--block-device-mapping <dev-name=mapping> [...] ]
140140
[--nic <net-id=net-uuid,v4-fixed-ip=ip-addr,v6-fixed-ip=ip-addr,port-id=port-uuid,auto,none> [...] ]
141+
[--network <network>]
142+
[--port <port>]
141143
[--hint <key=value> [...] ]
142144
[--config-drive <value>|True ]
143145
[--min <count>]
@@ -206,6 +208,20 @@ Create a new server
206208
Specifying a --nic of auto or none cannot be used with any other
207209
--nic value.
208210

211+
.. option:: --network <network>
212+
213+
Create a NIC on the server and connect it to network.
214+
Specify option multiple times to create multiple NICs.
215+
For more options on NICs see --nic parameter.
216+
network: attach NIC to this network
217+
218+
.. option:: --port <port>
219+
220+
Create a NIC on the server and connect it to port.
221+
Specify option multiple times to create multiple NICs.
222+
For more options on NICs see --nic parameter.
223+
port: attach NIC to this port
224+
209225
.. option:: --hint <key=value>
210226

211227
Hints for the scheduler (optional extension)
@@ -230,6 +246,16 @@ Create a new server
230246

231247
New server name
232248

249+
..
250+
251+
The parameters ``--network <network>`` and ``--port <port>`` are actually
252+
wrappers to ``--nic net-id=<network>`` and ``--nic port-id=<port>``. ``--nic``
253+
also provides additional options to specify an IP address, automatic network
254+
assignment and NICs which are not assigned to any port. This functionality
255+
is not part of ``--network`` and ``--port``, which aim to provide a simple
256+
syntax for the standard use cases of connecting a new server to a given
257+
network or port.
258+
233259
server delete
234260
-------------
235261

openstackclient/compute/v2/server.py

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

112112

113+
def _prefix_checked_value(prefix):
114+
def func(value):
115+
if ',' in value or '=' in value:
116+
msg = _("Invalid argument %s, "
117+
"characters ',' and '=' are not allowed") % value
118+
raise argparse.ArgumentTypeError(msg)
119+
return prefix + value
120+
return func
121+
122+
113123
def _prep_server_detail(compute_client, image_client, server):
114124
"""Prepare the detailed server dict for printing
115125
@@ -447,7 +457,6 @@ def get_parser(self, prog_name):
447457
metavar="<net-id=net-uuid,v4-fixed-ip=ip-addr,v6-fixed-ip=ip-addr,"
448458
"port-id=port-uuid,auto,none>",
449459
action='append',
450-
default=[],
451460
help=_("Create a NIC on the server. "
452461
"Specify option multiple times to create multiple NICs. "
453462
"Either net-id or port-id must be provided, but not both. "
@@ -460,6 +469,28 @@ def get_parser(self, prog_name):
460469
"allocate a network. Specifying a --nic of auto or none "
461470
"cannot be used with any other --nic value."),
462471
)
472+
parser.add_argument(
473+
'--network',
474+
metavar="<network>",
475+
action='append',
476+
dest='nic',
477+
type=_prefix_checked_value('net-id='),
478+
help=_("Create a NIC on the server and connect it to network. "
479+
"Specify option multiple times to create multiple NICs. "
480+
"For more options on NICs see --nic parameter. "
481+
"network: attach NIC to this network "),
482+
)
483+
parser.add_argument(
484+
'--port',
485+
metavar="<port>",
486+
action='append',
487+
dest='nic',
488+
type=_prefix_checked_value('port-id='),
489+
help=_("Create a NIC on the server and connect it to port. "
490+
"Specify option multiple times to create multiple NICs. "
491+
"For more options on NICs see --nic parameter. "
492+
"port: attach NIC this port "),
493+
)
463494
parser.add_argument(
464495
'--hint',
465496
metavar='<key=value>',
@@ -592,6 +623,8 @@ def take_action(self, parsed_args):
592623

593624
nics = []
594625
auto_or_none = False
626+
if parsed_args.nic is None:
627+
parsed_args.nic = []
595628
for nic_str in parsed_args.nic:
596629
# Handle the special auto/none cases
597630
if nic_str in ('auto', 'none'):
@@ -607,7 +640,7 @@ def take_action(self, parsed_args):
607640
msg = _('Invalid --nic argument %s.') % nic_str
608641
raise exceptions.CommandError(msg)
609642
if bool(nic_info["net-id"]) == bool(nic_info["port-id"]):
610-
msg = _("either net-id or port-id should be specified "
643+
msg = _("either network or port should be specified "
611644
"but not both")
612645
raise exceptions.CommandError(msg)
613646
if self.app.client_manager.is_network_endpoint_enabled():
@@ -636,7 +669,8 @@ def take_action(self, parsed_args):
636669
if auto_or_none:
637670
if len(nics) > 1:
638671
msg = _('Specifying a --nic of auto or none cannot '
639-
'be used with any other --nic value.')
672+
'be used with any other --nic, --network '
673+
'or --port value.')
640674
raise exceptions.CommandError(msg)
641675
nics = nics[0]
642676
else:

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,18 @@ def test_server_create_with_network(self):
447447
arglist = [
448448
'--image', 'image1',
449449
'--flavor', 'flavor1',
450-
'--nic', 'net-id=net1',
451-
'--nic', 'port-id=port1',
450+
'--network', 'net1',
451+
'--nic', 'net-id=net1,v4-fixed-ip=10.0.0.2',
452+
'--port', 'port1',
453+
'--network', 'net1',
454+
'--nic', 'port-id=port2',
452455
self.new_server.name,
453456
]
454457
verifylist = [
455458
('image', 'image1'),
456459
('flavor', 'flavor1'),
457-
('nic', ['net-id=net1', 'port-id=port1']),
460+
('nic', ['net-id=net1', 'net-id=net1,v4-fixed-ip=10.0.0.2',
461+
'port-id=port1', 'net-id=net1', 'port-id=port2']),
458462
('config_drive', False),
459463
('server_name', self.new_server.name),
460464
]
@@ -474,20 +478,28 @@ def test_server_create_with_network(self):
474478
network_client.find_port = find_port
475479
network_resource = mock.Mock()
476480
network_resource.id = 'net1_uuid'
477-
port_resource = mock.Mock()
478-
port_resource.id = 'port1_uuid'
481+
port1_resource = mock.Mock()
482+
port1_resource.id = 'port1_uuid'
483+
port2_resource = mock.Mock()
484+
port2_resource.id = 'port2_uuid'
479485
find_network.return_value = network_resource
480-
find_port.return_value = port_resource
486+
find_port.side_effect = (lambda port_id, ignore_missing:
487+
{"port1": port1_resource,
488+
"port2": port2_resource}[port_id])
481489

482490
# Mock sdk APIs.
483491
_network = mock.Mock()
484492
_network.id = 'net1_uuid'
485-
_port = mock.Mock()
486-
_port.id = 'port1_uuid'
493+
_port1 = mock.Mock()
494+
_port1.id = 'port1_uuid'
495+
_port2 = mock.Mock()
496+
_port2.id = 'port2_uuid'
487497
find_network = mock.Mock()
488498
find_port = mock.Mock()
489499
find_network.return_value = _network
490-
find_port.return_value = _port
500+
find_port.side_effect = (lambda port_id, ignore_missing:
501+
{"port1": _port1,
502+
"port2": _port2}[port_id])
491503
self.app.client_manager.network.find_network = find_network
492504
self.app.client_manager.network.find_port = find_port
493505

@@ -512,10 +524,22 @@ def test_server_create_with_network(self):
512524
'v4-fixed-ip': '',
513525
'v6-fixed-ip': '',
514526
'port-id': ''},
527+
{'net-id': 'net1_uuid',
528+
'v4-fixed-ip': '10.0.0.2',
529+
'v6-fixed-ip': '',
530+
'port-id': ''},
531+
{'net-id': '',
532+
'v4-fixed-ip': '',
533+
'v6-fixed-ip': '',
534+
'port-id': 'port1_uuid'},
535+
{'net-id': 'net1_uuid',
536+
'v4-fixed-ip': '',
537+
'v6-fixed-ip': '',
538+
'port-id': ''},
515539
{'net-id': '',
516540
'v4-fixed-ip': '',
517541
'v6-fixed-ip': '',
518-
'port-id': 'port1_uuid'}],
542+
'port-id': 'port2_uuid'}],
519543
scheduler_hints={},
520544
config_drive=None,
521545
)
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)