Skip to content

Commit de1fb73

Browse files
[databases]: add support for Kafka advanced configuration (#1587)
* [databases]: add support for Kafka advanced configuration * fix indention in test file * Update commands/displayers/database.go Co-authored-by: Andrew Starr-Bochicchio <[email protected]> * Update commands/displayers/database.go Co-authored-by: Andrew Starr-Bochicchio <[email protected]> --------- Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
1 parent 84a6e48 commit de1fb73

File tree

8 files changed

+312
-6
lines changed

8 files changed

+312
-6
lines changed

commands/databases.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -2443,6 +2443,7 @@ This command functions as a PATCH request, meaning that only the specified field
24432443
displayerType(&displayers.PostgreSQLConfiguration{}),
24442444
displayerType(&displayers.RedisConfiguration{}),
24452445
displayerType(&displayers.MongoDBConfiguration{}),
2446+
displayerType(&displayers.KafkaConfiguration{}),
24462447
)
24472448
AddStringFlag(
24482449
getDatabaseCfgCommand,
@@ -2501,9 +2502,10 @@ func RunDatabaseConfigurationGet(c *CmdConfig) error {
25012502
"pg": nil,
25022503
"redis": nil,
25032504
"mongodb": nil,
2505+
"kafka": nil,
25042506
}
25052507
if _, ok := allowedEngines[engine]; !ok {
2506-
return fmt.Errorf("(%s) command: engine must be one of: 'pg', 'mysql', 'redis', 'mongodb'", c.NS)
2508+
return fmt.Errorf("(%s) command: engine must be one of: 'pg', 'mysql', 'redis', 'mongodb', 'kafka'", c.NS)
25072509
}
25082510

25092511
dbId := args[0]
@@ -2547,6 +2549,16 @@ func RunDatabaseConfigurationGet(c *CmdConfig) error {
25472549
MongoDBConfig: *config,
25482550
}
25492551
return c.Display(&displayer)
2552+
} else if engine == "kafka" {
2553+
config, err := c.Databases().GetKafkaConfiguration(dbId)
2554+
if err != nil {
2555+
return err
2556+
}
2557+
2558+
displayer := displayers.KafkaConfiguration{
2559+
KafkaConfig: *config,
2560+
}
2561+
return c.Display(&displayer)
25502562
}
25512563

25522564
return nil
@@ -2571,9 +2583,10 @@ func RunDatabaseConfigurationUpdate(c *CmdConfig) error {
25712583
"pg": nil,
25722584
"redis": nil,
25732585
"mongodb": nil,
2586+
"kafka": nil,
25742587
}
25752588
if _, ok := allowedEngines[engine]; !ok {
2576-
return fmt.Errorf("(%s) command: engine must be one of: 'pg', 'mysql', 'redis', 'mongodb'", c.NS)
2589+
return fmt.Errorf("(%s) command: engine must be one of: 'pg', 'mysql', 'redis', 'mongodb', 'kafka'", c.NS)
25772590
}
25782591

25792592
configJson, err := c.Doit.GetString(c.NS, doctl.ArgDatabaseConfigJson)
@@ -2602,6 +2615,11 @@ func RunDatabaseConfigurationUpdate(c *CmdConfig) error {
26022615
if err != nil {
26032616
return err
26042617
}
2618+
} else if engine == "kafka" {
2619+
err := c.Databases().UpdateKafkaConfiguration(dbId, configJson)
2620+
if err != nil {
2621+
return err
2622+
}
26052623
}
26062624

26072625
return nil

commands/databases_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ var (
215215
MongoDBConfig: &godo.MongoDBConfig{},
216216
}
217217

218+
testKafkaConfiguration = do.KafkaConfig{
219+
KafkaConfig: &godo.KafkaConfig{},
220+
}
221+
218222
topicReplicationFactor = uint32(3)
219223
testKafkaTopic = do.DatabaseTopic{
220224
DatabaseTopic: &godo.DatabaseTopic{
@@ -1666,6 +1670,16 @@ func TestDatabaseConfigurationGet(t *testing.T) {
16661670
assert.NoError(t, err)
16671671
})
16681672

1673+
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
1674+
tm.databases.EXPECT().GetKafkaConfiguration(testDBCluster.ID).Return(&testKafkaConfiguration, nil)
1675+
config.Args = append(config.Args, testDBCluster.ID)
1676+
config.Doit.Set(config.NS, doctl.ArgDatabaseEngine, "kafka")
1677+
1678+
err := RunDatabaseConfigurationGet(config)
1679+
1680+
assert.NoError(t, err)
1681+
})
1682+
16691683
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
16701684
err := RunDatabaseConfigurationGet(config)
16711685

@@ -1730,6 +1744,16 @@ func TestDatabaseConfigurationUpdate(t *testing.T) {
17301744
assert.NoError(t, err)
17311745
})
17321746

