Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve all detectors #233

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@ func (c *Client) GetDetector(ctx context.Context, id string) (*detector.Detector
return finalDetector, err
}

// GetDetectors gets all detectors.
atoulme marked this conversation as resolved.
Show resolved Hide resolved
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))
atoulme marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
14 changes: 14 additions & 0 deletions detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
131 changes: 131 additions & 0 deletions testdata/fixtures/detector/get_detectors.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}