diff --git a/detector.go b/detector.go index c5a0f52..5837b11 100644 --- a/detector.go +++ b/detector.go @@ -119,6 +119,33 @@ func (c *Client) GetDetector(ctx context.Context, id string) (*detector.Detector return finalDetector, err } +// GetDetectors gets all detectors. +func (c *Client) GetDetectors(ctx context.Context, limit int, name string, offset int) ([]*detector.Detector, error) { + params := url.Values{} + params.Add("limit", strconv.Itoa(limit)) + params.Add("name", name) + params.Add("offset", strconv.Itoa(offset)) + resp, err := c.doRequest(ctx, "GET", DetectorAPIURL, nil, nil) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if err = newResponseError(resp, http.StatusOK); err != nil { + return nil, err + } + + var allDetectors = struct{ + Count int `json:"count"` + Results []*detector.Detector `json:"results"` + }{} + + err = json.NewDecoder(resp.Body).Decode(&allDetectors) + _, _ = io.Copy(ioutil.Discard, resp.Body) + + return allDetectors.Results, err +} + // UpdateDetector updates a detector. func (c *Client) UpdateDetector(ctx context.Context, id string, detectorRequest *detector.CreateUpdateDetectorRequest) (*detector.Detector, error) { payload, err := json.Marshal(detectorRequest) diff --git a/detector_test.go b/detector_test.go index 9d3957c..51c615a 100644 --- a/detector_test.go +++ b/detector_test.go @@ -108,6 +108,20 @@ func TestGetDetector(t *testing.T) { assert.Equal(t, result.Name, "string", "Name does not match") } +func TestGetDetectors(t *testing.T) { + teardown := setup() + defer teardown() + + mux.HandleFunc("/v2/detector", verifyRequest(t, "GET", true, http.StatusOK, nil, "detector/get_detectors.json")) + + result, err := client.GetDetectors(context.Background(), 1000, "", 0) + assert.NoError(t, err, "Unexpected error getting all detectors") + assert.Equal(t, len(result), 2, "Incorrect number of detectors returned") + assert.Equal(t, result[0].Id, "string", "ID does not match") + assert.Equal(t, result[0].ProgramText, "string", "Program text field does not match") + assert.Equal(t, result[1].Id, "string1", "ID does not match") +} + func TestGetMissingDetector(t *testing.T) { teardown := setup() defer teardown() diff --git a/testdata/fixtures/detector/get_detectors.json b/testdata/fixtures/detector/get_detectors.json new file mode 100644 index 0000000..9e63f79 --- /dev/null +++ b/testdata/fixtures/detector/get_detectors.json @@ -0,0 +1,131 @@ +{ + "count": 607, + "results": [ + { + "authorizedWriters": { + "teams": [ + "string" + ], + "users": [ + "string" + ] + }, + "created": 1533676829310, + "creator": "\"ZZZZZZZZZZZ\" if the system created the detector", + "customProperties": "string", + "description": "string", + "id": "string", + "parentDetectorId": "string", + "detectorOrigin": "Standard", + "labelResolutions": { + "DetectorA": 2000, + "DetectorB": 2000 + }, + "lastUpdated": 1533676829310, + "lastUpdatedBy": "string", + "locked": true, + "maxDelay": 60000, + "name": "string", + "overMTSLimit": true, + "programText": "string", + "rules": [ + { + "description": "string", + "detectLabel": "string", + "disabled": true, + "notifications": [ + { + "channel": "limit-notifications", + "credentialId": "ZZZZZZZAAAA", + "type": "Slack" + } + ], + "parameterizedBody": "string", + "parameterizedSubject": "string", + "runbookUrl": "string", + "severity": "Critical" + } + ], + "tags": [ + "string" + ], + "teams": [ + "string" + ], + "visualizationOptions": { + "disableSampling": true, + "showDataMarkers": true, + "showEventLines": true, + "time": { + "end": 0, + "range": 0, + "start": 0, + "type": "absolute" + } + } + }, + { + "authorizedWriters": { + "teams": [ + "string" + ], + "users": [ + "string" + ] + }, + "created": 1533676829319, + "creator": "\"AAAAAAAAAA\" if the system created the detector", + "customProperties": "string", + "description": "string", + "id": "string1", + "parentDetectorId": "string", + "detectorOrigin": "Standard", + "labelResolutions": { + "DetectorA": 3000, + "DetectorB": 5000 + }, + "lastUpdated": 1533676829319, + "lastUpdatedBy": "string", + "locked": true, + "maxDelay": 60000, + "name": "string", + "overMTSLimit": true, + "programText": "string", + "rules": [ + { + "description": "string", + "detectLabel": "string", + "disabled": true, + "notifications": [ + { + "channel": "limit-notifications", + "credentialId": "ZZZZZZZAAAA", + "type": "Slack" + } + ], + "parameterizedBody": "string", + "parameterizedSubject": "string", + "runbookUrl": "string", + "severity": "Critical" + } + ], + "tags": [ + "string" + ], + "teams": [ + "string" + ], + "visualizationOptions": { + "disableSampling": true, + "showDataMarkers": true, + "showEventLines": true, + "time": { + "end": 0, + "range": 0, + "start": 0, + "type": "absolute" + } + } + } + ] +}