From 0c65df2c7c01820dc2e578453f744f142b249582 Mon Sep 17 00:00:00 2001 From: Chris Morse Date: Wed, 15 Nov 2023 10:48:11 -0500 Subject: [PATCH] Add support for Managed Database usage endpoint --- database.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/database.go b/database.go index 17c3279..86769dc 100644 --- a/database.go +++ b/database.go @@ -21,6 +21,8 @@ type DatabaseService interface { Update(ctx context.Context, databaseID string, databaseReq *DatabaseUpdateReq) (*Database, *http.Response, error) Delete(ctx context.Context, databaseID string) error + GetUsage(ctx context.Context, databaseID string) (*DatabaseUsage, *http.Response, error) + ListUsers(ctx context.Context, databaseID string) ([]DatabaseUser, *Meta, *http.Response, error) CreateUser(ctx context.Context, databaseID string, databaseUserReq *DatabaseUserCreateReq) (*DatabaseUser, *http.Response, error) GetUser(ctx context.Context, databaseID string, username string) (*DatabaseUser, *http.Response, error) @@ -214,6 +216,37 @@ type DatabaseUpdateReq struct { RedisEvictionPolicy string `json:"redis_eviction_policy,omitempty"` } +// DatabaseUsage represents disk, memory, and CPU usage for a Managed Database +type DatabaseUsage struct { + Disk DatabaseDiskUsage `json:"disk"` + Memory DatabaseMemoryUsage `json:"memory"` + CPU DatabaseCPUUsage `json:"cpu"` +} + +// DatabaseDiskUsage represents disk usage details for a Managed Database +type DatabaseDiskUsage struct { + CurrentGB float32 `json:"current_gb"` + MaxGB int `json:"max_gb"` + Percentage float32 `json:"percentage"` +} + +// DatabaseMemoryUsage represents memory usage details for a Managed Database +type DatabaseMemoryUsage struct { + CurrentMB float32 `json:"current_mb"` + MaxMB int `json:"max_mb"` + Percentage float32 `json:"percentage"` +} + +// DatabaseCPUUsage represents average CPU usage for a Managed Database +type DatabaseCPUUsage struct { + Percentage float32 `json:"percentage"` +} + +// databaseUsageBase represents a migration status object API response for a Managed Database +type databaseUsageBase struct { + Usage *DatabaseUsage `json:"usage"` +} + // DatabaseUser represents a user within a Managed Database cluster type DatabaseUser struct { Username string `json:"username"` @@ -600,6 +633,24 @@ func (d *DatabaseServiceHandler) Delete(ctx context.Context, databaseID string) return err } +// GetUsage retrieves disk, memory, and CPU usage information for your Managed Database. +func (d *DatabaseServiceHandler) GetUsage(ctx context.Context, databaseID string) (*DatabaseUsage, *http.Response, error) { + uri := fmt.Sprintf("%s/%s/usage", databasePath, databaseID) + + req, err := d.client.NewRequest(ctx, http.MethodGet, uri, nil) + if err != nil { + return nil, nil, err + } + + databaseUsage := new(databaseUsageBase) + resp, err := d.client.DoWithContext(ctx, req, databaseUsage) + if err != nil { + return nil, nil, err + } + + return databaseUsage.Usage, resp, nil +} + // ListUsers retrieves all database users on your Managed Database. func (d *DatabaseServiceHandler) ListUsers(ctx context.Context, databaseID string) ([]DatabaseUser, *Meta, *http.Response, error) { //nolint:dupl,lll uri := fmt.Sprintf("%s/%s/users", databasePath, databaseID)