|
| 1 | +from typing import Optional |
1 | 2 | from typer import Typer |
2 | 3 | from labctl.core import cli_ready, Config, APIDriver, console |
3 | | -from rich.progress import Progress |
4 | 4 | from rich.table import Table |
5 | 5 |
|
6 | 6 | app = Typer() |
7 | 7 | project = Typer() |
| 8 | +quota = Typer() |
| 9 | + |
8 | 10 | app.add_typer(project, name="project") |
9 | | -## OpenStack commands |
10 | | -# reset-password |
11 | | -# project list |
12 | | -# project create <name> |
13 | | -# project delete <name> |
14 | | -# project add-user <project> <user> |
15 | | -# project del-user <project> <user> |
16 | | -# project list-users <project> |
| 11 | +app.add_typer(quota, name="quota") |
17 | 12 |
|
18 | 13 | @cli_ready |
19 | 14 | @app.command(name="reset-password") |
@@ -103,3 +98,62 @@ def del_user(project: str, user: str): |
103 | 98 | console.print(f"[red]Error: {call.text}[/red]") |
104 | 99 | return |
105 | 100 | console.print(f"[green]User {user} deleted from project {project}[/green]") |
| 101 | + |
| 102 | +# quota |
| 103 | +@cli_ready |
| 104 | +@quota.command(name="show-project") |
| 105 | +def show_project_quota(project: str): |
| 106 | + """ |
| 107 | + List OpenStack project quota |
| 108 | + """ |
| 109 | + call = APIDriver().get(f"/quota/project/{project}/adjustements") |
| 110 | + if call.status_code >= 400: |
| 111 | + console.print(f"[red]Error: {call.text}[/red]") |
| 112 | + return |
| 113 | + table = Table(title="Quotas for project " + project) |
| 114 | + |
| 115 | + table.add_column("Id") |
| 116 | + table.add_column("Type") |
| 117 | + table.add_column("Quantity") |
| 118 | + table.add_column("User") |
| 119 | + table.add_column("Comment") |
| 120 | + |
| 121 | + for quota in call.json(): |
| 122 | + table.add_row(str(quota['id']), quota['type'], str(quota['quantity']), quota['username'], quota['comment']) |
| 123 | + |
| 124 | + console.print(table) |
| 125 | + |
| 126 | +# labctl openstack quota add PROJECT_NAME QUOTATYPE VALUE |
| 127 | +@cli_ready |
| 128 | +@quota.command(name="add") |
| 129 | +def add_quota(project: str, quota_type: str, quantity: int, comment: Optional[str] = None): |
| 130 | + """ |
| 131 | + Add quota to OpenStack project |
| 132 | + """ |
| 133 | + config = Config() |
| 134 | + console.print(f"[cyan]Adding {quota_type}={quantity} to OpenStack project {project}[/cyan]") |
| 135 | + payload = { |
| 136 | + "username": config.username, |
| 137 | + "project_name": project, |
| 138 | + "type": quota_type, |
| 139 | + "quantity": quantity, |
| 140 | + "comment": comment |
| 141 | + } |
| 142 | + call = APIDriver().post(f"/quota/adjust-project", json=payload) |
| 143 | + if call.status_code >= 400: |
| 144 | + console.print(f"[red]Error: {call.text}[/red]") |
| 145 | + return |
| 146 | + console.print(f"[green]Quota {quota_type}={quantity} added to project {project}[/green]") |
| 147 | + |
| 148 | +@cli_ready |
| 149 | +@quota.command(name="del") |
| 150 | +def del_quota(id: int): |
| 151 | + """ |
| 152 | + Delete quota from OpenStack project |
| 153 | + """ |
| 154 | + console.print(f"[cyan]Deleting quota {id} from OpenStack project[/cyan]") |
| 155 | + call = APIDriver().delete(f"/quota/adjust-project/{id}/{Config().username}") |
| 156 | + if call.status_code >= 400: |
| 157 | + console.print(f"[red]Error: {call.text}[/red]") |
| 158 | + return |
| 159 | + console.print(f"[green]Quota {id} deleted from project[/green]") |
0 commit comments