forked from ClusterLabs/hawk-apiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_nodes.go
57 lines (49 loc) · 1.18 KB
/
api_nodes.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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strings"
)
func handleApiNodes(w http.ResponseWriter, r *http.Request, cib_data string) bool {
// parse xml into Cib struct
var cib Cib
err := xml.Unmarshal([]byte(cib_data), &cib)
if err != nil {
log.Error(err)
return false
}
cib.Configuration.URLType = "nodes"
w.Header().Set("Content-Type", "application/json")
urllist := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(urllist) == 3 {
// for url api/v[1-9]/nodes
cib.Configuration.Nodes.URLType = "all"
} else {
// for url api/v[1-9]/nodes/{nodeid}
cib.Configuration.Nodes.URLType = "node"
nodeIndex := urllist[3]
var index int = -1
for i, item := range cib.Configuration.Nodes.Node {
if nodeIndex == item.Uname || nodeIndex == item.Id {
index = i
break
}
}
if index == -1 {
http.Error(w, fmt.Sprintf("No route for %v.", r.URL.Path), 500)
return false
}
cib.Configuration.Nodes.URLIndex = index
}
jsonData, jsonError := json.Marshal(&cib)
if jsonError != nil {
log.Error(jsonError)
return false
}
io.WriteString(w, string(jsonData)+"\n")
return true
}