Revision 473ab806 lib/objects.py
b/lib/objects.py | ||
---|---|---|
263 | 263 |
obj = cls(**val_str) # pylint: disable=W0142 |
264 | 264 |
return obj |
265 | 265 |
|
266 |
@staticmethod |
|
267 |
def _ContainerToDicts(container): |
|
268 |
"""Convert the elements of a container to standard python types. |
|
269 |
|
|
270 |
This method converts a container with elements derived from |
|
271 |
ConfigData to standard python types. If the container is a dict, |
|
272 |
we don't touch the keys, only the values. |
|
273 |
|
|
274 |
""" |
|
275 |
if isinstance(container, dict): |
|
276 |
ret = dict([(k, v.ToDict()) for k, v in container.iteritems()]) |
|
277 |
elif isinstance(container, (list, tuple, set, frozenset)): |
|
278 |
ret = [elem.ToDict() for elem in container] |
|
279 |
else: |
|
280 |
raise TypeError("Invalid type %s passed to _ContainerToDicts" % |
|
281 |
type(container)) |
|
282 |
return ret |
|
283 |
|
|
284 |
@staticmethod |
|
285 |
def _ContainerFromDicts(source, c_type, e_type): |
|
286 |
"""Convert a container from standard python types. |
|
287 |
|
|
288 |
This method converts a container with standard python types to |
|
289 |
ConfigData objects. If the container is a dict, we don't touch the |
|
290 |
keys, only the values. |
|
291 |
|
|
292 |
""" |
|
293 |
if not isinstance(c_type, type): |
|
294 |
raise TypeError("Container type %s passed to _ContainerFromDicts is" |
|
295 |
" not a type" % type(c_type)) |
|
296 |
if source is None: |
|
297 |
source = c_type() |
|
298 |
if c_type is dict: |
|
299 |
ret = dict([(k, e_type.FromDict(v)) for k, v in source.iteritems()]) |
|
300 |
elif c_type in (list, tuple, set, frozenset): |
|
301 |
ret = c_type([e_type.FromDict(elem) for elem in source]) |
|
302 |
else: |
|
303 |
raise TypeError("Invalid container type %s passed to" |
|
304 |
" _ContainerFromDicts" % c_type) |
|
305 |
return ret |
|
306 |
|
|
307 | 266 |
def Copy(self): |
308 | 267 |
"""Makes a deep copy of the current object and its children. |
309 | 268 |
|
... | ... | |
446 | 405 |
mydict = super(ConfigData, self).ToDict() |
447 | 406 |
mydict["cluster"] = mydict["cluster"].ToDict() |
448 | 407 |
for key in "nodes", "instances", "nodegroups", "networks": |
449 |
mydict[key] = self._ContainerToDicts(mydict[key])
|
|
408 |
mydict[key] = objectutils.ContainerToDicts(mydict[key])
|
|
450 | 409 |
|
451 | 410 |
return mydict |
452 | 411 |
|
... | ... | |
457 | 416 |
""" |
458 | 417 |
obj = super(ConfigData, cls).FromDict(val) |
459 | 418 |
obj.cluster = Cluster.FromDict(obj.cluster) |
460 |
obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node) |
|
461 |
obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance) |
|
462 |
obj.nodegroups = cls._ContainerFromDicts(obj.nodegroups, dict, NodeGroup) |
|
463 |
obj.networks = cls._ContainerFromDicts(obj.networks, dict, Network) |
|
419 |
obj.nodes = objectutils.ContainerFromDicts(obj.nodes, dict, Node) |
|
420 |
obj.instances = \ |
|
421 |
objectutils.ContainerFromDicts(obj.instances, dict, Instance) |
|
422 |
obj.nodegroups = \ |
|
423 |
objectutils.ContainerFromDicts(obj.nodegroups, dict, NodeGroup) |
|
424 |
obj.networks = objectutils.ContainerFromDicts(obj.networks, dict, Network) |
|
464 | 425 |
return obj |
465 | 426 |
|
466 | 427 |
def HasAnyDiskOfType(self, dev_type): |
... | ... | |
768 | 729 |
for attr in ("children",): |
769 | 730 |
alist = bo.get(attr, None) |
770 | 731 |
if alist: |
771 |
bo[attr] = self._ContainerToDicts(alist)
|
|
732 |
bo[attr] = objectutils.ContainerToDicts(alist)
|
|
772 | 733 |
return bo |
773 | 734 |
|
774 | 735 |
@classmethod |
... | ... | |
778 | 739 |
""" |
779 | 740 |
obj = super(Disk, cls).FromDict(val) |
780 | 741 |
if obj.children: |
781 |
obj.children = cls._ContainerFromDicts(obj.children, list, Disk)
|
|
742 |
obj.children = objectutils.ContainerFromDicts(obj.children, list, Disk)
|
|
782 | 743 |
if obj.logical_id and isinstance(obj.logical_id, list): |
783 | 744 |
obj.logical_id = tuple(obj.logical_id) |
784 | 745 |
if obj.physical_id and isinstance(obj.physical_id, list): |
... | ... | |
1136 | 1097 |
for attr in "nics", "disks": |
1137 | 1098 |
alist = bo.get(attr, None) |
1138 | 1099 |
if alist: |
1139 |
nlist = self._ContainerToDicts(alist)
|
|
1100 |
nlist = objectutils.ContainerToDicts(alist)
|
|
1140 | 1101 |
else: |
1141 | 1102 |
nlist = [] |
1142 | 1103 |
bo[attr] = nlist |
... | ... | |
1155 | 1116 |
if "admin_up" in val: |
1156 | 1117 |
del val["admin_up"] |
1157 | 1118 |
obj = super(Instance, cls).FromDict(val) |
1158 |
obj.nics = cls._ContainerFromDicts(obj.nics, list, NIC)
|
|
1159 |
obj.disks = cls._ContainerFromDicts(obj.disks, list, Disk)
|
|
1119 |
obj.nics = objectutils.ContainerFromDicts(obj.nics, list, NIC)
|
|
1120 |
obj.disks = objectutils.ContainerFromDicts(obj.disks, list, Disk)
|
|
1160 | 1121 |
return obj |
1161 | 1122 |
|
1162 | 1123 |
def UpgradeConfig(self): |
... | ... | |
1344 | 1305 |
|
1345 | 1306 |
hv_state = data.get("hv_state", None) |
1346 | 1307 |
if hv_state is not None: |
1347 |
data["hv_state"] = self._ContainerToDicts(hv_state)
|
|
1308 |
data["hv_state"] = objectutils.ContainerToDicts(hv_state)
|
|
1348 | 1309 |
|
1349 | 1310 |
disk_state = data.get("disk_state", None) |
1350 | 1311 |
if disk_state is not None: |
1351 | 1312 |
data["disk_state"] = \ |
1352 |
dict((key, self._ContainerToDicts(value))
|
|
1313 |
dict((key, objectutils.ContainerToDicts(value))
|
|
1353 | 1314 |
for (key, value) in disk_state.items()) |
1354 | 1315 |
|
1355 | 1316 |
return data |
... | ... | |
1362 | 1323 |
obj = super(Node, cls).FromDict(val) |
1363 | 1324 |
|
1364 | 1325 |
if obj.hv_state is not None: |
1365 |
obj.hv_state = cls._ContainerFromDicts(obj.hv_state, dict, NodeHvState) |
|
1326 |
obj.hv_state = \ |
|
1327 |
objectutils.ContainerFromDicts(obj.hv_state, dict, NodeHvState) |
|
1366 | 1328 |
|
1367 | 1329 |
if obj.disk_state is not None: |
1368 | 1330 |
obj.disk_state = \ |
1369 |
dict((key, cls._ContainerFromDicts(value, dict, NodeDiskState))
|
|
1331 |
dict((key, objectutils.ContainerFromDicts(value, dict, NodeDiskState))
|
|
1370 | 1332 |
for (key, value) in obj.disk_state.items()) |
1371 | 1333 |
|
1372 | 1334 |
return obj |
... | ... | |
1935 | 1897 |
|
1936 | 1898 |
""" |
1937 | 1899 |
mydict = super(_QueryResponseBase, self).ToDict() |
1938 |
mydict["fields"] = self._ContainerToDicts(mydict["fields"])
|
|
1900 |
mydict["fields"] = objectutils.ContainerToDicts(mydict["fields"])
|
|
1939 | 1901 |
return mydict |
1940 | 1902 |
|
1941 | 1903 |
@classmethod |
... | ... | |
1944 | 1906 |
|
1945 | 1907 |
""" |
1946 | 1908 |
obj = super(_QueryResponseBase, cls).FromDict(val) |
1947 |
obj.fields = cls._ContainerFromDicts(obj.fields, list, QueryFieldDefinition) |
|
1909 |
obj.fields = \ |
|
1910 |
objectutils.ContainerFromDicts(obj.fields, list, QueryFieldDefinition) |
|
1948 | 1911 |
return obj |
1949 | 1912 |
|
1950 | 1913 |
|
Also available in: Unified diff