This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
desire_task_handler.go
72 lines (61 loc) · 1.74 KB
/
desire_task_handler.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
package handlers
import (
"encoding/json"
"errors"
"net/http"
"code.cloudfoundry.org/bbs"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/nsync/recipebuilder"
"code.cloudfoundry.org/runtimeschema/cc_messages"
)
type TaskHandler struct {
logger lager.Logger
recipeBuilders map[string]recipebuilder.RecipeBuilder
bbsClient bbs.Client
}
func NewTaskHandler(
logger lager.Logger,
bbsClient bbs.Client,
recipeBuilders map[string]recipebuilder.RecipeBuilder,
) TaskHandler {
return TaskHandler{
logger: logger,
recipeBuilders: recipeBuilders,
bbsClient: bbsClient,
}
}
func (h *TaskHandler) DesireTask(resp http.ResponseWriter, req *http.Request) {
logger := h.logger.Session("create-task", lager.Data{
"method": req.Method,
"request": req.URL.String(),
})
logger.Info("serving")
defer logger.Info("complete")
task := cc_messages.TaskRequestFromCC{}
err := json.NewDecoder(req.Body).Decode(&task)
if err != nil {
logger.Error("parse-task-request-failed", err)
resp.WriteHeader(http.StatusBadRequest)
return
}
builder, ok := h.recipeBuilders[task.Lifecycle]
if !ok {
logger.Error("builder-not-found", errors.New("no-builder"), lager.Data{"lifecycle": task.Lifecycle})
resp.WriteHeader(http.StatusBadRequest)
return
}
desiredTask, err := builder.BuildTask(&task)
if err != nil {
logger.Error("building-task-failed", err)
resp.WriteHeader(http.StatusBadRequest)
return
}
logger.Info("desiring-task", lager.Data{"task-guid": task.TaskGuid})
err = h.bbsClient.DesireTask(logger, task.TaskGuid, cc_messages.RunningTaskDomain, desiredTask)
if err != nil {
logger.Error("desire-task-failed", err)
resp.WriteHeader(http.StatusBadRequest)
return
}
resp.WriteHeader(http.StatusAccepted)
}