Skip to content

Commit

Permalink
use []DatabaseLogsink in ListLogsinks
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Oct 8, 2024
1 parent 93435d0 commit efb1576
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 49 deletions.
64 changes: 32 additions & 32 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -847,7 +847,7 @@ type databaseLogsinkRoot struct {
}

type databaseLogsinksRoot struct {
Sinks []interface{} `json:"sinks"`
Sinks []DatabaseLogsink `json:"sinks"`
}

type databaseMetricsCredentialsRoot struct {
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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
}
49 changes: 32 additions & 17 deletions databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]:25060",
IndexPrefix: "opensearch-logs",
}

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

want := []DatabaseLogsink{
{
"config": map[string]interface{}{
"index_prefix": "opensearch-logs",
"url": "https://user:[email protected]: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:[email protected]: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,
},
}

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

0 comments on commit efb1576

Please sign in to comment.