From 0558237c9927625c2a00121d49b7ae0bdae838dd Mon Sep 17 00:00:00 2001 From: Dmytro Werner <68282006+Yordaniss@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:12:45 +0200 Subject: [PATCH] ISSUE: 1585 | Add --wait flag to databases resize command (#1590) * ISSUE: 1585 * added bool flag into resize command * added wait functionality into resize * adjusted test * ISSUES:1585 + clean up with fmt * ISSUES:1585 + clean up make go fmt * ISSUES:1585 + removed test option to make it similar to create command * ISSUES:1585 + added new test case with wait flag + simplify logic in function * Small copy adjustment. --------- Co-authored-by: Anna Lushnikova Co-authored-by: Andrew Starr-Bochicchio --- commands/databases.go | 33 +++++++++++++++++++++++++++++++-- commands/databases_test.go | 14 ++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/commands/databases.go b/commands/databases.go index e0970054d..be8061a69 100644 --- a/commands/databases.go +++ b/commands/databases.go @@ -135,7 +135,9 @@ For PostgreSQL and MySQL clusters, you can also provide a disk size in MiB to sc AddIntFlag(cmdDatabaseResize, doctl.ArgDatabaseNumNodes, "", 0, nodeNumberDetails, requiredOpt()) AddStringFlag(cmdDatabaseResize, doctl.ArgSizeSlug, "", "", nodeSizeDetails, requiredOpt()) AddIntFlag(cmdDatabaseResize, doctl.ArgDatabaseStorageSizeMib, "", 0, storageSizeMiBDetails) - cmdDatabaseResize.Example = `The following example resizes a PostgreSQL or MySQL database to have two nodes, 16 vCPUs, 64 GB of memory, and 2048 GiB of storage space: doctl databases resize ca9f591d-9999-5555-a0ef-1c02d1d1e352 --num-nodes 2 --size db-s-16vcpu-64gb --storage-size-mib 2048000` + AddBoolFlag(cmdDatabaseResize, doctl.ArgCommandWait, "", false, + "Boolean that specifies whether to wait for the resize to complete before returning control to the terminal") + cmdDatabaseResize.Example = `The following example resizes a PostgreSQL or MySQL database to have two nodes, 16 vCPUs, 64 GB of memory, and 2048 GiB of storage space: doctl databases resize ca9f591d-9999-5555-a0ef-1c02d1d1e352 --num-nodes 2 --size db-s-16vcpu-64gb --storage-size-mib 2048000 --wait true` cmdDatabaseMigrate := CmdBuilder(cmd, RunDatabaseMigrate, "migrate ", "Migrate a database cluster to a new region", `Migrates the specified database cluster to a new region.`, Writer, aliasOpt("m")) @@ -511,13 +513,40 @@ func RunDatabaseResize(c *CmdConfig) error { } id := c.Args[0] + dbs := c.Databases() r, err := buildDatabaseResizeRequestFromArgs(c) if err != nil { return err } - return c.Databases().Resize(id, r) + // Resize the database + err = dbs.Resize(id, r) + if err != nil { + return err + } + + // Check if the --wait flag was passed + wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait) + if err != nil { + return err + } + + if wait { + notice("Database resizing is in progress, waiting for database to be online") + + err := waitForDatabaseReady(dbs, id) + if err != nil { + return fmt.Errorf( + "database couldn't enter the `online` state after resizing: %v", + err, + ) + } + + notice("Database resized successfully") + } + + return nil } func buildDatabaseResizeRequestFromArgs(c *CmdConfig) (*godo.DatabaseResizeRequest, error) { diff --git a/commands/databases_test.go b/commands/databases_test.go index e9cb1c50c..854cdef04 100644 --- a/commands/databases_test.go +++ b/commands/databases_test.go @@ -634,6 +634,20 @@ func TestDatabaseResize(t *testing.T) { assert.NoError(t, err) }) + // Success with wait flag + withTestClient(t, func(config *CmdConfig, tm *tcMocks) { + tm.databases.EXPECT().Resize(testDBCluster.ID, r).Return(nil) + tm.databases.EXPECT().Get(testDBCluster.ID).Return(&testDBCluster, nil) + config.Args = append(config.Args, testDBCluster.ID) + config.Doit.Set(config.NS, doctl.ArgSizeSlug, testDBCluster.SizeSlug) + config.Doit.Set(config.NS, doctl.ArgDatabaseNumNodes, testDBCluster.NumNodes) + config.Doit.Set(config.NS, doctl.ArgDatabaseStorageSizeMib, testDBCluster.StorageSizeMib) + config.Doit.Set(config.NS, doctl.ArgCommandWait, true) + + err := RunDatabaseResize(config) + assert.NoError(t, err) + }) + // Error withTestClient(t, func(config *CmdConfig, tm *tcMocks) { tm.databases.EXPECT().Resize(testDBCluster.ID, r).Return(errTest)