diff options
| -rw-r--r-- | daemon/api_quotas.go | 51 | ||||
| -rw-r--r-- | daemon/api_quotas_test.go | 6 |
2 files changed, 10 insertions, 47 deletions
diff --git a/daemon/api_quotas.go b/daemon/api_quotas.go index 1d3a240009..69e4830fcf 100644 --- a/daemon/api_quotas.go +++ b/daemon/api_quotas.go @@ -20,7 +20,6 @@ package daemon import ( - "encoding/json" "net/http" "sort" @@ -32,7 +31,6 @@ import ( "github.com/snapcore/snapd/overlord/state" "github.com/snapcore/snapd/snap/naming" "github.com/snapcore/snapd/snap/quota" - "github.com/snapcore/snapd/strutil" ) var ( @@ -52,13 +50,11 @@ var ( type postQuotaGroupData struct { // Action can be "ensure" or "remove" - Action string `json:"action"` - GroupName string `json:"group-name"` - Parent string `json:"parent,omitempty"` - Snaps []string `json:"snaps,omitempty"` - // Constraints is a map of resource type to constraint, currently accepted - // resource types is just "memory" - Constraints map[string]interface{} `json:"constraints,omitempty"` + Action string `json:"action"` + GroupName string `json:"group-name"` + Parent string `json:"parent,omitempty"` + Snaps []string `json:"snaps,omitempty"` + Constraints client.QuotaValues `json:"constraints,omitempty"` } var ( @@ -175,39 +171,6 @@ func postQuotaGroup(c *Command, r *http.Request, _ *auth.UserState) Response { var ts *state.TaskSet - var memSize uint64 - - var unknownResourceTypes []string - for resourceType := range data.Constraints { - switch resourceType { - case "memory": - default: - unknownResourceTypes = append(unknownResourceTypes, resourceType) - } - } - - if len(unknownResourceTypes) != 0 { - pluralization := "" - if len(unknownResourceTypes) > 1 { - pluralization = "s" - } - return BadRequest("unknown resource type constraint%s in request: %s", pluralization, strutil.Quoted(unknownResourceTypes)) - } - - if memVal, ok := data.Constraints["memory"]; ok { - memJsonNumber, ok := memVal.(json.Number) - if !ok { - return BadRequest("cannot decode quota action memory constraint as number (got %T)", memVal) - } - - memInt64, err := memJsonNumber.Int64() - if err != nil { - return BadRequest("cannot decode quota action memory constrain as uint: %v", err) - } - - memSize = uint64(memInt64) - } - switch data.Action { case "ensure": // check if the quota group exists first, if it does then we need to @@ -218,7 +181,7 @@ func postQuotaGroup(c *Command, r *http.Request, _ *auth.UserState) Response { } if err == servicestate.ErrQuotaNotFound { // then we need to create the quota - ts, err = servicestateCreateQuota(st, data.GroupName, data.Parent, data.Snaps, quantity.Size(memSize)) + ts, err = servicestateCreateQuota(st, data.GroupName, data.Parent, data.Snaps, data.Constraints.Memory) if err != nil { // XXX: dedicated error type? return BadRequest(err.Error()) @@ -228,7 +191,7 @@ func postQuotaGroup(c *Command, r *http.Request, _ *auth.UserState) Response { // the quota group already exists, update it updateOpts := servicestate.QuotaGroupUpdate{ AddSnaps: data.Snaps, - NewMemoryLimit: quantity.Size(memSize), + NewMemoryLimit: data.Constraints.Memory, } ts, err = servicestateUpdateQuota(st, data.GroupName, updateOpts) if err != nil { diff --git a/daemon/api_quotas_test.go b/daemon/api_quotas_test.go index 39093c8b6d..8248a047a3 100644 --- a/daemon/api_quotas_test.go +++ b/daemon/api_quotas_test.go @@ -116,7 +116,7 @@ func (s *apiQuotaSuite) TestPostEnsureQuotaUnhappy(c *check.C) { GroupName: "booze", Parent: "foo", Snaps: []string{"bar"}, - Constraints: map[string]interface{}{"memory": 1000}, + Constraints: client.QuotaValues{Memory: quantity.Size(1000)}, }) c.Assert(err, check.IsNil) @@ -146,7 +146,7 @@ func (s *apiQuotaSuite) TestPostEnsureQuotaCreateHappy(c *check.C) { GroupName: "booze", Parent: "foo", Snaps: []string{"some-snap"}, - Constraints: map[string]interface{}{"memory": 1000}, + Constraints: client.QuotaValues{Memory: quantity.Size(1000)}, }) c.Assert(err, check.IsNil) @@ -188,7 +188,7 @@ func (s *apiQuotaSuite) TestPostEnsureQuotaUpdateHappy(c *check.C) { Action: "ensure", GroupName: "ginger-ale", Snaps: []string{"some-snap"}, - Constraints: map[string]interface{}{"memory": 9000}, + Constraints: client.QuotaValues{Memory: quantity.Size(9000)}, }) c.Assert(err, check.IsNil) |
