From aa02b000969c78046a14f65de80edb4544be99f7 Mon Sep 17 00:00:00 2001 From: Louis PERDEREAU Date: Wed, 13 Nov 2024 16:24:27 +0100 Subject: [PATCH] feat(storagelist): Add storages list group shared by storage name --- src/pvecontrol/__init__.py | 6 +++++- src/pvecontrol/actions/storage.py | 27 +++++++++++++++++++++++++++ src/pvecontrol/cluster.py | 5 +++++ src/pvecontrol/storage.py | 26 ++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/pvecontrol/actions/storage.py create mode 100644 src/pvecontrol/storage.py diff --git a/src/pvecontrol/__init__.py b/src/pvecontrol/__init__.py index d335cb8..b69bf33 100644 --- a/src/pvecontrol/__init__.py +++ b/src/pvecontrol/__init__.py @@ -8,7 +8,7 @@ from pvecontrol.cluster import PVECluster from pvecontrol.config import set_config -from pvecontrol.actions import cluster, node, vm, task +from pvecontrol.actions import cluster, node, vm, storage, task def action_test(proxmox, args): """Hidden optional test action""" @@ -29,6 +29,10 @@ def _parser(): parser_clusterstatus = subparsers.add_parser('clusterstatus', help='Show cluster status') parser_clusterstatus.set_defaults(func=cluster.action_clusterstatus) + # storagelist parser + parser_clusterstatus = subparsers.add_parser('storagelist', help='Show cluster status') + parser_clusterstatus.set_defaults(func=storage.action_storagelist) + # nodelist parser parser_nodelist = subparsers.add_parser('nodelist', help='List nodes in the cluster') parser_nodelist.set_defaults(func=node.action_nodelist) diff --git a/src/pvecontrol/actions/storage.py b/src/pvecontrol/actions/storage.py new file mode 100644 index 0000000..cddb361 --- /dev/null +++ b/src/pvecontrol/actions/storage.py @@ -0,0 +1,27 @@ +from pvecontrol.utils import filter_keys, print_tableoutput + + +def action_storagelist(proxmox, args): + """Describe cluster storages""" + keys_to_order = ['storage', 'nodes', 'shared', 'usage', 'maxdisk', 'disk', 'plugintype', 'status'] + storages = {} + for storage in proxmox.storages: + d = storage.__dict__ + node = d.pop('node') + value = { + **d, + 'nodes': [], + 'usage': f"{storage.percentage:.1f}%" + } + if storage.shared: + storages[storage.storage] = storages.get(storage.storage, value) + storages[storage.storage]['nodes'] += [node] + else: + storages[storage.id] = value + storages[storage.id]['nodes'] += [node] + + for id, storage in storages.items(): + storages[id]['nodes'] = ', '.join(storages[id]['nodes']) + + output = [ filter_keys(n, keys_to_order) for n in storages.values()] + print_tableoutput(output, sortby='storage') diff --git a/src/pvecontrol/cluster.py b/src/pvecontrol/cluster.py index 545de27..d44a8b5 100644 --- a/src/pvecontrol/cluster.py +++ b/src/pvecontrol/cluster.py @@ -1,6 +1,7 @@ from proxmoxer import ProxmoxAPI from pvecontrol.node import PVENode +from pvecontrol.storage import PVEStorage from pvecontrol.task import PVETask @@ -21,6 +22,10 @@ def _initstatus(self): for node in self._api.nodes.get(): self.nodes.append(PVENode(self._api, node["node"], node["status"], node)) + self.storages = [] + for storage in self.get_resources_storages(): + self.storages.append(PVEStorage(storage.pop("node"), storage.pop("id"), **storage)) + self.tasks = [] for task in self._api.cluster.tasks.get(): self.tasks.append(PVETask(self._api, task["upid"])) diff --git a/src/pvecontrol/storage.py b/src/pvecontrol/storage.py new file mode 100644 index 0000000..f15c232 --- /dev/null +++ b/src/pvecontrol/storage.py @@ -0,0 +1,26 @@ +class PVEStorage: + """Proxmox VE Storage""" + + _acceptable_kwargs = ( + 'storage', 'shared', 'maxdisk', 'disk', 'plugintype', 'status' + ) + + def __init__(self, node, id, **kwargs): + self.id = id + self.node = node + + for k in kwargs.keys(): + if k in self._acceptable_kwargs: + self.__setattr__(k, kwargs[k]) + + @property + def percentage(self): + if self.maxdisk: + return self.disk / self.maxdisk *100 + return 0 + + def __str__(self): + output = f"Node: {self.node}\n" + f"Id: {self.id}\n" + for key in self._acceptable_kwargs: + output += f"{key.capitalize()}: {self.__getattribute__(key)}\n" + return output