Skip to content

Commit

Permalink
ISSUE: 1585
Browse files Browse the repository at this point in the history
* added bool flag into resize command
* added wait functionality into resize
* adjusted test
  • Loading branch information
Yordaniss committed Oct 8, 2024
1 parent de1fb73 commit 8168a79
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
50 changes: 48 additions & 2 deletions commands/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 an app 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 <database-cluster-id>", "Migrate a database cluster to a new region", `Migrates the specified database cluster to a new region.`, Writer,
aliasOpt("m"))
Expand Down Expand Up @@ -511,13 +513,57 @@ 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
}

// Retrieve the database object after resize (to check status or further actions)
db, err := dbs.Get(id)
if err != nil {
return fmt.Errorf("failed to retrieve the database after resize: %v", err)
}

// Check if the --wait flag was passed
wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait)
if err != nil {
return err
}

if wait {
connection := db.Connection

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,
)
}

db, err = dbs.Get(id)
if err != nil {
return fmt.Errorf(
"failed to retrieve the resized database: %v",
err,
)
}
db.Connection = connection
}

notice("Database resized successfully")

return displayDatabases(c, false, *db)
}

func buildDatabaseResizeRequestFromArgs(c *CmdConfig) (*godo.DatabaseResizeRequest, error) {
Expand Down
3 changes: 3 additions & 0 deletions commands/databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var (
PrivateNetworkUUID: "1fe49b6c-ac8e-11e9-98cb-3bec94f411bc",
Tags: []string{"testing"},
StorageSizeMib: 20480,
Wait: false,
},
}

Expand Down Expand Up @@ -625,6 +626,7 @@ func TestDatabaseResize(t *testing.T) {
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, testDBCluster.Wait)

err := RunDatabaseResize(config)
assert.NoError(t, err)
Expand All @@ -637,6 +639,7 @@ func TestDatabaseResize(t *testing.T) {
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, testDBCluster.Wait)

err := RunDatabaseResize(config)
assert.EqualError(t, err, errTest.Error())
Expand Down

0 comments on commit 8168a79

Please sign in to comment.