-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlimits.go
More file actions
74 lines (65 loc) · 2.07 KB
/
limits.go
File metadata and controls
74 lines (65 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Package limits provides methods for managing resource limits of apps.
package limits
import (
"encoding/json"
"fmt"
"strings"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// Specs is list all available limit specs
func Specs(c *drycc.Client, keywords string, results int) ([]api.LimitSpec, int, error) {
u := "/v2/limits/specs/"
if keywords != "" {
u += fmt.Sprintf("?keywords=%s", keywords)
}
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.LimitSpec{}, -1, reqErr
}
var limitSpecs []api.LimitSpec
if err := json.Unmarshal([]byte(body), &limitSpecs); err != nil {
return []api.LimitSpec{}, -1, err
}
return limitSpecs, count, reqErr
}
// Plans is list all available limit plans
func Plans(c *drycc.Client, specID string, cpu, memory, results int) ([]api.LimitPlan, int, error) {
var queryArray []string
if cpu > 0 {
queryArray = append(queryArray, fmt.Sprintf("cpu=%d", cpu))
}
if memory > 0 {
queryArray = append(queryArray, fmt.Sprintf("memory=%d", memory))
}
if specID != "" {
queryArray = append(queryArray, fmt.Sprintf("spec-id=%s", specID))
}
u := "/v2/limits/plans/"
if len(queryArray) > 0 {
u = fmt.Sprintf("%s?%s", u, strings.Join(queryArray, "&"))
}
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.LimitPlan{}, -1, reqErr
}
var limitPlans []api.LimitPlan
if err := json.Unmarshal([]byte(body), &limitPlans); err != nil {
return []api.LimitPlan{}, -1, err
}
return limitPlans, count, reqErr
}
// GetPlan is get a available Plan
func GetPlan(c *drycc.Client, planID string) (api.LimitPlan, error) {
u := fmt.Sprintf("/v2/limits/plans/%s/", planID)
res, reqErr := c.Request("GET", u, nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.LimitPlan{}, reqErr
}
defer res.Body.Close()
limitPlan := api.LimitPlan{}
if err := json.NewDecoder(res.Body).Decode(&limitPlan); err != nil {
return api.LimitPlan{}, err
}
return limitPlan, reqErr
}