From 2c2e059f2ea4b6f69c045d4fe70359c3db78ba6b Mon Sep 17 00:00:00 2001 From: Anna Lushnikova Date: Tue, 15 Oct 2024 14:27:33 -0400 Subject: [PATCH] use DatabaseLogsinkConfig instead of interface{} --- databases.go | 54 +++++++----------- databases_test.go | 140 +++++++++++++++------------------------------- 2 files changed, 65 insertions(+), 129 deletions(-) diff --git a/databases.go b/databases.go index ec9fb5b..bc38c25 100644 --- a/databases.go +++ b/databases.go @@ -501,53 +501,39 @@ type DatabaseFirewallRule struct { // DatabaseLogsink represents a logsink. type DatabaseLogsink struct { - ID string `json:"sink_id"` - Name string `json:"sink_name,required"` - Type string `json:"sink_type,required"` - Config *interface{} `json:"config,required"` + ID string `json:"sink_id"` + Name string `json:"sink_name,required"` + Type string `json:"sink_type,required"` + Config *DatabaseLogsinkConfig `json:"config,required"` } // DatabaseCreateLogsinkRequest is used to create logsink for a database cluster. type DatabaseCreateLogsinkRequest struct { - Name string `json:"sink_name"` - Type string `json:"sink_type"` - Config *interface{} `json:"config"` + Name string `json:"sink_name"` + Type string `json:"sink_type"` + Config *DatabaseLogsinkConfig `json:"config"` } // DatabaseUpdateLogsinkRequest is used to update logsink for a database cluster. type DatabaseUpdateLogsinkRequest struct { - Config *interface{} `json:"config"` -} - -// RsyslogLogsinkConfig represents rsyslog logsink configuration. -type RsyslogLogsinkConfig struct { - Server string `json:"server,required"` - Port int `json:"port,required"` - TLS bool `json:"tls,required"` - Format string `json:"format,required"` - Logline string `json:"logline,omitempty"` - SD string `json:"sd,omitempty"` - CA string `json:"ca,omitempty"` - Key string `json:"key,omitempty"` - Cert string `json:"cert,omitempty"` -} - -// ElasticsearchLogsinkConfig represents elasticsearch logsink configuration. -type ElasticsearchLogsinkConfig struct { - URL string `json:"url,required"` - IndexPrefix string `json:"index_prefix,required"` - IndexDaysMax int `json:"index_days_max,omitempty"` - Timeout float32 `json:"timeout,omitempty"` - CA string `json:"ca,omitempty"` + Config *DatabaseLogsinkConfig `json:"config"` } -// OpensearchLogsinkConfig represents opensearch logsink configuration. -type OpensearchLogsinkConfig struct { - URL string `json:"url,required"` - IndexPrefix string `json:"index_prefix,required"` +// DatabaseLogsinkConfig represents one of the configurable options (rsyslog_logsink, elasticsearch_logsink, or opensearch_logsink) for a logsink. +type DatabaseLogsinkConfig struct { + URL string `json:"url,omitempty"` + IndexPrefix string `json:"index_prefix,omitempty"` IndexDaysMax int `json:"index_days_max,omitempty"` Timeout float32 `json:"timeout,omitempty"` + Server string `json:"server,omitempty"` + Port int `json:"port,omitempty"` + TLS bool `json:"tls,omitempty"` + Format string `json:"format,omitempty"` + Logline string `json:"logline,omitempty"` + SD string `json:"sd,omitempty"` CA string `json:"ca,omitempty"` + Key string `json:"key,omitempty"` + Cert string `json:"cert,omitempty"` } // PostgreSQLConfig holds advanced configurations for PostgreSQL database clusters. diff --git a/databases_test.go b/databases_test.go index 7806b11..c31243b 100644 --- a/databases_test.go +++ b/databases_test.go @@ -5,7 +5,6 @@ import ( "fmt" "math/big" "net/http" - "reflect" "testing" "time" @@ -3842,17 +3841,15 @@ func TestDatabases_CreateLogsink(t *testing.T) { var ( dbID = "deadbeef-dead-4aa5-beef-deadbeef347d" ) - var i interface{} - i = OpensearchLogsinkConfig{ - URL: "https://user:passwd@192.168.0.1:25060", - IndexPrefix: "opensearch-logs", - } want := &DatabaseLogsink{ - ID: "deadbeef-dead-4aa5-beef-deadbeef347d", - Name: "logs-sink", - Type: "opensearch", - Config: &i, + ID: "deadbeef-dead-4aa5-beef-deadbeef347d", + Name: "logs-sink", + Type: "opensearch", + Config: &DatabaseLogsinkConfig{ + URL: "https://user:passwd@192.168.0.1:25060", + IndexPrefix: "opensearch-logs", + }, } body := `{ @@ -3875,26 +3872,16 @@ func TestDatabases_CreateLogsink(t *testing.T) { }) logsink, _, err := client.Databases.CreateLogsink(ctx, dbID, &DatabaseCreateLogsinkRequest{ - Name: "logs-sink", - Type: "opensearch", - Config: &i, + Name: "logs-sink", + Type: "opensearch", + Config: &DatabaseLogsinkConfig{ + URL: "https://user:passwd@192.168.0.1:25060", + IndexPrefix: "opensearch-logs", + }, }) require.NoError(t, err) - - var gotCfg interface{} - - if configMap, ok := (*logsink.Config).(map[string]interface{}); ok { - gotCfg = OpensearchLogsinkConfig{ - URL: configMap["url"].(string), - IndexPrefix: configMap["index_prefix"].(string), - } - } - logsink.Config = &gotCfg - - if !reflect.DeepEqual(*want, *logsink) { - t.Errorf("expected %v, got %v", *want, *logsink) - } + require.Equal(t, want, logsink) } func TestDatabases_GetLogsink(t *testing.T) { @@ -3905,17 +3892,15 @@ func TestDatabases_GetLogsink(t *testing.T) { dbID = "deadbeef-dead-4aa5-beef-deadbeef347d" logsinkID = "50484ec3-19d6-4cd3-b56f-3b0381c289a6" ) - var i interface{} - i = OpensearchLogsinkConfig{ - URL: "https://user:passwd@192.168.0.1:25060", - IndexPrefix: "opensearch-logs", - } want := &DatabaseLogsink{ - ID: "deadbeef-dead-4aa5-beef-deadbeef347d", - Name: "logs-sink", - Type: "opensearch", - Config: &i, + ID: "deadbeef-dead-4aa5-beef-deadbeef347d", + Name: "logs-sink", + Type: "opensearch", + Config: &DatabaseLogsinkConfig{ + URL: "https://user:passwd@192.168.0.1:25060", + IndexPrefix: "opensearch-logs", + }, } body := `{ @@ -3935,22 +3920,9 @@ func TestDatabases_GetLogsink(t *testing.T) { fmt.Fprint(w, body) }) - got, _, err := client.Databases.GetLogsink(ctx, dbID, logsinkID) + logsink, _, err := client.Databases.GetLogsink(ctx, dbID, logsinkID) require.NoError(t, err) - - var gotCfg interface{} - - if configMap, ok := (*got.Config).(map[string]interface{}); ok { - gotCfg = OpensearchLogsinkConfig{ - URL: configMap["url"].(string), - IndexPrefix: configMap["index_prefix"].(string), - } - } - got.Config = &gotCfg - - if !reflect.DeepEqual(*want, *got) { - t.Errorf("expected %v, got %v", *want, *got) - } + require.Equal(t, want, logsink) } func TestDatabases_UpdateLogsink(t *testing.T) { @@ -3981,16 +3953,13 @@ func TestDatabases_UpdateLogsink(t *testing.T) { fmt.Fprint(w, body) }) - var i interface{} - i = RsyslogLogsinkConfig{ - Server: "192.168.0.1", - Port: 514, - TLS: false, - Format: "rfc3164", - } - _, err := client.Databases.UpdateLogsink(ctx, dbID, logsinkID, &DatabaseUpdateLogsinkRequest{ - Config: &i, + Config: &DatabaseLogsinkConfig{ + Server: "192.168.0.1", + Port: 514, + TLS: false, + Format: "rfc3164", + }, }) require.NoError(t, err) @@ -4004,30 +3973,24 @@ func TestDatabases_ListLogsinks(t *testing.T) { dbID = "deadbeef-dead-4aa5-beef-deadbeef347d" ) - var cfg1 interface{} - cfg1 = OpensearchLogsinkConfig{ - URL: "https://user:passwd@192.168.0.1:25060", - IndexPrefix: "opensearch-logs", - } - - var cfg2 interface{} - cfg2 = OpensearchLogsinkConfig{ - URL: "https://user:passwd@192.168.0.1:25060", - IndexPrefix: "opensearch-logs", - } - want := []DatabaseLogsink{ { - ID: "deadbeef-dead-4aa5-beef-deadbeef347d", - Name: "logs-sink", - Type: "opensearch", - Config: &cfg1, + ID: "deadbeef-dead-4aa5-beef-deadbeef347d", + Name: "logs-sink", + Type: "opensearch", + Config: &DatabaseLogsinkConfig{ + URL: "https://user:passwd@192.168.0.1:25060", + IndexPrefix: "opensearch-logs", + }, }, { - ID: "d6e95157-5f58-48d0-9023-8cfb409d102a", - Name: "logs-sink-2", - Type: "opensearch", - Config: &cfg2, + ID: "d6e95157-5f58-48d0-9023-8cfb409d102a", + Name: "logs-sink-2", + Type: "opensearch", + Config: &DatabaseLogsinkConfig{ + URL: "https://user:passwd@192.168.0.1:25060", + IndexPrefix: "opensearch-logs", + }, }, } @@ -4061,22 +4024,9 @@ func TestDatabases_ListLogsinks(t *testing.T) { fmt.Fprint(w, body) }) - got, _, err := client.Databases.ListLogsinks(ctx, dbID, &ListOptions{}) + logsinks, _, err := client.Databases.ListLogsinks(ctx, dbID, &ListOptions{}) require.NoError(t, err) - - for i, v := range got { - var gotCfg interface{} - if configMap, ok := (*v.Config).(map[string]interface{}); ok { - gotCfg = OpensearchLogsinkConfig{ - URL: configMap["url"].(string), - IndexPrefix: configMap["index_prefix"].(string), - } - } - v.Config = &gotCfg - if !reflect.DeepEqual(want[i], v) { - t.Errorf("expected %v, got %v", want[i], got[i]) - } - } + require.Equal(t, want, logsinks) } func TestDatabases_DeleteLogsink(t *testing.T) {