Skip to content

Commit

Permalink
Merge pull request #38 from gisaia/feat/login
Browse files Browse the repository at this point in the history
add command login to arlas_cli confs
  • Loading branch information
sylvaingaudan authored Nov 15, 2024
2 parents 6a0bb93 + dd9df4a commit d741d37
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 30 deletions.
4 changes: 4 additions & 0 deletions arlas/cli/arlas_cloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ARLAS_SERVER = "https://cloud.arlas.io/arlas/server"
ARLAS_PERSISTENCE = "https://cloud.arlas.io/arlas/persistence"
CONTENT_TYPE = "Content-Type:application/json;charset=utf-8"
AUTH_TOKEN_URL = "https://cloud.arlas.io/arlas/iam/session"
7 changes: 2 additions & 5 deletions arlas/cli/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@


@collections.callback()
def configuration(config: str = typer.Option(help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))):
variables["arlas"] = config
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)
def configuration(config: str = typer.Option(default=None, help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))):
variables["arlas"] = Configuration.solve_config(config)


@collections.command(help="List collections", name="list")
Expand Down
58 changes: 57 additions & 1 deletion arlas/cli/configurations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import re
import sys
import typer
import yaml
from prettytable import PrettyTable
from arlas.cli.settings import ARLAS, AuthorizationService, Configuration, Resource
from arlas.cli.variables import variables
import arlas.cli.arlas_cloud as arlas_cloud

configurations = typer.Typer()

Expand All @@ -27,7 +29,7 @@ def create_configuration(
persistence: str = typer.Option(default=None, help="ARLAS Persistence url"),
persistence_headers: list[str] = typer.Option([], help="header (name:value)"),

elastic: str = typer.Option(default=None, help="dictionary of name/es resources"),
elastic: str = typer.Option(default=None, help="elasticsearch url"),
elastic_login: str = typer.Option(default=None, help="elasticsearch login"),
elastic_password: str = typer.Option(default=None, help="elasticsearch password"),
elastic_headers: list[str] = typer.Option([], help="header (name:value)"),
Expand Down Expand Up @@ -74,6 +76,60 @@ def create_configuration(
print("Configuration {} created.".format(name))


@configurations.command(help="Add a configuration for ARLAS Cloud", name="login")
def login(
auth_login: str = typer.Argument(help="login"),
elastic_login: str = typer.Argument(help="Elasticsearch login"),
elastic: str = typer.Argument(help="Elasticsearch url"),
auth_org: str = typer.Option(default=None, help="ARLAS IAM Organization, default is your email domain name"),
allow_delete: bool = typer.Option(default=True, help="Is delete command allowed for this configuration?"),
auth_password: str = typer.Option(default=None, help="ARLAS password"),
elastic_password: str = typer.Option(default=None, help="elasticsearch password")
):
if not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', auth_login):
print("Error: login {} is not a valid email".format(auth_login), file=sys.stderr)
exit(1)

name = "cloud.arlas.io." + auth_login.split("@")[0]
if Configuration.settings.arlas.get(name):
print("Error: a configuration with that name already exists, please remove it first.", file=sys.stderr)
exit(1)
print("Creating configuration for {} ...".format(name))
if not auth_org:
auth_org = auth_login.split("@")[1]
print("Using {} as your organisation name.".format(auth_org))
if not auth_password:
auth_password = typer.prompt("Please enter your password for ARLAS Cloud (account {})\n".format(auth_login), hide_input=True, prompt_suffix="Password:")
if not elastic_password:
elastic_password = typer.prompt("Thank you, now, please enter your password for elasticsearch (account {})\n".format(elastic_login), hide_input=True, prompt_suffix="Password:")

create_configuration(
name=name,
server=arlas_cloud.ARLAS_SERVER,
headers=[arlas_cloud.CONTENT_TYPE],
persistence=arlas_cloud.ARLAS_PERSISTENCE,
persistence_headers=[arlas_cloud.CONTENT_TYPE],
elastic=elastic,
elastic_login=elastic_login,
elastic_password=elastic_password,
elastic_headers=[arlas_cloud.CONTENT_TYPE],
allow_delete=allow_delete,
auth_token_url=arlas_cloud.AUTH_TOKEN_URL,
auth_headers=[arlas_cloud.CONTENT_TYPE],
auth_org=auth_org,
auth_login=auth_login,
auth_password=auth_password,
auth_arlas_iam=True,
auth_client_id=None,
auth_client_secret=None,
auth_grant_type=None,
)
Configuration.settings.default = name
Configuration.save(variables["configuration_file"])
Configuration.init(variables["configuration_file"])
print("{} is now your default configuration.".format(name))


@configurations.command(help="Delete a configuration", name="delete")
def delete_configuration(
config: str = typer.Argument(help="Name of the configuration"),
Expand Down
8 changes: 2 additions & 6 deletions arlas/cli/iam.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import sys
import typer

from arlas.cli.settings import Configuration
Expand All @@ -9,8 +8,5 @@


@iam.callback()
def configuration(config: str = typer.Option(help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))):
variables["arlas"] = config
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)
def configuration(config: str = typer.Option(default=None, help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))):
variables["arlas"] = Configuration.solve_config(config)
11 changes: 4 additions & 7 deletions arlas/cli/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@


