Skip to content

Commit

Permalink
Add. Configuration subcommand to databases
Browse files Browse the repository at this point in the history
Signed-off-by: titanventura <[email protected]>
  • Loading branch information
titanventura committed Oct 7, 2023
1 parent d082b6c commit 0a8bdd9
Show file tree
Hide file tree
Showing 40 changed files with 1,400 additions and 319 deletions.
99 changes: 95 additions & 4 deletions commands/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Database nodes cannot be resized to smaller sizes due to the risk of data loss.`
cmd.AddCommand(sqlMode())
cmd.AddCommand(databaseFirewalls())
cmd.AddCommand(databaseOptions())
cmd.AddCommand(databaseConfiguration())

return cmd
}
Expand Down Expand Up @@ -1630,7 +1631,6 @@ This would remove the firewall rule of uuid 12345d-1234-123d-123x-123eee456e for
AddStringFlag(cmdDatabaseFirewallRemove, doctl.ArgDatabaseFirewallRuleUUID, "", "", "", requiredOpt())

return cmd

}

// displayDatabaseFirewallRules calls Get Firewall Rules to list all current rules.
Expand Down Expand Up @@ -1689,7 +1689,6 @@ func RunDatabaseFirewallRulesUpdate(c *CmdConfig) error {
}

return displayDatabaseFirewallRules(c, true, id)

}

// buildDatabaseUpdateFirewallRulesRequestFromArgs will ingest the --rules arguments into a DatabaseUpdateFirewallRulesRequest object.
Expand All @@ -1712,7 +1711,6 @@ func buildDatabaseUpdateFirewallRulesRequestFromArgs(c *CmdConfig) (*godo.Databa
r.Rules = firewallRulesList

return r, nil

}

// extractFirewallRules will ingest the --rules arguments into a list of DatabaseFirewallRule objects.
Expand All @@ -1731,7 +1729,6 @@ func extractFirewallRules(rulesStringList []string) (rules []*godo.DatabaseFirew
}

return rules, nil

}

// RunDatabaseFirewallRulesAppend creates a firewall rule for a database cluster.
Expand Down Expand Up @@ -1874,3 +1871,97 @@ func waitForDatabaseReady(dbs do.DatabasesService, dbID string) error {
dbID,
)
}

func databaseConfiguration() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "configuration",
Aliases: []string{"cfg"},
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(
cmd,
RunDatabaseConfigurationGet,
"get <db-id>",
"Get a database cluster's configuration",
getMySQLConfigurationLongDesc,
Writer,
aliasOpt("g"),
displayerType(&displayers.MySQLConfiguration{}),
displayerType(&displayers.PostgreSQLConfiguration{}),
displayerType(&displayers.RedisConfiguration{}),
)
AddStringFlag(
getMySQLCfgCommand,
doctl.ArgDatabaseEngine,
"e",
"",
"the engine of the database you want to get the configuration for",
requiredOpt(),
)

return cmd
}

func RunDatabaseConfigurationGet(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)
}

dbId := args[0]
if engine == "mysql" {
config, err := c.Databases().GetMySQLConfiguration(dbId)
if err != nil {
return err
}

displayer := displayers.MySQLConfiguration{
MySQLConfiguration: *config,
}
return c.Display(&displayer)
} else if engine == "pg" {
config, err := c.Databases().GetPostgreSQLConfiguration(dbId)
if err != nil {
return err
}

displayer := displayers.PostgreSQLConfiguration{
PostgreSQLConfig: *config,
}
return c.Display(&displayer)
} else if engine == "redis" {
config, err := c.Databases().GetRedisConfiguration(dbId)
if err != nil {
return err
}

displayer := displayers.RedisConfiguration{
RedisConfig: *config,
}
return c.Display(&displayer)
}
return nil
}
73 changes: 73 additions & 0 deletions commands/databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ var (
DatabaseOptions: &godo.DatabaseOptions{},
}

testMySQLConfiguration = do.MySQLConfig{
MySQLConfig: &godo.MySQLConfig{},
}

testPostgreSQLConfiguration = do.PostgreSQLConfig{
PostgreSQLConfig: &godo.PostgreSQLConfig{},
}

testRedisConfiguration = do.RedisConfig{
RedisConfig: &godo.RedisConfig{},
}

errTest = errors.New("error")
)

Expand All @@ -201,6 +213,7 @@ func TestDatabasesCommand(t *testing.T) {
"pool",
"db",
"sql-mode",
"configuration",
)
}

Expand Down Expand Up @@ -271,6 +284,12 @@ func TestDatabaseOptionsCommand(t *testing.T) {
)
}

func TestDatabaseConfigurationCommand(t *testing.T) {
cmd := databaseConfiguration()
assert.NotNil(t, cmd)
assertCommandNames(t, cmd, "get")
}

func TestDatabasesGet(t *testing.T) {
// Successful call
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
Expand Down Expand Up @@ -1236,3 +1255,57 @@ func TestConvertUTCtoISO8601(t *testing.T) {

assert.Equal(t, "2023-02-01T17:32:15Z", isoTime)
}

func TestDatabaseConfigurationGet(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.databases.EXPECT().GetMySQLConfiguration(testDBCluster.ID).Return(&testMySQLConfiguration, nil)
config.Args = append(config.Args, testDBCluster.ID)
config.Doit.Set(config.NS, doctl.ArgDatabaseEngine, "mysql")

err := RunDatabaseConfigurationGet(config)

assert.NoError(t, err)
})

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.databases.EXPECT().GetPostgreSQLConfiguration(testDBCluster.ID).Return(&testPostgreSQLConfiguration, nil)
config.Args = append(config.Args, testDBCluster.ID)
config.Doit.Set(config.NS, doctl.ArgDatabaseEngine, "pg")

err := RunDatabaseConfigurationGet(config)

assert.NoError(t, err)
})

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.databases.EXPECT().GetRedisConfiguration(testDBCluster.ID).Return(&testRedisConfiguration, nil)
config.Args = append(config.Args, testDBCluster.ID)
config.Doit.Set(config.NS, doctl.ArgDatabaseEngine, "redis")

err := RunDatabaseConfigurationGet(config)

assert.NoError(t, err)
})

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
err := RunDatabaseConfigurationGet(config)

assert.Equal(t, err, doctl.NewMissingArgsErr(config.NS))
})

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
config.Args = append(config.Args, testDBCluster.ID, "extra arg")

err := RunDatabaseConfigurationGet(config)

assert.Equal(t, err, doctl.NewTooManyArgsErr(config.NS))
})

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
config.Args = append(config.Args, testDBCluster.ID)

err := RunDatabaseConfigurationGet(config)

assert.Error(t, err)
})
}
Loading

0 comments on commit 0a8bdd9

Please sign in to comment.