Skip to content

Commit

Permalink
Add database usage support (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
christhemorse committed Nov 17, 2023
1 parent 9f99244 commit dad833f
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dad833f

Please sign in to comment.