Skip to content

Commit a7975c4

Browse files
committed
compute: Add '--no-network', '--auto-network' flags
These are aliases for '--nic none' and '--nic auto', respectively. Change-Id: I7b4f7e5c3769a813bd8b2b9cd6090c6fe501e13d Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
1 parent ccd9356 commit a7975c4

3 files changed

Lines changed: 136 additions & 38 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ def get_parser(self, prog_name):
409409
'--tag',
410410
metavar='<tag>',
411411
help=_(
412-
"Tag for the attached interface. "
413-
"(Supported by API versions '2.49' - '2.latest')"
412+
'Tag for the attached interface '
413+
'(supported by --os-compute-api-version 2.49 or later)'
414414
)
415415
)
416416
return parser
@@ -652,29 +652,68 @@ def take_action(self, parsed_args):
652652
)
653653

654654

655-
# TODO(stephenfin): Replace with 'MultiKeyValueAction' when we no longer
656-
# support '--nic=auto' and '--nic=none'
655+
class NoneNICAction(argparse.Action):
656+
657+
def __init__(self, option_strings, dest, help=None):
658+
super().__init__(
659+
option_strings=option_strings,
660+
dest=dest,
661+
nargs=0,
662+
default=[],
663+
required=False,
664+
help=help,
665+
)
666+
667+
def __call__(self, parser, namespace, values, option_string=None):
668+
# Make sure we have an empty dict rather than None
669+
if getattr(namespace, self.dest, None) is None:
670+
setattr(namespace, self.dest, [])
671+
672+
getattr(namespace, self.dest).append('none')
673+
674+
675+
class AutoNICAction(argparse.Action):
676+
677+
def __init__(self, option_strings, dest, help=None):
678+
super().__init__(
679+
option_strings=option_strings,
680+
dest=dest,
681+
nargs=0,
682+
default=[],
683+
required=False,
684+
help=help,
685+
)
686+
687+
def __call__(self, parser, namespace, values, option_string=None):
688+
# Make sure we have an empty dict rather than None
689+
if getattr(namespace, self.dest, None) is None:
690+
setattr(namespace, self.dest, [])
691+
692+
getattr(namespace, self.dest).append('auto')
693+
694+
657695
class NICAction(argparse.Action):
658696

659697
def __init__(
660698
self,
661699
option_strings,
662700
dest,
663-
nargs=None,
664-
const=None,
665-
default=None,
666-
type=None,
667-
choices=None,
668-
required=False,
669701
help=None,
670702
metavar=None,
671703
key=None,
672704
):
673705
self.key = key
674706
super().__init__(
675-
option_strings=option_strings, dest=dest, nargs=nargs, const=const,
676-
default=default, type=type, choices=choices, required=required,
677-
help=help, metavar=metavar,
707+
option_strings=option_strings,
708+
dest=dest,
709+
nargs=None,
710+
const=None,
711+
default=[],
712+
type=None,
713+
choices=None,
714+
required=False,
715+
help=help,
716+
metavar=metavar,
678717
)
679718

680719
def __call__(self, parser, namespace, values, option_string=None):
@@ -707,7 +746,7 @@ def __call__(self, parser, namespace, values, option_string=None):
707746
}
708747

709748
for kv_str in values.split(','):
710-
k, sep, v = kv_str.partition("=")
749+
k, sep, v = kv_str.partition('=')
711750

