Skip to content

Commit

Permalink
Add update configuration subcommand to databases (#1455)
Browse files Browse the repository at this point in the history
* Add. Configuration subcommand to databases

Signed-off-by: titanventura <[email protected]>

* commands: add update config subcommand to databases (#1430)

Signed-off-by: titanventura <[email protected]>

* fix database command test

Signed-off-by: titanventura <[email protected]>

* Fix formatting of help output.

* Add integration tests.

---------

Signed-off-by: titanventura <[email protected]>
Co-authored-by: danaelhe <[email protected]>
Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2023
1 parent b73c8f7 commit 4977364
Show file tree
Hide file tree
Showing 7 changed files with 592 additions and 8 deletions.
2 changes: 2 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ const (
ArgDatabaseRestoreFromTimestamp = "restore-from-timestamp"
// ArgDatabaseEngine is a flag for specifying which database engine to use
ArgDatabaseEngine = "engine"
// ArgDatabaseConfigJson is a flag for specifying the database configuration in JSON format for an update
ArgDatabaseConfigJson = "config-json"
// ArgDatabaseNumNodes is the number of nodes in the database cluster
ArgDatabaseNumNodes = "num-nodes"
// ArgDatabaseStorageSizeMib is the amount of disk space, in MiB, that should be allocated to the database cluster
Expand Down
88 changes: 81 additions & 7 deletions commands/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -2246,35 +2246,61 @@ func databaseConfiguration() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "configuration",
Aliases: []string{"cfg"},
Aliases: []string{"cfg", "config"},
Short: "View the configuration of a database cluster given its ID and Engine",
Long: "The subcommands of `doctl databases configuration` are used to view a database cluster's configuration.",
},
}
getMySQLConfigurationLongDesc := `
This will get a database cluster's configuration given its ID and Engine
`
getMySQLCfgCommand := CmdBuilder(
getConfigurationLongDesc := "This will get a database cluster's configuration given its ID and Engine"
updateConfigurationLongDesc := "This will update a database cluster's configuration given its ID and Engine and Desired Configuration (as JSON string)"

getDatabaseCfgCommand := CmdBuilder(
cmd,
RunDatabaseConfigurationGet,
"get <db-id>",
"Get a database cluster's configuration",
getMySQLConfigurationLongDesc,
getConfigurationLongDesc,
Writer,
aliasOpt("g"),
displayerType(&displayers.MySQLConfiguration{}),
displayerType(&displayers.PostgreSQLConfiguration{}),
displayerType(&displayers.RedisConfiguration{}),
)
AddStringFlag(
getMySQLCfgCommand,
getDatabaseCfgCommand,
doctl.ArgDatabaseEngine,
"e",
"",
"the engine of the database you want to get the configuration for",
requiredOpt(),
)

updateDatabaseCfgCommand := CmdBuilder(
cmd,
RunDatabaseConfigurationUpdate,
"update <db-id>",
"Update a database cluster's configuration",
updateConfigurationLongDesc,
Writer,
aliasOpt("u"),
)
AddStringFlag(
updateDatabaseCfgCommand,
doctl.ArgDatabaseEngine,
"e",
"",
"the engine of the database you want to update the configuration for",
requiredOpt(),
)
AddStringFlag(
updateDatabaseCfgCommand,
doctl.ArgDatabaseConfigJson,
"",
"{}",
"the desired configuration of the database cluster you want to update",
requiredOpt(),
)

return cmd
}

Expand Down Expand Up @@ -2335,3 +2361,51 @@ func RunDatabaseConfigurationGet(c *CmdConfig) error {
}
return nil
}

