]> git.proxmox.com Git - ceph.git/blob - ceph/src/python-common/ceph/deployment/inventory.py
361adf3c36845f828bcef8e4c7f1c5067cf921eb
[ceph.git] / ceph / src / python-common / ceph / deployment / inventory.py
1 try:
2 from typing import List, Optional, Dict, Any
3 except ImportError:
4 pass # for type checking
5
6 import json
7
8
9 class Devices(object):
10 """
11 A container for Device instances with reporting
12 """
13
14 def __init__(self, devices):
15 # type: (List[Device]) -> None
16 self.devices = devices # type: List[Device]
17
18 def __eq__(self, other):
19 return self.to_json() == other.to_json()
20
21 def to_json(self):
22 # type: () -> List[dict]
23 return [d.to_json() for d in self.devices]
24
25 @classmethod
26 def from_json(cls, input):
27 # type: (List[Dict[str, Any]]) -> Devices
28 return cls([Device.from_json(i) for i in input])
29
30 def copy(self):
31 return Devices(devices=list(self.devices))
32
33
34 class Device(object):
35 report_fields = [
36 'rejected_reasons',
37 'available',
38 'path',
39 'sys_api',
40 'lvs',
41 'human_readable_type',
42 'device_id'
43 ]
44
45 def __init__(self,
46 path, # type: str
47 sys_api=None, # type: Optional[Dict[str, Any]]
48 available=None, # type: Optional[bool]
49 rejected_reasons=None, # type: Optional[List[str]]
50 lvs=None, # type: Optional[List[str]]
51 device_id=None, # type: Optional[str]
52 ):
53 self.path = path
54 self.sys_api = sys_api if sys_api is not None else {} # type: Dict[str, Any]
55 self.available = available
56 self.rejected_reasons = rejected_reasons if rejected_reasons is not None else []
57 self.lvs = lvs
58 self.device_id = device_id
59
60 def to_json(self):
61 # type: () -> dict
62 return {
63 k: getattr(self, k) for k in self.report_fields
64 }
65
66 @classmethod
67 def from_json(cls, input):
68 # type: (Dict[str, Any]) -> Device
69 if not isinstance(input, dict):
70 raise ValueError('Device: Expected dict. Got `{}...`'.format(json.dumps(input)[:10]))
71 ret = cls(
72 **{
73 key: input.get(key, None)
74 for key in Device.report_fields
75 if key != 'human_readable_type'
76 }
77 )
78 return ret
79
80 @property
81 def human_readable_type(self):
82 # type: () -> str
83 if self.sys_api is None or 'rotational' not in self.sys_api:
84 return "unknown"
85 return 'hdd' if self.sys_api["rotational"] == "1" else 'ssd'