Skip to content

Commit 43dd2f9

Browse files
committed
Lookup blockdev D-Bus properties without raising not found exception
If the pool object path is not on the D-Bus, stratisd will not even have a field for that item in the GetManagedObjects result and will raise an exception. Do not allow the exception to propagate in this case, but catch it and return the unknown string. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent 00643ca commit 43dd2f9

1 file changed

Lines changed: 36 additions & 9 deletions

File tree

src/stratis_cli/_actions/_physical.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717

1818
# isort: STDLIB
1919
from argparse import Namespace
20+
from typing import Callable
2021

2122
# isort: THIRDPARTY
2223
from justbytes import Range
2324

25+
# isort: FIRSTPARTY
26+
from dbus_client_gen import DbusClientMissingPropertyError
27+
2428
from .._stratisd_constants import BlockDevTiers
2529
from ._connection import get_object
2630
from ._constants import TOP_OBJECT
@@ -79,7 +83,23 @@ def list_devices(namespace: Namespace): # pylint: disable=too-many-locals
7983
).search(managed_objects)
8084
)
8185

82-
def paths(modev):
86+
def catch_missing_property(modev, prop_to_name: Callable) -> str:
87+
"""
88+
Get a property from an modev. If the property is missing,
89+
return the TABLE_UNKNOWN_STRING value.
90+
"""
91+
try:
92+
return prop_to_name(modev)
93+
except DbusClientMissingPropertyError: # pragma: no cover
94+
return TABLE_UNKNOWN_STRING
95+
96+
def pool_name_lookup(modev) -> str:
97+
"""
98+
Look up a name for a pool.
99+
"""
100+
return path_to_name.get(modev.Pool(), TABLE_UNKNOWN_STRING)
101+
102+
def paths(modev) -> str:
83103
"""
84104
Return <physical_path> (<metadata_path>) if they are different,
85105
otherwise, just <metadata_path>.
@@ -100,37 +120,44 @@ def paths(modev):
100120
else f"{physical_path} ({metadata_path})"
101121
)
102122

103-
def size(modev):
123+
def size(modev) -> str:
104124
"""
105125
Return in-use size (observed size) if they are different, otherwise
106126
just in-use size.
107127
"""
108128
in_use_size = Range(modev.TotalPhysicalSize())
109129
observed_size = get_property(modev.NewPhysicalSize(), Range, in_use_size)
130+
110131
return (
111132
f"{in_use_size}"
112133
if in_use_size == observed_size
113134
else f"{in_use_size} ({observed_size})"
114135
)
115136

116-
def tier_str(value):
137+
def tier_str(modev) -> str:
117138
"""
118139
String representation of a tier.
119140
"""
120141
try:
121-
return str(BlockDevTiers(value))
142+
return str(BlockDevTiers(modev.Tier()))
122143
except ValueError: # pragma: no cover
123144
return TABLE_UNKNOWN_STRING
124145

125146
format_uuid = get_uuid_formatter(namespace.unhyphenated_uuids)
126147

148+
def uuid_str(modev) -> str:
149+
"""
150+
String representation of UUID.
151+
"""
152+
return format_uuid(modev.Uuid())
153+
127154
tables = [
128155
[
129-
path_to_name.get(modev.Pool(), TABLE_UNKNOWN_STRING),
130-
paths(modev),
131-
size(modev),
132-
tier_str(modev.Tier()),
133-
format_uuid(modev.Uuid()),
156+
catch_missing_property(modev, pool_name_lookup),
157+
catch_missing_property(modev, paths),
158+
catch_missing_property(modev, size),
159+
catch_missing_property(modev, tier_str),
160+
catch_missing_property(modev, uuid_str),
134161
]
135162
for modev in modevs
136163
]

0 commit comments

Comments
 (0)