Skip to content

Commit 0a14239

Browse files
committed
network: Address review comments on VPNaaS addition
- Fold single-caller methods to their call site - Replace str2dict_type with existing utility Change-Id: Icab0efce27d3676a8350b911ab738344e5e60735 Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent f475f4e commit 0a14239

4 files changed

Lines changed: 29 additions & 111 deletions

File tree

openstackclient/network/v2/vpnaas/ikepolicy.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Any
1919

2020
from osc_lib.cli import identity as identity_utils
21+
from osc_lib.cli import parseractions
2122
from osc_lib import exceptions
2223
from osc_lib import utils
2324
from osc_lib.utils import columns as column_util
@@ -163,9 +164,14 @@ def _get_common_parser(
163164
)
164165
parser.add_argument(
165166
'--lifetime',
166-
metavar="units=UNITS,value=VALUE",
167-
type=vpn_utils.str2dict_type(optional_keys=['units', 'value']),
168-
help=vpn_utils.lifetime_help("IKE"),
167+
metavar='units=<units>,value=<value>',
168+
action=parseractions.MultiKeyValueAction,
169+
optional_keys=['units', 'value'],
170+
help=_(
171+
"IKE lifetime attributes. "
172+
"'units'-seconds, default:seconds. "
173+
"'value'-non negative integer, default:3600."
174+
),
169175
)
170176
return parser
171177

