blob: 42946dac6721b63a16522314508da9c07814aeb2 [file] [log] [blame]
David K. Bainbridgef0da8732016-06-01 16:15:37 -07001package main
2
3import (
4 "bufio"
5 "encoding/json"
6 "github.com/gorilla/mux"
David K. Bainbridgef0da8732016-06-01 16:15:37 -07007 "net/http"
8 "strings"
9)
10
11type RequestInfo struct {
12 Id string
13 Name string
14 Ip string
15 Mac string
16}
17
18func (c *Context) GetRole(info *RequestInfo) (string, error) {
19 if c.config.RoleSelectorURL == "" {
20 return c.config.DefaultRole, nil
21 }
22 r, err := http.Get(c.config.RoleSelectorURL)
23 if err != nil {
24 return "", err
25 }
26
27 s := bufio.NewScanner(r.Body)
28 defer r.Body.Close()
29 role := strings.TrimSpace(s.Text())
30 if role == "" {
31 return c.config.DefaultRole, nil
32 }
33 return role, nil
34}
35
36func (c *Context) validateData(info *RequestInfo) bool {
37 if strings.TrimSpace(info.Id) == "" ||
38 strings.TrimSpace(info.Name) == "" ||
39 strings.TrimSpace(info.Ip) == "" ||
40 strings.TrimSpace(info.Mac) == "" {
41 return false
42 }
43 return true
44}
45
46func (c *Context) ProvisionRequestHandler(w http.ResponseWriter, r *http.Request) {
47 var info RequestInfo
48 decoder := json.NewDecoder(r.Body)
49 defer r.Body.Close()
David K. Bainbridged86d96d2016-06-01 17:28:46 -070050 if err := decoder.Decode(&info); err != nil {
51 http.Error(w, err.Error(), http.StatusBadRequest)
52 return
53 }
54 if !c.validateData(&info) {
David K. Bainbridgef0da8732016-06-01 16:15:37 -070055 w.WriteHeader(http.StatusBadRequest)
56 return
57 }
58
David K. Bainbridgef0da8732016-06-01 16:15:37 -070059 role, err := c.GetRole(&info)
60 if err != nil {
61 http.Error(w, err.Error(), http.StatusInternalServerError)
62 return
63 }
David K. Bainbridge38501582016-06-01 18:15:45 -070064 err = c.dispatcher.Dispatch(&info, role, c.config.Script)
David K. Bainbridgef0da8732016-06-01 16:15:37 -070065 if err != nil {
66 http.Error(w, err.Error(), http.StatusInternalServerError)
67 return
68 }
69
70 w.WriteHeader(http.StatusAccepted)
71}
72
73func (c *Context) ListRequestsHandler(w http.ResponseWriter, r *http.Request) {
74 list, err := c.storage.List()
75 bytes, err := json.Marshal(list)
76 if err != nil {
77 http.Error(w, err.Error(), http.StatusInternalServerError)
78 return
79 }
80 w.Write(bytes)
81}
82
83func (c *Context) QueryStatusHandler(w http.ResponseWriter, r *http.Request) {
84 vars := mux.Vars(r)
85 id, ok := vars["nodeid"]
86 if !ok || strings.TrimSpace(id) == "" {
87 w.WriteHeader(http.StatusBadRequest)
88 return
89 }
90 s, err := c.storage.Get(id)
91 if err != nil {
92 http.Error(w, err.Error(), http.StatusInternalServerError)
93 return
94 }
95 if s == nil {
96 w.WriteHeader(http.StatusNotFound)
97 return
98 }
99 bytes, err := json.Marshal(s)
100 if err != nil {
101 http.Error(w, err.Error(), http.StatusInternalServerError)
102 return
103 }
David K. Bainbridge8352c592016-06-02 12:48:37 -0700104
105 switch s.Status {
106 case Pending, Running:
107 w.WriteHeader(http.StatusAccepted)
108 case Complete:
109 w.WriteHeader(http.StatusOK)
110 default:
111 w.WriteHeader(http.StatusInternalServerError)
112 }
113
David K. Bainbridgef0da8732016-06-01 16:15:37 -0700114 w.Write(bytes)
115}