-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8456d99
commit 82e2e0f
Showing
5 changed files
with
434 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import typer | ||
from rich import print | ||
from rich.table import Table | ||
|
||
from .client import EnvironmentClient, cycleops_client | ||
from .exceptions import NotFound | ||
from .utils import display_error_message | ||
|
||
app = typer.Typer() | ||
|
||
environment_client: EnvironmentClient = EnvironmentClient(cycleops_client) | ||
|
||
|
||
@app.command() | ||
def list() -> None: | ||
""" | ||
List all of the available environments. | ||
""" | ||
|
||
try: | ||
environments = environment_client.list() | ||
|
||
if not environments: | ||
raise NotFound("No environments available") | ||
|
||
table = Table(show_header=True, leading=True) | ||
table.add_column("ID", width=5) | ||
table.add_column("Name", width=30) | ||
|
||
for environment in environments: | ||
table.add_row(str(environment["id"]), environment["name"]) | ||
|
||
print(table) | ||
except Exception as error: | ||
display_error_message(error) | ||
raise typer.Exit(code=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
from typing import Any, Dict, List, Optional | ||
|
||
import typer | ||
from rich import print | ||
|
||
from .client import HostgroupClient, cycleops_client | ||
from .exceptions import NotFound | ||
from .utils import display_error_message, display_success_message | ||
|
||
app = typer.Typer() | ||
|
||
hostgroup_client: HostgroupClient = HostgroupClient(cycleops_client) | ||
|
||
|
||
@app.command() | ||
def list() -> None: | ||
""" | ||
List all of the available hostgroups. | ||
""" | ||
|
||
try: | ||
hostgroups = hostgroup_client.list() | ||
|
||
if not hostgroups: | ||
raise NotFound("No hostgroups available") | ||
|
||
print(hostgroups) | ||
except Exception as error: | ||
display_error_message(error) | ||
raise typer.Exit(code=1) | ||
|
||
|
||
@app.command() | ||
def retrieve( | ||
hostgroup_identifier: str = typer.Argument( | ||
..., help="The ID or name of the hostgroup. Names take precedence." | ||
), | ||
) -> None: | ||
""" | ||
Retrieve the hostgroup specified by the given ID. | ||
""" | ||
|
||
try: | ||
hostgroup = get_hostgroup(hostgroup_identifier) | ||
|
||
print(hostgroup) | ||
except Exception as error: | ||
display_error_message(error) | ||
raise typer.Exit(code=1) | ||
|
||
|
||
@app.command() | ||
def create( | ||
name: str = typer.Option( | ||
..., | ||
help="The name of the hostgroup.", | ||
), | ||
environment_id: int = typer.Option(None, help="The ID of the environment."), | ||
hosts: Optional[List[int]] = typer.Option( | ||
None, | ||
"--host-id", | ||
help="The ID of the host.", | ||
), | ||
) -> None: | ||
""" | ||
Create a hostgroup with the specified option values. | ||
""" | ||
|
||
try: | ||
hostgroup_client.create( | ||
name=name, | ||
environment=environment_id, | ||
hosts=hosts, | ||
) | ||
|
||
display_success_message(f"Hostgroup {name} has been created") | ||
except Exception as error: | ||
display_error_message(error) | ||
raise typer.Abort() | ||
|
||
|
||
@app.command() | ||
def update( | ||
hostgroup_identifier: str = typer.Argument( | ||
..., help="The ID or name of the hostgroup. Names take precedence." | ||
), | ||
name: Optional[str] = typer.Option( | ||
None, | ||
help="The name of the hostgroup.", | ||
), | ||
environment_id: Optional[int] = typer.Option( | ||
None, help="The ID of the environment." | ||
), | ||
hosts: Optional[List[int]] = typer.Option( | ||
None, | ||
"--host-id", | ||
help="The ID of the host.", | ||
), | ||
) -> None: | ||
""" | ||
Update a hostgroup with the specified option values. | ||
""" | ||
|
||
try: | ||
hostgroup = get_hostgroup(hostgroup_identifier) | ||
|
||
hostgroup_client.update( | ||
hostgroup["id"], | ||
name=name, | ||
environment=environment_id, | ||
hosts=hosts, | ||
) | ||
|
||
display_success_message(f"Hostgroup {hostgroup_identifier} has been updated") | ||
except Exception as error: | ||
display_error_message(error) | ||
raise typer.Abort() | ||
|
||
|
||
@app.command() | ||
def delete( | ||
hostgroup_identifier: str = typer.Argument( | ||
..., help="The ID or name of the hostgroup. Names take precedence." | ||
), | ||
) -> None: | ||
""" | ||
Delete the hostgroup specified by the given name. | ||
""" | ||
|
||
try: | ||
hostgroup = get_hostgroup(hostgroup_identifier) | ||
|
||
hostgroup_client.delete(hostgroup["id"]) | ||
display_success_message(f"Hostgroup {hostgroup_identifier} has been deleted") | ||
except Exception as error: | ||
display_error_message(error) | ||
raise typer.Abort() | ||
|
||
|
||
def get_hostgroup(hostgroup_identifier: str) -> Optional[Dict[str, Any]]: | ||
""" | ||
Retrieves a hostgroup with either a name or ID. Names take precedence. | ||
""" | ||
|
||
hostgroup = hostgroup_client.retrieve(params={"name": hostgroup_identifier}) | ||
|
||
if len(hostgroup) == 1: | ||
return hostgroup[0] | ||
|
||
hostgroup = hostgroup_client.retrieve(hostgroup_identifier) | ||
|
||
return hostgroup |
Oops, something went wrong.