Skip to content

Commit

Permalink
commands: add update config subcommand to databases (digitalocean#1430)
Browse files Browse the repository at this point in the history
Signed-off-by: titanventura <[email protected]>
  • Loading branch information
titanventura committed Oct 28, 2023
1 parent 3f0c3de commit 1915f37
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 4 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"
// ArgDatabaseMaintenanceDay is the new day for the maintenance window
Expand Down
86 changes: 82 additions & 4 deletions commands/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -1881,30 +1881,60 @@ func databaseConfiguration() *Command {
Long: "The subcommands of `doctl databases configuration` are used to view a database cluster's configuration.",
},
}
getMySQLConfigurationLongDesc := `
getConfigurationLongDesc := `
This will get a database cluster's configuration given its ID and Engine
`
getMySQLCfgCommand := CmdBuilder(
upadateConfigurationLongDesc := `
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",
upadateConfigurationLongDesc,
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 @@ -1965,3 +1995,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
}
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 @@ -159,6 +160,10 @@ type DatabasesService interface {
GetMySQLConfiguration(databaseID string) (*MySQLConfig, error)
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
}

type databasesService struct {
Expand Down Expand Up @@ -618,3 +623,48 @@ func (ds *databasesService) GetRedisConfiguration(databaseID string) (*RedisConf
RedisConfig: cfg,
}, 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
}
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.

0 comments on commit 1915f37

Please sign in to comment.