Skip to content

Commit 94a24fc

Browse files
committed
Handle missing D-Bus property in pool listing
Signed-off-by: mulhern <amulhern@redhat.com>
1 parent b26ab06 commit 94a24fc

1 file changed

Lines changed: 122 additions & 48 deletions

File tree

src/stratis_cli/_actions/_list_pool.py

Lines changed: 122 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
# isort: THIRDPARTY
3131
from justbytes import Range
3232

33+
# isort: FIRSTPARTY
34+
from dbus_client_gen import DbusClientMissingPropertyError
35+
3336
from .._alerts import (
3437
PoolAllocSpaceAlert,
3538
PoolDeviceSizeChangeAlert,
@@ -251,6 +254,8 @@ def metadata_version(mopool: Any) -> MetadataVersion | None:
251254
return MetadataVersion(int(mopool.MetadataVersion()))
252255
except ValueError: # pragma: no cover
253256
return None
257+
except DbusClientMissingPropertyError: # pragma: no cover
258+
return None
254259

255260
@staticmethod
256261
def _volume_key_loaded(mopool: Any) -> tuple[bool, bool] | tuple[bool, str]:
@@ -307,23 +312,36 @@ def size_triple(mopool: Any) -> SizeTriple:
307312
"""
308313
Calculate SizeTriple from size information.
309314
"""
310-
return SizeTriple(
311-
Range(mopool.TotalPhysicalSize()),
312-
get_property(mopool.TotalPhysicalUsed(), Range, None),
313-
)
315+
try:
316+
size = Range(mopool.TotalPhysicalSize())
317+
except DbusClientMissingPropertyError: # pragma: no cover
318+
size = None
319+
320+
try:
321+
used = get_property(mopool.TotalPhysicalUsed(), Range, None)
322+
except DbusClientMissingPropertyError: # pragma: no cover
323+
used = None
324+
325+
return SizeTriple(size, used)
314326

315327
def uuid_str(self, mopool: Any) -> str:
316328
"""
317329
Return a string representation of UUID, correctly formatted.
318330
"""
319-
return self.uuid_formatter(mopool.Uuid())
331+
try:
332+
return self.uuid_formatter(mopool.Uuid())
333+
except DbusClientMissingPropertyError: # pragma: no cover
334+
return TABLE_UNKNOWN_STRING
320335

321336
@staticmethod
322337
def name_str(mopool: Any) -> str:
323338
"""
324339
Return a string representation of the pool name.
325340
"""
326-
return mopool.Name()
341+
try:
342+
return mopool.Name()
343+
except DbusClientMissingPropertyError: # pragma: no cover
344+
return TABLE_UNKNOWN_STRING
327345

328346

329347
class DefaultDetail(Default): # pylint: disable=too-few-public-methods
@@ -343,7 +361,7 @@ def __init__(self, uuid_formatter: Callable[[str | UUID], str], selection: PoolI
343361

344362
def _print_detail_view(
345363
self, pool_object_path: str, mopool: Any, alerts: DeviceSizeChangedAlerts
346-
): # pylint: disable=too-many-locals
364+
): # pylint: disable=too-many-locals,too-many-statements,too-many-branches
347365
"""
348366
Print the detailed view for a single pool.
349367
@@ -352,8 +370,6 @@ def _print_detail_view(
352370
:param MOPool mopool: properties of the pool
353371
:param DeviceSizeChangedAlerts alerts: pool alerts
354372
"""
355-
encrypted = mopool.Encrypted()
356-
357373
print(f"UUID: {self.uuid_str(mopool)}")
358374
print(f"Name: {Default.name_str(mopool)}")
359375

@@ -367,71 +383,129 @@ def _print_detail_view(
367383
print(f" {line}")
368384

369385
metadata_version = Default.metadata_version(mopool)
386+
metadata_version_str = (
387+
metadata_version if metadata_version is not None else TABLE_UNKNOWN_STRING
388+
)
389+
print(f"Metadata Version: {metadata_version_str}")
390+
391+
try:
392+
actions_allowed_str = PoolActionAvailability[
393+
str(mopool.AvailableActions())
394+
].name
395+
except DbusClientMissingPropertyError:
396+
actions_allowed_str = TABLE_UNKNOWN_STRING
397+
print(f"Actions Allowed: {actions_allowed_str}")
370398

371-
print(f"Metadata Version: {metadata_version}")
399+
try:
400+
cache_str = "Yes" if mopool.HasCache() else "No"
401+
except DbusClientMissingPropertyError:
402+
cache_str = TABLE_UNKNOWN_STRING
403+
print(f"Cache: {cache_str}")
372404

373-
print(
374-
f"Actions Allowed: "
375-
f"{PoolActionAvailability[str(mopool.AvailableActions())].name}"
376-
)
377-
print(f"Cache: {'Yes' if mopool.HasCache() else 'No'}")
378-
print(f"Filesystem Limit: {mopool.FsLimit()}")
379-
print(
380-
f"Allows Overprovisioning: "
381-
f"{'Yes' if mopool.Overprovisioning() else 'No'}"
382-
)
405+
try:
406+
fs_limit_str = mopool.FsLimit()
407+
except DbusClientMissingPropertyError: # pragma: no cover
408+
fs_limit_str = TABLE_UNKNOWN_STRING
409+
print(f"Filesystem Limit: {fs_limit_str}")
410+
411+
try:
412+
allow_overprovisioning_str = "Yes" if mopool.Overprovisioning() else "No"
413+
except DbusClientMissingPropertyError: # pragma: no cover
414+
allow_overprovisioning_str = TABLE_UNKNOWN_STRING
415+
print(f"Allows Overprovisioning: {allow_overprovisioning_str}")
416+
417+
try:
418+
encrypted = bool(mopool.Encrypted())
419+
except DbusClientMissingPropertyError: # pragma: no cover
420+
encrypted = None
383421

384-
if encrypted:
422+
if encrypted is None:
423+
print("Encryption Enabled: {TABLE_UNKNOWN_STRING}")
424+
elif encrypted is True:
385425
print("Encryption Enabled: Yes")
386426

387427
if metadata_version is MetadataVersion.V1: # pragma: no cover
388-
key_description_str = _non_existent_or_inconsistent_to_str(
389-
EncryptionInfoKeyDescription(mopool.KeyDescriptions())
390-
)
428+
try:
429+
key_description_str = _non_existent_or_inconsistent_to_str(
430+
EncryptionInfoKeyDescription(mopool.KeyDescriptions())
431+
)
432+
except DbusClientMissingPropertyError:
433+
key_description_str = TABLE_UNKNOWN_STRING
391434
print(f" Key Description: {key_description_str}")
392435

393-
clevis_info_str = _non_existent_or_inconsistent_to_str(
394-
EncryptionInfoClevis(mopool.ClevisInfos()),
395-
interp=_clevis_to_str,
396-
)
436+
try:
437+
clevis_info_str = _non_existent_or_inconsistent_to_str(
438+
EncryptionInfoClevis(mopool.ClevisInfos()),
439+
interp=_clevis_to_str,
440+
)
441+
except DbusClientMissingPropertyError:
442+
clevis_info_str = TABLE_UNKNOWN_STRING
443+
397444
print(f" Clevis Configuration: {clevis_info_str}")
398445
elif metadata_version is MetadataVersion.V2:
399-
encryption_infos = sorted(
400-
[
446+
447+
try:
448+
free_str = get_property(
449+
mopool.FreeTokenSlots(),
450+
lambda x: str(int(x)),
451+
TABLE_UNKNOWN_STRING,
452+
)
453+
except DbusClientMissingPropertyError: # pragma: no cover
454+
free_str = TABLE_UNKNOWN_STRING
455+
print(f" Free Token Slots Remaining: {free_str}")
456+
457+
try:
458+
key_encryption_infos = [
401459
TokenSlotInfo(token_slot, key=str(description))
402460
for token_slot, description in mopool.KeyDescriptions()
403461
]
404-
+ [
462+
except DbusClientMissingPropertyError: # pragma: no cover
463+
key_encryption_infos = []
464+
465+
try:
466+
clevis_encryption_infos = [
405467
TokenSlotInfo(
406468
token_slot, clevis=(str(pin), json.loads(str(config)))
407469
)
408470
for token_slot, (pin, config) in mopool.ClevisInfos()
409-
],
410-
key=lambda x: x.token_slot,
411-
)
471+
]
472+
except DbusClientMissingPropertyError: # pragma: no cover
473+
clevis_encryption_infos = []
412474

413-
free_valid, free = mopool.FreeTokenSlots()
414-
print(
415-
f' Free Token Slots Remaining: {int(free) if free_valid else "<UNKNOWN>"}'
475+
encryption_infos = sorted(
476+
key_encryption_infos + clevis_encryption_infos,
477+
key=lambda x: x.token_slot,
416478
)
417-
418-
for info in encryption_infos:
419-
for line in str(info).split(os.linesep):
420-
print(f" {line}")
479+
if encryption_infos == []:
480+
print(" No Token Slot Information Available")
481+
else:
482+
for info in encryption_infos:
483+
for line in str(info).split(os.linesep):
484+
print(f" {line}")
421485
else: # pragma: no cover
422486
pass
423487
else:
424488
print("Encryption Enabled: No")
425489

426490
size_triple = Default.size_triple(mopool)
427491

428-
print(f"Fully Allocated: {'Yes' if mopool.NoAllocSpace() else 'No'}")
429-
print(f" Size: {size_triple.total()}")
430-
print(f" Allocated: {Range(mopool.AllocatedSize())}")
431-
print(
432-
" Used: "
433-
f"{TABLE_UNKNOWN_STRING if size_triple.used() is None else size_triple.used()}"
434-
)
492+
def size_str(value: Range | None) -> str:
493+
return TABLE_UNKNOWN_STRING if value is None else str(value)
494+
495+
try:
496+
fully_allocated_str = "Yes" if mopool.NoAllocSpace() else "No"
497+
except DbusClientMissingPropertyError: # pragma: no cover
498+
fully_allocated_str = TABLE_UNKNOWN_STRING
499+
print(f"Fully Allocated: {fully_allocated_str}")
500+
501+
print(f" Size: {size_str(size_triple.total())}")
502+
503+
try:
504+
allocated_size = Range(mopool.AllocatedSize())
505+
except DbusClientMissingPropertyError: # pragma: no cover
506+
allocated_size = None
507+
print(f" Allocated: {size_str(allocated_size)}")
508+
print(f" Used: {size_str(size_triple.used())}")
435509

436510
def display(self):
437511
"""

0 commit comments

Comments
 (0)