Skip to content

Commit c85a73f

Browse files
Added support for DBaaS suspend/resume (#680)
* Added support for DBaaS suspend/resume * Added unit tests * Add comments
1 parent 41a199a commit c85a73f

File tree

8 files changed

+3522
-1562
lines changed

8 files changed

+3522
-1562
lines changed

mysql.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,16 @@ func (c *Client) PatchMySQLDatabase(ctx context.Context, databaseID int) error {
232232
e := formatAPIPath("databases/mysql/instances/%d/patch", databaseID)
233233
return doPOSTRequestNoRequestResponseBody(ctx, c, e)
234234
}
235+
236+
// SuspendMySQLDatabase suspends a MySQL Managed Database, releasing idle resources and keeping only necessary data.
237+
// All service data is lost if there are no backups available.
238+
func (c *Client) SuspendMySQLDatabase(ctx context.Context, databaseID int) error {
239+
e := formatAPIPath("databases/mysql/instances/%d/suspend", databaseID)
240+
return doPOSTRequestNoRequestResponseBody(ctx, c, e)
241+
}
242+
243+
// ResumeMySQLDatabase resumes a suspended MySQL Managed Database
244+
func (c *Client) ResumeMySQLDatabase(ctx context.Context, databaseID int) error {
245+
e := formatAPIPath("databases/mysql/instances/%d/resume", databaseID)
246+
return doPOSTRequestNoRequestResponseBody(ctx, c, e)
247+
}

postgres.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,16 @@ func (c *Client) CreatePostgresDatabaseBackup(ctx context.Context, databaseID in
252252
e := formatAPIPath("databases/postgresql/instances/%d/backups", databaseID)
253253
return doPOSTRequestNoResponseBody(ctx, c, e, opts)
254254
}
255+
256+
// SuspendPostgresDatabase suspends a PostgreSQL Managed Database, releasing idle resources and keeping only necessary data.
257+
// All service data is lost if there are no backups available.
258+
func (c *Client) SuspendPostgresDatabase(ctx context.Context, databaseID int) error {
259+
e := formatAPIPath("databases/postgresql/instances/%d/suspend", databaseID)
260+
return doPOSTRequestNoRequestResponseBody(ctx, c, e)
261+
}
262+
263+
// ResumePostgresDatabase resumes a suspended PostgreSQL Managed Database
264+
func (c *Client) ResumePostgresDatabase(ctx context.Context, databaseID int) error {
265+
e := formatAPIPath("databases/postgresql/instances/%d/resume", databaseID)
266+
return doPOSTRequestNoRequestResponseBody(ctx, c, e)
267+
}

test/integration/fixtures/TestDatabase_MySQL_Suite.yaml

Lines changed: 1698 additions & 776 deletions
Large diffs are not rendered by default.

test/integration/fixtures/TestDatabase_Postgres_Suite.yaml

Lines changed: 1713 additions & 785 deletions
Large diffs are not rendered by default.

test/integration/mysql_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,29 @@ func TestDatabase_MySQL_Suite(t *testing.T) {
126126
if err := client.WaitForDatabaseStatus(
127127
context.Background(), database.ID, linodego.DatabaseEngineTypeMySQL,
128128
linodego.DatabaseStatusActive, 2400); err != nil {
129-
t.Fatalf("failed to wait for database updating: %s", err)
129+
t.Fatalf("failed to wait for database active: %s", err)
130+
}
131+
132+
if err := client.SuspendMySQLDatabase(context.Background(), database.ID); err != nil {
133+
t.Fatalf("failed to suspend database: %s", err)
134+
}
135+
136+
// Wait for the DB to enter suspended status
137+
if err := client.WaitForDatabaseStatus(
138+
context.Background(), database.ID, linodego.DatabaseEngineTypeMySQL,
139+
linodego.DatabaseStatusSuspended, 240); err != nil {
140+
t.Fatalf("failed to wait for database suspended: %s", err)
141+
}
142+
143+
if err := client.ResumeMySQLDatabase(context.Background(), database.ID); err != nil {
144+
t.Fatalf("failed to resume database: %s", err)
145+
}
146+
147+
// Wait for the DB to re-enter active status
148+
if err := client.WaitForDatabaseStatus(
149+
context.Background(), database.ID, linodego.DatabaseEngineTypeMySQL,
150+
linodego.DatabaseStatusActive, 2400); err != nil {
151+
t.Fatalf("failed to wait for database active: %s", err)
130152
}
131153
}
132154

test/integration/postgres_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ func TestDatabase_Postgres_Suite(t *testing.T) {
132132
linodego.DatabaseStatusActive, 2400); err != nil {
133133
t.Fatalf("failed to wait for database updating: %s", err)
134134
}
135+
136+
if err := client.SuspendPostgresDatabase(context.Background(), database.ID); err != nil {
137+
t.Fatalf("failed to suspend database: %s", err)
138+
}
139+
140+
// Wait for the DB to enter suspended status
141+
if err := client.WaitForDatabaseStatus(
142+
context.Background(), database.ID, linodego.DatabaseEngineTypePostgres,
143+
linodego.DatabaseStatusSuspended, 2400); err != nil {
144+
t.Fatalf("failed to wait for database suspended: %s", err)
145+
}
146+
147+
if err := client.ResumePostgresDatabase(context.Background(), database.ID); err != nil {
148+
t.Fatalf("failed to resume database: %s", err)
149+
}
150+
151+
// Wait for the DB to re-enter active status
152+
if err := client.WaitForDatabaseStatus(
153+
context.Background(), database.ID, linodego.DatabaseEngineTypePostgres,
154+
linodego.DatabaseStatusActive, 2400); err != nil {
155+
t.Fatalf("failed to wait for database active: %s", err)
156+
}
135157
}
136158