1747+
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
1748+
tm.databases.EXPECT().UpdateKafkaConfiguration(testDBCluster.ID, "").Return(nil)
1749+
config.Args = append(config.Args, testDBCluster.ID)
1750+
config.Doit.Set(config.NS, doctl.ArgDatabaseEngine, "kafka")
1751+
1752+
err := RunDatabaseConfigurationUpdate(config)
1753+
1754+
assert.NoError(t, err)
1755+
})
1756+
17331757
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
17341758
err := RunDatabaseConfigurationUpdate(config)
17351759

commands/displayers/database.go

+133
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,139 @@ func (dc *MongoDBConfiguration) KV() []map[string]any {
17391739
return o
17401740
}
17411741

1742+
type KafkaConfiguration struct {
1743+
KafkaConfig do.KafkaConfig
1744+
}
1745+
1746+
var _ Displayable = &KafkaConfiguration{}
1747+
1748+
func (dc *KafkaConfiguration) JSON(out io.Writer) error {
1749+
return writeJSON(dc.KafkaConfig, out)
1750+
}
1751+
1752+
func (dc *KafkaConfiguration) Cols() []string {
1753+
return []string{
1754+
"key",
1755+
"value",
1756+
}
1757+
}
1758+
1759+
func (dc *KafkaConfiguration) ColMap() map[string]string {
1760+
return map[string]string{
1761+
"key": "key",
1762+
"value": "value",
1763+
}
1764+
}
1765+
1766+
func (dc *KafkaConfiguration) KV() []map[string]any {
1767+
c := dc.KafkaConfig
1768+
o := []map[string]any{}
1769+
if c.GroupInitialRebalanceDelayMs != nil {
1770+
o = append(o, map[string]any{
1771+
"key": "GroupInitialRebalanceDelayMs",
1772+
"value": *c.GroupInitialRebalanceDelayMs,
1773+
})
1774+
}
1775+
if c.GroupMinSessionTimeoutMs != nil {
1776+
o = append(o, map[string]any{
1777+
"key": "GroupMinSessionTimeoutMs",
1778+
"value": *c.GroupMinSessionTimeoutMs,
1779+
})
1780+
}
1781+
if c.GroupMaxSessionTimeoutMs != nil {
1782+
o = append(o, map[string]any{
1783+
"key": "GroupMaxSessionTimeoutMs",
1784+
"value": *c.GroupMaxSessionTimeoutMs,
1785+
})
1786+
}
1787+
if c.MessageMaxBytes != nil {
1788+
o = append(o, map[string]any{
1789+
"key": "MessageMaxBytes",
1790+
"value": *c.MessageMaxBytes,
1791+
})
1792+
}
1793+
if c.LogCleanerDeleteRetentionMs != nil {
1794+
o = append(o, map[string]any{
1795+
"key": "LogCleanerDeleteRetentionMs",
1796+
"value": *c.LogCleanerDeleteRetentionMs,
1797+
})
1798+
}
1799+
if c.LogCleanerMinCompactionLagMs != nil {
1800+
o = append(o, map[string]any{
1801+
"key": "LogCleanerMinCompactionLagMs",
1802+
"value": *c.LogCleanerMinCompactionLagMs,
1803+
})
1804+
}
1805+
if c.LogFlushIntervalMs != nil {
1806+
o = append(o, map[string]any{
1807+
"key": "LogFlushIntervalMs",
1808+
"value": *c.LogFlushIntervalMs,
1809+
})
1810+
}
1811+
if c.LogIndexIntervalBytes != nil {
1812+
o = append(o, map[string]any{
1813+
"key": "LogIndexIntervalBytes",
1814+
"value": *c.LogIndexIntervalBytes,
1815+
})
1816+
}
1817+
if c.LogMessageDownconversionEnable != nil {
1818+
o = append(o, map[string]any{
1819+
"key": "LogMessageDownconversionEnable",
1820+
"value": *c.LogMessageDownconversionEnable,
1821+
})
1822+
}
1823+
if c.LogMessageTimestampDifferenceMaxMs != nil {
1824+
o = append(o, map[string]any{
1825+
"key": "LogMessageTimestampDifferenceMaxMs",
1826+
"value": *c.LogMessageTimestampDifferenceMaxMs,
1827+
})
1828+
}
1829+
if c.LogPreallocate != nil {
1830+
o = append(o, map[string]any{
1831+
"key": "LogPreallocate",
1832+
"value": *c.LogPreallocate,
1833+
})
1834+
}
1835+
if c.LogRetentionBytes != nil {
1836+
o = append(o, map[string]any{
1837+
"key": "LogRetentionBytes",
1838+
"value": c.LogRetentionBytes.String(),
1839+
})
1840+
}
1841+
if c.LogRetentionHours != nil {
1842+
o = append(o, map[string]any{
1843+
"key": "LogRetentionHours",
1844+
"value": *c.LogRetentionHours,
1845+
})
1846+
}
1847+
if c.LogRetentionMs != nil {
1848+
o = append(o, map[string]any{
1849+
"key": "LogRetentionMs",
1850+
"value": c.LogRetentionMs.String(),
1851+
})
1852+
}
1853+
if c.LogRollJitterMs != nil {
1854+
o = append(o, map[string]any{
1855+
"key": "LogRollJitterMs",
1856+
"value": *c.LogRollJitterMs,
1857+
})
1858+
}
1859+
if c.LogSegmentDeleteDelayMs != nil {
1860+
o = append(o, map[string]any{
1861+
"key": "LogSegmentDeleteDelayMs",
1862+
"value": *c.LogSegmentDeleteDelayMs,
1863+
})
1864+
}
1865+
if c.AutoCreateTopicsEnable != nil {
1866+
o = append(o, map[string]any{
1867+
"key": "AutoCreateTopicsEnable",
1868+
"value": *c.AutoCreateTopicsEnable,
1869+
})
1870+
}
1871+
1872+
return o
1873+
}
1874+
17421875
type DatabaseEvents struct {
17431876
DatabaseEvents do.DatabaseEvents
17441877
}

