Skip to content

Commit

Permalink
Add ListDatabaseEvents to Godo (#675)
Browse files Browse the repository at this point in the history
* Add ListProjectEvents to Godo

* Fix naming for database events

---------

Co-authored-by: Rahul Bhardwaj <rahulbhardwaj@digitalocean.com>
  • Loading branch information
bhardwajRahul and Rahul Bhardwaj committed Apr 4, 2024
1 parent 05d1155 commit c56902f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
36 changes: 36 additions & 0 deletions databases.go
Expand Up @@ -35,6 +35,7 @@ const (
databaseTopicPath = databaseBasePath + "/%s/topics/%s"
databaseTopicsPath = databaseBasePath + "/%s/topics"
databaseMetricsCredentialsPath = databaseBasePath + "/metrics/credentials"
databaseEvents = databaseBasePath + "/%s/events"
)

// SQL Mode constants allow for MySQL-specific SQL flavor configuration.
Expand Down Expand Up @@ -157,6 +158,7 @@ type DatabasesService interface {
UpdateTopic(context.Context, string, string, *DatabaseUpdateTopicRequest) (*Response, error)
GetMetricsCredentials(context.Context) (*DatabaseMetricsCredentials, *Response, error)
UpdateMetricsCredentials(context.Context, *DatabaseUpdateMetricsCredentialsRequest) (*Response, error)
ListDatabaseEvents(context.Context, string) ([]*DatabaseEvent, *Response, error)
}

// DatabasesServiceOp handles communication with the Databases related methods
Expand Down Expand Up @@ -711,6 +713,23 @@ type DatabaseLayout struct {
Sizes []string `json:"sizes"`
}

// ListDatabaseEvents contains a list of project events.
type ListDatabaseEvents struct {
Events []*DatabaseEvent `json:"events"`
}

// DatbaseEvent contains the information about a Datbase event.
type DatabaseEvent struct {
ID string `json:"id"`
ServiceName string `json:"cluster_name"`
EventType string `json:"event_type"`
CreateTime string `json:"create_time"`
}

type ListDatabaseEventsRoot struct {
Events []*DatabaseEvent `json:"events"`
}

// URN returns a URN identifier for the database
func (d Database) URN() string {
return ToURN("dbaas", d.ID)
Expand Down Expand Up @@ -1517,3 +1536,20 @@ func (svc *DatabasesServiceOp) UpdateMetricsCredentials(ctx context.Context, upd
}
return resp, nil
}

func (svc *DatabasesServiceOp) ListDatabaseEvents(ctx context.Context, databaseID string) ([]*DatabaseEvent, *Response, error) {
path := fmt.Sprintf(databaseEvents, databaseID)
root := new(ListDatabaseEventsRoot)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root.Events, resp, nil

}
38 changes: 38 additions & 0 deletions databases_test.go
Expand Up @@ -3128,3 +3128,41 @@ func TestDatabases_UpdateMetricsCredentials(t *testing.T) {
_, err := client.Databases.UpdateMetricsCredentials(ctx, updateRequest)
require.NoError(t, err)
}

func TestDatabases_ListDatabaseEvents(t *testing.T) {
setup()
defer teardown()

dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"

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

want := []*DatabaseEvent{
{
ID: "pe8u2huh",
ServiceName: "customer-events",
EventType: "cluster_create",
CreateTime: "2020-10-29T15:57:38Z",
},
}

body := `{
"events": [
{
"id": "pe8u2huh",
"cluster_name": "customer-events",
"event_type": "cluster_create",
"create_time": "2020-10-29T15:57:38Z"
}
]
} `

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, body)
})

got, _, err := client.Databases.ListDatabaseEvents(ctx, dbID)
require.NoError(t, err)
require.Equal(t, want, got)
}

0 comments on commit c56902f

Please sign in to comment.