712751
if k not in list(info) + ['tag'] or not v:
713752
msg = _(
@@ -998,28 +1037,23 @@ def get_parser(self, prog_name):
9981037
)
9991038
parser.add_argument(
10001039
'--network',
1001-
metavar="<network>",
1040+
metavar='<network>',
10021041
dest='nics',
1003-
default=[],
10041042
action=NICAction,
10051043
key='net-id',
1006-
# NOTE(RuiChen): Add '\n' to the end of line to improve formatting;
1007-
# see cliff's _SmartHelpFormatter for more details.
10081044
help=_(
10091045
"Create a NIC on the server and connect it to network. "
10101046
"Specify option multiple times to create multiple NICs. "
10111047
"This is a wrapper for the '--nic net-id=<network>' "
10121048
"parameter that provides simple syntax for the standard "
10131049
"use case of connecting a new server to a given network. "
1014-
"For more advanced use cases, refer to the '--nic' "
1015-
"parameter."
1050+
"For more advanced use cases, refer to the '--nic' parameter."
10161051
),
10171052
)
10181053
parser.add_argument(
10191054
'--port',
1020-
metavar="<port>",
1055+
metavar='<port>',
10211056
dest='nics',
1022-
default=[],
10231057
action=NICAction,
10241058
key='port-id',
10251059
help=_(
@@ -1031,13 +1065,41 @@ def get_parser(self, prog_name):
10311065
"more advanced use cases, refer to the '--nic' parameter."
10321066
),
10331067
)
1068+
parser.add_argument(
1069+
'--no-network',
1070+
dest='nics',
1071+
action=NoneNICAction,
1072+
help=_(
1073+
"Do not attach a network to the server. "
1074+
"This is a wrapper for the '--nic none' option that provides "
1075+
"a simple syntax for disabling network connectivity for a new "
1076+
"server. "
1077+
"For more advanced use cases, refer to the '--nic' parameter. "
1078+
"(supported by --os-compute-api-version 2.37 or above)"
1079+
),
1080+
)
1081+
parser.add_argument(
1082+
'--auto-network',
1083+
dest='nics',
1084+
action=AutoNICAction,
1085+
help=_(
1086+
"Automatically allocate a network to the server. "
1087+
"This is the default network allocation policy. "
1088+
"This is a wrapper for the '--nic auto' option that provides "
1089+
"a simple syntax for enabling automatic configuration of "
1090+
"network connectivity for a new server. "
1091+
"For more advanced use cases, refer to the '--nic' parameter. "
1092+
"(supported by --os-compute-api-version 2.37 or above)"
1093+
),
1094+
)
10341095
parser.add_argument(
10351096
'--nic',
10361097
metavar="<net-id=net-uuid,port-id=port-uuid,v4-fixed-ip=ip-addr,"
10371098
"v6-fixed-ip=ip-addr,tag=tag,auto,none>",
1038-
action=NICAction,
10391099
dest='nics',
1040-
default=[],
1100+
action=NICAction,
1101+
# NOTE(RuiChen): Add '\n' to the end of line to improve formatting;
1102+
# see cliff's _SmartHelpFormatter for more details.
10411103
help=_(
10421104
"Create a NIC on the server.\n"
10431105
"NIC in the format:\n"

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

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,13 +1900,7 @@ def test_server_create_with_network_tag_pre_v243(self):
19001900
self.assertRaises(
19011901
exceptions.CommandError, self.cmd.take_action, parsed_args)
19021902

1903-
def test_server_create_with_auto_network(self):
1904-
arglist = [
1905-
'--image', 'image1',
1906-
'--flavor', 'flavor1',
1907-
'--nic', 'auto',
1908-
self.new_server.name,
1909-
]
1903+
def _test_server_create_with_auto_network(self, arglist):
19101904
verifylist = [
19111905
('image', 'image1'),
19121906
('flavor', 'flavor1'),
@@ -1946,6 +1940,27 @@ def test_server_create_with_auto_network(self):
19461940
self.assertEqual(self.columns, columns)
19471941
self.assertEqual(self.datalist(), data)
19481942

1943+
# NOTE(stephenfin): '--auto-network' is an alias for '--nic auto' so the
1944+
# tests are nearly identical
1945+
1946+
def test_server_create_with_auto_network_legacy(self):
1947+
arglist = [
1948+
'--image', 'image1',
1949+
'--flavor', 'flavor1',
1950+
'--nic', 'auto',
1951+
self.new_server.name,
1952+
]
1953+
self._test_server_create_with_auto_network(arglist)
1954+
1955+
def test_server_create_with_auto_network(self):
1956+
arglist = [
1957+
'--image', 'image1',
1958+
'--flavor', 'flavor1',
1959+
'--auto-network',
1960+
self.new_server.name,
1961+
]
1962+
self._test_server_create_with_auto_network(arglist)
1963+
19491964
def test_server_create_with_auto_network_default_v2_37(self):
19501965
"""Tests creating a server without specifying --nic using 2.37."""
19511966
arglist = [
@@ -1996,13 +2011,7 @@ def test_server_create_with_auto_network_default_v2_37(self):
19962011
self.assertEqual(self.columns, columns)
19972012
self.assertEqual(self.datalist(), data)
19982013

1999-
def test_server_create_with_none_network(self):
2000-
arglist = [
2001-
'--image', 'image1',
2002-
'--flavor', 'flavor1',
2003-
'--nic', 'none',
2004-
self.new_server.name,
2005-
]
2014+
def _test_server_create_with_none_network(self, arglist):
20062015
verifylist = [
20072016
('image', 'image1'),
20082017
('flavor', 'flavor1'),
@@ -2042,6 +2051,27 @@ def test_server_create_with_none_network(self):
20422051
self.assertEqual(self.columns, columns)
20432052
self.assertEqual(self.datalist(), data)
20442053

2054+
# NOTE(stephenfin): '--no-network' is an alias for '--nic none' so the
2055+
# tests are nearly identical
2056+
2057+
def test_server_create_with_none_network_legacy(self):
2058+
arglist = [
2059+
'--image', 'image1',
2060+
'--flavor', 'flavor1',
2061+
'--nic', 'none',
2062+
self.new_server.name,
2063+
]
2064+
self._test_server_create_with_none_network(arglist)
2065+
2066+
def test_server_create_with_none_network(self):
2067+
arglist = [
2068+
'--image', 'image1',
2069+
'--flavor', 'flavor1',
2070+
'--no-network',
2071+
self.new_server.name,
2072+
]
2073+
self._test_server_create_with_none_network(arglist)
2074+
20452075
def test_server_create_with_conflict_network_options(self):
20462076
arglist = [
20472077
'--image', 'image1',
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
The ``server create`` command now accepts two new options, ``--no-network``
5+
and ``--auto-network``. These are aliases for ``--nic none`` and
6+
``--nic auto``, respectively.

0 commit comments

Comments
 (0)