forked from ClusterLabs/hawk-apiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_constraints.go
63 lines (54 loc) · 1.5 KB
/
api_constraints.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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strings"
)
func handleApiConstraints(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 = "constraints"
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]/constraints
cib.Configuration.Constraints.URLType = "all"
} else {
// for url api/v[1-9]/constraints/{resid}
consId := urllist[3]
mapIdType := make(map[string]TypeIndex)
for li, litem := range cib.Configuration.Constraints.RscLocation {
mapIdType[litem.Id] = TypeIndex{"location", li}
}
for ci, citem := range cib.Configuration.Constraints.RscColocation {
mapIdType[citem.Id] = TypeIndex{"colocation", ci}
}
for oi, oitem := range cib.Configuration.Constraints.RscOrder {
mapIdType[oitem.Id] = TypeIndex{"order", oi}
}
val, ok := mapIdType[consId]
if ok {
cib.Configuration.Constraints.URLType = val.Type
cib.Configuration.Constraints.URLIndex = val.Index
} else {
http.Error(w, fmt.Sprintf("No route for %v.", r.URL.Path), 500)
return false
}
}
jsonData, jsonError := json.Marshal(&cib)
if jsonError != nil {
log.Error(jsonError)
return false
}
io.WriteString(w, string(jsonData)+"\n")
return true
}