-
Notifications
You must be signed in to change notification settings - Fork 53
/
api_plan.go
88 lines (79 loc) · 2.5 KB
/
api_plan.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2021 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package daemon
import (
"encoding/json"
"net/http"
"gopkg.in/yaml.v3"
"github.com/canonical/pebble/internals/overlord/servstate"
"github.com/canonical/pebble/internals/plan"
)
func v1GetPlan(c *Command, r *http.Request, _ *UserState) Response {
format := r.URL.Query().Get("format")
if format != "yaml" {
return statusBadRequest("invalid format %q", format)
}
servmgr := overlordServiceManager(c.d.overlord)
plan, err := servmgr.Plan()
if err != nil {
return statusInternalError("%v", err)
}
planYAML, err := yaml.Marshal(plan)
if err != nil {
return statusInternalError("cannot serialize plan: %v", err)
}
return SyncResponse(string(planYAML))
}
func v1PostLayers(c *Command, r *http.Request, _ *UserState) Response {
var payload struct {
Action string `json:"action"`
Combine bool `json:"combine"`
Label string `json:"label"`
Format string `json:"format"`
Layer string `json:"layer"`
}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
return statusBadRequest("cannot decode request body: %v", err)
}
if payload.Action != "add" {
return statusBadRequest("invalid action %q", payload.Action)
}
if payload.Label == "" {
return statusBadRequest("label must be set")
}
if payload.Format != "yaml" {
return statusBadRequest("invalid format %q", payload.Format)
}
layer, err := plan.ParseLayer(0, payload.Label, []byte(payload.Layer))
if err != nil {
return statusBadRequest("cannot parse layer YAML: %v", err)
}
servmgr := overlordServiceManager(c.d.overlord)
if payload.Combine {
err = servmgr.CombineLayer(layer)
} else {
err = servmgr.AppendLayer(layer)
}
if err != nil {
if _, ok := err.(*servstate.LabelExists); ok {
return statusBadRequest("%v", err)
}
if _, ok := err.(*plan.FormatError); ok {
return statusBadRequest("%v", err)
}
return statusInternalError("%v", err)
}
return SyncResponse(true)
}