Skip to content

Commit

Permalink
Merge pull request #1012 from mulkieran/PoolIdType-move
Browse files Browse the repository at this point in the history
Rearrange and simplify enums
  • Loading branch information
mulkieran authored Aug 23, 2023
2 parents 6bbe5c3 + 3b5ceff commit 259ff85
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 199 deletions.
4 changes: 2 additions & 2 deletions src/stratis_cli/_actions/_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
# isort: STDLIB
import json

from .._constants import EncryptionMethod
from .._errors import StratisCliEngineError, StratisCliNoChangeError
from .._stratisd_constants import (
CLEVIS_KEY_TANG_TRUST_URL,
CLEVIS_KEY_THP,
CLEVIS_KEY_URL,
CLEVIS_PIN_TANG,
CLEVIS_PIN_TPM2,
EncryptionMethod,
StratisdErrors,
)
from ._connection import get_object
Expand Down Expand Up @@ -151,7 +151,7 @@ def unbind(namespace):

unbind_method = (
Pool.Methods.UnbindClevis
if namespace.method == str(EncryptionMethod.CLEVIS)
if namespace.method == EncryptionMethod.CLEVIS.value
else Pool.Methods.UnbindKeyring
)

Expand Down
4 changes: 2 additions & 2 deletions src/stratis_cli/_actions/_list_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def alert_codes(mopool):
:returns: list of PoolErrorCode
"""
action_availability = PoolActionAvailability.from_str(mopool.AvailableActions())
action_availability = PoolActionAvailability[str(mopool.AvailableActions())]
availability_error_codes = action_availability.pool_maintenance_error_codes()

no_alloc_space_error_codes = (
Expand Down Expand Up @@ -251,7 +251,7 @@ def _print_detail_view(self, mopool, size_change_codes):

print(
f"Actions Allowed: "
f"{PoolActionAvailability.from_str(mopool.AvailableActions())}"
f"{PoolActionAvailability[str(mopool.AvailableActions())].name}"
)
print(f"Cache: {'Yes' if mopool.HasCache() else 'No'}")
print(f"Filesystem Limit: {mopool.FsLimit()}")
Expand Down
4 changes: 2 additions & 2 deletions src/stratis_cli/_actions/_physical.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def tier_str(value):
String representation of a tier.
"""
try:
return str(BlockDevTiers.from_int(value))
except StopIteration: # pragma: no cover
return str(BlockDevTiers(value))
except ValueError: # pragma: no cover
return TABLE_UNKNOWN_STRING

format_uuid = get_uuid_formatter(namespace.unhyphenated_uuids)
Expand Down
18 changes: 9 additions & 9 deletions src/stratis_cli/_actions/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# isort: THIRDPARTY
from justbytes import Range

from .._constants import YesOrNo
from .._constants import PoolIdType, YesOrNo
from .._error_codes import PoolErrorCode
from .._errors import (
StratisCliEngineError,
Expand All @@ -41,7 +41,7 @@
StratisCliPartialChangeError,
StratisCliResourceNotFoundError,
)
from .._stratisd_constants import BlockDevTiers, PoolIdType, StratisdErrors
from .._stratisd_constants import BlockDevTiers, StratisdErrors
from ._connection import get_object
from ._constants import TOP_OBJECT
from ._formatting import get_property, get_uuid_formatter
Expand Down Expand Up @@ -226,16 +226,16 @@ def stop_pool(namespace):
proxy = get_object(TOP_OBJECT)

(pool_id, id_type) = (
(namespace.uuid.hex, PoolIdType.UUID)
(namespace.uuid.hex, "uuid")
if getattr(namespace, "name") is None
else (namespace.name, PoolIdType.NAME)
else (namespace.name, "name")
)

((stopped, _), return_code, message) = Manager.Methods.StopPool(
proxy,
{
"id": pool_id,
"id_type": str(id_type),
"id_type": id_type,
},
)

Expand All @@ -259,16 +259,16 @@ def start_pool(namespace):
proxy = get_object(TOP_OBJECT)

(pool_id, id_type) = (
(namespace.uuid.hex, PoolIdType.UUID)
(namespace.uuid.hex, "uuid")
if getattr(namespace, "name") is None
else (namespace.name, PoolIdType.NAME)
else (namespace.name, "name")
)

