Skip to content

Commit

Permalink
feat(storagelist): Add storages list group shared by storage name
Browse files Browse the repository at this point in the history
  • Loading branch information
lperdereau committed Nov 13, 2024
1 parent 32224a9 commit aa02b00
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/pvecontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions src/pvecontrol/actions/storage.py
Original file line number Diff line number Diff line change
@@ -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')
5 changes: 5 additions & 0 deletions src/pvecontrol/cluster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from proxmoxer import ProxmoxAPI

from pvecontrol.node import PVENode
from pvecontrol.storage import PVEStorage
from pvecontrol.task import PVETask


Expand All @@ -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"]))
Expand Down
26 changes: 26 additions & 0 deletions src/pvecontrol/storage.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit aa02b00

Please sign in to comment.