func RunDatabaseConfigurationUpdate(c *CmdConfig) error {
args := c.Args
if len(args) == 0 {
return doctl.NewMissingArgsErr(c.NS)
}
if len(args) > 1 {
return doctl.NewTooManyArgsErr(c.NS)
}

engine, err := c.Doit.GetString(c.NS, doctl.ArgDatabaseEngine)
if err != nil {
return doctl.NewMissingArgsErr(c.NS)
}

allowedEngines := map[string]any{
"mysql": nil,
"pg": nil,
"redis": nil,
}
if _, ok := allowedEngines[engine]; !ok {
return fmt.Errorf("(%s) command: engine must be one of: 'pg', 'mysql', 'redis'", c.NS)
}

configJson, err := c.Doit.GetString(c.NS, doctl.ArgDatabaseConfigJson)
if err != nil {
return doctl.NewMissingArgsErr(c.NS)
}

dbId := args[0]
if engine == "mysql" {
err := c.Databases().UpdateMySQLConfiguration(dbId, configJson)
if err != nil {
return err
}
} else if engine == "pg" {
err := c.Databases().UpdatePostgreSQLConfiguration(dbId, configJson)
if err != nil {
return err
}
} else if engine == "redis" {
err := c.Databases().UpdateRedisConfiguration(dbId, configJson)
if err != nil {
return err
}
}
return nil
}
2 changes: 1 addition & 1 deletion commands/databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func TestDatabaseOptionsCommand(t *testing.T) {
func TestDatabaseConfigurationCommand(t *testing.T) {
cmd := databaseConfiguration()
assert.NotNil(t, cmd)
assertCommandNames(t, cmd, "get")
assertCommandNames(t, cmd, "get", "update")
}

func TestDatabaseKafkaTopicCommand(t *testing.T) {
Expand Down
50 changes: 50 additions & 0 deletions do/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package do

import (
"context"
"encoding/json"
"strings"

"github.com/digitalocean/godo"
Expand Down Expand Up @@ -173,6 +174,10 @@ type DatabasesService interface {
GetPostgreSQLConfiguration(databaseID string) (*PostgreSQLConfig, error)
GetRedisConfiguration(databaseID string) (*RedisConfig, error)

UpdateMySQLConfiguration(databaseID string, confString string) error
UpdatePostgreSQLConfiguration(databaseID string, confString string) error
UpdateRedisConfiguration(databaseID string, confString string) error

ListTopics(string) (DatabaseTopics, error)
GetTopic(string, string) (*DatabaseTopic, error)
CreateTopic(string, *godo.DatabaseCreateTopicRequest) (*DatabaseTopic, error)
Expand Down Expand Up @@ -638,6 +643,51 @@ func (ds *databasesService) GetRedisConfiguration(databaseID string) (*RedisConf
}, nil
}

func (ds *databasesService) UpdateMySQLConfiguration(databaseID string, confString string) error {
var conf godo.MySQLConfig
err := json.Unmarshal([]byte(confString), &conf)
if err != nil {
return err
}

_, err = ds.client.Databases.UpdateMySQLConfig(context.TODO(), databaseID, &conf)
if err != nil {
return err
}

return nil
}

func (ds *databasesService) UpdatePostgreSQLConfiguration(databaseID string, confString string) error {
var conf godo.PostgreSQLConfig
err := json.Unmarshal([]byte(confString), &conf)
if err != nil {
return err
}

_, err = ds.client.Databases.UpdatePostgreSQLConfig(context.TODO(), databaseID, &conf)
if err != nil {
return err
}

return nil
}

func (ds *databasesService) UpdateRedisConfiguration(databaseID string, confString string) error {
var conf godo.RedisConfig
err := json.Unmarshal([]byte(confString), &conf)
if err != nil {
return err
}

_, err = ds.client.Databases.UpdateRedisConfig(context.TODO(), databaseID, &conf)
if err != nil {
return err
}

return nil
}

func (ds *databasesService) ListTopics(databaseID string) (DatabaseTopics, error) {
f := func(opt *godo.ListOptions) ([]interface{}, *godo.Response, error) {
list, resp, err := ds.client.Databases.ListTopics(context.TODO(), databaseID, opt)
Expand Down
42 changes: 42 additions & 0 deletions do/mocks/DatabasesService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4977364

Please sign in to comment.