diff --git a/exasol/saas/client/gapi.py b/exasol/saas/client/gapi.py new file mode 100644 index 0000000..5499437 --- /dev/null +++ b/exasol/saas/client/gapi.py @@ -0,0 +1,47 @@ +import json + +from typing import Iterable +from exasol.saas.client.api import Database + +from exasol.saas.client import model + +from exasol.saas.client.model import ( + generated, + SAAS_HOST, +) +from exasol.saas.client.model.generated.api.databases import list_databases +from exasol.saas.client.model.generated.api.databases import create_database +# from exasol.saas.client.model.generated.api import databases + + +class Session: + """ + Session for interacting with an Exasol SaaS instance. + """ + + def __init__(self, token: str, account_id: str): + self._token = token + self._account = account_id + self._client = generated.AuthenticatedClient( + base_url=SAAS_HOST, + token=token, + ) + + def databases(self) -> Iterable[Database]: + """List existing databases""" + response = list_databases.sync_detailed(self._account, client=self._client) + return ( Database(self, x) for x in json.loads(response.content) ) + + def create_database(self, name: str) -> Database: + """Create a new database.""" + db = create_database( + self._account, + client=self._client, + body=generated.models.CreateDatabase( + name=name, + initial_cluster=model.ClusterSpec(name="my-cluster"), + provider="aws", + region='us-east-1', + ) + ) + return None # Database(self, db) diff --git a/test/integration/generated_api_test.py b/test/integration/generated_api_test.py index 9246506..df36ad2 100644 --- a/test/integration/generated_api_test.py +++ b/test/integration/generated_api_test.py @@ -1,17 +1,62 @@ -import os import json +import os +import pytest +from exasol.saas.client import gapi from exasol.saas.client.model import generated from exasol.saas.client.model.generated.api.databases import list_databases +from exasol.saas.client.model.generated.api.databases import create_database from exasol.saas.client.model import SAAS_API, SAAS_HOST -def test_databases_list_client(): - auth = generated.AuthenticatedClient( + +@pytest.fixture +def saas_client(): + return generated.AuthenticatedClient( base_url=SAAS_HOST, token=os.environ["SAAS_PAT"], ) - with auth as client: - account_id = os.environ["SAAS_ACCOUNT_ID"] - dbs = list_databases.sync_detailed(account_id, client=client) - c = json.loads(dbs.content) - print(f'{json.dumps(c, indent=4)}') + + +@pytest.fixture +def saas_session(): + return gapi.Session( + os.environ["SAAS_PAT"], + os.environ["SAAS_ACCOUNT_ID"], + ) + + +def pretty_print_response(response): + c = json.loads(response.content) + print(f'{json.dumps(c, indent=4)}') + + +def test_databases_list_raw(saas_client): + account_id = os.environ["SAAS_ACCOUNT_ID"] + response = list_databases.sync_detailed(account_id, client=saas_client) + print(f'{response}') + # pretty_print_response(response) + + +def test_databases_list(saas_session): + dbs = saas_session.databases() + for db in dbs: + print(f'{db}') + + +def test_database_create_raw(saas_client): + account_id = os.environ["SAAS_ACCOUNT_ID"] + cluster_spec = generated.models.CreateCluster( + name="my-cluster", + size="XS", + ) + response = create_database.sync_detailed( + account_id, + client=saas_client, + body=generated.models.CreateDatabase( + name="my-name", + initial_cluster=cluster_spec, + provider="aws", + region='us-east-1', + ) + ) + pretty_print_response(response)