Skip to content

Commit 147d356

Browse files
authored
Merge pull request Cloud-Foundations#167 from rgooch/master
cmd/mdbd: support listing machines in HTML table format and selecting dataSourceType.
2 parents 71e0b29 + 320b948 commit 147d356

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

cmd/mdbd/httpd.go

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/Cloud-Foundations/Dominator/lib/format"
1414
"github.com/Cloud-Foundations/Dominator/lib/html"
1515
"github.com/Cloud-Foundations/Dominator/lib/json"
16+
"github.com/Cloud-Foundations/Dominator/lib/mdb"
1617
"github.com/Cloud-Foundations/Dominator/lib/url"
1718
)
1819

@@ -100,18 +101,21 @@ func (s *httpServer) statusHandler(w http.ResponseWriter, req *http.Request) {
100101
totalFilteredMachines += numFilteredMachines
101102
totalRawMachines += numRawMachines
102103
columns = append(columns,
103-
makeNumMachinesText(numFilteredMachines, numRawMachines))
104+
fmt.Sprintf("<a href=\"showMdb?dataSourceType=%s\">%s</a>",
105+
genInfo.driverName,
106+
makeNumMachinesText(numFilteredMachines, numRawMachines)))
104107
tw.WriteRow("", "", columns...)
105108
}
106109
columns := make([]string, s.generators.maxArgs+2)
107110
columns[0] = "<b>TOTAL</b>"
108-
columns[s.generators.maxArgs+1] = makeNumMachinesText(totalFilteredMachines,
109-
totalRawMachines)
111+
columns[s.generators.maxArgs+1] = fmt.Sprintf("<a href=\"showMdb\">%s</a>",
112+
makeNumMachinesText(totalFilteredMachines, totalRawMachines))
110113
tw.WriteRow("", "", columns...)
111114
tw.Close()
112-
fmt.Fprintf(writer, "Number of machines: <a href=\"showMdb\">%d</a>",
115+
fmt.Fprintf(writer, "Number of machines: <a href=\"showMdb\">%d</a> (",
113116
len(s.mdb.Machines))
114-
fmt.Fprintln(writer, " <a href=\"showMdb?output=text\">(text)</a><br>")
117+
fmt.Fprint(writer, "<a href=\"showMdb?output=json\">JSON</a>")
118+
fmt.Fprintln(writer, ", <a href=\"showMdb?output=text\">text)</a><br>")
115119
if pauseTableLength := s.pauseTable.len(); pauseTableLength > 0 {
116120
fmt.Fprintf(writer,
117121
"Number of paused machines: <a href=\"showPaused\">%d</a> (",
@@ -169,16 +173,66 @@ func (s *httpServer) showMachineHandler(w http.ResponseWriter,
169173
}
170174

171175
func (s *httpServer) showMdbHandler(w http.ResponseWriter, req *http.Request) {
176+
if err := req.ParseForm(); err != nil {
177+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
178+
w.WriteHeader(http.StatusBadRequest)
179+
return
180+
}
172181
parsedQuery := url.ParseQuery(req.URL)
182+
selectedDataSourceType := req.FormValue("dataSourceType")
173183
writer := bufio.NewWriter(w)
174184
defer writer.Flush()
185+
var machines []*mdb.Machine
186+
if selectedDataSourceType == "" {
187+
machines = s.mdb.Machines
188+
} else {
189+
machines = make([]*mdb.Machine, 0, len(s.mdb.Machines))
190+
for _, machine := range s.mdb.Machines {
191+
if selectedDataSourceType == machine.DataSourceType {
192+
machines = append(machines, machine)
193+
}
194+
}
195+
}
175196
switch parsedQuery.OutputType() {
197+
case url.OutputTypeHtml:
198+
fieldArgs := []string{
199+
"Hostname",
200+
"IP Address",
201+
"Data Source Type",
202+
"Required Image",
203+
"Planned Image",
204+
}
205+
fmt.Fprintln(writer, "<title>MDB Machines</title>")
206+
fmt.Fprintln(writer, `<style>
207+
table, th, td {
208+
border-collapse: collapse;
209+
}
210+
</style>`)
211+
fmt.Fprintln(writer, "<body>")
212+
fmt.Fprintln(writer, `<table border="1" style="width:100%">`)
213+
tw, _ := html.NewTableWriter(writer, true, fieldArgs...)
214+
for _, machine := range machines {
215+
columns := []string{
216+
fmt.Sprintf("<a href=\"showMachine?%s\">%s</a>",
217+
machine.Hostname, machine.Hostname),
218+
machine.IpAddress,
219+
fmt.Sprintf("<a href=\"showMdb?dataSourceType=%s\">%s</a>",
220+
machine.DataSourceType, machine.DataSourceType),
221+
machine.RequiredImage,
222+
machine.PlannedImage,
223+
}
224+
tw.WriteRow("", "", columns...)
225+
}
226+
tw.Close()
227+
fmt.Fprintln(writer, "</body>")
228+
case url.OutputTypeJson:
229+
mdbData := *s.mdb
230+
mdbData.Machines = machines
231+
json.WriteWithIndent(writer, " ", mdbData)
176232
case url.OutputTypeText:
177-
for _, machine := range s.mdb.Machines {
233+
for _, machine := range machines {
178234
fmt.Fprintln(writer, machine.Hostname)
179235
}
180-
case url.OutputTypeHtml, url.OutputTypeJson:
181-
json.WriteWithIndent(writer, " ", s.mdb)
182236
}
183237
}
184238

0 commit comments

Comments
 (0)