Skip to content

Commit 5c32b4b

Browse files
authored
feat: display more info for quota (#19)
2 parents 495f11b + 3a1e955 commit 5c32b4b

5 files changed

Lines changed: 47 additions & 60 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 0 additions & 45 deletions
This file was deleted.

.devcontainer/devcontainer.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
*.pyc
22
dist/
3-
3+
.ruff_cache/
44
.DS_Store

labctl/commands/quota.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def show_project_quota(project: str):
3030

3131
@cli_ready
3232
@app.command(name="set")
33-
def add_quota(project: str, quota_type: str, quantity: int, comment: Optional[str] = None):
33+
def set_quota(project: str, quota_type: str, quantity: int, comment: Optional[str] = None):
3434
"""
3535
Add quota to OpenStack project
3636
"""
@@ -48,3 +48,24 @@ def add_quota(project: str, quota_type: str, quantity: int, comment: Optional[st
4848
console.print(f"[red]Error: {call.text}[/red]")
4949
return
5050
console.print(f"[green]Quota {quota_type}={quantity} set to project {project}[/green]")
51+
52+
@cli_ready
53+
@app.command(name="unset")
54+
def unset_quota(project: str, quota_type: str):
55+
"""
56+
Add quota to OpenStack project
57+
"""
58+
config = Config()
59+
console.print(f"[cyan]Unsetting {quota_type} to OpenStack project {project}[/cyan]")
60+
payload = {
61+
"username": config.username,
62+
"project_name": project,
63+
"type": quota_type,
64+
"quantity": 0,
65+
"comment": ""
66+
}
67+
call = APIDriver().put(f"/quota/adjust-project", json=payload)
68+
if call.status_code >= 400:
69+
console.print(f"[red]Error: {call.text}[/red]")
70+
return
71+
console.print(f"[green]Quota {quota_type} unset from project {project}[/green]")

labctl/main.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,39 @@ def me(
6969
tree.add("[bold]Username:[/bold] " + data.get("username"))
7070
tree.add("[bold]Email:[/bold] " + data.get("email"))
7171

72+
# show projects info
7273
project_list = api_driver.get("/openstack/projects/" + config.username).json()
7374
project_tree = tree.add(":open_file_folder: Projects")
7475
for project in project_list:
7576
project_tree_item = project_tree.add(":computer: " + project.get('name'))
7677
project_tree_item.add("[bold]Owner:[/bold] " + project.get('owner', ''))
7778
project_tree_item.add("Members: " + " ".join(project.get('members', [])))
79+
80+
# show quotas for project
81+
project_quota_list = api_driver.get(f"/quota/project/{project.get('name')}/adjustements")
82+
if project_quota_list.status_code >= 400:
83+
project_tree_item.add(f":warning: Error fetching quotas for project {project.get('name')}")
84+
continue
85+
86+
project_quota_tree = project_tree_item.add(":open_file_folder: Quotas for project")
87+
quota_types = set([quota.get('type') for quota in project_quota_list.json()])
88+
total_quotas = {quota_type: 0 for quota_type in quota_types}
89+
self_quotas = {quota_type: 0 for quota_type in quota_types}
90+
for quota in project_quota_list.json():
91+
total_quotas[quota.get('type')] += quota.get('quantity')
92+
if quota.get('username') == config.username:
93+
self_quotas[quota.get('type')] += quota.get('quantity')
94+
95+
for quota_type, quantity in total_quotas.items():
96+
if quantity == 0:
97+
continue
98+
project_quota_tree.add(f"{quota_type} Total: {quantity}")
99+
project_quota_tree.add(f"{quota_type} You give: {self_quotas[quota_type]}")
100+
78101
if not project_list:
79102
project_tree.add(":warning: No projects found")
80103

81-
quota_tree = tree.add(":open_file_folder: Quotas")
104+
quota_tree = tree.add(":open_file_folder: Quotas owned")
82105
quota_list = api_driver.get(f"/quota/user/{config.username}/total").json()
83106
for quota in quota_list:
84107
quota_tree.add(f"Type: {quota.get('type')} / {quota.get('quantity')}")

0 commit comments

Comments
 (0)