Skip to content

Commit 9b06eb4

Browse files
committed
fix: custom checks de/serialization
1 parent dde1e5e commit 9b06eb4

File tree

8 files changed

+1769
-1142
lines changed

8 files changed

+1769
-1142
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Unreleased
2+
3+
### Fixes
4+
- [#403](https://github.com/influxdata/influxdb-client-go/pull/403) Custom checks de/serialization to allow calling server Check API
5+
16
## 2.13.0 [2023-12-05]
27

38
### Features

api/checks_test.go

Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
//go:build e2e
2+
3+
// Copyright 2024 InfluxData, Inc. All rights reserved.
4+
// Use of this source code is governed by MIT
5+
// license that can be found in the LICENSE fil
6+
package api_test
7+
8+
import (
9+
"context"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
"testing"
13+
14+
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
15+
"github.com/influxdata/influxdb-client-go/v2/domain"
16+
)
17+
18+
var msg = "Check: ${ r._check_name } is: ${ r._level }"
19+
var flux = `from(bucket: "foo") |> range(start: -1d, stop: now()) |> aggregateWindow(every: 1m, fn: mean) |> filter(fn: (r) => r._field == "usage_user") |> yield()`
20+
var every = "1h"
21+
var offset = "0s"
22+
var timeSince = "90m"
23+
var staleTime = "30m"
24+
var level = domain.CheckStatusLevelCRIT
25+
26+
func TestCreateGetDeleteThresholdCheck(t *testing.T) {
27+
ctx := context.Background()
28+
client := influxdb2.NewClient(serverURL, authToken)
29+
30+
greater := domain.GreaterThreshold{}
31+
greater.Value = 10.0
32+
lc := domain.CheckStatusLevelCRIT
33+
greater.Level = &lc
34+
greater.AllValues = &[]bool{true}[0]
35+
36+
lesser := domain.LesserThreshold{}
37+
lesser.Value = 1.0
38+
lo := domain.CheckStatusLevelOK
39+
lesser.Level = &lo
40+
41+
rang := domain.RangeThreshold{}
42+
rang.Min = 3.0
43+
rang.Max = 8.0
44+
lw := domain.CheckStatusLevelWARN
45+
rang.Level = &lw
46+
47+
org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org")
48+
require.Nil(t, err)
49+
50+
thresholds := []domain.Threshold{&greater, &lesser, &rang}
51+
52+
check := domain.ThresholdCheck{
53+
CheckBaseExtend: domain.CheckBaseExtend{
54+
CheckBase: domain.CheckBase{
55+
Name: "ThresholdCheck test",
56+
OrgID: *org.Id,
57+
Query: domain.DashboardQuery{Text: &flux},
58+
Status: domain.TaskStatusTypeActive,
59+
},
60+
Every: &every,
61+
Offset: &offset,
62+
StatusMessageTemplate: &msg,
63+
},
64+
Thresholds: &thresholds,
65+
}
66+
params := domain.CreateCheckAllParams{
67+
Body: domain.CreateCheckJSONRequestBody(&check),
68+
}
69+
nc, err := client.APIClient().CreateCheck(context.Background(), &params)
70+
require.Nil(t, err)
71+
tc := validateTC(t, nc, *org.Id)
72+
73+
gp := domain.GetChecksIDAllParams{CheckID: *tc.Id}
74+
75+
c, err := client.APIClient().GetChecksID(context.Background(), &gp)
76+
require.Nil(t, err)
77+
tc = validateTC(t, c, *org.Id)
78+
79+
dp := domain.DeleteChecksIDAllParams{
80+
CheckID: *tc.Id,
81+
}
82+
83+
err = client.APIClient().DeleteChecksID(context.Background(), &dp)
84+
require.Nil(t, err)
85+
86+
_, err = client.APIClient().GetChecksID(context.Background(), &gp)
87+
require.NotNil(t, err)
88+
}
89+
90+
func TestCreateGetDeleteDeadmanCheck(t *testing.T) {
91+
client := influxdb2.NewClient(serverURL, authToken)
92+
93+
ctx := context.Background()
94+
95+
org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org")
96+
require.Nil(t, err)
97+
98+
check := domain.DeadmanCheck{
99+
CheckBaseExtend: domain.CheckBaseExtend{
100+
CheckBase: domain.CheckBase{
101+
Name: "DeadmanCheck test",
102+
OrgID: *org.Id,
103+
Query: domain.DashboardQuery{Text: &flux},
104+
Status: domain.TaskStatusTypeActive,
105+
},
106+
Every: &every,
107+
Offset: &offset,
108+
StatusMessageTemplate: &msg,
109+
},
110+
TimeSince: &timeSince,
111+
StaleTime: &staleTime,
112+
Level: &level,
113+
}
114+
params := domain.CreateCheckAllParams{
115+
Body: domain.CreateCheckJSONRequestBody(&check),
116+
}
117+
nc, err := client.APIClient().CreateCheck(context.Background(), &params)
118+
require.Nil(t, err)
119+
dc := validateDC(t, nc, *org.Id)
120+
121+
checkId := dc.Id
122+
gp := domain.GetChecksIDAllParams{CheckID: *checkId}
123+
124+
c, err := client.APIClient().GetChecksID(context.Background(), &gp)
125+
require.Nil(t, err)
126+
dc = validateDC(t, c, *org.Id)
127+
128+
dp := domain.DeleteChecksIDAllParams{
129+
CheckID: *dc.Id,
130+
}
131+
132+
err = client.APIClient().DeleteChecksID(context.Background(), &dp)
133+
require.Nil(t, err)
134+
135+
}
136+
137+
func TestUpdateThresholdCheck(t *testing.T) {
138+
ctx := context.Background()
139+
client := influxdb2.NewClient(serverURL, authToken)
140+
141+
greater := domain.GreaterThreshold{}
142+
greater.Value = 10.0
143+
lc := domain.CheckStatusLevelCRIT
144+
greater.Level = &lc
145+
greater.AllValues = &[]bool{true}[0]
146+
147+
org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org")
148+
require.Nil(t, err)
149+
150+
thresholds := []domain.Threshold{&greater}
151+
152+
ev := "1m"
153+
154+
check := domain.ThresholdCheck{
155+
CheckBaseExtend: domain.CheckBaseExtend{
156+
CheckBase: domain.CheckBase{
157+
Name: "ThresholdCheck update test",
158+
OrgID: *org.Id,
159+
Query: domain.DashboardQuery{Text: &flux},
160+
Status: domain.TaskStatusTypeActive,
161+
},
162+
Every: &ev,
163+
Offset: &offset,
164+
StatusMessageTemplate: &msg,
165+
},
166+
Thresholds: &thresholds,
167+
}
168+
params := domain.CreateCheckAllParams{
169+
Body: domain.CreateCheckJSONRequestBody(&check),
170+
}
171+
nc, err := client.APIClient().CreateCheck(context.Background(), &params)
172+
require.Nil(t, err)
173+
require.NotNil(t, nc)
174+
tc := nc.(*domain.ThresholdCheck)
175+
176+
lesser := domain.LesserThreshold{}
177+
lesser.Value = 1.0
178+
lo := domain.CheckStatusLevelOK
179+
lesser.Level = &lo
180+
181+
rang := domain.RangeThreshold{}
182+
rang.Min = 3.0
183+
rang.Max = 8.0
184+
lw := domain.CheckStatusLevelWARN
185+
rang.Level = &lw
186+
187+
thresholds = []domain.Threshold{&greater, &lesser, &rang}
188+
tc.Thresholds = &thresholds
189+
tc.Every = &every
190+
tc.Name = "ThresholdCheck test"
191+
192+
updateParams := domain.PutChecksIDAllParams{
193+
CheckID: *tc.Id,
194+
Body: tc,
195+
}
196+
nc, err = client.APIClient().PutChecksID(context.Background(), &updateParams)
197+
require.Nil(t, err)
198+
require.NotNil(t, nc)
199+
tc = validateTC(t, nc, *org.Id)
200+
201+
dp := domain.DeleteChecksIDAllParams{
202+
CheckID: *tc.Id,
203+
}
204+
205+
err = client.APIClient().DeleteChecksID(context.Background(), &dp)
206+
require.Nil(t, err)
207+
}
208+
209+
func TestGetChecks(t *testing.T) {
210+
client := influxdb2.NewClient(serverURL, authToken)
211+
212+
ctx := context.Background()
213+
214+
org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org")
215+
require.Nil(t, err)
216+
217+
check := domain.DeadmanCheck{
218+
CheckBaseExtend: domain.CheckBaseExtend{
219+
CheckBase: domain.CheckBase{
220+
Name: "DeadmanCheck test",
221+
OrgID: *org.Id,
222+
Query: domain.DashboardQuery{Text: &flux},
223+
Status: domain.TaskStatusTypeActive,
224+
},
225+
Every: &every,
226+
Offset: &offset,
227+
StatusMessageTemplate: &msg,
228+
},
229+
TimeSince: &timeSince,
230+
StaleTime: &staleTime,
231+
Level: &level,
232+
}
233+
params := domain.CreateCheckAllParams{
234+
Body: domain.CreateCheckJSONRequestBody(&check),
235+
}
236+
nc, err := client.APIClient().CreateCheck(context.Background(), &params)
237+
require.Nil(t, err)
238+
validateDC(t, nc, *org.Id)
239+
240+
greater := domain.GreaterThreshold{}
241+
greater.Value = 10.0
242+
lc := domain.CheckStatusLevelCRIT
243+
greater.Level = &lc
244+
greater.AllValues = &[]bool{true}[0]
245+
246+
lesser := domain.LesserThreshold{}
247+
lesser.Value = 1.0
248+
lo := domain.CheckStatusLevelOK
249+
lesser.Level = &lo
250+
251+
rang := domain.RangeThreshold{}
252+
rang.Min = 3.0
253+
rang.Max = 8.0
254+
lw := domain.CheckStatusLevelWARN
255+
rang.Level = &lw
256+
257+
thresholds := []domain.Threshold{&greater, &lesser, &rang}
258+
259+
check2 := domain.ThresholdCheck{
260+
CheckBaseExtend: domain.CheckBaseExtend{
261+
CheckBase: domain.CheckBase{
262+
Name: "ThresholdCheck test",
263+
OrgID: *org.Id,
264+
Query: domain.DashboardQuery{Text: &flux},
265+
Status: domain.TaskStatusTypeActive,
266+
},
267+
Every: &every,
268+
Offset: &offset,
269+
StatusMessageTemplate: &msg,
270+
},
271+
Thresholds: &thresholds,
272+
}
273+
params2 := domain.CreateCheckAllParams{
274+
Body: domain.CreateCheckJSONRequestBody(&check2),
275+
}
276+
nc, err = client.APIClient().CreateCheck(context.Background(), &params2)
277+
require.Nil(t, err)
278+
validateTC(t, nc, *org.Id)
279+
280+
gp := domain.GetChecksParams{
281+
OrgID: *org.Id,
282+
}
283+
checks, err := client.APIClient().GetChecks(context.Background(), &gp)
284+
require.Nil(t, err)
285+
require.NotNil(t, checks)
286+
require.NotNil(t, checks.Checks)
287+
assert.Len(t, *checks.Checks, 2)
288+
dc := validateDC(t, (*checks.Checks)[0], *org.Id)
289+
tc := validateTC(t, (*checks.Checks)[1], *org.Id)
290+
291+
dp := domain.DeleteChecksIDAllParams{
292+
CheckID: *dc.Id,
293+
}
294+
err = client.APIClient().DeleteChecksID(context.Background(), &dp)
295+
require.Nil(t, err)
296+
297+
dp2 := domain.DeleteChecksIDAllParams{
298+
CheckID: *tc.Id,
299+
}
300+
err = client.APIClient().DeleteChecksID(context.Background(), &dp2)
301+
require.Nil(t, err)
302+
303+
checks, err = client.APIClient().GetChecks(context.Background(), &gp)
304+
require.Nil(t, err)
305+
require.NotNil(t, checks)
306+
assert.Nil(t, checks.Checks)
307+
}
308+
309+
func validateDC(t *testing.T, nc domain.Check, orgId string) *domain.DeadmanCheck {
310+
require.NotNil(t, nc)
311+
require.Equal(t, "deadman", nc.Type())
312+
dc := nc.(*domain.DeadmanCheck)
313+
require.NotNil(t, dc)
314+
assert.Equal(t, "DeadmanCheck test", dc.Name)
315+
assert.Equal(t, orgId, dc.OrgID)
316+
assert.Equal(t, msg, *dc.StatusMessageTemplate)
317+
assert.Equal(t, flux, *dc.Query.Text)
318+
assert.Equal(t, every, *dc.Every)
319+
assert.Equal(t, offset, *dc.Offset)
320+
assert.Equal(t, domain.TaskStatusTypeActive, dc.Status)
321+
assert.Equal(t, timeSince, *dc.TimeSince)
322+
assert.Equal(t, staleTime, *dc.StaleTime)
323+
assert.Equal(t, domain.CheckStatusLevelCRIT, *dc.Level)
324+
return dc
325+
}
326+
327+
func validateTC(t *testing.T, check domain.Check, orgId string) *domain.ThresholdCheck {
328+
require.NotNil(t, check)
329+
require.Equal(t, "threshold", check.Type())
330+
tc := check.(*domain.ThresholdCheck)
331+
require.NotNil(t, tc)
332+
assert.Equal(t, "ThresholdCheck test", tc.Name)
333+
assert.Equal(t, orgId, tc.OrgID)
334+
assert.Equal(t, msg, *tc.StatusMessageTemplate)
335+
assert.Equal(t, flux, *tc.Query.Text)
336+
assert.Equal(t, every, *tc.Every)
337+
assert.Equal(t, offset, *tc.Offset)
338+
assert.Equal(t, domain.TaskStatusTypeActive, tc.Status)
339+
assert.Len(t, *tc.Thresholds, 3)
340+
require.Equal(t, "greater", (*tc.Thresholds)[0].Type())
341+
gt := (*tc.Thresholds)[0].(*domain.GreaterThreshold)
342+
require.NotNil(t, gt)
343+
assert.Equal(t, float32(10.0), gt.Value)
344+
assert.Equal(t, domain.CheckStatusLevelCRIT, *gt.Level)
345+
assert.Equal(t, true, *gt.AllValues)
346+
require.Equal(t, "lesser", (*tc.Thresholds)[1].Type())
347+
lt := (*tc.Thresholds)[1].(*domain.LesserThreshold)
348+
require.NotNil(t, lt)
349+
assert.Equal(t, float32(1.0), lt.Value)
350+
assert.Equal(t, domain.CheckStatusLevelOK, *lt.Level)
351+
require.Equal(t, "range", (*tc.Thresholds)[2].Type())
352+
rt := (*tc.Thresholds)[2].(*domain.RangeThreshold)
353+
require.NotNil(t, rt)
354+
assert.Equal(t, float32(3.0), rt.Min)
355+
assert.Equal(t, float32(8.0), rt.Max)
356+
assert.Equal(t, domain.CheckStatusLevelWARN, *rt.Level)
357+
return tc
358+
}

domain/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ to maintain full compatibility with the latest InfluxDB release
1616

1717
## Generate
1818
### Generate types
19-
`oapi-codegen -generate types -o types.gen.go -package domain -templates .\templates oss.yml`
19+
`oapi-codegen -generate types -exclude-tags Checks -o types.gen.go -package domain -templates .\templates oss.yml`
2020

2121
### Generate client
22-
`oapi-codegen -generate client -o client.gen.go -package domain -templates .\templates oss.yml`
22+
`oapi-codegen -generate client -exclude-tags Checks -o client.gen.go -package domain -templates .\templates oss.yml`
2323

0 commit comments

Comments
 (0)