Skip to content
Merged
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
48 changes: 48 additions & 0 deletions linode_api4/objects/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,30 @@ def invalidate(self):

Base.invalidate(self)

def suspend(self):
"""
Suspend a MySQL Managed Database, releasing idle resources and keeping only necessary data.

API documentation: https://techdocs.akamai.com/linode-api/reference/suspend-databases-mysql-instance
"""
self._client.post(
"{}/suspend".format(MySQLDatabase.api_endpoint), model=self
)

return self.invalidate()

def resume(self):
"""
Resume a suspended MySQL Managed Database.

API documentation: https://techdocs.akamai.com/linode-api/reference/resume-databases-mysql-instance
"""
self._client.post(
"{}/resume".format(MySQLDatabase.api_endpoint), model=self
)

return self.invalidate()


class PostgreSQLDatabase(Base):
"""
Expand Down Expand Up @@ -405,6 +429,30 @@ def invalidate(self):

Base.invalidate(self)

def suspend(self):
"""
Suspend a PostgreSQL Managed Database, releasing idle resources and keeping only necessary data.

API documentation: https://techdocs.akamai.com/linode-api/reference/suspend-databases-postgre-sql-instance
"""
self._client.post(
"{}/suspend".format(PostgreSQLDatabase.api_endpoint), model=self
)

return self.invalidate()

def resume(self):
"""
Resume a suspended PostgreSQL Managed Database.

API documentation: https://techdocs.akamai.com/linode-api/reference/resume-databases-postgre-sql-instance
"""
self._client.post(
"{}/resume".format(PostgreSQLDatabase.api_endpoint), model=self
)

return self.invalidate()


ENGINE_TYPE_TRANSLATION = {
"mysql": MySQLDatabase,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/databases_mysql_instances_123_resume.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/fixtures/databases_mysql_instances_123_suspend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
68 changes: 68 additions & 0 deletions test/integration/models/database/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,40 @@ def test_database_instance(test_linode_client, test_create_sql_db):
assert str(test_create_sql_db.id) in str(dbs.lists)


@pytest.mark.skipif(
os.getenv("RUN_DB_TESTS", "").strip().lower() not in {"yes", "true"},
reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)",
)
def test_mysql_suspend_resume(test_linode_client, test_create_sql_db):
db = test_linode_client.load(MySQLDatabase, test_create_sql_db.id)

db.suspend()

wait_for_condition(
10,
300,
get_sql_db_status,
test_linode_client,
test_create_sql_db.id,
"suspended",
)
Comment on lines +177 to +184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads up for the future, we'll probably want to replace this with event polling once the update event issue is resolved in the API. I don't have an ETA for that so this should be fine for now 👍


assert db.status == "suspended"

db.resume()

wait_for_condition(
30,
600,
get_sql_db_status,
test_linode_client,
test_create_sql_db.id,
"active",
)

assert db.status == "active"


# ------- POSTGRESQL DB Test cases -------
@pytest.mark.skipif(
os.getenv("RUN_DB_TESTS", "").strip().lower() not in {"yes", "true"},
Expand Down Expand Up @@ -411,3 +445,37 @@ def test_reset_postgres_credentials(

assert db.credentials.username == "akmadmin"
assert db.credentials.password != old_pass


@pytest.mark.skipif(
os.getenv("RUN_DB_TESTS", "").strip().lower() not in {"yes", "true"},
reason="RUN_DB_TESTS environment variable must be set to 'yes' or 'true' (case insensitive)",
)
def test_postgres_suspend_resume(test_linode_client, test_create_postgres_db):
db = test_linode_client.load(PostgreSQLDatabase, test_create_postgres_db.id)

db.suspend()

wait_for_condition(
10,
300,
get_postgres_db_status,
test_linode_client,
test_create_postgres_db.id,
"suspended",
)

assert db.status == "suspended"

db.resume()

wait_for_condition(
30,
600,
get_postgres_db_status,
test_linode_client,
test_create_postgres_db.id,
"active",
)

assert db.status == "active"
56 changes: 56 additions & 0 deletions test/unit/objects/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,34 @@ def test_reset_credentials(self):
m.call_url, "/databases/mysql/instances/123/credentials/reset"
)

def test_suspend(self):
"""
Test MySQL Database suspend logic.
"""
with self.mock_post("/databases/mysql/instances/123/suspend") as m:
db = MySQLDatabase(self.client, 123)

db.suspend()

self.assertEqual(m.method, "post")
self.assertEqual(
m.call_url, "/databases/mysql/instances/123/suspend"
)

def test_resume(self):
"""
Test MySQL Database resume logic.
"""
with self.mock_post("/databases/mysql/instances/123/resume") as m:
db = MySQLDatabase(self.client, 123)

db.resume()

self.assertEqual(m.method, "post")
self.assertEqual(
m.call_url, "/databases/mysql/instances/123/resume"
)


class PostgreSQLDatabaseTest(ClientBaseCase):
"""
Expand Down Expand Up @@ -451,3 +479,31 @@ def test_reset_credentials(self):
m.call_url,
"/databases/postgresql/instances/123/credentials/reset",
)

def test_suspend(self):
"""
Test PostgreSQL Database suspend logic.
"""
with self.mock_post("/databases/postgresql/instances/123/suspend") as m:
db = PostgreSQLDatabase(self.client, 123)

db.suspend()

self.assertEqual(m.method, "post")
self.assertEqual(
m.call_url, "/databases/postgresql/instances/123/suspend"
)

def test_resume(self):
"""
Test PostgreSQL Database resume logic.
"""
with self.mock_post("/databases/postgresql/instances/123/resume") as m:
db = PostgreSQLDatabase(self.client, 123)

db.resume()

self.assertEqual(m.method, "post")
self.assertEqual(
m.call_url, "/databases/postgresql/instances/123/resume"
)