go-ceph/rgw/admin/usage.go
Sébastien Han 8179bd4437 rgw/admin: stop returning pointers
Since we are not passing a pointer, let's not return a pointer either.
Also, our use case fits well the non-returning pointer approach since
the content of the struct type is not expected to be modified.

Signed-off-by: Sébastien Han <seb@redhat.com>
2021-06-07 12:23:46 -04:00

70 lines
2.0 KiB
Go

package admin
import (
"context"
"encoding/json"
"fmt"
)
// Usage struct
type Usage struct {
Entries []struct {
User string `json:"user"`
Buckets []struct {
Bucket string `json:"bucket"`
Time string `json:"time"`
Epoch uint64 `json:"epoch"`
Owner string `json:"owner"`
Categories []struct {
Category string `json:"category"`
BytesSent uint64 `json:"bytes_sent"`
BytesReceived uint64 `json:"bytes_received"`
Ops uint64 `json:"ops"`
SuccessfulOps uint64 `json:"successful_ops"`
} `json:"categories"`
} `json:"buckets"`
} `json:"entries"`
Summary []struct {
User string `json:"user"`
Categories []struct {
Category string `json:"category"`
BytesSent uint64 `json:"bytes_sent"`
BytesReceived uint64 `json:"bytes_received"`
Ops uint64 `json:"ops"`
SuccessfulOps uint64 `json:"successful_ops"`
} `json:"categories"`
Total struct {
BytesSent uint64 `json:"bytes_sent"`
BytesReceived uint64 `json:"bytes_received"`
Ops uint64 `json:"ops"`
SuccessfulOps uint64 `json:"successful_ops"`
} `json:"total"`
} `json:"summary"`
Start string `url:"start"` //Example: 2012-09-25 16:00:00
End string `url:"end"`
ShowEntries *bool `url:"show-entries"`
ShowSummary *bool `url:"show-summary"`
RemoveAll *bool `url:"remove-all"` //true
}
// GetUsage request bandwidth usage information on the object store
func (api *API) GetUsage(ctx context.Context, usage Usage) (Usage, error) {
body, err := api.call(ctx, get, "/usage", valueToURLParams(usage))
if err != nil {
return Usage{}, err
}
u := Usage{}
err = json.Unmarshal(body, &u)
if err != nil {
return Usage{}, fmt.Errorf("%s. %s. %w", unmarshalError, string(body), err)
}
return u, nil
}
// TrimUsage removes bandwidth usage information. With no dates specified, removes all usage information.
func (api *API) TrimUsage(ctx context.Context, usage Usage) error {
_, err := api.call(ctx, delete, "/usage", valueToURLParams(usage))
return err
}