Skip to content

Commit b036199

Browse files
committed
daemon/api_quotas.go: use client.QuotaValues for post struct too
Missed this in the previous commit. Thanks to Samuele for spotting this irregularity. Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
1 parent 1a0553e commit b036199

File tree

2 files changed

+10
-47
lines changed

2 files changed

+10
-47
lines changed

daemon/api_quotas.go

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package daemon
2121

2222
import (
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

3836
var (
@@ -52,13 +50,11 @@ var (
5250

5351
type 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

6460
var (
@@ -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 {

daemon/api_quotas_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (s *apiQuotaSuite) TestPostEnsureQuotaUnhappy(c *check.C) {
116116
GroupName: "booze",
117117
Parent: "foo",
118118
Snaps: []string{"bar"},
119-
Constraints: map[string]interface{}{"memory": 1000},
119+
Constraints: client.QuotaValues{Memory: quantity.Size(1000)},
120120
})
121121
c.Assert(err, check.IsNil)
122122

@@ -146,7 +146,7 @@ func (s *apiQuotaSuite) TestPostEnsureQuotaCreateHappy(c *check.C) {
146146
GroupName: "booze",
147147
Parent: "foo",
148148
Snaps: []string{"some-snap"},
149-
Constraints: map[string]interface{}{"memory": 1000},
149+
Constraints: client.QuotaValues{Memory: quantity.Size(1000)},
150150
})
151151
c.Assert(err, check.IsNil)
152152

@@ -188,7 +188,7 @@ func (s *apiQuotaSuite) TestPostEnsureQuotaUpdateHappy(c *check.C) {
188188
Action: "ensure",
189189
GroupName: "ginger-ale",
190190
Snaps: []string{"some-snap"},
191-
Constraints: map[string]interface{}{"memory": 9000},
191+
Constraints: client.QuotaValues{Memory: quantity.Size(9000)},
192192
})
193193
c.Assert(err, check.IsNil)
194194

0 commit comments

Comments
 (0)