-
Notifications
You must be signed in to change notification settings - Fork 23
/
agent.go
145 lines (130 loc) · 5.08 KB
/
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
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
package thousandeyes
import (
"encoding/json"
"fmt"
)
// Agents - list of agents
type Agents []Agent
// Agent - Agent struct
type Agent struct {
AgentID *int64 `json:"agentId,omitempty"`
AgentName *string `json:"agentName,omitempty"`
AgentType *string `json:"agentType,omitempty"`
CountryID *string `json:"countryId,omitempty"`
ClusterMembers *[]ClusterMember `json:"clusterMembers,omitempty"`
IPAddresses *[]string `json:"ipAddresses,omitempty"`
Groups *GroupLabels `json:"groups,omitempty"`
Location *string `json:"location,omitempty"`
ErrorDetails *[]AgentErrorDetails `json:"errorDetails,omitempty"`
Hostname *string `json:"hostname,omitempty"`
Prefix *string `json:"prefix,omitempty"`
Enabled *bool `json:"enabled,omitempty" te:"int-bool"`
Network *string `json:"network,omitempty"`
CreatedDate *string `json:"createdDate,omitempty"`
LastSeen *string `json:"lastSeen,omitempty"`
AgentState *string `json:"agentState,omitempty"`
VerifySslCertificates *bool `json:"verifySslCertificate,omitempty" te:"int-bool"`
KeepBrowserCache *bool `json:"keepBrowserCache,omitempty" te:"int-bool"`
Utilization *int `json:"utilization,omitempty"`
Ipv6Policy *string `json:"IPV6Policy,omitempty"`
TargetForTests *string `json:"targetForTests,omitempty"`
}
// ClusterMember - ClusterMember struct
type ClusterMember struct {
MemberID *int64 `json:"memberId,omitempty"`
Name *string `json:"name,omitempty"`
IPAddresses *[]string `json:"IPAddresses,omitempty"`
PublicIPAddresses *[]string `json:"PublicIPAddresses,omitempty"`
Prefix *string `json:"Prefix,omitempty"`
Network *string `json:"network,omitempty"`
LastSeen *string `json:"lastSeen,omitempty"`
AgentState *string `json:"agentState,omitempty"`
Utilization *int `json:"utilization,omitempty"`
TargetForTests *string `json:"targetForTests,omitempty"`
}
// AgentErrorDetails - Agent error details
type AgentErrorDetails struct {
Code *string `json:"code,omitempty"`
Description *string `json:"description,omitempty"`
}
// MarshalJSON implements the json.Marshaler interface. It ensures
// that ThousandEyes int fields that only use the values 0 or 1 are
// treated as booleans.
func (t Agent) MarshalJSON() ([]byte, error) {
type alias Agent
data, err := json.Marshal((alias)(t))
if err != nil {
return nil, err
}
return jsonBoolToInt(&t, data)
}
// UnmarshalJSON implements the json.Unmarshaler interface. It ensures
// that ThousandEyes int fields that only use the values 0 or 1 are
// treated as booleans.
func (t *Agent) UnmarshalJSON(data []byte) error {
type alias Agent
test := (*alias)(t)
data, err := jsonIntToBool(t, data)
if err != nil {
return err
}
return json.Unmarshal(data, &test)
}
// GetAgents - Get agents
func (c *Client) GetAgents() (*Agents, error) {
resp, err := c.get("/agents")
if err != nil {
return &Agents{}, err
}
var target map[string]Agents
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
agents := target["agents"]
return &agents, nil
}
// GetAgent - Get agent
func (c *Client) GetAgent(id int64) (*Agent, error) {
resp, err := c.get(fmt.Sprintf("/agents/%d", id))
if err != nil {
return nil, err
}
var target map[string][]Agent
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
agent := target["agents"][0]
return &agent, nil
}
// AddAgentsToCluster - add agent to cluster
func (c *Client) AddAgentsToCluster(cluster int, ids []int) (*[]Agent, error) {
resp, err := c.post(fmt.Sprintf("/agents/%d/add-to-cluster", cluster), ids, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to add agents to cluster, response code %d", resp.StatusCode)
}
var target map[string][]Agent
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
agent := target["agents"]
return &agent, nil
}
// RemoveAgentsFromCluster - remove agent from cluster
func (c *Client) RemoveAgentsFromCluster(cluster int, ids []int) (*[]Agent, error) {
resp, err := c.post(fmt.Sprintf("/agents/%d/remove-from-cluster", cluster), ids, nil)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to remove agents from cluster, response code %d", resp.StatusCode)
}
var target map[string][]Agent
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
agent := target["agents"]
return &agent, nil
}