((started, _), return_code, message) = Manager.Methods.StartPool(
proxy,
{
"id": pool_id,
"id_type": str(id_type),
"id_type": id_type,
"unlock_method": (False, "")
if namespace.unlock_method is None
else (True, namespace.unlock_method),
Expand Down Expand Up @@ -689,7 +689,7 @@ def set_overprovisioning_mode(namespace):
# pylint: disable=import-outside-toplevel
from ._data import MOPool, ObjectManager, Pool, pools

decision = bool(YesOrNo.from_str(namespace.decision))
decision = bool(YesOrNo(namespace.decision))

proxy = get_object(TOP_OBJECT)
managed_objects = ObjectManager.Methods.GetManagedObjects(proxy, {})
Expand Down
4 changes: 2 additions & 2 deletions src/stratis_cli/_actions/_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ def get_report(namespace):
"""

# pylint: disable=import-outside-toplevel
if namespace.report_name == str(ReportKey.MANAGED_OBJECTS):
if namespace.report_name == ReportKey.MANAGED_OBJECTS.value:
from ._data import ObjectManager

json_report = ObjectManager.Methods.GetManagedObjects(
get_object(TOP_OBJECT), {}
)

else:
if namespace.report_name == str(ReportKey.ENGINE_STATE):
if namespace.report_name == ReportKey.ENGINE_STATE.value:
from ._data import Manager

(report, return_code, message) = Manager.Methods.EngineStateReport(
Expand Down
6 changes: 2 additions & 4 deletions src/stratis_cli/_actions/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import json
from uuid import UUID

from .._constants import PoolIdType
from .._errors import (
StratisCliMissingClevisTangURLError,
StratisCliMissingClevisThumbprintError,
Expand All @@ -30,7 +31,6 @@
CLEVIS_KEY_URL,
CLEVIS_PIN_TANG,
CLEVIS_PIN_TPM2,
PoolIdType,
)


Expand Down Expand Up @@ -236,7 +236,5 @@ def selection_func(_uuid, info):
return selection_func

def __str__(self):
pool_id_type_str = (
"UUID" if self.pool_id_type is PoolIdType.UUID else str(self.pool_id_type)
)
pool_id_type_str = "UUID" if self.pool_id_type is PoolIdType.UUID else "name"
return f"pool with {pool_id_type_str} {self.value}"
31 changes: 17 additions & 14 deletions src/stratis_cli/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ class YesOrNo(Enum):
YES = "yes"
NO = "no"

def __str__(self):
return self.value

def __bool__(self):
return self is YesOrNo.YES

@staticmethod
def from_str(code_str):
"""
From a string code, return the YesOrNo value.
:param str code_str: the string code
:returns: the YesOrNo value
:rtype: YesOrNo
:raises: StopIteration
"""
return next(item for item in YesOrNo if code_str == str(item))

class PoolIdType(Enum):
"""
Whether the pool identifier is a UUID or a name.
"""

UUID = 0
NAME = 1


class EncryptionMethod(Enum):
"""
Encryption method.
"""

KEYRING = "keyring"
CLEVIS = "clevis"
4 changes: 2 additions & 2 deletions src/stratis_cli/_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ def __init__(self, rc, message):
"""
# pylint: disable=super-init-not-called
try:
self.error_code = StratisdErrors.from_int(rc)
except StopIteration:
self.error_code = StratisdErrors(rc)
except ValueError:
self.error_code = None

self.message = message
Expand Down
2 changes: 1 addition & 1 deletion src/stratis_cli/_parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def wrapped_func(*args):
"type": str,
"help": ("Name of the report to display"),
"nargs": "?",
"choices": [str(x) for x in list(ReportKey)],
"choices": [x.value for x in list(ReportKey)],
},
)
],
Expand Down
9 changes: 4 additions & 5 deletions src/stratis_cli/_parser/_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
from uuid import UUID

from .._actions import BindActions, PoolActions
from .._constants import YesOrNo
from .._constants import EncryptionMethod, YesOrNo
from .._error_codes import PoolErrorCode
from .._stratisd_constants import EncryptionMethod
from ._bind import BIND_SUBCMDS, REBIND_SUBCMDS
from ._debug import POOL_DEBUG_SUBCMDS

