2020package daemon
2121
2222import (
23- "encoding/json"
2423 "net/http"
2524 "sort"
2625
@@ -32,7 +31,6 @@ import (
3231 "github.com/snapcore/snapd/overlord/state"
3332 "github.com/snapcore/snapd/snap/naming"
3433 "github.com/snapcore/snapd/snap/quota"
35- "github.com/snapcore/snapd/strutil"
3634)
3735
3836var (
@@ -52,13 +50,11 @@ var (
5250
5351type postQuotaGroupData struct {
5452 // Action can be "ensure" or "remove"
55- Action string `json:"action"`
56- GroupName string `json:"group-name"`
57- Parent string `json:"parent,omitempty"`
58- Snaps []string `json:"snaps,omitempty"`
59- // Constraints is a map of resource type to constraint, currently accepted
60- // resource types is just "memory"
61- Constraints map [string ]interface {} `json:"constraints,omitempty"`
53+ Action string `json:"action"`
54+ GroupName string `json:"group-name"`
55+ Parent string `json:"parent,omitempty"`
56+ Snaps []string `json:"snaps,omitempty"`
57+ Constraints client.QuotaValues `json:"constraints,omitempty"`
6258}
6359
6460var (
@@ -175,39 +171,6 @@ func postQuotaGroup(c *Command, r *http.Request, _ *auth.UserState) Response {
175171
176172 var ts * state.TaskSet
177173
178- var memSize uint64
179-
180- var unknownResourceTypes []string
181- for resourceType := range data .Constraints {
182- switch resourceType {
183- case "memory" :
184- default :
185- unknownResourceTypes = append (unknownResourceTypes , resourceType )
186- }
187- }
188-
189- if len (unknownResourceTypes ) != 0 {
190- pluralization := ""
191- if len (unknownResourceTypes ) > 1 {
192- pluralization = "s"
193- }
194- return BadRequest ("unknown resource type constraint%s in request: %s" , pluralization , strutil .Quoted (unknownResourceTypes ))
195- }
196-
197- if memVal , ok := data .Constraints ["memory" ]; ok {
198- memJsonNumber , ok := memVal .(json.Number )
199- if ! ok {
200- return BadRequest ("cannot decode quota action memory constraint as number (got %T)" , memVal )
201- }
202-
203- memInt64 , err := memJsonNumber .Int64 ()
204- if err != nil {
205- return BadRequest ("cannot decode quota action memory constrain as uint: %v" , err )
206- }
207-
208- memSize = uint64 (memInt64 )
209- }
210-
211174 switch data .Action {
212175 case "ensure" :
213176 // 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 {
218181 }
219182 if err == servicestate .ErrQuotaNotFound {
220183 // then we need to create the quota
221- ts , err = servicestateCreateQuota (st , data .GroupName , data .Parent , data .Snaps , quantity . Size ( memSize ) )
184+ ts , err = servicestateCreateQuota (st , data .GroupName , data .Parent , data .Snaps , data . Constraints . Memory )
222185 if err != nil {
223186 // XXX: dedicated error type?
224187 return BadRequest (err .Error ())
@@ -228,7 +191,7 @@ func postQuotaGroup(c *Command, r *http.Request, _ *auth.UserState) Response {
228191 // the quota group already exists, update it
229192 updateOpts := servicestate.QuotaGroupUpdate {
230193 AddSnaps : data .Snaps ,
231- NewMemoryLimit : quantity . Size ( memSize ) ,
194+ NewMemoryLimit : data . Constraints . Memory ,
232195 }
233196 ts , err = servicestateUpdateQuota (st , data .GroupName , updateOpts )
234197 if err != nil {
0 commit comments