Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/changes/unreleased.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# Unreleased

This release updates the Python API generated from file `openapi.json`.

The following items are added to `open-api.json`:

* `paths` / `/api/v1/accounts/{accountId}/databases/{databaseId}/schedules/{actionId}/state`
* `paths` / `/api/v1/accounts/{accountId}/databases/{databaseId}/schedules/{actionId}/cronRule`
* `components` / `schemas` / `Schedule`
* `components` / `schemas` / `ScheduleState`

## Refactorings

* #119: Updated open API client
71 changes: 54 additions & 17 deletions exasol/saas/client/api_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
delete_allowed_ip,
list_allowed_i_ps,
)
from exasol.saas.client.openapi.models.status import Status
from exasol.saas.client.openapi.models import (
ApiError,
Status,
)
from exasol.saas.client.openapi.types import UNSET

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -79,6 +82,11 @@ class DatabaseDeleteTimeout(Exception):
"""


class OpenApiError(RuntimeError):
def __init__(self, message: str, error: ApiError | None):
super().__init__(f"{message}: {error.message}." if error else message)


def create_saas_client(
host: str,
pat: str,
Expand Down Expand Up @@ -172,17 +180,21 @@ def get_connection_params(
cluster_id = next(
filter(lambda cl: cl.main_cluster, clusters) # type: ignore
).id
connections = get_cluster_connection.sync(
connection = get_cluster_connection.sync(
account_id, database_id, cluster_id, client=client
)
if connections is None:
raise RuntimeError("Failed to get the SaaS connection data.")
connection_params = {
"dsn": f"{connections.dns}:{connections.port}",
"user": connections.db_username,
if connection is None or isinstance(connection, ApiError):
raise OpenApiError(
"Failed to get the connection data to"
f" host {host}, account {account_id},"
f" database with ID {database_id} named {database_name}",
connection,
)
return {
"dsn": f"{connection.dns}:{connection.port}",
"user": connection.db_username,
"password": pat,
}
return connection_params


class OpenApiAccess:
Expand All @@ -200,9 +212,9 @@ def create_database(
self,
name: str,
cluster_size: str = "XS",
region: str = "eu-central-1",
idle_time: timedelta | None = None,
) -> openapi.models.exasol_database.ExasolDatabase | None:
region: str = "eu-central-1",
) -> openapi.models.ExasolDatabase | None:
def minutes(x: timedelta) -> int:
return x.seconds // 60

Expand All @@ -216,7 +228,7 @@ def minutes(x: timedelta) -> int:
),
)
LOG.info(f"Creating database {name}")
return create_database.sync(
result = create_database.sync(
self._account_id,
client=self._client,
body=openapi.models.CreateDatabase(
Expand All @@ -227,6 +239,9 @@ def minutes(x: timedelta) -> int:
stream_type="feature-release",
),
)
if isinstance(result, ApiError):
raise OpenApiError(f"Failed to create database {name}", result)
return result

@contextmanager
def _ignore_failures(self, ignore: bool = False):
Expand Down Expand Up @@ -259,6 +274,8 @@ def delete_database(self, database_id: str, ignore_failures=False):

def list_database_ids(self) -> Iterable[str]:
dbs = list_databases.sync(self._account_id, client=self._client) or []
if isinstance(dbs, ApiError):
raise OpenApiError("Failed to list databases", dbs)
return (db.id for db in dbs)

@contextmanager
Expand Down Expand Up @@ -292,12 +309,15 @@ def database(
def get_database(
self,
database_id: str,
) -> openapi.models.exasol_database.ExasolDatabase | None:
return get_database.sync(
) -> openapi.models.ExasolDatabase | None:
result = get_database.sync(
self._account_id,
database_id,
client=self._client,
)
if isinstance(result, ApiError):
raise OpenApiError(f"Failed to get database {database_id}", result)
return result

def wait_until_running(
self,
Expand All @@ -324,23 +344,35 @@ def clusters(
self,
database_id: str,
) -> list[openapi.models.Cluster] | None:
return list_clusters.sync(
result = list_clusters.sync(
self._account_id,
database_id,
client=self._client,
)
if isinstance(result, ApiError):
raise OpenApiError(
f"Failed to list clusters of database {database_id}", result
)
return result

def get_connection(
self,
database_id: str,
cluster_id: str,
) -> openapi.models.ClusterConnection | None:
return get_cluster_connection.sync(
result = get_cluster_connection.sync(
self._account_id,
database_id,
cluster_id,
client=self._client,
)
if isinstance(result, ApiError):
raise OpenApiError(
"Failed to retrieve a connection to "
f"database {database_id} cluster {cluster_id}",
result,
)
return result

def list_allowed_ip_ids(self) -> Iterable[str]:
ips = (
Expand All @@ -350,12 +382,14 @@ def list_allowed_ip_ids(self) -> Iterable[str]:
)
or []
)
if isinstance(ips, ApiError):
raise OpenApiError("Failed to retrieve the list of allowed ips", ips)
return (x.id for x in ips)

def add_allowed_ip(
self,
cidr_ip: str = "0.0.0.0/0",
) -> openapi.models.allowed_ip.AllowedIP | None:
) -> openapi.models.AllowedIP | None:
"""
Suggested values for cidr_ip:
* 185.17.207.78/32
Expand All @@ -366,11 +400,14 @@ def add_allowed_ip(
name=timestamp_name(),
cidr_ip=cidr_ip,
)
return add_allowed_ip.sync(
result = add_allowed_ip.sync(
self._account_id,
client=self._client,
body=rule,
)
if isinstance(result, ApiError):
raise OpenApiError(f"Failed to add allowed IP address {cidr_ip}", result)
return result

def delete_allowed_ip(self, id: str, ignore_failures=False):
with self._ignore_failures(ignore_failures) as client:
Expand Down
29 changes: 15 additions & 14 deletions exasol/saas/client/openapi/api/clusters/create_cluster.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading