Skip to content

Commit

Permalink
use DatabaseLogsinkConfig instead of interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Oct 15, 2024
1 parent 0f32c4e commit 2c2e059
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 129 deletions.
54 changes: 20 additions & 34 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
140 changes: 45 additions & 95 deletions databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math/big"
"net/http"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -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:[email protected]: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:[email protected]:25060",
IndexPrefix: "opensearch-logs",
},
}

body := `{
Expand All @@ -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:[email protected]: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) {
Expand All @@ -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:[email protected]: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:[email protected]:25060",
IndexPrefix: "opensearch-logs",
},
}

body := `{
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -4004,30 +3973,24 @@ func TestDatabases_ListLogsinks(t *testing.T) {
dbID = "deadbeef-dead-4aa5-beef-deadbeef347d"
)

var cfg1 interface{}
cfg1 = OpensearchLogsinkConfig{
URL: "https://user:[email protected]:25060",
IndexPrefix: "opensearch-logs",
}

var cfg2 interface{}
cfg2 = OpensearchLogsinkConfig{
URL: "https://user:[email protected]: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:[email protected]: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:[email protected]:25060",
IndexPrefix: "opensearch-logs",
},
},
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2c2e059

Please sign in to comment.