forked from remind101/empire
/
formations.go
97 lines (80 loc) · 1.84 KB
/
formations.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
89
90
91
92
93
94
95
96
97
package heroku
import (
"net/http"
"github.com/remind101/empire"
"github.com/remind101/empire/pkg/heroku"
"github.com/remind101/empire/server/auth"
)
type Formation heroku.Formation
type PatchFormationForm struct {
Updates []struct {
Process string `json:"process"` // Refers to process type
Quantity int `json:"quantity"`
Size *empire.Constraints `json:"size"`
} `json:"updates"`
}
func (h *Server) PatchFormation(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
var form PatchFormationForm
if err := Decode(r, &form); err != nil {
return err
}
app, err := h.findApp(r)
if err != nil {
return err
}
m, err := findMessage(r)
if err != nil {
return err
}
var updates []*empire.ProcessUpdate
for _, up := range form.Updates {
updates = append(updates, &empire.ProcessUpdate{
Process: up.Process,
Quantity: up.Quantity,
Constraints: up.Size,
})
}
ps, err := h.Scale(ctx, empire.ScaleOpts{
User: auth.UserFromContext(ctx),
App: app,
Updates: updates,
Message: m,
})
if err != nil {
return err
}
var resp []*Formation
for i, p := range ps {
up := updates[i]
resp = append(resp, &Formation{
Type: up.Process,
Quantity: p.Quantity,
Size: p.Constraints().String(),
})
}
w.WriteHeader(200)
return Encode(w, resp)
}
// ServeHTTPContext handles the http response
func (h *Server) GetFormation(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
app, err := h.findApp(r)
if err != nil {
return err
}
formation, err := h.ListScale(ctx, app)
if err != nil {
return err
}
var resp []*Formation
for name, proc := range formation {
resp = append(resp, &Formation{
Type: name,
Quantity: proc.Quantity,
Size: proc.Constraints().String(),
})
}
w.WriteHeader(200)
return Encode(w, resp)
}