Skip to content

Commit

Permalink
Merge branch 'main' into APPS-7962/update-cnb-image
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivaniKumar1 authored Nov 20, 2023
2 parents 9b3cc57 + 91a25b2 commit 74539bd
Show file tree
Hide file tree
Showing 11 changed files with 609 additions and 20 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 74539bd

Please sign in to comment.