Skip to content

Commit 2545dc5

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "network: Accept name as positional arg for FWaaS create commands"
2 parents e12fc94 + 6c813fa commit 2545dc5

4 files changed

Lines changed: 129 additions & 9 deletions

File tree

openstackclient/network/v2/fwaas/group.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def human_readable(self) -> str:
6060
def _get_common_parser(
6161
parser: argparse.ArgumentParser,
6262
) -> argparse.ArgumentParser:
63-
parser.add_argument('--name', help=_('Name for the firewall group'))
6463
parser.add_argument(
6564
'--description',
6665
metavar='<description>',
@@ -184,6 +183,23 @@ class CreateFirewallGroup(command.ShowOne):
184183
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
185184
parser = super().get_parser(prog_name)
186185
_get_common_parser(parser)
186+
# TODO(slaweq): Remove the deprecated --name option and make the
187+
# positional name argument required (remove nargs='?') once the
188+
# deprecation period is over.
189+
parser.add_argument(
190+
'positional_name',
191+
nargs='?',
192+
metavar='<name>',
193+
default=None,
194+
help=_('Name for the firewall group'),
195+
)
196+
parser.add_argument(
197+
'--name',
198+
help=_(
199+
'(Deprecated, please pass name as a positional argument) '
200+
'Name for the firewall group'
201+
),
202+
)
187203
identity_utils.add_project_owner_option_to_parser(parser)
188204
port_group = parser.add_mutually_exclusive_group()
189205
port_group.add_argument(
@@ -207,6 +223,22 @@ def take_action(
207223
self, parsed_args: argparse.Namespace
208224
) -> tuple[Sequence[str], Iterable[Any]]:
209225
client = self.app.client_manager.network
226+
# TODO(slaweq): Remove this --name deprecation handling once the
227+
# deprecation period is over.
228+
if parsed_args.positional_name and parsed_args.name:
229+
msg = _(
230+
"Cannot specify name as both a positional argument "
231+
"and with the --name option."
232+
)
233+
raise exceptions.CommandError(msg)
234+
if parsed_args.name:
235+
LOG.warning(
236+
'The --name option is deprecated for the "firewall group '
237+
'create" command, please pass the name as a positional '
238+
'argument instead.'
239+
)
240+
elif parsed_args.positional_name:
241+
parsed_args.name = parsed_args.positional_name
210242
attrs = _get_common_attrs(self.app.client_manager, parsed_args)
211243
if 'project' in parsed_args and parsed_args.project is not None:
212244
attrs['project_id'] = identity_common.find_project(
@@ -300,6 +332,7 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
300332
metavar='<firewall-group>',
301333
help=_('Firewall group to update (name or ID)'),
302334
)
335+
parser.add_argument('--name', help=_('Name for the firewall group'))
303336
parser.add_argument(
304337
'--port',
305338
metavar='<port>',

openstackclient/network/v2/fwaas/rule.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ def _convert_to_lowercase(string: str) -> str:
7474
def _get_common_parser(
7575
parser: argparse.ArgumentParser,
7676
) -> argparse.ArgumentParser:
77-
parser.add_argument(
78-
'--name', metavar='<name>', help=_('Name of the firewall rule')
79-
)
8077
parser.add_argument(
8178
'--description',
8279
metavar='<description>',
@@ -288,13 +285,47 @@ class CreateFirewallRule(command.ShowOne):
288285
def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
289286
parser = super().get_parser(prog_name)
290287
_get_common_parser(parser)
288+
# TODO(slaweq): Remove the deprecated --name option and make the
289+
# positional name argument required (remove nargs='?') once the
290+
# deprecation period is over.
291+
parser.add_argument(
292+
'positional_name',
293+
nargs='?',
294+
metavar='<name>',
295+
default=None,
296+
help=_('Name of the firewall rule'),
297+
)
298+
parser.add_argument(
299+
'--name',
300+
metavar='<name>',
301+
help=_(
302+
'(Deprecated, please pass name as a positional argument) '
303+
'Name of the firewall rule'
304+
),
305+
)
291306
identity_utils.add_project_owner_option_to_parser(parser)
292307
return parser
293308

294309
def take_action(
295310
self, parsed_args: argparse.Namespace
296311
) -> tuple[Sequence[str], Iterable[Any]]:
297312
client = self.app.client_manager.network
313+
# TODO(slaweq): Remove this --name deprecation handling once the
314+
# deprecation period is over.
315+
if parsed_args.positional_name and parsed_args.name:
316+
msg = _(
317+
"Cannot specify name as both a positional argument "
318+
"and with the --name option."
319+
)
320+
raise exceptions.CommandError(msg)
321+
if parsed_args.name:
322+
LOG.warning(
323+
'The --name option is deprecated for the "firewall group '
324+
'rule create" command, please pass the name as a positional '
325+
'argument instead.'
326+
)
327+
elif parsed_args.positional_name:
328+
parsed_args.name = parsed_args.positional_name
298329
attrs = _get_common_attrs(self.app.client_manager, parsed_args)
299330
if 'project' in parsed_args and parsed_args.project is not None:
300331
attrs['project_id'] = identity_common.find_project(
@@ -415,6 +446,11 @@ def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
415446
metavar='<firewall-rule>',
416447
help=_('Firewall rule to set (name or ID)'),
417448
)
449+
parser.add_argument(
450+
'--name',
451+
metavar='<name>',
452+
help=_('Name of the firewall rule'),
453+
)
418454
return parser
419455

420456
def take_action(self, parsed_args: argparse.Namespace) -> None:

openstackclient/tests/unit/network/v2/fwaas/test_group.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'egress_firewall_policy': 'egress_firewall_policy_id',
3333
'no_ingress_firewall_policy': 'ingress_firewall_policy_id',
3434
'no_egress_firewall_policy': 'egress_firewall_policy_id',
35+
'positional_name': 'name',
3536
'project': 'project_id',
3637
'port': 'ports',
3738
}
@@ -247,7 +248,6 @@ def _mock_find(*args, **kwargs):
247248
self.network_client.find_port.side_effect = _mock_find
248249
project_id = 'my-project'
249250
arglist = [
250-
'--name',
251251
name,
252252
'--description',
253253
description,
@@ -263,7 +263,7 @@ def _mock_find(*args, **kwargs):
263263
'--disable',
264264
]
265265
verifylist = [
266-
('name', name),
266+
('positional_name', name),
267267
('description', description),
268268
('ingress_firewall_policy', ingress_policy),
269269
('egress_firewall_policy', egress_policy),
@@ -279,6 +279,32 @@ def _mock_find(*args, **kwargs):
279279

280280
self.check_results(headers, data, request)
281281

282+
def test_create_with_name_option_deprecated(self):
283+
name = 'my-name'
284+
arglist = ['--name', name]
285+
verifylist = [('name', name)]
286+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
287+
with mock.patch.object(fwaas_group.LOG, 'warning') as mock_warning:
288+
headers, _data = self.cmd.take_action(parsed_args)
289+
mock_warning.assert_called_once_with(
290+
'The --name option is deprecated for the "firewall group '
291+
'create" command, please pass the name as a positional '
292+
'argument instead.'
293+
)
294+
self.assertEqual(self.ordered_headers, tuple(sorted(headers)))
295+
296+
def test_create_with_both_positional_and_option_name(self):
297+
name = 'my-name'
298+
arglist = [name, '--name', 'other-name']
299+
verifylist = [
300+
('positional_name', name),
301+
('name', 'other-name'),
302+
]
303+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
304+
self.assertRaises(
305+
exceptions.CommandError, self.cmd.take_action, parsed_args
306+
)
307+
282308
def test_create_with_shared_and_no_share(self):
283309
arglist = [
284310
'--share',

openstackclient/tests/unit/network/v2/fwaas/test_rule.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030

3131
CONVERT_MAP = {
32+
'positional_name': 'name',
3233
'project': 'project_id',
3334
}
3435

@@ -222,10 +223,9 @@ def _set_all_params(self, args={}):
222223
destination_port = args.get('destination_port') or '0:65535'
223224
project_id = args.get('project_id') or 'my-tenant'
224225
arglist = [
226+
name,
225227
'--description',
226228
description,
227-
'--name',
228-
name,
229229
'--protocol',
230230
protocol,
231231
'--ip-version',
@@ -247,7 +247,7 @@ def _set_all_params(self, args={}):
247247
]
248248

249249
verifylist = [
250-
('name', name),
250+
('positional_name', name),
251251
('description', description),
252252
('shared', True),
253253
('protocol', protocol),
@@ -320,6 +320,31 @@ def test_create_with_all_params_protocol_upper_capitalized(self):
320320
verifylist,
321321
)
322322

323+
def test_create_with_name_option_deprecated(self):
324+
name = 'my-name'
325+
arglist = ['--name', name]
326+
verifylist = [('name', name)]
327+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
328+
with mock.patch.object(fwaas_rule.LOG, 'warning') as mock_warning:
329+
_headers, _data = self.cmd.take_action(parsed_args)
330+
mock_warning.assert_called_once_with(
331+
'The --name option is deprecated for the "firewall group '
332+
'rule create" command, please pass the name as a positional '
333+
'argument instead.'
334+
)
335+
336+
def test_create_with_both_positional_and_option_name(self):
337+
name = 'my-name'
338+
arglist = [name, '--name', 'other-name']
339+
verifylist = [
340+
('positional_name', name),
341+
('name', 'other-name'),
342+
]
343+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
344+
self.assertRaises(
345+
exceptions.CommandError, self.cmd.take_action, parsed_args
346+
)
347+
323348

324349
class TestListFirewallRule(TestFirewallRule):
325350
def _setup_summary(self, expect=None):

0 commit comments

Comments
 (0)