Skip to content

Commit

Permalink
Add token slot option to unbind command
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jan 14, 2025
1 parent d7ce08a commit 45997ab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
9 changes: 8 additions & 1 deletion src/stratis_cli/_actions/_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ def unbind(namespace):
)

(changed, return_code, return_msg) = unbind_method(
get_object(pool_object_path), {"token_slot": (False, 0)}
get_object(pool_object_path),
{
"token_slot": (
(False, 0)
if namespace.token_slot is None
else (True, namespace.token_slot)
)
},
)

if return_code != StratisdErrors.OK:
Expand Down
30 changes: 13 additions & 17 deletions src/stratis_cli/_parser/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# isort: STDLIB
import copy
from argparse import SUPPRESS, ArgumentTypeError
from argparse import SUPPRESS
from uuid import UUID

# isort: THIRDPARTY
Expand All @@ -35,7 +35,7 @@
from .._error_codes import PoolErrorCode
from ._bind import BIND_SUBCMDS, REBIND_SUBCMDS
from ._debug import POOL_DEBUG_SUBCMDS
from ._range import DefaultAction, RejectAction, parse_range
from ._range import DefaultAction, RejectAction, ensure_nat, parse_range


class ClevisEncryptionOptions: # pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -147,20 +147,6 @@ def verify(self, namespace, parser):
self.integrity_options.verify(namespace, parser)


def _ensure_nat(arg):
"""
Raise error if argument is not an natural number.
"""
try:
result = int(arg)
except Exception as err:
raise ArgumentTypeError(f"Argument {arg} is not a natural number.") from err

if result < 0:
raise ArgumentTypeError(f"Argument {arg} is not a natural number.")
return result


POOL_SUBCMDS = [
(
"create",
Expand Down Expand Up @@ -592,6 +578,16 @@ def _ensure_nat(arg):
},
),
("pool_name", {"help": "Pool name"}),
(
"--token-slot",
{
"help": (
"token slot; must be specified if there is more "
"than one binding with the specified method"
),
"type": ensure_nat,
},
),
],
"func": BindActions.unbind,
},
Expand All @@ -605,7 +601,7 @@ def _ensure_nat(arg):
(
"amount",
{
"type": _ensure_nat,
"type": ensure_nat,
"help": "Number of filesystems.",
},
),
Expand Down
16 changes: 16 additions & 0 deletions src/stratis_cli/_parser/_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,19 @@ class DefaultAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values)
setattr(namespace, self.dest + "_default", False)


def ensure_nat(arg):
"""
Raise error if argument is not an natural number.
"""
try:
result = int(arg)
except Exception as err:
raise argparse.ArgumentTypeError(
f"Argument {arg} is not a natural number."
) from err

if result < 0:
raise argparse.ArgumentTypeError(f"Argument {arg} is not a natural number.")
return result

0 comments on commit 45997ab

Please sign in to comment.