|
| 1 | +from typer import Typer |
| 2 | +from labctl.core import cli_ready, Config, APIDriver, console |
| 3 | +from rich.progress import Progress |
| 4 | +from rich.table import Table |
| 5 | + |
| 6 | +app = Typer() |
| 7 | +project = Typer() |
| 8 | +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> |
| 17 | + |
| 18 | +@cli_ready |
| 19 | +@app.command(name="reset-password") |
| 20 | +def reset_password(): |
| 21 | + """ |
| 22 | + Reset OpenStack password |
| 23 | + """ |
| 24 | + console.print("[cyan]Resetting your OpenStack user password[/cyan]") |
| 25 | + config = Config() |
| 26 | + call = APIDriver().put(f"/openstack/users/{config.username}/reset-password") |
| 27 | + if call.status_code >= 400: |
| 28 | + console.print(f"[red]Error: {call.text}[/red]") |
| 29 | + return |
| 30 | + console.print(f"[green]New password for {config.username} is [/green][bright_yellow]{call.json()['password']}[/bright_yellow]") |
| 31 | + console.print("[yellow]Please change it after login on console[/yellow]") |
| 32 | + |
| 33 | +@cli_ready |
| 34 | +@project.command(name="list") |
| 35 | +def list_projects(): |
| 36 | + """ |
| 37 | + List OpenStack projects |
| 38 | + """ |
| 39 | + config = Config() |
| 40 | + console.print("[cyan]Listing OpenStack projects[/cyan]") |
| 41 | + call = APIDriver().get(f"/openstack/projects/{config.username}") |
| 42 | + if call.status_code >= 400: |
| 43 | + console.print(f"[red]Error: {call.text}[/red]") |
| 44 | + return |
| 45 | + table = Table(title="Projects") |
| 46 | + table.add_column("Id") |
| 47 | + table.add_column("Name") |
| 48 | + for project in call.json(): |
| 49 | + table.add_row(str(project['id']), project['name']) |
| 50 | + console.print(table) |
| 51 | + |
| 52 | +@cli_ready |
| 53 | +@project.command(name="create") |
| 54 | +def create_project(name: str): |
| 55 | + """ |
| 56 | + Create OpenStack project |
| 57 | + """ |
| 58 | + config = Config() |
| 59 | + console.print(f"[cyan]Creating OpenStack project {name}[/cyan]") |
| 60 | + call = APIDriver().post(f"/openstack/projects/{name}") |
| 61 | + if call.status_code >= 400: |
| 62 | + console.print(f"[red]Error: {call.text}[/red]") |
| 63 | + return |
| 64 | + console.print(f"[green]Project {name} created[/green]") |
| 65 | + |
| 66 | +@cli_ready |
| 67 | +@project.command(name="delete") |
| 68 | +def delete_project(name: str): |
| 69 | + """ |
| 70 | + Delete OpenStack project |
| 71 | + """ |
| 72 | + config = Config() |
| 73 | + console.print(f"[cyan]Deleting OpenStack project {name}[/cyan]") |
| 74 | + call = APIDriver().delete(f"/openstack/projects/{name}") |
| 75 | + if call.status_code >= 400: |
| 76 | + console.print(f"[red]Error: {call.text}[/red]") |
| 77 | + return |
| 78 | + console.print(f"[green]Project {name} deleted[/green]") |
| 79 | + |
| 80 | +@cli_ready |
| 81 | +@project.command(name="add-user") |
| 82 | +def add_user(project: str, user: str): |
| 83 | + """ |
| 84 | + Add user to OpenStack project |
| 85 | + """ |
| 86 | + console.print(f"[cyan]Adding user {user} to OpenStack project {project}[/cyan]") |
| 87 | + call = APIDriver().put(f"/openstack/projects/{project}/users/{user}") |
| 88 | + if call.status_code >= 400: |
| 89 | + console.print(f"[red]Error: {call.text}[/red]") |
| 90 | + return |
| 91 | + console.print(f"[green]User {user} added to project {project}[/green]") |
| 92 | + |
| 93 | +@cli_ready |
| 94 | +@project.command(name="del-user") |
| 95 | +def del_user(project: str, user: str): |
| 96 | + """ |
| 97 | + Delete user from OpenStack project |
| 98 | + """ |
| 99 | + config = Config() |
| 100 | + console.print(f"[cyan]Deleting user {user} from OpenStack project {project}[/cyan]") |
| 101 | + call = APIDriver().delete(f"/openstack/projects/{project}/users/{user}") |
| 102 | + if call.status_code >= 400: |
| 103 | + console.print(f"[red]Error: {call.text}[/red]") |
| 104 | + return |
| 105 | + console.print(f"[green]User {user} deleted from project {project}[/green]") |
0 commit comments