Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[databases]: update structs for logsinks by sink type #738

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,6 @@ type DatabaseTopic struct {
Config *TopicConfig `json:"config,omitempty"`
}

// DatabaseLogsink represents a logsink
type DatabaseLogsink struct {
ID string `json:"sink_id"`
Name string `json:"sink_name,omitempty"`
Type string `json:"sink_type,omitempty"`
Config *DatabaseLogsinkConfig `json:"config,omitempty"`
}

// TopicPartition represents the state of a Kafka topic partition
type TopicPartition struct {
EarliestOffset uint64 `json:"earliest_offset,omitempty"`
Expand Down Expand Up @@ -507,14 +499,22 @@ type DatabaseFirewallRule struct {
CreatedAt time.Time `json:"created_at"`
}

// DatabaseCreateLogsinkRequest is used to create logsink for a database cluster
// 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 *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 *DatabaseLogsinkConfig `json:"config"`
}

// DatabaseUpdateLogsinkRequest is used to update logsink for a database cluster
// DatabaseUpdateLogsinkRequest is used to update logsink for a database cluster.
type DatabaseUpdateLogsinkRequest struct {
Config *DatabaseLogsinkConfig `json:"config"`
}
Expand Down Expand Up @@ -828,6 +828,10 @@ type databaseTopicsRoot struct {
Topics []DatabaseTopic `json:"topics"`
}

type databaseLogsinkRoot struct {
Sink DatabaseLogsink `json:"sink"`
}

type databaseLogsinksRoot struct {
Sinks []DatabaseLogsink `json:"sinks"`
}
Expand Down Expand Up @@ -1878,23 +1882,24 @@ func (svc *DatabasesServiceOp) DeleteIndex(ctx context.Context, databaseID, name
return resp, nil
}

// CreateLogsink creates a new logsink for a database
// 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)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, createLogsink)
if err != nil {
return nil, nil, err
}

root := new(DatabaseLogsink)
root := new(databaseLogsinkRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, 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 @@ -1910,7 +1915,7 @@ func (svc *DatabasesServiceOp) GetLogsink(ctx context.Context, databaseID string
return root, resp, nil
}

// ListTopics returns all topics for a given kafka cluster
// 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)
path, err := addOptions(path, opts)
Expand All @@ -1929,7 +1934,7 @@ func (svc *DatabasesServiceOp) ListLogsinks(ctx context.Context, databaseID stri
return root.Sinks, resp, nil
}

// UpdateLogsink updates a logsink for a database cluster
// UpdateLogsink updates a logsink for a database cluster.
func (svc *DatabasesServiceOp) UpdateLogsink(ctx context.Context, databaseID string, logsinkID string, updateLogsink *DatabaseUpdateLogsinkRequest) (*Response, error) {
path := fmt.Sprintf(databaseLogsinkPath, databaseID, logsinkID)
req, err := svc.client.NewRequest(ctx, http.MethodPut, path, updateLogsink)
Expand All @@ -1944,7 +1949,7 @@ func (svc *DatabasesServiceOp) UpdateLogsink(ctx context.Context, databaseID str
return resp, nil
}

// DeleteLogsink deletes a logsink for a database cluster
// 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)
Expand Down
106 changes: 55 additions & 51 deletions databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3853,14 +3853,16 @@ func TestDatabases_CreateLogsink(t *testing.T) {
}

body := `{
"sink_id":"deadbeef-dead-4aa5-beef-deadbeef347d",
"sink_name": "logs-sink",
"sink_type": "opensearch",
"config": {
"url": "https://user:[email protected]:25060",
"index_prefix": "opensearch-logs"
}
}`
"sink":{
"sink_id":"deadbeef-dead-4aa5-beef-deadbeef347d",
"sink_name":"logs-sink",
"sink_type":"opensearch",
"config":{
"url":"https://user:[email protected]:25060",
"index_prefix":"opensearch-logs"
}
}
}`

path := fmt.Sprintf("/v2/databases/%s/logsink", dbID)

Expand All @@ -3869,7 +3871,7 @@ func TestDatabases_CreateLogsink(t *testing.T) {
fmt.Fprint(w, body)
})

log, _, err := client.Databases.CreateLogsink(ctx, dbID, &DatabaseCreateLogsinkRequest{
logsink, _, err := client.Databases.CreateLogsink(ctx, dbID, &DatabaseCreateLogsinkRequest{
Name: "logs-sink",
Type: "opensearch",
Config: &DatabaseLogsinkConfig{
Expand All @@ -3879,8 +3881,7 @@ func TestDatabases_CreateLogsink(t *testing.T) {
})

require.NoError(t, err)

require.Equal(t, want, log)
require.Equal(t, want, logsink)
}

func TestDatabases_GetLogsink(t *testing.T) {
Expand All @@ -3903,14 +3904,14 @@ func TestDatabases_GetLogsink(t *testing.T) {
}

body := `{
"sink_id":"deadbeef-dead-4aa5-beef-deadbeef347d",
"sink_name": "logs-sink",
"sink_type": "opensearch",
"config": {
"url": "https://user:[email protected]:25060",
"index_prefix": "opensearch-logs"
}
}`
"sink_id":"deadbeef-dead-4aa5-beef-deadbeef347d",
"sink_name":"logs-sink",
"sink_type":"opensearch",
"config":{
"url":"https://user:[email protected]:25060",
"index_prefix":"opensearch-logs"
}
}`

path := fmt.Sprintf("/v2/databases/%s/logsink/%s", dbID, logsinkID)

Expand All @@ -3919,9 +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)
require.Equal(t, want, got)
require.Equal(t, want, logsink)
}