do/databases.go

+33
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ type MongoDBConfig struct {
125125
*godo.MongoDBConfig
126126
}
127127

128+
// KafkaConfig is a wrapper for godo.KafkaConfig
129+
type KafkaConfig struct {
130+
*godo.KafkaConfig
131+
}
132+
128133
// DatabaseTopics is a slice of DatabaseTopic
129134
type DatabaseTopics []DatabaseTopic
130135

@@ -206,11 +211,13 @@ type DatabasesService interface {
206211
GetPostgreSQLConfiguration(databaseID string) (*PostgreSQLConfig, error)
207212
GetRedisConfiguration(databaseID string) (*RedisConfig, error)
208213
GetMongoDBConfiguration(databaseID string) (*MongoDBConfig, error)
214+
GetKafkaConfiguration(databaseID string) (*KafkaConfig, error)
209215

210216
UpdateMySQLConfiguration(databaseID string, confString string) error
211217
UpdatePostgreSQLConfiguration(databaseID string, confString string) error
212218
UpdateRedisConfiguration(databaseID string, confString string) error
213219
UpdateMongoDBConfiguration(databaseID string, confString string) error
220+
UpdateKafkaConfiguration(databaseID string, confString string) error
214221

215222
ListTopics(string) (DatabaseTopics, error)
216223
GetTopic(string, string) (*DatabaseTopic, error)
@@ -713,6 +720,17 @@ func (ds *databasesService) GetMongoDBConfiguration(databaseID string) (*MongoDB
713720
}, nil
714721
}
715722

723+
func (ds *databasesService) GetKafkaConfiguration(databaseID string) (*KafkaConfig, error) {
724+
cfg, _, err := ds.client.Databases.GetKafkaConfig(context.TODO(), databaseID)
725+
if err != nil {
726+
return nil, err
727+
}
728+
729+
return &KafkaConfig{
730+
KafkaConfig: cfg,
731+
}, nil
732+
}
733+
716734
func (ds *databasesService) UpdateMySQLConfiguration(databaseID string, confString string) error {
717735
var conf godo.MySQLConfig
718736
err := json.Unmarshal([]byte(confString), &conf)
@@ -773,6 +791,21 @@ func (ds *databasesService) UpdateMongoDBConfiguration(databaseID string, confSt
773791
return nil
774792
}
775793

794+
func (ds *databasesService) UpdateKafkaConfiguration(databaseID string, confString string) error {
795+
var conf godo.KafkaConfig
796+
err := json.Unmarshal([]byte(confString), &conf)
797+
if err != nil {
798+
return err
799+
}
800+
801+
_, err = ds.client.Databases.UpdateKafkaConfig(context.TODO(), databaseID, &conf)
802+
if err != nil {
803+
return err
804+
}
805+
806+
return nil
807+
}
808+
776809
func (ds *databasesService) ListTopics(databaseID string) (DatabaseTopics, error) {
777810
f := func(opt *godo.ListOptions) ([]any, *godo.Response, error) {
778811
list, resp, err := ds.client.Databases.ListTopics(context.TODO(), databaseID, opt)

do/mocks/DatabasesService.go

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
9191
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9292
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
9393
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
94-
github.com/digitalocean/godo v1.125.0 h1:wGPBQRX9Wjo0qCF0o8d25mT3A84Iw8rfHnZOPyvHcMQ=
95-
github.com/digitalocean/godo v1.125.0/go.mod h1:PU8JB6I1XYkQIdHFop8lLAY9ojp6M0XcU0TWaQSxbrc=
96-
github.com/digitalocean/godo v1.125.1-0.20240925184037-40ea734536f0 h1:hEi5W+TPrYUjq1PLt1lJmhrt+ezpzUrAvwYr9f1Xo4U=
97-
github.com/digitalocean/godo v1.125.1-0.20240925184037-40ea734536f0/go.mod h1:PU8JB6I1XYkQIdHFop8lLAY9ojp6M0XcU0TWaQSxbrc=
9894
github.com/digitalocean/godo v1.126.0 h1:+Znh7VMQj/E8ArbjWnc7OKGjWfzC+I8OCSRp7r1MdD8=
9995
github.com/digitalocean/godo v1.126.0/go.mod h1:PU8JB6I1XYkQIdHFop8lLAY9ojp6M0XcU0TWaQSxbrc=
10096
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=

0 commit comments

Comments
 (0)