@indices.callback()
def configuration(config: str = typer.Option(help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))):
variables["arlas"] = config
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)
def configuration(config: str = typer.Option(default=None, help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))):
variables["arlas"] = Configuration.solve_config(config)


@indices.command(help="List indices", name="list")
Expand Down Expand Up @@ -161,8 +158,8 @@ def delete(
print("Error: delete on \"{}\" is not allowed. To allow delete, change your configuration file ({}).".format(config, variables["configuration_file"]), file=sys.stderr)
exit(1)

if typer.confirm("You are about to delete the index '{}' on '{}' configuration.\n".format(index, config),
prompt_suffix="Do you want to continue (del {} on {})?".format(index, config),
if typer.confirm("You are about to delete the index '{}' on '{}' configuration.\n".format(index, config),
prompt_suffix="Do you want to continue (del {} on {})?".format(index, config),
default=False, ):
if config != "local" and config.find("test") < 0:
if typer.prompt("WARNING: You are not on a test environment. To delete {} on {}, type the name of the configuration ({})".format(index, config, config)) != config:
Expand Down
14 changes: 5 additions & 9 deletions arlas/cli/persist.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import json
import typer
import os
import sys

import typer
from prettytable import PrettyTable

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

persist = typer.Typer()


@persist.callback()
def configuration(config: str = typer.Option(help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))):
variables["arlas"] = config
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)
def configuration(config: str = typer.Option(default=None, help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))):
variables["arlas"] = Configuration.solve_config(config)


@persist.command(help="Add an entry, returns its ID", name="add")
Expand Down
15 changes: 15 additions & 0 deletions arlas/cli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,26 @@ class Settings(BaseModel):
arlas: dict[str, ARLAS] = Field(default=None, title="dictionary of name/arlas configurations")
mappings: dict[str, Resource] = Field(default=None, title="dictionary of name/mapping resources")
models: dict[str, Resource] = Field(default=None, title="dictionary of name/model resources")
default: str | None = Field(default=None, title="Name of the default configuration")


class Configuration:
settings: Settings = None

@staticmethod
def solve_config(config: str):
if not config:
if Configuration.settings.default:
print("Using default configuration {}".format(Configuration.settings.default))
return Configuration.settings.default
else:
print("Error: No default configuration, please provide one among [{}]".format(", ".join(Configuration.settings.arlas.keys())), file=sys.stderr)
exit(1)
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)
return config

@staticmethod
def save(configuration_file: str):
with open(configuration_file, 'w') as file:
Expand Down
14 changes: 12 additions & 2 deletions scripts/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ if test -f "/tmp/arlas_cli_persist"; then
rm -rf /tmp/arlas_cli_persist
fi

python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml --config-file /tmp/arlas_cli.yaml --version
python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml --config-file /tmp/arlas_cli.yaml confs \
python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml --version
python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs \
create tests \
--server http://localhost:9999/arlas \
--persistence http://localhost:9997/arlas_persistence_server \
Expand All @@ -19,6 +19,16 @@ python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml --config-file /tmp
--elastic-headers "Content-Type:application/json" \
--allow-delete

# ----------------------------------------------------------
echo "TEST configuration added with login"
python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs login [email protected] [email protected] https://some_elastic_search_server --auth-password toto --elastic-password titi
if python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs list | grep "cloud.arlas.io.support" ; then
echo "OK: configuration found"
else
echo "ERROR: configuration not found"
exit 1
fi

# ----------------------------------------------------------
echo "TEST configuration placed in /tmp/"
FILE=/tmp/arlas_cli.yaml
Expand Down

0 comments on commit d741d37

Please sign in to comment.