func TestDatabases_UpdateLogsink(t *testing.T) {
Expand All @@ -3934,14 +3935,16 @@ func TestDatabases_UpdateLogsink(t *testing.T) {
)

body := `{
"sink_id":"deadbeef-dead-4aa5-beef-deadbeef347d",
"sink_name": "logs-sink",
"sink_type": "opensearch",
"config": {
"url": "https://user:[email protected]:25060",
"index_prefix": "opensearch-logs"
}
}`
"sink":{
"sink_id":"deadbeef-dead-4aa5-beef-deadbeef347d",
"sink_name":"logs-sink",
"sink_type":"opensearch",
"config":{
"url":"https://user:[email protected]:25060",
"index_prefix":"opensearch-logs"
}
}
}`

path := fmt.Sprintf("/v2/databases/%s/logsink/%s", dbID, logsinkID)

Expand Down Expand Up @@ -3988,30 +3991,31 @@ func TestDatabases_ListLogsinks(t *testing.T) {
URL: "https://user:[email protected]:25060",
IndexPrefix: "opensearch-logs",
},
}}
},
}

body := `{
"sinks": [
{
"sink_id": "deadbeef-dead-4aa5-beef-deadbeef347d",
"sink_name": "logs-sink",
"sink_type": "opensearch",
"config": {
"url": "https://user:[email protected]:25060",
"index_prefix": "opensearch-logs"
}
},
{
"sink_id": "d6e95157-5f58-48d0-9023-8cfb409d102a",
"sink_name": "logs-sink-2",
"sink_type": "opensearch",
"config": {
"url": "https://user:[email protected]:25060",
"index_prefix": "opensearch-logs"
"sinks":[
{
"sink_id":"deadbeef-dead-4aa5-beef-deadbeef347d",
"sink_name":"logs-sink",
"sink_type":"opensearch",
"config":{
"url":"https://user:[email protected]:25060",
"index_prefix":"opensearch-logs"
}
},
{
"sink_id":"d6e95157-5f58-48d0-9023-8cfb409d102a",
"sink_name":"logs-sink-2",
"sink_type":"opensearch",
"config":{
"url":"https://user:[email protected]:25060",
"index_prefix":"opensearch-logs"
}
}
}
]
}`
}`

path := fmt.Sprintf("/v2/databases/%s/logsink", dbID)

Expand All @@ -4020,9 +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)
require.Equal(t, want, got)
require.Equal(t, want, logsinks)
}

func TestDatabases_DeleteLogsink(t *testing.T) {
Expand Down
Loading