Expand Down Expand Up @@ -192,7 +191,7 @@ def _ensure_nat(arg):
"default": None,
"dest": "unlock_method",
"action": "store",
"choices": [str(x) for x in list(EncryptionMethod)],
"choices": [x.value for x in list(EncryptionMethod)],
"help": "Method to use to unlock the pool if encrypted.",
},
),
Expand Down Expand Up @@ -373,7 +372,7 @@ def _ensure_nat(arg):
"method",
{
"action": "store",
"choices": [str(x) for x in list(EncryptionMethod)],
"choices": [x.value for x in list(EncryptionMethod)],
"help": "Encryption method to unbind",
},
),
Expand Down Expand Up @@ -411,7 +410,7 @@ def _ensure_nat(arg):
{
"action": "store",
"help": "yes to allow overprovisioning, otherwise no",
"choices": [str(x) for x in list(YesOrNo)],
"choices": [x.value for x in list(YesOrNo)],
},
),
],
Expand Down
93 changes: 7 additions & 86 deletions src/stratis_cli/_stratisd_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,8 @@ class StratisdErrors(IntEnum):
OK = 0
ERROR = 1

@staticmethod
def from_int(code):
"""
From an integer code, return the value.
:param int code: the code
:rtype: StratisdErrors
:raises: StopIteration
"""
return next(item for item in StratisdErrors if code == int(item))

def __str__(self):
if self is StratisdErrors.OK:
return "OK"
if self is StratisdErrors.ERROR:
return "ERROR"
assert False, "impossible value reached" # pragma: no cover
return self.name


class BlockDevTiers(IntEnum):
Expand All @@ -56,35 +41,8 @@ class BlockDevTiers(IntEnum):
DATA = 0
CACHE = 1

@staticmethod
def from_int(code):
"""
From an integer code, return the value.
:param int code: the code
:rtype: StratisdErrors
:raises: StopIteration
"""
return next(item for item in BlockDevTiers if code == int(item))

def __str__(self):
if self is BlockDevTiers.DATA:
return "DATA"
if self is BlockDevTiers.CACHE:
return "CACHE"
assert False, "impossible value reached" # pragma: no cover


class EncryptionMethod(Enum):
"""
Encryption method, used as argument to unlock.
"""

KEYRING = "keyring"
CLEVIS = "clevis"

def __str__(self):
return self.value
return self.name


CLEVIS_KEY_TANG_TRUST_URL = "stratis:tang:trust_url"
Expand All @@ -107,40 +65,15 @@ class ReportKey(Enum):
MANAGED_OBJECTS = "managed_objects_report"
STOPPED_POOLS = "stopped_pools"

def __str__(self):
return self.value


class PoolActionAvailability(IntEnum):
"""
What category of interactions a pool is enabled for.
"""

FULLY_OPERATIONAL = 0
NO_IPC_REQUESTS = 1
NO_POOL_CHANGES = 2

def __str__(self):
if self is PoolActionAvailability.FULLY_OPERATIONAL:
return "fully_operational"
if self is PoolActionAvailability.NO_IPC_REQUESTS:
return "no_ipc_requests"
if self is PoolActionAvailability.NO_POOL_CHANGES: # pragma: no cover
return "no_pool_changes"

assert False, "impossible value reached" # pragma: no cover

@staticmethod
def from_str(code_str):
"""
Get ActionAvailability object from a string.
:param str code_str: a code string
:rtype: str or NoneType
"""
for item in list(PoolActionAvailability):
if code_str == str(item):
return item
return None
fully_operational = 0 # pylint: disable=invalid-name
no_ipc_requests = 1 # pylint: disable=invalid-name
no_pool_changes = 2 # pylint: disable=invalid-name

def pool_maintenance_error_codes(self):
"""
Expand All @@ -149,22 +82,10 @@ def pool_maintenance_error_codes(self):
:rtype: list of PoolMaintenanceErrorCode
"""
codes = []
if self >= PoolActionAvailability.NO_IPC_REQUESTS:
if self >= PoolActionAvailability.no_ipc_requests:
codes.append(PoolMaintenanceErrorCode.NO_IPC_REQUESTS)

if self >= PoolActionAvailability.NO_POOL_CHANGES:
if self >= PoolActionAvailability.no_pool_changes:
codes.append(PoolMaintenanceErrorCode.NO_POOL_CHANGES)

return codes


class PoolIdType(Enum):
"""
Whether the pool identifier is a UUID or a name.
"""

UUID = "uuid"
NAME = "name"

def __str__(self):
return self.value
Loading

0 comments on commit 259ff85

Please sign in to comment.