Skip to content

Commit 05364aa

Browse files
authored
feat: Implement /check/query endpoint as QueryCheck (#388)
* feat: Implement /check/query endpoint as QueryCheck Adds a new method `QueryCheck` that is a wrapper for the [/check/query](https://synthetic-monitoring-api-dev.grafana-dev.net/api/v1/swagger#/operations/GET_/api/v1/check/query) api end point. It accepts a job and a target which gets added as query parameters to the url. QueryCheck returns a single check that gets matched. Implements a basic set of tests that emulates the api end filtering checks by job and target. Ensures that it handles the case where no checks are matched. * Update QueryCheck to handle missing job/target Return an error from `QueryCheck` if job or target are empty strings. Refactor the unit tests to handle 404 errors and validate that job and target cannot be set. * Update Query method documentation * Fix linting errors
1 parent 3ace3c4 commit 05364aa

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

smapi.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,30 @@ func (h *Client) ListChecks(ctx context.Context) ([]synthetic_monitoring.Check,
505505
return result, nil
506506
}
507507

508+
// QueryCheck returns a Synthetic Monitoring check for the
509+
// authenticated tenant that matches the job and target passed in.
510+
// Job and Target must be set to non empty strings.
511+
func (h *Client) QueryCheck(ctx context.Context, job string, target string) (*synthetic_monitoring.Check, error) {
512+
if job == "" || target == "" {
513+
return nil, fmt.Errorf("check query request: target and job must be set")
514+
}
515+
if err := h.requireAuthToken(); err != nil {
516+
return nil, err
517+
}
518+
519+
resp, err := h.Get(ctx, fmt.Sprintf("/check/query?job=%s&target=%s", job, target), true, nil)
520+
if err != nil {
521+
return nil, fmt.Errorf("check query err: %w", err)
522+
}
523+
524+
var result *synthetic_monitoring.Check
525+
526+
if err := ValidateResponse("check query request", resp, &result); err != nil {
527+
return nil, err
528+
}
529+
return result, nil
530+
}
531+
508532
// GetTenant retrieves the information associated with the authenticated
509533
// tenant.
510534
func (h *Client) GetTenant(ctx context.Context) (*synthetic_monitoring.Tenant, error) {

smapi_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,79 @@ func TestListChecks(t *testing.T) {
12941294
require.ElementsMatch(t, checks, actualChecks)
12951295
}
12961296

1297+
func TestQueryChecks(t *testing.T) {
1298+
orgs := orgs()
1299+
testTenant := orgs.findTenantByOrg(1000)
1300+
testTenantId := testTenant.id
1301+
checks := []synthetic_monitoring.Check{
1302+
{
1303+
Id: 42,
1304+
TenantId: testTenantId,
1305+
Job: "testing",
1306+
Target: "icanhazip.com",
1307+
},
1308+
{
1309+
Id: 84,
1310+
TenantId: testTenantId,
1311+
Job: "not-testing",
1312+
Target: "nocanhazip.com",
1313+
},
1314+
}
1315+
url, mux, cleanup := newTestServer(t)
1316+
defer cleanup()
1317+
mux.Handle("/api/v1/check/query", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1318+
var resp *synthetic_monitoring.Check
1319+
fmt.Println(r.URL.RawQuery)
1320+
job := r.URL.Query().Get("job")
1321+
target := r.URL.Query().Get("target")
1322+
// Emulate searching for a check
1323+
for _, check := range checks {
1324+
if check.Job == job && check.Target == target {
1325+
resp = &check
1326+
break
1327+
}
1328+
}
1329+
if resp != nil {
1330+
writeResponse(w, http.StatusOK, &resp)
1331+
return
1332+
}
1333+
errorResponse(w, http.StatusNotFound, fmt.Sprintf("check with target %s and job %s", target, job))
1334+
}))
1335+
1336+
c := NewClient(url, testTenant.token, http.DefaultClient)
1337+
t.Run("Validate checks can be found", func(t *testing.T) {
1338+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1339+
defer cancel()
1340+
filteredChecks, err := c.QueryCheck(ctx, "testing", "icanhazip.com")
1341+
require.NoError(t, err)
1342+
require.NotNil(t, filteredChecks)
1343+
require.Equal(t, filteredChecks.Target, "icanhazip.com")
1344+
})
1345+
t.Run("Validate another check can be found", func(t *testing.T) {
1346+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1347+
defer cancel()
1348+
filteredChecks, err := c.QueryCheck(ctx, "not-testing", "nocanhazip.com")
1349+
require.NoError(t, err)
1350+
require.NotNil(t, filteredChecks)
1351+
require.Equal(t, filteredChecks.Target, "nocanhazip.com")
1352+
})
1353+
t.Run("Validate a not found check returns a 404", func(t *testing.T) {
1354+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1355+
defer cancel()
1356+
filteredChecks, err := c.QueryCheck(ctx, "not-found", "nocanhazip.com")
1357+
require.Error(t, err)
1358+
require.Nil(t, filteredChecks)
1359+
})
1360+
1361+
t.Run("Ensure the client checks for missing jobs and checks", func(t *testing.T) {
1362+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1363+
defer cancel()
1364+
filteredChecks, err := c.QueryCheck(ctx, "", "")
1365+
require.EqualError(t, err, "check query request: target and job must be set")
1366+
require.Nil(t, filteredChecks)
1367+
})
1368+
}
1369+
12971370
func TestGetTenant(t *testing.T) {
12981371
orgs := orgs()
12991372

0 commit comments

Comments
 (0)