-
Notifications
You must be signed in to change notification settings - Fork 6
/
handler_ui.go
214 lines (173 loc) · 5.27 KB
/
handler_ui.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package server
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"text/template"
"time"
"github.com/Masterminds/sprig"
"github.com/gbolo/vsummary/common"
"github.com/spf13/viper"
)
// default handler for landing page
func handlerUiIndex(w http.ResponseWriter, req *http.Request) {
// log time on debug
defer common.ExecutionTime(time.Now(), "handleUiIndex")
// for now just return a 302 to the VM endpoint
http.Redirect(w, req, "/ui/virtualmachines", http.StatusTemporaryRedirect)
}
func handlerUiVirtualmachines(w http.ResponseWriter, req *http.Request) {
// log time on debug
defer common.ExecutionTime(time.Now(), "handlerUiVirtualmachines")
// output the page
writeSummaryPage(w, &virtualMachineView)
}
func handlerUiEsxi(w http.ResponseWriter, req *http.Request) {
// log time on debug
defer common.ExecutionTime(time.Now(), "handlerUiEsxi")
// output the page
writeSummaryPage(w, &esxiView)
return
}
func handlerUiDatastore(w http.ResponseWriter, req *http.Request) {
// log time on debug
defer common.ExecutionTime(time.Now(), "handlerUiDatastore")
// output the page
writeSummaryPage(w, &datastoreView)
return
}
// return list of all template filenames
func findAllTemplates() (templateFiles []string, err error) {
files, err := ioutil.ReadDir(viper.GetString("server.templates_dir"))
if err != nil {
return
}
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".gohtml") {
templateFiles = append(templateFiles, viper.GetString("server.templates_dir")+"/"+filename)
}
}
return
}
// write generic vsummary table page
func writeSummaryPage(w http.ResponseWriter, uiview *UiView) {
// add version info for templates
uiview.Version = common.Version
uiview.CommitSHA = common.CommitSHA
// read in all templates
templateFiles, err := findAllTemplates()
if err != nil {
fmt.Fprintf(w, "Error reading template(s). See logs")
log.Errorf("error geting template files: %s", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
// parse and add function to templates
templates, err := template.New("index").
Funcs(sprig.TxtFuncMap()).
ParseFiles(templateFiles...)
if err == nil {
execErr := templates.ExecuteTemplate(w, "index", uiview)
if execErr != nil {
fmt.Fprintf(w, "Error executing template(s). See logs")
log.Errorf("template execute error: %s", execErr)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
} else {
fmt.Fprintf(w, "Error parsing template(s). See logs")
log.Errorf("template parse error: %s", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
return
}
// TODO: consolidate this with writeSummaryPage
func writePollerPage(w http.ResponseWriter, t string) {
// read in all templates
templateFiles, err := findAllTemplates()
if err != nil {
fmt.Fprintf(w, "Error reading template(s). See logs")
log.Errorf("error geting template files: %s", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
// parse and add function to templates
templates, err := template.New(t).
Funcs(sprig.TxtFuncMap()).
ParseFiles(templateFiles...)
if err == nil {
pollers, _ := backend.GetPollers()
// TODO: for now we only support global polling interval
for i := range pollers {
if pollers[i].Internal {
pollers[i].IntervalMin = viper.GetInt("poller.interval")
}
}
execErr := templates.ExecuteTemplate(w, t, UiView{
Title: "vCenter Pollers",
Pollers: pollers,
AjaxEndpoint: common.EndpointPoller,
Version: common.Version,
CommitSHA: common.CommitSHA,
})
if execErr != nil {
fmt.Fprintf(w, "Error executing template(s). See logs")
log.Errorf("template execute error: %s", execErr)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
} else {
fmt.Fprintf(w, "Error parsing template(s). See logs")
log.Errorf("template parse error: %s", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
return
}
// TODO: consolidate this with writeSummaryPage
func writePollerEditPage(w http.ResponseWriter, t string, id string) {
// read in all templates
templateFiles, err := findAllTemplates()
if err != nil {
fmt.Fprintf(w, "Error reading template(s). See logs")
log.Errorf("error geting template files: %s", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
// parse and add function to templates
templates, err := template.New(t).
Funcs(sprig.TxtFuncMap()).
ParseFiles(templateFiles...)
if err == nil {
poller, errPoller := backend.SelectPoller(id)
if err != nil {
fmt.Fprintf(w, "Error finding poller. See logs")
log.Errorf("template execute error: %s", errPoller)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
pollers := []common.Poller{poller}
execErr := templates.ExecuteTemplate(w, t, UiView{
Title: "vCenter Pollers",
Pollers: pollers,
AjaxEndpoint: common.EndpointPoller,
Version: common.Version,
CommitSHA: common.CommitSHA,
})
if execErr != nil {
fmt.Fprintf(w, "Error executing template(s). See logs")
log.Errorf("template execute error: %s", execErr)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
} else {
fmt.Fprintf(w, "Error parsing template(s). See logs")
log.Errorf("template parse error: %s", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
return
}