generated from crossplane/provider-template
-
Notifications
You must be signed in to change notification settings - Fork 4
feat(sdk): qos #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(sdk): qos #124
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
32b47f1
draft: qos
tenstad 395d801
refactor: switch
tenstad 335a342
chore: names and validation
tenstad 82406b7
fix: soft/hard
tenstad 1527884
refactor: no ptr
tenstad 2ccb352
refactor: map it
tenstad f8d389a
refactor: rename
tenstad dfd4d79
refactor: KiBs
tenstad 17364bc
fix: lint
tenstad 3dc778a
refactor: warning
tenstad 7d43821
refactor: pointer
tenstad 86a9ffb
refactor: unmarshalQOSList
tenstad f3dd7e9
fix: GET
tenstad 47bb62a
fix: error on -1 and return -1
tenstad cfda87d
refactor: queryparams map fn
tenstad 3568d24
refactor: map in
tenstad ea1da58
fix: funcnames
tenstad 599ce95
fix: POST
tenstad 336a19c
fix: point
tenstad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package cloudian | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
// QualityOfService configures data limits for a Group or User. | ||
type QualityOfService struct { | ||
// Warning is the soft limit that triggers a warning. | ||
Warning QualityOfServiceLimits | ||
// Hard is the hard limit. | ||
Hard QualityOfServiceLimits | ||
} | ||
|
||
// QualityOfService configures data limits. | ||
type QualityOfServiceLimits struct { | ||
// StorageQuotaKiBs is the limit for total stored data in KiB. | ||
StorageQuotaKiBs *int64 | ||
// StorageQuotaCount is the limit for total number of objects. | ||
StorageQuotaCount *int64 | ||
// RequestsPerMin is the limit for number of HTTP requests per minute. | ||
RequestsPerMin *int64 | ||
// InboundKiBsPerMin is the limit for inbound data per minute in KiB. | ||
InboundKiBsPerMin *int64 | ||
// OutboundKiBsPerMin is the limit for outbound data per minute in KiB. | ||
OutboundKiBsPerMin *int64 | ||
} | ||
|
||
// nolint: gocyclo | ||
func (qos *QualityOfService) unmarshalQOSList(raw []byte) error { | ||
var data struct { | ||
QOSLimitList []struct { | ||
Type string `json:"type"` | ||
Value int64 `json:"value"` | ||
} `json:"qosLimitList"` | ||
} | ||
|
||
if err := json.Unmarshal(raw, &data); err != nil { | ||
return err | ||
} | ||
|
||
for _, item := range data.QOSLimitList { | ||
v := &item.Value | ||
switch item.Type { | ||
case "STORAGE_QUOTA_KBYTES_LH": | ||
qos.Hard.StorageQuotaKiBs = v | ||
case "STORAGE_QUOTA_KBYTES_LW": | ||
qos.Warning.StorageQuotaKiBs = v | ||
case "STORAGE_QUOTA_COUNT_LH": | ||
qos.Hard.StorageQuotaCount = v | ||
case "STORAGE_QUOTA_COUNT_LW": | ||
qos.Warning.StorageQuotaCount = v | ||
case "REQUEST_RATE_LH": | ||
qos.Hard.RequestsPerMin = v | ||
case "REQUEST_RATE_LW": | ||
qos.Warning.RequestsPerMin = v | ||
case "DATAKBYTES_IN_LH": | ||
qos.Hard.InboundKiBsPerMin = v | ||
case "DATAKBYTES_IN_LW": | ||
qos.Warning.InboundKiBsPerMin = v | ||
case "DATAKBYTES_OUT_LH": | ||
qos.Hard.OutboundKiBsPerMin = v | ||
case "DATAKBYTES_OUT_LW": | ||
qos.Warning.OutboundKiBsPerMin = v | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (qos QualityOfService) queryParams(params map[string]string) error { | ||
rawParams := map[string]*int64{ | ||
"hlStorageQuotaKBytes": qos.Hard.StorageQuotaKiBs, | ||
"wlStorageQuotaKBytes": qos.Warning.StorageQuotaKiBs, | ||
"hlStorageQuotaCount": qos.Hard.StorageQuotaCount, | ||
"wlStorageQuotaCount": qos.Warning.StorageQuotaCount, | ||
"hlRequestRate": qos.Hard.RequestsPerMin, | ||
"wlRequestRate": qos.Warning.RequestsPerMin, | ||
"hlDataKBytesIn": qos.Hard.InboundKiBsPerMin, | ||
"wlDataKBytesIn": qos.Warning.InboundKiBsPerMin, | ||
"hlDataKBytesOut": qos.Hard.OutboundKiBsPerMin, | ||
"wlDataKBytesOut": qos.Warning.OutboundKiBsPerMin, | ||
} | ||
|
||
for key, raw := range rawParams { | ||
val := int64(-1) | ||
if raw != nil { | ||
val = *raw | ||
} | ||
if val < -1 { | ||
return fmt.Errorf("invalid QoS limit value: %d", val) | ||
} | ||
params[key] = strconv.FormatInt(val, 10) | ||
tenstad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return nil | ||
} | ||
|
||
// SetQOS sets QualityOfService limits for a Group or User, depending on the value of GroupID and UserID. | ||
// | ||
// User-level QoS for a specific user (GroupID="<groupId>", UserID="<userId>") | ||
// Default user-level QoS for a specific group (GroupID="<groupId>", UserID="ALL") | ||
// Default user-level QoS for the whole region (GroupID="*", UserID="ALL") | ||
// Group-level QoS for a specific group (GroupID="<groupId>", UserID="*") | ||
// Default group-level QoS for the whole region (GroupID="ALL", UserID="*") | ||
func (client Client) SetQOS(ctx context.Context, user User, qos QualityOfService) error { | ||
params := make(map[string]string) | ||
if err := qos.queryParams(params); err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.newRequest(ctx). | ||
SetQueryParam("userId", user.UserID). | ||
SetQueryParam("groupId", user.GroupID). | ||
SetQueryParams(params). | ||
Post("/qos/limits") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch resp.StatusCode() { | ||
case 200: | ||
return nil | ||
default: | ||
return fmt.Errorf("POST quota unexpected status: %d", resp.StatusCode()) | ||
} | ||
} | ||
|
||
// SetQOS gets QualityOfService limits for a Group or User, depending on the value of GroupID and UserID. | ||
// See SetQOS for details. | ||
func (client Client) GetQOS(ctx context.Context, user User) (*QualityOfService, error) { | ||
resp, err := client.newRequest(ctx). | ||
SetQueryParam("userId", user.UserID). | ||
SetQueryParam("groupId", user.GroupID). | ||
Get("/qos/limits") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch resp.StatusCode() { | ||
case 200: | ||
qos := &QualityOfService{} | ||
return qos, qos.unmarshalQOSList(resp.Body()) | ||
default: | ||
return nil, fmt.Errorf("GET quota unexpected status: %d", resp.StatusCode()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.