-
Notifications
You must be signed in to change notification settings - Fork 12
/
alert_agent.go
95 lines (80 loc) · 2.05 KB
/
alert_agent.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
package gobizfly
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
)
// Agents contains agent information.
type Agents struct {
Created string `json:"_created"`
Hostname string `json:"hostname"`
ID string `json:"_id"`
Name string `json:"name"`
Online bool `json:"online"`
ProjectID string `json:"project_id"`
Runtime string `json:"runtime"`
UserID string `json:"user_id"`
}
func (a *agents) ResourcePath() string {
return strings.Join([]string{agentsResourcePath}, "/")
}
func (a *agents) ItemPath(id string) string {
return strings.Join([]string{agentsResourcePath, id}, "/")
}
// List returns a list of agents.
func (a *agents) List(ctx context.Context, filters *string) ([]*Agents, error) {
req, err := a.client.NewRequest(ctx, http.MethodGet, cloudwatcherServiceName, a.ResourcePath(), nil)
if err != nil {
return nil, err
}
if filters != nil {
q := req.URL.Query()
q.Add("where", *filters)
req.URL.RawQuery = q.Encode()
}
resp, err := a.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data struct {
Agents []*Agents `json:"_items"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.Agents, nil
}
// Get an agent
func (a *agents) Get(ctx context.Context, id string) (*Agents, error) {
req, err := a.client.NewRequest(ctx, http.MethodGet, cloudwatcherServiceName, a.ItemPath(id), nil)
if err != nil {
return nil, err
}
resp, err := a.client.Do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
agent := &Agents{}
if err := json.NewDecoder(resp.Body).Decode(agent); err != nil {
return nil, err
}
return agent, nil
}
// Delete an agent
func (a *agents) Delete(ctx context.Context, id string) error {
req, err := a.client.NewRequest(ctx, http.MethodDelete, cloudwatcherServiceName, a.ItemPath(id), nil)
if err != nil {
return err
}
resp, err := a.client.Do(ctx, req)
if err != nil {
return err
}
_, _ = io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
}