diff --git a/sip_server.go b/sip_server.go new file mode 100644 index 0000000..9cccda3 --- /dev/null +++ b/sip_server.go @@ -0,0 +1,103 @@ +package thousandeyes + +import ( + "fmt" +) + +// SIPServer - SIPServer trace test +type SIPServer struct { + Agents []Agent `json:"agents,omitempty"` + AlertsEnabled int `json:"alertsEnabled,omitempty"` + 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 int `json:"enabled,omitempty"` + Groups []GroupLabel `json:"groups,omitempty"` + LiveShare int `json:"liveShare,omitempty"` + ModifiedBy string `json:"modifiedBy,omitempty"` + ModifiedDate string `json:"modifiedDate,omitempty"` + SavedEvent int `json:"savedEvent,omitempty"` + SharedWithAccounts []AccountGroup `json:"sharedWithAccounts,omitempty"` + TestID int `json:"testId,omitempty"` + TestName string `json:"testName,omitempty"` + Type string `json:"type,omitempty"` + Interval int `json:"interval,omitempty"` + BgpMeasurements int `json:"bgpMeasurements,omitempty"` + MtuMeasurements int `json:"mtuMeasurements,omitempty"` + NetworkMeasurements int `json:"networkMeasurements,omitempty"` + Port int `json:"port,omitempty"` + Protocol string `json:"protocol,omitempty"` + User string `json:"user,omitempty"` + Password string `json:"password,omitempty"` + AuthUser string `json:"authUser,omitempty"` + OptionsRegex string `json:"options_regex,omitempty"` + RegisterEnabled int `json:"registerEnabled,omitempty"` + SIPRegistrar string `json:"sipRegistrar,omitempty"` + SIPTargetTime int `json:"sipTargetTime,omitempty"` + SIPTimeLimit int `json:"sipTimeLimit,omitempty"` +} + +// AddAgent - Add agemt to sip server test +func (t *SIPServer) AddAgent(id int) { + agent := Agent{AgentId: id} + t.Agents = append(t.Agents, agent) +} + +// GetSIPServer - get sip server test +func (c *Client) GetSIPServer(id int) (*SIPServer, error) { + resp, err := c.get(fmt.Sprintf("/tests/%d", id)) + if err != nil { + return &SIPServer{}, err + } + 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 +} + +//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 int) 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 int, 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 +} diff --git a/sip_server_test.go b/sip_server_test.go new file mode 100644 index 0000000..208eb89 --- /dev/null +++ b/sip_server_test.go @@ -0,0 +1,285 @@ +package thousandeyes + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestClient_GetSIPServer(t *testing.T) { + out := `{"test":[{"createdDate":"2020-02-06 15:28:07","createdBy":"William Fleming (wfleming@grumpysysadm.com)","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"sip-server","interval":300,"alertsEnabled":1,"liveShare":0,"agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dns-trace/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}` + setup() + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + mux.HandleFunc("/tests/122621.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "GET", r.Method) + _, _ = w.Write([]byte(out)) + }) + + // Define expected values from the API (based on the JSON we print out above) + expected := SIPServer{ + + TestID: 122621, + Enabled: 1, + CreatedBy: "William Fleming (wfleming@grumpysysadm.com)", + CreatedDate: "2020-02-06 15:28:07", + SavedEvent: 0, + AlertsEnabled: 1, + TestName: "test123", + Type: "sip-server", + Interval: 300, + Agents: []Agent{ + { + AgentId: 48620, + AgentName: "Seattle, WA (Trial) - IPv6", + CountryId: "US", + IpAddresses: []string{"135.84.184.153"}, + Location: "Seattle Area", + Network: "Astute Hosting Inc. (AS 54527)", + Prefix: "135.84.184.0/22", + }, + }, + SharedWithAccounts: []AccountGroup{ + { + Aid: 176592, + Name: "Cloudreach", + }, + }, + APILinks: []ApiLink{ + { + Href: "https://api.thousandeyes.com/v6/tests/1226221", + Rel: "self", + }, + { + Href: "https://api.thousandeyes.com/v6/web/dns-trace/1226221", + Rel: "data", + }, + { + Href: "https://api.thousandeyes.com/v6/net/metrics/1226221", + Rel: "data", + }, + { + Href: "https://api.thousandeyes.com/v6/net/path-vis/1226221", + Rel: "data", + }, + { + Href: "https://api.thousandeyes.com/v6/net/bgp-metrics/1226221", + Rel: "data", + }, + }, + } + + res, err := client.GetSIPServer(122621) + teardown() + assert.Nil(t, err) + assert.Equal(t, &expected, res) +} + +func TestClient_GetSIPServerJsonError(t *testing.T) { + out := `{"test":[{"createdDate":"2020-02-06 15:28:07",createdBy":"William Fleming (wfleming@grumpysysadm.com)","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"sip-server","interval":300,"alertsEnabled":1,"liveShare":0,"agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"domain": "webex.com","dnsTransportProtocol": "UDP"}]"apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dns-trace/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}` + setup() + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + mux.HandleFunc("/tests/122621.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "GET", r.Method) + _, _ = w.Write([]byte(out)) + }) + _, err := client.GetSIPServer(122621) + assert.Error(t, err) + assert.EqualError(t, err, "Could not decode JSON response: invalid character 'c' looking for beginning of object key string") +} + +func TestClient_CreateSIPServer(t *testing.T) { + out := `{"test":[{"createdDate":"2020-02-06 15:28:07","createdBy":"William Fleming (wfleming@grumpysysadm.com)","user" : "usernameA", "password" : "secret", "authUser": "usernameB","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"sip-server","interval":300,"alertsEnabled":1,"liveShare":0,"agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dns-trace/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}` + setup() + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + mux.HandleFunc("/tests/sip-server/new.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "POST", r.Method) + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(out)) + }) + + // Define expected values from the API (based on the JSON we print out above) + // Define expected values from the API (based on the JSON we print out above) + expected := SIPServer{ + + TestID: 122621, + Enabled: 1, + CreatedBy: "William Fleming (wfleming@grumpysysadm.com)", + CreatedDate: "2020-02-06 15:28:07", + SavedEvent: 0, + TestName: "test123", + Type: "sip-server", + AuthUser: "usernameB", + Password: "secret", + User: "usernameA", + Interval: 300, + AlertsEnabled: 1, + Agents: []Agent{ + { + AgentId: 48620, + AgentName: "Seattle, WA (Trial) - IPv6", + CountryId: "US", + IpAddresses: []string{"135.84.184.153"}, + Location: "Seattle Area", + Network: "Astute Hosting Inc. (AS 54527)", + Prefix: "135.84.184.0/22", + }, + }, + SharedWithAccounts: []AccountGroup{ + { + Aid: 176592, + Name: "Cloudreach", + }, + }, + + APILinks: []ApiLink{ + { + Href: "https://api.thousandeyes.com/v6/tests/1226221", + Rel: "self", + }, + { + Href: "https://api.thousandeyes.com/v6/web/dns-trace/1226221", + Rel: "data", + }, + { + Href: "https://api.thousandeyes.com/v6/net/metrics/1226221", + Rel: "data", + }, + { + Href: "https://api.thousandeyes.com/v6/net/path-vis/1226221", + Rel: "data", + }, + { + Href: "https://api.thousandeyes.com/v6/net/bgp-metrics/1226221", + Rel: "data", + }, + }, + } + create := SIPServer{ + TestName: "test1", + AuthUser: "usernameB", + Password: "secret", + User: "usernameA", + Interval: 300, + } + res, err := client.CreateSIPServer(create) + teardown() + assert.Nil(t, err) + assert.Equal(t, &expected, res) +} + +func TestClient_DeleteSIPServer(t *testing.T) { + setup() + + mux.HandleFunc("/tests/sip-server/1/delete.json", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNoContent) + assert.Equal(t, "POST", r.Method) + }) + + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + id := 1 + err := client.DeleteSIPServer(id) + + if err != nil { + t.Fatal(err) + } +} + +func TestClient_UpdateSIPServer(t *testing.T) { + setup() + out := `{"test":[{"testId":1,"testName":"test123","type":"sip-server","user" : "usernameA", "password" : "secret", "authUser": "usernameB" }]}` + mux.HandleFunc("/tests/sip-server/1/update.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "POST", r.Method) + _, _ = w.Write([]byte(out)) + }) + + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + id := 1 + sipS := SIPServer{ + TestName: "test1", + AuthUser: "usernameB", + Password: "secret", + User: "usernameA", + } + res, err := client.UpdateSIPServer(id, sipS) + if err != nil { + t.Fatal(err) + } + expected := SIPServer{TestID: 1, TestName: "test123", Type: "sip-server", AuthUser: "usernameB", User: "usernameA", Password: "secret"} + assert.Equal(t, &expected, res) + +} + +func TestSIPServer_AddAgent(t *testing.T) { + test := SIPServer{TestName: "test", Agents: Agents{}} + expected := SIPServer{TestName: "test", Agents: []Agent{{AgentId: 1}}} + test.AddAgent(1) + assert.Equal(t, expected, test) +} + +func TestClient_GetSIPServerError(t *testing.T) { + setup() + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + mux.HandleFunc("/tests/sip-server/1.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "GET", r.Method) + w.WriteHeader(http.StatusBadRequest) + }) + + _, err := client.GetSIPServer(1) + teardown() + assert.Error(t, err) +} + +func TestClient_GetSIPServerStatusCode(t *testing.T) { + setup() + out := `{"test":[{"testId":1,"testName":"test123","type":"sip-server"}]}` + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + mux.HandleFunc("/tests/1.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "GET", r.Method) + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(out)) + }) + + _, err := client.GetSIPServer(1) + teardown() + assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}") +} + +func TestClient_CreateSIPServerStatusCode(t *testing.T) { + setup() + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + mux.HandleFunc("/tests/sip-server/new.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "POST", r.Method) + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(`{}`)) + }) + _, err := client.CreateSIPServer(SIPServer{}) + teardown() + assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}") +} + +func TestClient_UpdateSIPServerStatusCode(t *testing.T) { + setup() + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + mux.HandleFunc("/tests/sip-server/1/update.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "POST", r.Method) + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(`{}`)) + }) + _, err := client.UpdateSIPServer(1, SIPServer{}) + teardown() + assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}") +} + +func TestClient_DeleteSIPServerStatusCode(t *testing.T) { + setup() + var client = &Client{ApiEndpoint: server.URL, AuthToken: "foo"} + mux.HandleFunc("/tests/sip-server/1/delete.json", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "POST", r.Method) + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(`{}`)) + }) + err := client.DeleteSIPServer(1) + teardown() + assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}") +}