David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "encoding/json" |
| 6 | "github.com/gorilla/mux" |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 7 | "net/http" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type RequestInfo struct { |
| 12 | Id string |
| 13 | Name string |
| 14 | Ip string |
| 15 | Mac string |
| 16 | } |
| 17 | |
| 18 | func (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 | |
| 36 | func (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 | |
| 46 | func (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. Bainbridge | d86d96d | 2016-06-01 17:28:46 -0700 | [diff] [blame] | 50 | 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. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 55 | w.WriteHeader(http.StatusBadRequest) |
| 56 | return |
| 57 | } |
| 58 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 59 | role, err := c.GetRole(&info) |
| 60 | if err != nil { |
| 61 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 62 | return |
| 63 | } |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 64 | err = c.dispatcher.Dispatch(&info, role, c.config.Script) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 65 | if err != nil { |
| 66 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | w.WriteHeader(http.StatusAccepted) |
| 71 | } |
| 72 | |
| 73 | func (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 | |
| 83 | func (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. Bainbridge | 8352c59 | 2016-06-02 12:48:37 -0700 | [diff] [blame] | 104 | |
| 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. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 114 | w.Write(bytes) |
| 115 | } |