-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhandlers.go
181 lines (158 loc) · 5.24 KB
/
handlers.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package backend
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
// returns version information
func handlerVersion(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, getVersionResponse())
}
// handler for all environments
func handlerEnvAll(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
// refresh the data
if err := refreshTable(); err != nil {
log.Errorf("refresh error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
// get vars from request to determine if environment id was specified
vars := mux.Vars(req)
group := vars["group"]
envAllResponse := struct {
EnvList envList `json:"envList" groups:"summary,details"`
TotalBillsAccrued string `json:"totalBillsAccrued,omitempty" groups:"summary,details"`
TotalBillsSaved string `json:"totalBillsSaved,omitempty" groups:"summary,details"`
}{
EnvList: cachedTable,
}
if experimentalEnabled {
envAllResponse.TotalBillsAccrued = fmt.Sprintf("%.02f", totalBillsAccrued)
envAllResponse.TotalBillsSaved = fmt.Sprintf("%.02f", totalBillsSaved)
}
// prepare result and return it
if response, err := getMarshaledResponse(envAllResponse, group); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
} else {
w.Write(response)
}
}
// handler for single environment
func handlerEnvSingle(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
// refresh the data
if err := refreshTable(); err != nil {
log.Errorf("refresh error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
return
}
// get vars from request to determine if environment id was specified
vars := mux.Vars(req)
envID := vars["env-id"]
group := vars["group"]
// filter this environment id
envData, found := getEnvironmentByID(envID)
if !found {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "{\"error\":\"environment not found\"}\n")
return
}
response, err := getMarshaledResponse(envData, group)
// return filtered result
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
} else {
w.Write(response)
}
}
// handler for power toggling an environment
func handlerEnvPowerToggle(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
// re-calculate env bills before toggling
if experimentalEnabled {
calculateEnvBills()
}
// get vars from request to determine environment
vars := mux.Vars(req)
envID := vars["env-id"]
state := vars["state"]
switch state {
case "start":
response, err := startupEnv(envID)
writeJSONResponse(w, err, response)
case "stop":
response, err := shutdownEnv(envID)
writeJSONResponse(w, err, response)
default:
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "{\"error\":\"invalid request\"}\n")
}
}
// handler for power toggling an instance
func handlerInstancePowerToggle(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
// get vars from request to determine environment
vars := mux.Vars(req)
id := vars["instance-id"]
state := vars["state"]
if state == "start" || state == "stop" {
response, err := toggleInstance(id, state)
writeJSONResponse(w, err, response)
} else {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "{\"error\":\"invalid request\"}\n")
}
}
// handler to refresh cache
func handlerRefresh(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := refreshTable(); err != nil {
log.Errorf("refresh error: %v", err)
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
} else {
log.Info("refresh successful")
fmt.Fprint(w, "{\"status\":\"OK\"}\n")
}
}
// handler for displaying relevant config
func handlerConfig(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
configuredOption := map[string]interface{}{
"aws_polling_interval": viper.GetInt("aws.polling_interval"),
"aws_regions": awsRegions,
"aws_required_tag_key": requiredTagKey,
"aws_required_tag_value": requiredTagValue,
"aws_environment_tag_key": environmentTagKey,
"aws_max_instances_to_shutdown": maxInstancesToShutdown,
"aws_ignore_instance_types": instanceTypeIgnore,
"aws_ignore_environments": envNameIgnore,
"slack_enabled": slackEnabled,
"mock_enabled": mockEnabled,
"mock_delay": viper.GetBool("mock.delay"),
"mock_errors": viper.GetBool("mock.errors"),
}
jsonResponse, _ := json.MarshalIndent(configuredOption, "", " ")
fmt.Fprint(w, string(jsonResponse))
}
// wrapper for json responses with error support
func writeJSONResponse(w http.ResponseWriter, err error, response []byte) {
if err == nil {
w.WriteHeader(http.StatusOK)
w.Write(response)
} else {
w.WriteHeader(http.StatusInternalServerError)
if len(response) > 0 {
w.Write(response)
} else {
fmt.Fprintf(w, "{\"error\":\"%v\"}\n", err)
}
}
}