This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
handle_deployments.go
106 lines (99 loc) · 2.43 KB
/
handle_deployments.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"encoding/json"
"errors"
"fmt"
"html"
"net/http"
"strings"
)
// TOOD: refactor
func DeploymentsHandlerFunc(alerter *Alerter) HandlerFunc {
return func(w *JsonResponseWriter, r *http.Request) {
lookup := html.EscapeString(r.URL.Path)
urlParts := strings.Split(lookup, "/")
if urlParts[0] == "" {
if strings.ToUpper(r.Method) == "POST" {
RegisterDeployment(w, r)
} else {
UnknownEndpoint(w, r)
}
} else if len(urlParts) == 1 && strings.ToUpper(r.Method) == "GET" && urlParts[0] != "" {
GetDeployment(w, r, urlParts[0], alerter)
} else {
deployment, err := LookupDeploymentById(urlParts[0])
if err != nil {
w.WriteError(errors.New("deployment not found"))
return
}
if len(urlParts) == 1 {
if strings.ToUpper(r.Method) == "DELETE" {
UnregisterDeployment(w, r, deployment)
} else {
UnknownEndpoint(w, r)
}
} else if strings.Contains(lookup, "/checks") {
if strings.ToUpper(r.Method) == "GET" {
if len(urlParts) == 2 {
GetDeploymentChecks(w, r, deployment)
} else {
GetDeploymentCheck(w, r, deployment, urlParts[2])
}
} else {
UnknownEndpoint(w, r)
}
} else {
UnknownEndpoint(w, r)
}
}
}
}
func GetDeployment(w *JsonResponseWriter, r *http.Request, deployment string, alerter *Alerter) {
alerts, err := alerter.GetAll(fmt.Sprintf("%s:*", deployment))
if err != nil {
w.WriteError(err)
} else {
w.WriteJson(alerts)
}
}
func RegisterDeployment(w *JsonResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var deployment Deployment
err := decoder.Decode(&deployment)
if err != nil {
w.WriteError(err)
return
}
if err = deployment.Validate(); err != nil {
w.WriteError(err)
return
}
if err = deployment.Save(); err != nil {
w.WriteError(err)
} else {
w.WriteOk(201)
}
}
func UnregisterDeployment(w *JsonResponseWriter, r *http.Request, deployment Deployment) {
if err := deployment.Delete(); err != nil {
w.WriteError(err)
} else {
w.WriteOk(200)
}
}
func GetDeploymentChecks(w *JsonResponseWriter, r *http.Request, deployment Deployment) {
checks, err := deployment.CurrentChecks()
if err != nil {
w.WriteError(err)
} else {
w.WriteJson(checks)
}
}
func GetDeploymentCheck(w *JsonResponseWriter, r *http.Request, deployment Deployment, name string) {
check, err := deployment.CheckByName(name)
if err != nil {
w.WriteError(err)
} else {
w.WriteJson(check)
}
}