Skip to content

Commit 4320d2b

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 <[email protected]>
1 parent ca0a8f3 commit 4320d2b

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/stratis_cli/_actions/_physical.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
# isort: THIRDPARTY
2222
from justbytes import Range
2323

24+
# isort: FIRSTPARTY
25+
from dbus_client_gen import DbusClientMissingPropertyError
26+
2427
from .._stratisd_constants import BlockDevTiers
2528
from ._connection import get_object
2629
from ._constants import TOP_OBJECT
@@ -79,6 +82,16 @@ def list_devices(namespace: Namespace): # pylint: disable=too-many-locals
7982
).search(managed_objects)
8083
)
8184

85+
def pool_name_lookup(modev) -> str:
86+
"""
87+
Look up a name for a pool. Return place holder if the modev
88+
has no pool attribute.
89+
"""
90+
try:
91+
return path_to_name.get(modev.Pool(), TABLE_UNKNOWN_STRING)
92+
except DbusClientMissingPropertyError: # pragma: no cover
93+
return TABLE_UNKNOWN_STRING
94+
8295
def paths(modev):
8396
"""
8497
Return <physical_path> (<metadata_path>) if they are different,
@@ -91,8 +104,11 @@ def paths(modev):
91104
:returns: the string to print
92105
:rtype: str
93106
"""
94-
metadata_path = modev.Devnode()
95-
physical_path = modev.PhysicalPath()
107+
try:
108+
metadata_path = modev.Devnode()
109+
physical_path = modev.PhysicalPath()
110+
except DbusClientMissingPropertyError: # pragma: no cover
111+
return TABLE_UNKNOWN_STRING
96112

97113
return (
98114
metadata_path
@@ -105,32 +121,49 @@ def size(modev):
105121
Return in-use size (observed size) if they are different, otherwise
106122
just in-use size.
107123
"""
108-
in_use_size = Range(modev.TotalPhysicalSize())
109-
observed_size = get_property(modev.NewPhysicalSize(), Range, in_use_size)
124+
try:
125+
in_use_size = Range(modev.TotalPhysicalSize())
126+
observed_size = get_property(
127+
modev.NewPhysicalSize(), Range, in_use_size
128+
)
129+
except DbusClientMissingPropertyError: # pragma: no cover
130+
return TABLE_UNKNOWN_STRING
131+
110132
return (
111133
f"{in_use_size}"
112134
if in_use_size == observed_size
113135
else f"{in_use_size} ({observed_size})"
114136
)
115137

116-
def tier_str(value):
138+
def tier_str(modev) -> str:
117139
"""
118140
String representation of a tier.
119141
"""
120142
try:
121-
return str(BlockDevTiers(value))
143+
return str(BlockDevTiers(modev.Tier()))
122144
except ValueError: # pragma: no cover
123145
return TABLE_UNKNOWN_STRING
146+
except DbusClientMissingPropertyError: # pragma: no cover
147+
return TABLE_UNKNOWN_STRING
124148

125149
format_uuid = get_uuid_formatter(namespace.unhyphenated_uuids)
126150

151+
def uuid_str(modev) -> str:
152+
"""
153+
String representation of UUID.
154+
"""
155+
try:
156+
return format_uuid(modev.Uuid())
157+
except DbusClientMissingPropertyError: # pragma: no cover
158+
return TABLE_UNKNOWN_STRING
159+
127160
tables = [
128161
[
129-
path_to_name.get(modev.Pool(), TABLE_UNKNOWN_STRING),
162+
pool_name_lookup(modev),
130163
paths(modev),
131164
size(modev),
132-
tier_str(modev.Tier()),
133-
format_uuid(modev.Uuid()),
165+
tier_str(modev),
166+
uuid_str(modev),
134167
]
135168
for modev in modevs
136169
]

0 commit comments

Comments
 (0)