137159
type postgresDatabaseModifier func(options *linodego.PostgresCreateOptions)

test/unit/mysql_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,23 @@ func TestDatabaseMySQL_Patch(t *testing.T) {
192192
t.Fatal(err)
193193
}
194194
}
195+
196+
func TestDatabaseMySQL_Suspend(t *testing.T) {
197+
client := createMockClient(t)
198+
199+
httpmock.RegisterRegexpResponder("POST", mockRequestURL(t, "databases/mysql/instances/123/suspend"), httpmock.NewStringResponder(200, "{}"))
200+
201+
if err := client.SuspendMySQLDatabase(context.Background(), 123); err != nil {
202+
t.Fatal(err)
203+
}
204+
}
205+
206+
func TestDatabaseMySQL_Resume(t *testing.T) {
207+
client := createMockClient(t)
208+
209+
httpmock.RegisterRegexpResponder("POST", mockRequestURL(t, "databases/mysql/instances/123/resume"), httpmock.NewStringResponder(200, "{}"))
210+
211+
if err := client.ResumeMySQLDatabase(context.Background(), 123); err != nil {
212+
t.Fatal(err)
213+
}
214+
}

test/unit/postgres_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,23 @@ func TestDatabasePostgreSQL_Patch(t *testing.T) {
191191
t.Fatal(err)
192192
}
193193
}
194+
195+
func TestDatabasePostgreSQL_Suspend(t *testing.T) {
196+
client := createMockClient(t)
197+
198+
httpmock.RegisterRegexpResponder("POST", mockRequestURL(t, "databases/postgresql/instances/123/suspend"), httpmock.NewStringResponder(200, "{}"))
199+
200+
if err := client.SuspendPostgresDatabase(context.Background(), 123); err != nil {
201+
t.Fatal(err)
202+
}
203+
}
204+
205+
func TestDatabasePostgreSQL_Resume(t *testing.T) {
206+
client := createMockClient(t)
207+
208+
httpmock.RegisterRegexpResponder("POST", mockRequestURL(t, "databases/postgresql/instances/123/resume"), httpmock.NewStringResponder(200, "{}"))
209+
210+
if err := client.ResumePostgresDatabase(context.Background(), 123); err != nil {
211+
t.Fatal(err)
212+
}
213+
}

0 commit comments

Comments
 (0)