-
Notifications
You must be signed in to change notification settings - Fork 10
/
traverse.go
98 lines (84 loc) · 2.17 KB
/
traverse.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
package healthchecks
import (
"fmt"
)
func Traverse(s []StatusEndpoint, dependencies []string, action string, protocol string, aboutFilePath string, versionFilePath string, customData map[string]interface{}) string {
if action == "" {
action = "about"
}
// base case
if len(dependencies) == 0 {
// run the action
switch action {
case "about":
aboutResp, _ := About(s, protocol, aboutFilePath, versionFilePath, customData, APIV1, true)
return aboutResp
default:
sl := StatusList{
StatusList: []Status{
{
Description: "Unsupported action",
Result: CRITICAL,
Details: fmt.Sprintf("Unsupported traversal action '%s'", action),
},
},
}
return SerializeStatusList(sl, APIV1)
}
}
headDependency := dependencies[0]
headStatusEndpoint := FindStatusEndpoint(s, headDependency)
if headStatusEndpoint == nil {
sl := StatusList{
StatusList: []Status{
{
Description: "Can't traverse",
Result: CRITICAL,
Details: fmt.Sprintf("Status path '%s' is not registered", headDependency),
},
},
}
return SerializeStatusList(sl, APIV1)
}
if !headStatusEndpoint.IsTraversable {
sl := StatusList{
StatusList: []Status{
{
Description: "Can't traverse",
Result: CRITICAL,
Details: fmt.Sprintf("%s is not traversable", headStatusEndpoint.Name),
},
},
}
return SerializeStatusList(sl, APIV1)
}
if headStatusEndpoint.TraverseCheck == nil {
sl := StatusList{
StatusList: []Status{
{
Description: "Can't traverse",
Result: CRITICAL,
Details: fmt.Sprintf("%s does not have a TraverseCheck() function defined", headStatusEndpoint.Name),
},
},
}
return SerializeStatusList(sl, APIV1)
}
// found dependency, continue to traverse with the tail of the dependencies
tailDependencies := dependencies[1:]
resp, err := headStatusEndpoint.TraverseCheck.Traverse(tailDependencies, action)
if err != nil {
sl := StatusList{
StatusList: []Status{
{
Description: "Traverse",
Result: CRITICAL,
Details: err.Error(),
},
},
}
return SerializeStatusList(sl, APIV1)
} else {
return resp
}
}