go-ceph/rgw/admin/usage.go
Sebastian Riese 1de6083536 rgw/admin: Clean up the parameter serialization
The following changes have been done:

* Up until now everything in the argument objects was serialized to
  the API calls. This was updated to restrict the serialization to
  the API call parameters that are parsed in the Ceph RGW source code.

  Parameters not yet supported by us are documented as comments.
  Note, that a superset of the documented parameters is supported.

  Documentation for the API:
  <https://docs.ceph.com/en/pacific/radosgw/adminops/>

  Link to the used source tree:
  <193895ffba/src/rgw>

  The argument parsing happens in the rgw_rest_*.cc files.

* The serialization code (valueToURLParams) has been updated to
  be more in line with other serialization methods:

  - A tag "-" causes the field to be ignored
  - Only the first item in a list of tag items is interpreted as
    name.
  - The handling of pointer and direct data types has been
    harmonized (the same rules for the names and value apply now).

* There is still room for improvement to make things more consistent:
  A pointer to a non-elementary data type will emit unexpected
  request parameters.

* Presence of required parameters is not validated by the library,
  this is left to the API.

Signed-off-by: Sebastian Riese <sebastian.riese@cloudandheat.com>
2022-03-17 17:47:25 +00:00

73 lines
2.3 KiB
Go

package admin
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// 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) {
// valid parameters not supported by go-ceph: category, bucket
body, err := api.call(ctx, http.MethodGet, "/usage", valueToURLParams(usage, []string{"uid", "start", "end", "show-entries", "show-summary"}))
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 {
// valid parameters not supported by go-ceph: bucket
_, err := api.call(ctx, http.MethodDelete, "/usage", valueToURLParams(usage, []string{"uid", "start", "end", "remove-all"}))
return err
}