-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
72 lines (62 loc) · 1.69 KB
/
keys.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 main
import (
"encoding/json"
"net/http"
"strconv"
"github.com/grimdork/foreman/api"
)
// keysGet endpoint.
func (srv *Server) keysGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
list, err := srv.GetKeys()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
return
}
keys := api.KeyList{Keys: list}
if len(keys.Keys) == 0 {
w.WriteHeader(http.StatusNoContent)
}
err = json.NewEncoder(w).Encode(keys)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
}
}
// keyPost endpoint.
func (srv *Server) keyPost(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id := r.Header.Get("keyid")
value := r.Header.Get("value")
if id == "" || value == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"missing keyid or value"}`))
return
}
admin := r.Header.Get("admin")
adminbool := false
if admin != "" {
adminbool, _ = strconv.ParseBool(admin)
}
err := srv.SetKey(id, value, adminbool)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
}
}
// keyDelete endpoint.
func (srv *Server) keyDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id := r.Header.Get("keyid")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":"missing keyid"}`))
return
}
err := srv.DeleteKey(id)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":"` + err.Error() + `"}`))
}
}