Skip to content

Support for dbaas v2 #479

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

Merged
merged 11 commits into from
Feb 4, 2025
134 changes: 125 additions & 9 deletions linode_api4/groups/database.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from linode_api4.errors import UnexpectedResponseError
from linode_api4.groups import Group
from linode_api4.objects import (
Base,
Database,
DatabaseEngine,
DatabaseType,
MySQLDatabase,
PostgreSQLDatabase,
drop_null_keys,
)
from linode_api4.objects.base import _flatten_request_body_recursive


class DatabaseGroup(Group):
Expand Down Expand Up @@ -126,13 +127,71 @@ def mysql_create(self, label, region, engine, ltype, **kwargs):

params = {
"label": label,
"region": region.id if issubclass(type(region), Base) else region,
"engine": engine.id if issubclass(type(engine), Base) else engine,
"type": ltype.id if issubclass(type(ltype), Base) else ltype,
"region": region,
"engine": engine,
"type": ltype,
}
params.update(kwargs)

result = self.client.post("/databases/mysql/instances", data=params)
result = self.client.post(
"/databases/mysql/instances",
data=_flatten_request_body_recursive(drop_null_keys(params)),
)

if "id" not in result:
raise UnexpectedResponseError(
"Unexpected response when creating MySQL Database", json=result
)

d = MySQLDatabase(self.client, result["id"], result)
return d

def mysql_fork(self, source, restore_time, **kwargs):
"""
Forks an :any:`MySQLDatabase` on this account with
the given restore_time. label, region, engine, and ltype are optional.
For example::

client = LinodeClient(TOKEN)

db_to_fork = client.database.mysql_instances()[0]

new_fork = client.database.mysql_fork(
db_to_fork.id,
db_to_fork.updated,
label="new-fresh-label"
)

API Documentation: https://techdocs.akamai.com/linode-api/reference/post-databases-mysql-instances

:param source: The id of the source database
:type source: int
:param restore_time: The timestamp for the fork
:type source: datetime
:param label: The name for this cluster
:type label: str
:param region: The region to deploy this cluster in
:type region: str or Region
:param engine: The engine to deploy this cluster with
:type engine: str or Engine
:param ltype: The Linode Type to use for this cluster
:type ltype: str or Type
"""

params = {
"fork": {
"source": source,
"restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S"),
}
}
if "ltype" in kwargs:
params["type"] = kwargs["ltype"]
params.update(kwargs)

result = self.client.post(
"/databases/mysql/instances",
data=_flatten_request_body_recursive(drop_null_keys(params)),
)

if "id" not in result:
raise UnexpectedResponseError(
Expand Down Expand Up @@ -191,14 +250,71 @@ def postgresql_create(self, label, region, engine, ltype, **kwargs):

params = {
"label": label,
"region": region.id if issubclass(type(region), Base) else region,
"engine": engine.id if issubclass(type(engine), Base) else engine,
"type": ltype.id if issubclass(type(ltype), Base) else ltype,
"region": region,
"engine": engine,
"type": ltype,
}
params.update(kwargs)

result = self.client.post(
"/databases/postgresql/instances",
data=_flatten_request_body_recursive(drop_null_keys(params)),
)

if "id" not in result:
raise UnexpectedResponseError(
"Unexpected response when creating PostgreSQL Database",
json=result,
)

d = PostgreSQLDatabase(self.client, result["id"], result)
return d

def postgresql_fork(self, source, restore_time, **kwargs):
"""
Forks an :any:`PostgreSQLDatabase` on this account with
the given restore_time. label, region, engine, and ltype are optional.
For example::

client = LinodeClient(TOKEN)

db_to_fork = client.database.postgresql_instances()[0]

new_fork = client.database.postgresql_fork(
db_to_fork.id,
db_to_fork.updated,
label="new-fresh-label"
)

API Documentation: https://techdocs.akamai.com/linode-api/reference/post-databases-postgresql-instances

:param source: The id of the source database
:type source: int
:param restore_time: The timestamp for the fork
:type source: datetime
:param label: The name for this cluster
:type label: str
:param region: The region to deploy this cluster in
:type region: str or Region
:param engine: The engine to deploy this cluster with
:type engine: str or Engine
:param ltype: The Linode Type to use for this cluster
:type ltype: str or Type
"""

params = {
"fork": {
"source": source,
"restore_time": restore_time.strftime("%Y-%m-%dT%H:%M:%S"),
}
}
if "ltype" in kwargs:
params["type"] = kwargs["ltype"]
params.update(kwargs)

result = self.client.post(
"/databases/postgresql/instances", data=params
"/databases/postgresql/instances",
data=_flatten_request_body_recursive(drop_null_keys(params)),
)

if "id" not in result:
Expand Down
1 change: 1 addition & 0 deletions linode_api4/linode_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def _api_call(
body = None
if data is not None:
body = json.dumps(data)
print(body)

response = method(
url,
Expand Down
13 changes: 9 additions & 4 deletions linode_api4/objects/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class MySQLDatabase(Base):
"label": Property(mutable=True),
"allow_list": Property(mutable=True, unordered=True),
"backups": Property(derived_class=MySQLDatabaseBackup),
"cluster_size": Property(),
"cluster_size": Property(mutable=True),
"created": Property(is_datetime=True),
"encrypted": Property(),
"engine": Property(),
Expand All @@ -141,7 +141,9 @@ class MySQLDatabase(Base):
"replication_type": Property(),
"ssl_connection": Property(),
"status": Property(volatile=True),
"type": Property(),
"type": Property(mutable=True),
"fork": Property(),
"oldest_restore_time": Property(is_datetime=True),
"updated": Property(volatile=True, is_datetime=True),
"updates": Property(mutable=True),
"version": Property(),
Expand Down Expand Up @@ -264,7 +266,7 @@ class PostgreSQLDatabase(Base):
"label": Property(mutable=True),
"allow_list": Property(mutable=True, unordered=True),
"backups": Property(derived_class=PostgreSQLDatabaseBackup),
"cluster_size": Property(),
"cluster_size": Property(mutable=True),
"created": Property(is_datetime=True),
"encrypted": Property(),
"engine": Property(),
Expand All @@ -275,7 +277,9 @@ class PostgreSQLDatabase(Base):
"replication_type": Property(),
"ssl_connection": Property(),
"status": Property(volatile=True),
"type": Property(),
"type": Property(mutable=True),
"fork": Property(),
"oldest_restore_time": Property(is_datetime=True),
"updated": Property(volatile=True, is_datetime=True),
"updates": Property(mutable=True),
"version": Property(),
Expand Down Expand Up @@ -414,6 +418,7 @@ class Database(Base):
"region": Property(),
"status": Property(),
"type": Property(),
"fork": Property(),
"updated": Property(),
"updates": Property(),
"version": Property(),
Expand Down
Loading
Loading