-
Notifications
You must be signed in to change notification settings - Fork 23
/
sip_server.go
190 lines (171 loc) · 7.12 KB
/
sip_server.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package thousandeyes
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
)
// SIPAuthData - Authentication fields for SIP tests
type SIPAuthData struct {
AuthUser *string `json:"authUser,omitempty"`
Password *string `json:"password,omitempty"`
Port *int `json:"port,omitempty"`
Protocol *string `json:"protocol,omitempty"`
SIPProxy *string `json:"sipProxy,omitempty"`
SIPRegistrar *string `json:"sipRegistrar,omitempty"`
User *string `json:"user,omitempty"`
}
// SIPServer - SIPServer trace test
type SIPServer struct {
// Common test fields
AlertsEnabled *bool `json:"alertsEnabled,omitempty" te:"int-bool"`
AlertRules *[]AlertRule `json:"alertRules,omitempty"`
APILinks *[]APILink `json:"apiLinks,omitempty"`
CreatedBy *string `json:"createdBy,omitempty"`
CreatedDate *string `json:"createdDate,omitempty"`
Description *string `json:"description,omitempty"`
Enabled *bool `json:"enabled,omitempty" te:"int-bool"`
Groups *[]GroupLabel `json:"groups,omitempty"`
ModifiedBy *string `json:"modifiedBy,omitempty"`
ModifiedDate *string `json:"modifiedDate,omitempty"`
SavedEvent *bool `json:"savedEvent,omitempty" te:"int-bool"`
SharedWithAccounts *[]SharedWithAccount `json:"sharedWithAccounts,omitempty"`
TestID *int64 `json:"testId,omitempty"`
TestName *string `json:"testName,omitempty"`
Type *string `json:"type,omitempty"`
LiveShare *bool `json:"liveShare,omitempty" te:"int-bool"`
// Fields unique to this test
Agents *[]Agent `json:"agents,omitempty"`
BandwidthMeasurements *bool `json:"bandwidthMeasurements,omitempty" te:"int-bool"`
BGPMeasurements *bool `json:"bgpMeasurements,omitempty" te:"int-bool"`
Interval *int `json:"interval,omitempty"`
MTUMeasurements *bool `json:"mtuMeasurements,omitempty" te:"int-bool"`
NetworkMeasurements *bool `json:"networkMeasurements,omitempty" te:"int-bool"`
NumPathTraces *int `json:"numPathTraces,omitempty"`
OptionsRegex *string `json:"options_regex,omitempty"`
PathTraceMode *string `json:"pathTraceMode,omitempty"`
ProbeMode *string `json:"probeMode,omitempty"`
RegisterEnabled *bool `json:"registerEnabled,omitempty" te:"int-bool"`
SIPTargetTime *int `json:"sipTargetTime,omitempty"`
SIPTimeLimit *int `json:"sipTimeLimit,omitempty"`
TargetSIPCredentials *SIPAuthData `json:"targetSipCredentials,omitempty"`
UsePublicBGP *bool `json:"usePublicBgp,omitempty" te:"int-bool"`
}
// 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 SIPServer) MarshalJSON() ([]byte, error) {
type aliasTest SIPServer
data, err := json.Marshal((aliasTest)(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 *SIPServer) UnmarshalJSON(data []byte) error {
type aliasTest SIPServer
test := (*aliasTest)(t)
data, err := jsonIntToBool(t, data)
if err != nil {
return err
}
return json.Unmarshal(data, &test)
}
// AddAgent - Add agemt to sip server test
func (t *SIPServer) AddAgent(id int64) {
agent := Agent{AgentID: Int64(id)}
*t.Agents = append(*t.Agents, agent)
}
// AddAlertRule - Adds an alert to agent test
func (t *SIPServer) AddAlertRule(id int64) {
alertRule := AlertRule{RuleID: Int64(id)}
*t.AlertRules = append(*t.AlertRules, alertRule)
}
// GetSIPServer - get sip server test
func (c *Client) GetSIPServer(id int64) (*SIPServer, error) {
resp, err := c.get(fmt.Sprintf("/tests/%d", id))
if err != nil {
return &SIPServer{}, err
}
// Duplicate http response so we can read JSON directly
// and still use the normal client interaction to process
// the http response.
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Could not decode HTTP response: %v", err)
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(respBody))
var target map[string][]SIPServer
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
// A design flaw in ThousandEyes V6 API results in field on sip-server tests which
// should be part of a targetSipCredentials object (matching the behavior of voice-call
// tests) are instead part of the sip-server test object itself.
// As this is not intended to be fixed until V7, The solution will be to have a
// separate struct for reads, which will be converted before being passed.
resp.Body = ioutil.NopCloser(bytes.NewBuffer(respBody))
var sipTarget map[string][]SIPAuthData
if dErr := c.decodeJSON(resp, &sipTarget); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
for i := range target["test"] {
if sipTarget["test"][i].AuthUser != nil {
sipAuth := &SIPAuthData{
AuthUser: sipTarget["test"][i].AuthUser,
Password: sipTarget["test"][i].Password,
Port: sipTarget["test"][i].Port,
Protocol: sipTarget["test"][i].Protocol,
SIPProxy: sipTarget["test"][i].SIPProxy,
SIPRegistrar: sipTarget["test"][i].SIPRegistrar,
User: sipTarget["test"][i].User,
}
target["test"][i].TargetSIPCredentials = sipAuth
}
}
return &target["test"][0], nil
}
// CreateSIPServer - Create sip server test
func (c Client) CreateSIPServer(t SIPServer) (*SIPServer, error) {
resp, err := c.post("/tests/sip-server/new", t, nil)
if err != nil {
return &t, err
}
if resp.StatusCode != 201 {
return &t, fmt.Errorf("failed to create sip-server test, response code %d", resp.StatusCode)
}
var target map[string][]SIPServer
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
return &target["test"][0], nil
}
// DeleteSIPServer - delete sip server test
func (c *Client) DeleteSIPServer(id int64) error {
resp, err := c.post(fmt.Sprintf("/tests/sip-server/%d/delete", id), nil, nil)
if err != nil {
return err
}
if resp.StatusCode != 204 {
return fmt.Errorf("failed to delete sip test, response code %d", resp.StatusCode)
}
return nil
}
// UpdateSIPServer - - update sip server test
func (c *Client) UpdateSIPServer(id int64, t SIPServer) (*SIPServer, error) {
resp, err := c.post(fmt.Sprintf("/tests/sip-server/%d/update", id), t, nil)
if err != nil {
return &t, err
}
if resp.StatusCode != 200 {
return &t, fmt.Errorf("failed to update test, response code %d", resp.StatusCode)
}
var target map[string][]SIPServer
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}
return &target["test"][0], nil
}