-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
148 lines (119 loc) · 3.15 KB
/
client.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
package uchiwa
import (
"fmt"
"github.com/sensu/uchiwa/uchiwa/helpers"
"github.com/sensu/uchiwa/uchiwa/logger"
)
func (u *Uchiwa) buildClientHistory(client map[string]interface{}, dc string, history []interface{}) []interface{} {
for _, h := range history {
m, ok := h.(map[string]interface{})
if !ok {
logger.Warningf("Could not assert this client history to an interface: %+v", h)
continue
}
m["client"] = client["name"].(string)
m["dc"] = dc
checkMap, ok := m["last_result"].(map[string]interface{})
if !ok {
logger.Warningf("Could not assert this check to a struct: %+v", m["last_result"])
continue
}
m["silenced"], m["silenced_by"] = helpers.IsCheckSilenced(checkMap, client, dc, u.Data.Silenced)
}
return history
}
// DeleteClient send a DELETE request to the /clients/*client* endpoint in order to delete a client
func (u *Uchiwa) DeleteClient(dc, name, invalidate, expire string) error {
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
return err
}
err = api.DeleteClient(name, invalidate, expire)
if err != nil {
logger.Warning(err)
return err
}
return nil
}
func (u *Uchiwa) findClient(name string) ([]interface{}, error) {
var clients []interface{}
for _, c := range u.Data.Clients {
m, ok := c.(map[string]interface{})
if !ok {
logger.Warningf("Could not assert this client to an interface %+v", c)
continue
}
if m["name"] == name {
clients = append(clients, m)
}
}
if len(clients) == 0 {
return nil, fmt.Errorf("Could not find any client with the name '%s'", name)
}
return clients, nil
}
// GetClient retrieves a specific client
func (u *Uchiwa) GetClient(dc, name string) (map[string]interface{}, error) {
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
return nil, err
}
client, err := api.GetClient(name)
if err != nil {
logger.Warning(err)
return nil, err
}
// lock results
u.Mu.Lock()
defer u.Mu.Unlock()
client["_id"] = fmt.Sprintf("%s/%s", dc, name)
client["dc"] = dc
client["silenced"] = helpers.IsClientSilenced(name, dc, u.Data.Silenced)
return client, nil
}
// GetClientHistory retrieves a specific client history
func (u *Uchiwa) GetClientHistory(dc, name string) ([]interface{}, error) {
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
return nil, err
}
h, err := api.GetClientHistory(name)
if err != nil {
logger.Warning(err)
return nil, err
}
// Get the client
client, err := u.GetClient(dc, name)
if err != nil {
// The error would already have been logged at this point
return nil, err
}
// lock results
u.Mu.Lock()
defer u.Mu.Unlock()
history := u.buildClientHistory(client, dc, h)
return history, nil
}
func (u *Uchiwa) UpdateClient(payload interface{}) error {
client, ok := payload.(map[string]interface{})
if !ok {
return fmt.Errorf("Unable to decode the payload")
}
dc, ok := client["dc"].(string)
if !ok {
return fmt.Errorf("Unable to identify the datacenter")
}
api, err := getAPI(u.Datacenters, dc)
if err != nil {
logger.Warning(err)
return err
}
_, err = api.UpdateClient(payload)
if err != nil {
return err
}
return nil
}