Skip to content

Added support for DB Configurable Params #527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: proj/configurable-db-params
Choose a base branch
from
54 changes: 52 additions & 2 deletions linode_api4/groups/database.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from typing import Any, Dict, Union

from linode_api4 import (
MySQLDatabaseConfigOptions,
PostgreSQLDatabaseConfigOptions,
)
from linode_api4.errors import UnexpectedResponseError
from linode_api4.groups import Group
from linode_api4.objects import (
Expand Down Expand Up @@ -63,6 +69,26 @@ def engines(self, *filters):
"""
return self.client._get_and_filter(DatabaseEngine, *filters)

def mysql_config_options(self):
"""
Returns a detailed list of all the configuration options for MySQL Databases.

API Documentation: TODO

:returns: The JSON configuration options for MySQL Databases.
"""
return self.client.get("databases/mysql/config", model=self)

def postgresql_config_options(self):
"""
Returns a detailed list of all the configuration options for PostgreSQL Databases.

API Documentation: TODO

:returns: The JSON configuration options for PostgreSQL Databases.
"""
return self.client.get("databases/postgresql/config", model=self)

def instances(self, *filters):
"""
Returns a list of Managed Databases active on this account.
Expand Down Expand Up @@ -93,7 +119,15 @@ def mysql_instances(self, *filters):
"""
return self.client._get_and_filter(MySQLDatabase, *filters)

def mysql_create(self, label, region, engine, ltype, **kwargs):
def mysql_create(
self,
label,
region,
engine,
ltype,
engine_config: Union[MySQLDatabaseConfigOptions, Dict[str, Any]] = None,
**kwargs,
):
"""
Creates an :any:`MySQLDatabase` on this account with
the given label, region, engine, and node type. For example::
Expand Down Expand Up @@ -123,13 +157,16 @@ def mysql_create(self, label, region, engine, ltype, **kwargs):
:type engine: str or Engine
:param ltype: The Linode Type to use for this cluster
:type ltype: str or Type
:param engine_config: The configuration options for this MySQL cluster
:type engine_config: Dict[str, Any] or MySQLDatabaseConfigOptions
"""

params = {
"label": label,
"region": region,
"engine": engine,
"type": ltype,
"engine_config": engine_config,
}
params.update(kwargs)

Expand Down Expand Up @@ -216,7 +253,17 @@ def postgresql_instances(self, *filters):
"""
return self.client._get_and_filter(PostgreSQLDatabase, *filters)

def postgresql_create(self, label, region, engine, ltype, **kwargs):
def postgresql_create(
self,
label,
region,
engine,
ltype,
engine_config: Union[
PostgreSQLDatabaseConfigOptions, Dict[str, Any]
] = None,
**kwargs,
):
"""
Creates an :any:`PostgreSQLDatabase` on this account with
the given label, region, engine, and node type. For example::
Expand Down Expand Up @@ -246,13 +293,16 @@ def postgresql_create(self, label, region, engine, ltype, **kwargs):
:type engine: str or Engine
:param ltype: The Linode Type to use for this cluster
:type ltype: str or Type
:param engine_config: The configuration options for this PostgreSQL cluster
:type engine_config: Dict[str, Any] or PostgreSQLDatabaseConfigOptions
"""

params = {
"label": label,
"region": region,
"engine": engine,
"type": ltype,
"engine_config": engine_config,
}
params.update(kwargs)

Expand Down
151 changes: 150 additions & 1 deletion linode_api4/objects/database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from dataclasses import dataclass, field
from typing import Optional

from deprecated import deprecated

from linode_api4.objects import Base, DerivedBase, MappedObject, Property
from linode_api4.objects import (
Base,
DerivedBase,
JSONObject,
MappedObject,
Property,
)


class DatabaseType(Base):
Expand Down Expand Up @@ -128,6 +137,140 @@ class PostgreSQLDatabaseBackup(DatabaseBackup):
api_endpoint = "/databases/postgresql/instances/{database_id}/backups/{id}"


@dataclass
class MySQLDatabaseConfigMySQLOptions(JSONObject):
"""
MySQLDatabaseConfigMySQLOptions represents the fields in the mysql
field of the MySQLDatabaseConfigOptions class
"""

connect_timeout: Optional[int] = None
default_time_zone: Optional[str] = None
group_concat_max_len: Optional[float] = None
information_schema_stats_expiry: Optional[int] = None
innodb_change_buffer_max_size: Optional[int] = None
innodb_flush_neighbors: Optional[int] = None
innodb_ft_min_token_size: Optional[int] = None
innodb_ft_server_stopword_table: Optional[str] = None
innodb_lock_wait_timeout: Optional[int] = None
innodb_log_buffer_size: Optional[int] = None
innodb_online_alter_log_max_size: Optional[int] = None
innodb_read_io_threads: Optional[int] = None
innodb_rollback_on_timeout: Optional[bool] = None
innodb_thread_concurrency: Optional[int] = None
innodb_write_io_threads: Optional[int] = None
interactive_timeout: Optional[int] = None
internal_tmp_mem_storage_engine: Optional[str] = None
max_allowed_packet: Optional[int] = None
max_heap_table_size: Optional[int] = None
net_buffer_length: Optional[int] = None
net_read_timeout: Optional[int] = None
net_write_timeout: Optional[int] = None
sort_buffer_size: Optional[int] = None
sql_mode: Optional[str] = None
sql_require_primary_key: Optional[bool] = None
tmp_table_size: Optional[int] = None
wait_timeout: Optional[int] = None


@dataclass
class MySQLDatabaseConfigOptions(JSONObject):
"""
MySQLDatabaseConfigOptions is used to specify
a MySQL Database Cluster's configuration options during its creation.
"""

mysql: Optional[MySQLDatabaseConfigMySQLOptions] = None
binlog_retention_period: Optional[int] = None


@dataclass
class PostgreSQLDatabaseConfigPGLookoutOptions(JSONObject):
"""
PostgreSQLDatabasePGLookoutConfigOptions represents the fields in the pglookout
field of the PostgreSQLDatabasePGConfigOptions class
"""

max_failover_replication_time_lag: Optional[int] = None


@dataclass
class PostgreSQLDatabaseConfigPGOptions(JSONObject):
"""
PostgreSQLDatabasePGConfigOptions represents the fields in the pg
field of the PostgreSQLDatabasePGConfigOptions class
"""

autovacuum_analyze_scale_factor: Optional[float] = None
autovacuum_analyze_threshold: Optional[int] = None
autovacuum_max_workers: Optional[int] = None
autovacuum_naptime: Optional[int] = None
autovacuum_vacuum_cost_delay: Optional[int] = None
autovacuum_vacuum_cost_limit: Optional[int] = None
autovacuum_vacuum_scale_factor: Optional[float] = None
autovacuum_vacuum_threshold: Optional[int] = None
bgwriter_delay: Optional[int] = None
bgwriter_flush_after: Optional[int] = None
bgwriter_lru_maxpages: Optional[int] = None
bgwriter_lru_multiplier: Optional[float] = None
deadlock_timeout: Optional[int] = None
default_toast_compression: Optional[str] = None
idle_in_transaction_session_timeout: Optional[int] = None
jit: Optional[bool] = None
max_files_per_process: Optional[int] = None
max_locks_per_transaction: Optional[int] = None
max_logical_replication_workers: Optional[int] = None
max_parallel_workers: Optional[int] = None
max_parallel_workers_per_gather: Optional[int] = None
max_pred_locks_per_transaction: Optional[int] = None
max_replication_slots: Optional[int] = None
max_slot_wal_keep_size: Optional[int] = None
max_stack_depth: Optional[int] = None
max_standby_archive_delay: Optional[int] = None
max_standby_streaming_delay: Optional[int] = None
max_wal_senders: Optional[int] = None
max_worker_processes: Optional[int] = None
password_encryption: Optional[str] = None
pg_partman_bgw_interval: Optional[int] = field(
default=None, metadata={"json_key": "pg_partman_bgw.interval"}
)
pg_partman_bgw_role: Optional[str] = field(
default=None, metadata={"json_key": "pg_partman_bgw.role"}
)
pg_stat_monitor_pgsm_enable_query_plan: Optional[bool] = field(
default=None,
metadata={"json_key": "pg_stat_monitor.pgsm_enable_query_plan"},
)
pg_stat_monitor_pgsm_max_buckets: Optional[int] = field(
default=None, metadata={"json_key": "pg_stat_monitor.pgsm_max_buckets"}
)
pg_stat_statements_track: Optional[str] = field(
default=None, metadata={"json_key": "pg_stat_statements.track"}
)
temp_file_limit: Optional[int] = None
timezone: Optional[str] = None
track_activity_query_size: Optional[int] = None
track_commit_timestamp: Optional[str] = None
track_functions: Optional[str] = None
track_io_timing: Optional[str] = None
wal_sender_timeout: Optional[int] = None
wal_writer_delay: Optional[int] = None


@dataclass
class PostgreSQLDatabaseConfigOptions(JSONObject):
"""
PostgreSQLDatabaseConfigOptions is used to specify
a PostgreSQL Database Cluster's configuration options during its creation.
"""

pg: Optional[PostgreSQLDatabaseConfigPGOptions] = None
pg_stat_monitor_enable: Optional[bool] = None
pglookout: Optional[PostgreSQLDatabaseConfigPGLookoutOptions] = None
shared_buffers_percentage: Optional[float] = None
work_mem: Optional[int] = None


class MySQLDatabase(Base):
"""
An accessible Managed MySQL Database.
Expand Down Expand Up @@ -158,6 +301,9 @@ class MySQLDatabase(Base):
"updated": Property(volatile=True, is_datetime=True),
"updates": Property(mutable=True),
"version": Property(),
"engine_config": Property(
mutable=True, json_object=MySQLDatabaseConfigOptions
),
}

@property
Expand Down Expand Up @@ -321,6 +467,9 @@ class PostgreSQLDatabase(Base):
"updated": Property(volatile=True, is_datetime=True),
"updates": Property(mutable=True),
"version": Property(),
"engine_config": Property(
mutable=True, json_object=PostgreSQLDatabaseConfigOptions
),
}

@property
Expand Down
19 changes: 14 additions & 5 deletions linode_api4/objects/serializable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
from dataclasses import dataclass
from dataclasses import fields as dataclass_fields
from enum import Enum
from types import SimpleNamespace
from typing import (
Expand Down Expand Up @@ -140,7 +141,7 @@ def _parse_attr(cls, json_value: Any, field_type: type):
@classmethod
def from_json(cls, json: Dict[str, Any]) -> Optional["JSONObject"]:
"""
Creates an instance of this class from a JSON dict.
Creates an instance of this class from a JSON dict, respecting json_key metadata.
"""
if json is None:
return None
Expand All @@ -149,8 +150,12 @@ def from_json(cls, json: Dict[str, Any]) -> Optional["JSONObject"]:

type_hints = get_type_hints(cls)

for k in vars(obj):
setattr(obj, k, cls._parse_attr(json.get(k), type_hints.get(k)))
for f in dataclass_fields(cls):
json_key = f.metadata.get("json_key", f.name)
field_type = type_hints.get(f.name)
value = json.get(json_key)
parsed_value = cls._parse_attr(value, field_type)
setattr(obj, f.name, parsed_value)

return obj

Expand Down Expand Up @@ -193,7 +198,11 @@ def should_include(key: str, value: Any) -> bool:

result = {}

for k, v in vars(self).items():
for f in dataclass_fields(self):
k = f.name
json_key = f.metadata.get("json_key", k)
v = getattr(self, k)

if not should_include(k, v):
continue

Expand All @@ -204,7 +213,7 @@ def should_include(key: str, value: Any) -> bool:
else:
v = attempt_serialize(v)

result[k] = v
result[json_key] = v

return result

Expand Down
Loading