Skip to content

Commit

Permalink
set default, check conf
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvaingaudan committed Jan 6, 2025
1 parent ee0752a commit d9d6c4f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
31 changes: 31 additions & 0 deletions arlas/cli/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,41 @@
from arlas.cli.settings import ARLAS, AuthorizationService, Configuration, Resource
from arlas.cli.variables import variables
import arlas.cli.arlas_cloud as arlas_cloud
from arlas.cli.service import Service

configurations = typer.Typer()


@configurations.command(help="Set default configuration among existing configurations", name="set", epilog=variables["help_epilog"])
def set_default_configuration(name: str = typer.Argument(help="Name of the configuration to become default")):
if not Configuration.settings.arlas.get(name):
print("Error: configuration {} not found among {}.".format(name, ",".join(Configuration.settings.arlas.keys())), file=sys.stderr)
exit(1)
Configuration.settings.default = name
Configuration.save(variables["configuration_file"])
Configuration.init(variables["configuration_file"])
print("Default configuration is now {}".format(name))


@configurations.command(help="Display the default configuration", name="default", epilog=variables["help_epilog"])
def default():
print("Default configuration is {}".format(Configuration.settings.default))


@configurations.command(help="Check the services of a configuration", name="check", epilog=variables["help_epilog"])
def test_configuration(name: str = typer.Argument(help="Configuration to be checked")):
if not Configuration.settings.arlas.get(name):
print("Error: configuration {} not found among {}.".format(name, ",".join(Configuration.settings.arlas.keys())), file=sys.stderr)
exit(1)
print("ARLAS Server: ... ", end="")
print(" {}".format(Service.test_arlas_server(name)))
print("ARLAS Persistence: ... ", end="")
print(" {}".format(Service.test_arlas_persistence(name)))
print("ARLAS IAM: ... ", end="")
print(" {}".format(Service.test_arlas_iam(name)))
print("Elasticsearch: ... ", end="")
print(" {}".format(Service.test_es(name)))

@configurations.command(help="List configurations", name="list", epilog=variables["help_epilog"])
def list_configurations():
confs = []
Expand Down
40 changes: 36 additions & 4 deletions arlas/cli/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ class Services(Enum):
class Service:
curl: bool = False

def test_arlas_server(arlas: str):
try:
Service.__arlas__(arlas, "explore/_list", exit_on_failure=False)
return "ok"
except Exception as e:
return "not ok ({} ...)".format(str(e)[:20])

def test_arlas_iam(arlas: str):
try:
Service.__arlas__(arlas, "organisations", service=Services.iam, exit_on_failure=False)
return "ok"
except Exception as e:
return "not ok ({} ...)".format(str(e)[:20])

def test_arlas_persistence(arlas: str):
try:
url = "/".join(["persist", "resources", "config.json"]) + "?size=10000&page=1&order=desc&pretty=false"
Service.__arlas__(arlas, url, service=Services.persistence_server, exit_on_failure=False)
return "ok"
except Exception as e:
return "not ok ({} ...)".format(str(e)[:20])

def test_es(arlas: str):
try:
Service.__es__(arlas, "/".join(["*"]), exit_on_failure=False)
return "ok"
except Exception as e:
return "not ok ({} ...)".format(str(e)[:20])

def create_user(arlas: str, email: str):
return Service.__arlas__(arlas, "users", post=json.dumps({"email": email}), service=Services.iam)

Expand Down Expand Up @@ -427,7 +456,7 @@ def __get_fields__(origin: list[str], properties: dict[str:dict]):
fields.append([".".join(o), type])
return fields

def __arlas__(arlas: str, suffix, post=None, put=None, patch=None, delete=None, service=Services.arlas_server):
def __arlas__(arlas: str, suffix, post=None, put=None, patch=None, delete=None, service=Services.arlas_server, exit_on_failure: bool = False):
configuration: ARLAS = Configuration.settings.arlas.get(arlas, None)
if configuration is None:
print("Error: arlas configuration {} not found among [{}] for {}.".format(arlas, ", ".join(Configuration.settings.arlas.keys()), service.name), file=sys.stderr)
Expand Down Expand Up @@ -469,9 +498,12 @@ def __arlas__(arlas: str, suffix, post=None, put=None, patch=None, delete=None,
print(r.content)
exit(1)
except Exception as e:
print("Error: request {} failed on {}".format(method, e), file=sys.stderr)
print(" url: {}".format(url), file=sys.stderr)
exit(1)
if exit_on_failure:
print("Error: request {} failed on {}".format(method, e), file=sys.stderr)
print(" url: {}".format(url), file=sys.stderr)
exit(1)
else:
raise e

def __es__(arlas: str, suffix, post=None, put=None, delete=None, exit_on_failure: bool = True, headers: dict[str, str] = {}):
endpoint = Configuration.settings.arlas.get(arlas)
Expand Down
40 changes: 40 additions & 0 deletions scripts/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,46 @@ else
exit 1
fi

# ----------------------------------------------------------
echo "TEST default configuration set to tests"
python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs set tests
if python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs default | grep "Default configuration is tests" ; then
echo "OK: default is local"
else
echo "ERROR: local is not default"
exit 1
fi

# ----------------------------------------------------------
echo "TEST check local configuration"
if python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs check tests | grep "ARLAS Server: ... ok" ; then
echo "OK: ARLAS Server is ok"
else
echo "ERROR: ARLAS Server is not ok"
exit 1
fi

if python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs check tests | grep "ARLAS Persistence: ... ok" ; then
echo "OK: ARLAS Persistence is ok"
else
echo "ERROR: ARLAS Persistence is not ok"
exit 1
fi

if python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs check tests | grep "ARLAS IAM: ... not ok " ; then
echo "OK: ARLAS IAM is not ok"
else
echo "ERROR: ARLAS IAM not ok not found"
exit 1
fi

if python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml confs check tests | grep "Elasticsearch: ... ok" ; then
echo "OK: Elasticsearch is ok"
else
echo "ERROR: Elasticsearch is not ok"
exit 1
fi

# ----------------------------------------------------------
echo "TEST add direct mapping on ES"
python3.10 -m arlas.cli.cli --config-file /tmp/arlas_cli.yaml indices --config tests create direct_mappping_index --mapping tests/mapping.json
Expand Down

0 comments on commit d9d6c4f

Please sign in to comment.