Skip to content

Commit 12b1e7e

Browse files
committed
ceph-volume: add type annotations to devices.lvm.listing
This commit adds the Python type annotations to `devices.lvm.listing`. Signed-off-by: Guillaume Abrioux <[email protected]>
1 parent e65bc61 commit 12b1e7e

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/ceph-volume/ceph_volume/api/lvm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ class PVolume:
352352
def __init__(self, **kw: Any) -> None:
353353
self.pv_name: str = ''
354354
self.pv_uuid: str = ''
355+
self.lv_uuid: str = ''
355356
for k, v in kw.items():
356357
setattr(self, k, v)
357358
self.pv_api = kw

src/ceph-volume/ceph_volume/devices/lvm/listing.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import argparse
33
import json
44
import logging
5+
import typing
56
from textwrap import dedent
67
from ceph_volume import decorators
78
from ceph_volume.api import lvm as api
89

10+
911
logger = logging.getLogger(__name__)
1012

1113

@@ -22,12 +24,12 @@
2224
{tag_name: <25} {value}"""
2325

2426

25-
def readable_tag(tag):
27+
def readable_tag(tag: str) -> str:
2628
actual_name = tag.split('.')[-1]
2729
return actual_name.replace('_', ' ')
2830

2931

30-
def pretty_report(report):
32+
def pretty_report(report: typing.Dict[str, typing.Any]) -> None:
3133
output = []
3234
for osd_id, devices in sorted(report.items()):
3335
output.append(
@@ -60,7 +62,7 @@ def pretty_report(report):
6062
print(''.join(output))
6163

6264

63-
def direct_report():
65+
def direct_report() -> typing.Dict[str, typing.Any]:
6466
"""
6567
Other non-cli consumers of listing information will want to consume the
6668
report without the need to parse arguments or other flags. This helper
@@ -71,15 +73,15 @@ def direct_report():
7173

7274

7375
# TODO: Perhaps, get rid of this class and simplify this module further?
74-
class List(object):
76+
class List:
7577

7678
help = 'list logical volumes and devices associated with Ceph'
7779

78-
def __init__(self, argv):
80+
def __init__(self, argv: typing.List[str]) -> None:
7981
self.argv = argv
8082

8183
@decorators.needs_root
82-
def list(self, args):
84+
def list(self, args: argparse.Namespace) -> None:
8385
report = self.single_report(args.device) if args.device else \
8486
self.full_report()
8587
if args.format == 'json':
@@ -94,12 +96,12 @@ def list(self, args):
9496
raise SystemExit('No valid Ceph lvm devices found')
9597
pretty_report(report)
9698

97-
def create_report(self, lvs):
99+
def create_report(self, lvs: typing.List[api.Volume]) -> typing.Dict[str, typing.Any]:
98100
"""
99101
Create a report for LVM dev(s) passed. Returns '{}' to denote failure.
100102
"""
101103

102-
report = {}
104+
report: typing.Dict[str, typing.Any] = {}
103105

104106
pvs = api.get_pvs()
105107

@@ -120,7 +122,7 @@ def create_report(self, lvs):
120122

121123
return report
122124

123-
def create_report_non_lv_device(self, lv):
125+
def create_report_non_lv_device(self, lv: api.Volume) -> typing.Dict[str, typing.Any]:
124126
report = {}
125127
if lv.tags.get('ceph.type', '') in ['data', 'block']:
126128
for dev_type in ['journal', 'wal', 'db']:
@@ -134,13 +136,13 @@ def create_report_non_lv_device(self, lv):
134136
'path': dev}
135137
return report
136138

137-
def full_report(self):
139+
def full_report(self) -> typing.Dict[str, typing.Any]:
138140
"""
139141
Create a report of all Ceph LVs. Returns '{}' to denote failure.
140142
"""
141143
return self.create_report(api.get_lvs())
142144

143-
def single_report(self, osd):
145+
def single_report(self, osd: str) -> typing.Dict[str, typing.Any]:
144146
"""
145147
Generate a report for a single device. This can be either a logical
146148
volume in the form of vg/lv, a device with an absolute path like
@@ -154,8 +156,12 @@ def single_report(self, osd):
154156
lv = api.get_lvs_from_path(osd)
155157
else:
156158
vg_name, lv_name = osd.split('/')
157-
lv = [api.get_single_lv(filters={'lv_name': lv_name,
158-
'vg_name': vg_name})]
159+
_lv = api.get_single_lv(filters={'lv_name': lv_name,
160+
'vg_name': vg_name})
161+
if _lv is not None:
162+
lv = [_lv]
163+
else:
164+
raise RuntimeError(f'Unexpected error while reporting {osd}')
159165

160166
report = self.create_report(lv)
161167

@@ -166,16 +172,16 @@ def single_report(self, osd):
166172
'ceph.{}_device'.format(dev_type): osd})
167173
if lvs:
168174
# just taking the first lv here should work
169-
lv = lvs[0]
170-
phys_dev = self.create_report_non_lv_device(lv)
171-
osd_id = lv.tags.get('ceph.osd_id')
175+
_lv = lvs[0]
176+
phys_dev = self.create_report_non_lv_device(_lv)
177+
osd_id = _lv.tags.get('ceph.osd_id')
172178
if osd_id:
173179
report[osd_id] = [phys_dev]
174180

175181

176182
return report
177183

178-
def main(self):
184+
def main(self) -> None:
179185
sub_command_help = dedent("""
180186
List devices or logical volumes associated with Ceph. An association is
181187
determined if a device has information relating to an OSD. This is

0 commit comments

Comments
 (0)