Skip to content

Commit

Permalink
Added code to send a request to the endpoint that allows a user to re…
Browse files Browse the repository at this point in the history
…trieve all detectors.
  • Loading branch information
Bader Aljishi committed Dec 15, 2024
1 parent 395dfde commit 2ff842f
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
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.
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)
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"
}
}
}
]
}

0 comments on commit 2ff842f

Please sign in to comment.