From efb15764f26af90a4c11c9706125450ee56e3301 Mon Sep 17 00:00:00 2001 From: Anna Lushnikova Date: Tue, 8 Oct 2024 14:52:45 -0400 Subject: [PATCH] use []DatabaseLogsink in ListLogsinks --- databases.go | 64 +++++++++++++++++++++++------------------------ databases_test.go | 49 +++++++++++++++++++++++------------- 2 files changed, 64 insertions(+), 49 deletions(-) diff --git a/databases.go b/databases.go index adc699e..7cb9e98 100644 --- a/databases.go +++ b/databases.go @@ -176,7 +176,7 @@ type DatabasesService interface { DeleteIndex(context.Context, string, string) (*Response, error) CreateLogsink(ctx context.Context, databaseID string, createLogsink *DatabaseCreateLogsinkRequest) (*DatabaseLogsink, *Response, error) GetLogsink(ctx context.Context, databaseID string, logsinkID string) (*DatabaseLogsink, *Response, error) - ListLogsinks(ctx context.Context, databaseID string, opts *ListOptions) ([]interface{}, *Response, error) + ListLogsinks(ctx context.Context, databaseID string, opts *ListOptions) ([]DatabaseLogsink, *Response, error) UpdateLogsink(ctx context.Context, databaseID string, logsinkID string, updateLogsink *DatabaseUpdateLogsinkRequest) (*Response, error) DeleteLogsink(ctx context.Context, databaseID, logsinkID string) (*Response, error) } @@ -847,7 +847,7 @@ type databaseLogsinkRoot struct { } type databaseLogsinksRoot struct { - Sinks []interface{} `json:"sinks"` + Sinks []DatabaseLogsink `json:"sinks"` } type databaseMetricsCredentialsRoot struct { @@ -1896,40 +1896,24 @@ func (svc *DatabasesServiceOp) DeleteIndex(ctx context.Context, databaseID, name return resp, nil } -// ListTopics returns all topics for a given kafka cluster. -func (svc *DatabasesServiceOp) ListLogsinks(ctx context.Context, databaseID string, opts *ListOptions) ([]interface{}, *Response, error) { +// CreateLogsink creates a new logsink for a database cluster. +func (svc *DatabasesServiceOp) CreateLogsink(ctx context.Context, databaseID string, createLogsink *DatabaseCreateLogsinkRequest) (*DatabaseLogsink, *Response, error) { path := fmt.Sprintf(databaseLogsinksPath, databaseID) - path, err := addOptions(path, opts) - if err != nil { - return nil, nil, err - } - req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil) + req, err := svc.client.NewRequest(ctx, http.MethodPost, path, createLogsink) if err != nil { return nil, nil, err } - root := new(databaseLogsinksRoot) + + root := new(databaseLogsinkRoot) resp, err := svc.client.Do(ctx, req, root) if err != nil { return nil, resp, err } - return root.Sinks, resp, nil -} -// DeleteLogsink deletes a logsink for a database cluster. -func (svc *DatabasesServiceOp) DeleteLogsink(ctx context.Context, databaseID, logsinkID string) (*Response, error) { - path := fmt.Sprintf(databaseLogsinkPath, databaseID, logsinkID) - req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil) - if err != nil { - return nil, err - } - resp, err := svc.client.Do(ctx, req, nil) - if err != nil { - return resp, err - } - return resp, nil + return &root.Sink, resp, nil } -// GetLogsink gets a logsink for a database. +// GetLogsink gets a logsink for a database cluster. func (svc *DatabasesServiceOp) GetLogsink(ctx context.Context, databaseID string, logsinkID string) (*DatabaseLogsink, *Response, error) { path := fmt.Sprintf(databaseLogsinkPath, databaseID, logsinkID) req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil) @@ -1945,21 +1929,23 @@ func (svc *DatabasesServiceOp) GetLogsink(ctx context.Context, databaseID string return &root.Sink, resp, nil } -// CreateLogsink creates a new logsink for a database. -func (svc *DatabasesServiceOp) CreateLogsink(ctx context.Context, databaseID string, createLogsink *DatabaseCreateLogsinkRequest) (*DatabaseLogsink, *Response, error) { +// ListTopics returns all logsinks for a given database cluster. +func (svc *DatabasesServiceOp) ListLogsinks(ctx context.Context, databaseID string, opts *ListOptions) ([]DatabaseLogsink, *Response, error) { path := fmt.Sprintf(databaseLogsinksPath, databaseID) - req, err := svc.client.NewRequest(ctx, http.MethodPost, path, createLogsink) + path, err := addOptions(path, opts) if err != nil { return nil, nil, err } - - root := new(databaseLogsinkRoot) + req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil) + if err != nil { + return nil, nil, err + } + root := new(databaseLogsinksRoot) resp, err := svc.client.Do(ctx, req, root) if err != nil { return nil, resp, err } - - return &root.Sink, resp, nil + return root.Sinks, resp, nil } // UpdateLogsink updates a logsink for a database cluster. @@ -1976,3 +1962,17 @@ func (svc *DatabasesServiceOp) UpdateLogsink(ctx context.Context, databaseID str } return resp, nil } + +// DeleteLogsink deletes a logsink for a database cluster. +func (svc *DatabasesServiceOp) DeleteLogsink(ctx context.Context, databaseID, logsinkID string) (*Response, error) { + path := fmt.Sprintf(databaseLogsinkPath, databaseID, logsinkID) + req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil) + if err != nil { + return nil, err + } + resp, err := svc.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + return resp, nil +} diff --git a/databases_test.go b/databases_test.go index 2edaf68..26247bd 100644 --- a/databases_test.go +++ b/databases_test.go @@ -4006,24 +4006,30 @@ func TestDatabases_ListLogsinks(t *testing.T) { dbID = "deadbeef-dead-4aa5-beef-deadbeef347d" ) - want := []map[string]interface{}{ + 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{ { - "config": map[string]interface{}{ - "index_prefix": "opensearch-logs", - "url": "https://user:passwd@192.168.0.1:25060", - }, - "sink_id": "deadbeef-dead-4aa5-beef-deadbeef347d", - "sink_name": "logs-sink", - "sink_type": "opensearch", + ID: "deadbeef-dead-4aa5-beef-deadbeef347d", + Name: "logs-sink", + Type: "opensearch", + Config: &cfg1, }, { - "config": map[string]interface{}{ - "index_prefix": "opensearch-logs", - "url": "https://user:passwd@192.168.0.1:25060", - }, - "sink_id": "d6e95157-5f58-48d0-9023-8cfb409d102a", - "sink_name": "logs-sink-2", - "sink_type": "opensearch", + ID: "d6e95157-5f58-48d0-9023-8cfb409d102a", + Name: "logs-sink-2", + Type: "opensearch", + Config: &cfg2, }, } @@ -4059,8 +4065,17 @@ func TestDatabases_ListLogsinks(t *testing.T) { got, _, err := client.Databases.ListLogsinks(ctx, dbID, &ListOptions{}) require.NoError(t, err) - for i := range got { - if !reflect.DeepEqual(want[i], got[i]) { + + 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]) } }