-
Notifications
You must be signed in to change notification settings - Fork 74
/
terminate.go
54 lines (44 loc) · 1.28 KB
/
terminate.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
package daemon
import (
"encoding/json"
"net/http"
"github.com/testground/testground/pkg/api"
"github.com/testground/testground/pkg/logging"
"github.com/testground/testground/pkg/rpc"
)
func (d *Daemon) terminateHandler(engine api.Engine) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log := logging.S().With("req_id", r.Header.Get("X-Request-ID"))
log.Debugw("handle request", "command", "terminate")
defer log.Debugw("request handled", "command", "terminate")
tgw := rpc.NewOutputWriter(w, r)
var req api.TerminateRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
tgw.WriteError("terminate json decode", "err", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
var (
ctype api.ComponentType
ref string
)
switch {
case req.Builder != "" && req.Runner != "":
tgw.WriteError("cannot terminate a runner and a builder at the same time")
return
case req.Builder != "":
ctype = api.BuilderType
ref = req.Builder
case req.Runner != "":
ctype = api.RunnerType
ref = req.Runner
}
err = engine.DoTerminate(r.Context(), ctype, ref, tgw)
if err != nil {
tgw.WriteError("terminate error", "err", err.Error())
return
}
tgw.WriteResult("Done")
}
}