openstackclient/network/v2/vpnaas/ipsec_site_connection.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from openstack.network import v2 as network_v2
2121
from osc_lib.cli import format_columns
2222
from osc_lib.cli import identity as identity_utils
23+
from osc_lib.cli import parseractions
2324
from osc_lib import exceptions
2425
from osc_lib import utils
2526
from osc_lib.utils import columns as column_util
@@ -98,11 +99,17 @@ def _get_common_parser(
9899
)
99100
parser.add_argument(
100101
'--dpd',
101-
metavar="action=ACTION,interval=INTERVAL,timeout=TIMEOUT",
102-
type=vpn_utils.str2dict_type(
103-
optional_keys=['action', 'interval', 'timeout']
102+
metavar="action=<action>,interval=<interval>,timeout=<timeout>",
103+
action=parseractions.MultiKeyValueAction,
104+
optional_keys=['action', 'interval', 'timeout'],
105+
help=_(
106+
"IPSec Connection Dead Peer Detection attributes. "
107+
"'action'-hold,clear,disabled,restart,restart-by-peer. "
108+
"'interval' and 'timeout' are non negative integers. "
109+
"'interval' should be less than 'timeout' value. "
110+
"'action', default:hold 'interval', default:30, "
111+
"'timeout', default:120."
104112
),
105-
help=vpn_utils.dpd_help("IPsec connection"),
106113
)
107114
parser.add_argument('--mtu', help=_('MTU size for the connection'))
108115
parser.add_argument(

openstackclient/network/v2/vpnaas/ipsecpolicy.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Any
1919

2020
from osc_lib.cli import identity as identity_utils
21+
from osc_lib.cli import parseractions
2122
from osc_lib import exceptions
2223
from osc_lib import utils
2324
from osc_lib.utils import columns as column_util
@@ -145,9 +146,14 @@ def _get_common_parser(parser: argparse.ArgumentParser) -> None:
145146
)
146147
parser.add_argument(
147148
'--lifetime',
148-
metavar="units=UNITS,value=VALUE",
149-
type=vpn_utils.str2dict_type(optional_keys=['units', 'value']),
150-
help=vpn_utils.lifetime_help("IPsec"),
149+
metavar='units=<units>,value=<value>',
150+
action=parseractions.MultiKeyValueAction,
151+
optional_keys=['units', 'value'],
152+
help=_(
153+
"IPsec lifetime attributes. "
154+
"'units'-seconds, default:seconds. "
155+
"'value'-non negative integer, default:3600."
156+
),
151157
)
152158
parser.add_argument(
153159
'--pfs',

openstackclient/network/v2/vpnaas/utils.py

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
"""VPN Utilities and helper functions."""
1717

18-
import argparse
19-
from collections.abc import Callable
20-
import functools
2118
from typing import Any
2219

2320
from osc_lib import exceptions
@@ -100,101 +97,3 @@ def validate_lifetime_dict(lifetime_dict: dict[str, Any]) -> None:
10097
else:
10198
lifetime_dict['value'] = int(value)
10299
return
103-
104-
105-
def lifetime_help(policy: str) -> str:
106-
lifetime = (
107-
_(
108-
"%s lifetime attributes. "
109-
"'units'-seconds, default:seconds. "
110-
"'value'-non negative integer, default:3600."
111-
)
112-
% policy
113-
)
114-
return lifetime
115-
116-
117-
def dpd_help(policy: str) -> str:
118-
dpd = (
119-
_(
120-
" %s Dead Peer Detection attributes."
121-
" 'action'-hold,clear,disabled,restart,restart-by-peer."
122-
" 'interval' and 'timeout' are non negative integers. "
123-
" 'interval' should be less than 'timeout' value. "
124-
" 'action', default:hold 'interval', default:30, "
125-
" 'timeout', default:120."
126-
)
127-
% policy.capitalize()
128-
)
129-
return dpd
130-
131-
132-
def str2dict(
133-
strdict: str,
134-
required_keys: list[str] | None = None,
135-
optional_keys: list[str] | None = None,
136-
) -> dict[str, str]:
137-
"""Convert key1=value1,key2=value2,... string into dictionary.
138-
139-
:param strdict: string in the form of key1=value1,key2=value2
140-
:param required_keys: list of required keys. All keys in this list must be
141-
specified. Otherwise ArgumentTypeError will be raised.
142-
If this parameter is unspecified, no required key check
143-
will be done.
144-
:param optional_keys: list of optional keys.
145-
This parameter is used for valid key check.
146-
When at least one of required_keys and optional_keys,
147-
a key must be a member of either of required_keys or
148-
optional_keys. Otherwise, ArgumentTypeError will be
149-
raised. When both required_keys and optional_keys are
150-
unspecified, no valid key check will be done.
151-
"""
152-
result = {}
153-
if strdict:
154-
i = 0
155-
kvlist = []
156-
for kv in strdict.split(','):
157-
if '=' in kv:
158-
kvlist.append(kv)
159-
i += 1
160-
elif i == 0:
161-
msg = _("missing value for key '%s'")
162-
raise argparse.ArgumentTypeError(msg % kv)
163-
else:
164-
kvlist[i - 1] = f"{kvlist[i - 1]},{kv}"
165-
for kv in kvlist:
166-
key, sep, value = kv.partition('=')
167-
if not sep:
168-
msg = _("invalid key-value '%s', expected format: key=value")
169-
raise argparse.ArgumentTypeError(msg % kv)
170-
result[key] = value
171-
valid_keys = set(required_keys or []) | set(optional_keys or [])
172-
if valid_keys:
173-
invalid_keys = [k for k in result if k not in valid_keys]
174-
if invalid_keys:
175-
msg = _(
176-
"Invalid key(s) '%(invalid_keys)s' specified. "
177-
"Valid key(s): '%(valid_keys)s'."
178-
)
179-
raise argparse.ArgumentTypeError(
180-
msg
181-
% {
182-
'invalid_keys': ', '.join(sorted(invalid_keys)),
183-
'valid_keys': ', '.join(sorted(valid_keys)),
184-
}
185-
)
186-
if required_keys:
187-
not_found_keys = [k for k in required_keys if k not in result]
188-
if not_found_keys:
189-
msg = _("Required key(s) '%s' not specified.")
190-
raise argparse.ArgumentTypeError(msg % ', '.join(not_found_keys))
191-
return result
192-
193-
194-
def str2dict_type(
195-
optional_keys: list[str] | None = None,
196-
required_keys: list[str] | None = None,
197-
) -> Callable[[str], dict[str, str]]:
198-
return functools.partial(
199-
str2dict, optional_keys=optional_keys, required_keys=required_keys
200-
)

0 commit comments

Comments
 (0)