Skip to content

Commit

Permalink
Merge pull request #67 from gisaia/dev/add_cmd
Browse files Browse the repository at this point in the history
add commands in arlas cli
  • Loading branch information
sylvaingaudan authored Feb 20, 2025
2 parents 8465ad7 + a521140 commit b5c68dd
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 4 deletions.
2 changes: 1 addition & 1 deletion arlas/cli/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ def describe_configuration(
if Configuration.settings.arlas.get(config, None) is None:
print("Error: arlas configuration {} not found among [{}]".format(config, ", ".join(Configuration.settings.arlas.keys())), file=sys.stderr)
exit(1)
print(yaml.dump(Configuration.settings.arlas[config].model_dump()))
print(yaml.dump(Configuration.settings.arlas[config].model_dump()))
75 changes: 73 additions & 2 deletions arlas/cli/org.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys
import typer
from prettytable import PrettyTable

from arlas.cli.service import Service
from arlas.cli.settings import Configuration
from arlas.cli.variables import variables

org = typer.Typer()
Expand All @@ -18,9 +20,12 @@ def list_organisations():


@org.command(help="Create organisation with the given name", name="add", epilog=variables["help_epilog"])
def create_organisation(organisation: str = typer.Argument(help="Organisation's name")):
def create_organisation(organisation: str = typer.Argument(default="", help="Organisation's name")):
config = variables["arlas"]
print(Service.create_organisation(config, organisation).get("id"))
if organisation:
print(Service.create_organisation(config, organisation).get("id"))
else:
print(Service.create_organisation_from_user_domain(config, organisation).get("id"))


@org.command(help="Delete the organisation", name="delete", epilog=variables["help_epilog"])
Expand Down Expand Up @@ -148,3 +153,69 @@ def delete_user_from_group(org_id: str = typer.Argument(help="Organisation's ide
group_id: str = typer.Argument(help="Group identifier")):
config = variables["arlas"]
print(Service.delete_permission_from_group_in_organisation(config, org_id, user_id, group_id))


@org.command(help="Add and return an new API Key with permissions associated to provided groups. Use the key id and key secret with the arlas-api-key-id and arlas-api-key-secret headers.", name="add-apikey",
epilog=variables["help_epilog"])
def add_apikey(org_id: str = typer.Argument(help="Organisation's identifier"),
name: str = typer.Argument(help="API Key name"),
user_id: str = typer.Option(help="User identifier", default=None),
ttlInDays: int = typer.Option(help="Time To Live in days", default=365),
gids: list[str] = typer.Option(help="Group identifiers. If not provided, all groups of the user are used.", default=None)
):
config = variables["arlas"]
if not gids or len(gids) == 0:
gids = list(map(lambda arr: arr[0], Service.list_organisation_groups(config, org_id) + Service.list_organisation_roles(config, org_id)))
print(Service.create_api_key(config, org_id, name, ttlInDays, __solve_user_id__(config, org_id, user_id), gids))


@org.command(help="Delete an API Key", name="delete-apikey",
epilog=variables["help_epilog"])
def delete_apikey(org_id: str = typer.Argument(help="Organisation's identifier"),
key_id: str = typer.Argument(help="API Key identifier"),
user_id: str = typer.Option(help="User identifier", default=None),
):
config = variables["arlas"]
print(Service.delete_api_key(config, org_id, __solve_user_id__(config, org_id, user_id), key_id))


def __solve_user_id__(config: str, org_id: str, user_id: str):
if not user_id:
c = Configuration.settings.arlas.get(config)
if c and c.authorization and c.authorization.token_url and c.authorization.token_url.login:
user_id = Service.get_user_from_organisation(config, org_id, c.authorization.token_url.login)[0]
if not user_id:
print("Error : user id not found for {}.".format(config), file=sys.stderr)
sys.exit(1)
else:
print("Error : no login found for {}.".format(config), file=sys.stderr)
sys.exit(1)
return user_id


@org.command(help="Check if user's organisation exists", name="check",
epilog=variables["help_epilog"])
def check():
config = variables["arlas"]
print(Service.check_organisation(config))


@org.command(help="List forbidden organisations.", name="forbidden",
epilog=variables["help_epilog"])
def forbidden():
config = variables["arlas"]
print(Service.forbidden_organisations(config))


@org.command(help="Forbid an organisation name.", name="forbid",
epilog=variables["help_epilog"])
def forbid(name: str = typer.Argument(help="Name of the organisation to forbid")):
config = variables["arlas"]
print(Service.forbid_organisation(config, name))


@org.command(help="Remove an organisation name from the forbidden list.", name="authorize",
epilog=variables["help_epilog"])
def authorize(name: str = typer.Argument(help="Name of the organisation to authorize")):
config = variables["arlas"]
print(Service.authorize_organisation(config, name))
Loading

0 comments on commit b5c68dd

Please sign in to comment.