Skip to content

Commit 187dfd2

Browse files
authored
fix: make it possible to unset a check/monitor's group on update (#144)
1 parent 8984366 commit 187dfd2

File tree

2 files changed

+331
-81
lines changed

2 files changed

+331
-81
lines changed

checkly.go

Lines changed: 134 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,41 @@ func (c *client) CreateCheck(
149149
return c.createCheck(ctx, check, endpoint)
150150
}
151151

152+
type checkPayload struct {
153+
Check
154+
GroupID *int64 `json:"groupId"`
155+
}
156+
157+
func createCheckPayload(check Check) checkPayload {
158+
payload := checkPayload{
159+
Check: check,
160+
}
161+
162+
// GroupID must be null if empty or the group will not get unset on update.
163+
if check.GroupID != 0 {
164+
payload.GroupID = &check.GroupID
165+
}
166+
167+
// A nil value for a list will cause the backend to not update the value.
168+
// We must send empty lists instead.
169+
if check.Locations == nil {
170+
payload.Locations = []string{}
171+
}
172+
173+
if check.PrivateLocations == nil {
174+
payload.PrivateLocations = &[]string{}
175+
}
176+
177+
return payload
178+
}
179+
152180
func (c *client) createCheck(
153181
ctx context.Context,
154182
check Check,
155183
endpoint string,
156184
) (*Check, error) {
157-
data, err := json.Marshal(check)
185+
payload := createCheckPayload(check)
186+
data, err := json.Marshal(payload)
158187
if err != nil {
159188
return nil, err
160189
}
@@ -220,18 +249,45 @@ func (c *client) CreateTCPCheck(
220249
return c.CreateTCPMonitor(ctx, check)
221250
}
222251

223-
func (c *client) CreateTCPMonitor(
224-
ctx context.Context,
225-
monitor TCPMonitor,
226-
) (*TCPMonitor, error) {
227-
payload := struct {
228-
TCPMonitor
229-
DoubleCheck bool `json:"doubleCheck"`
230-
}{
252+
type tcpMonitorPayload struct {
253+
TCPMonitor
254+
Type string `json:"checkType"`
255+
DoubleCheck bool `json:"doubleCheck"`
256+
GroupID *int64 `json:"groupId"`
257+
}
258+
259+
func createTCPMonitorPayload(monitor TCPMonitor) tcpMonitorPayload {
260+
payload := tcpMonitorPayload{
231261
TCPMonitor: monitor,
262+
// Unfortunately `checkType` is required for this endpoint.
263+
Type: "TCP",
232264
// Unfortunately, this will default to true if not set.
233265
DoubleCheck: false,
234266
}
267+
268+
// GroupID must be null if empty or the group will not get unset on update.
269+
if monitor.GroupID != 0 {
270+
payload.GroupID = &monitor.GroupID
271+
}
272+
273+
// A nil value for a list will cause the backend to not update the value.
274+
// We must send empty lists instead.
275+
if monitor.Locations == nil {
276+
payload.Locations = []string{}
277+
}
278+
279+
if monitor.PrivateLocations == nil {
280+
payload.PrivateLocations = &[]string{}
281+
}
282+
283+
return payload
284+
}
285+
286+
func (c *client) CreateTCPMonitor(
287+
ctx context.Context,
288+
monitor TCPMonitor,
289+
) (*TCPMonitor, error) {
290+
payload := createTCPMonitorPayload(monitor)
235291
data, err := json.Marshal(payload)
236292
if err != nil {
237293
return nil, err
@@ -255,20 +311,47 @@ func (c *client) CreateTCPMonitor(
255311
return &result, nil
256312
}
257313

258-
func (c *client) CreateURLMonitor(
259-
ctx context.Context,
260-
monitor URLMonitor,
261-
) (*URLMonitor, error) {
262-
payload := struct {
263-
URLMonitor
264-
Request Request `json:"request"`
265-
DoubleCheck bool `json:"doubleCheck"`
266-
}{
314+
type urlMonitorPayload struct {
315+
URLMonitor
316+
Type string `json:"checkType"`
317+
Request Request `json:"request"`
318+
DoubleCheck bool `json:"doubleCheck"`
319+
GroupID *int64 `json:"groupId"`
320+
}
321+
322+
func createURLMonitorPayload(monitor URLMonitor) urlMonitorPayload {
323+
payload := urlMonitorPayload{
267324
URLMonitor: monitor,
268-
Request: monitor.Request.toRequest(),
325+
// Unfortunately `checkType` is required for this endpoint.
326+
Type: "URL",
327+
Request: monitor.Request.toRequest(),
269328
// Unfortunately, this will default to true if not set.
270329
DoubleCheck: false,
271330
}
331+
332+
// GroupID must be null if empty or the group will not get unset on update.
333+
if monitor.GroupID != 0 {
334+
payload.GroupID = &monitor.GroupID
335+
}
336+
337+
// A nil value for a list will cause the backend to not update the value.
338+
// We must send empty lists instead.
339+
if monitor.Locations == nil {
340+
payload.Locations = []string{}
341+
}
342+
343+
if monitor.PrivateLocations == nil {
344+
payload.PrivateLocations = &[]string{}
345+
}
346+
347+
return payload
348+
}
349+
350+
func (c *client) CreateURLMonitor(
351+
ctx context.Context,
352+
monitor URLMonitor,
353+
) (*URLMonitor, error) {
354+
payload := createURLMonitorPayload(monitor)
272355
data, err := json.Marshal(payload)
273356
if err != nil {
274357
return nil, err
@@ -296,17 +379,11 @@ func (c *client) CreateURLMonitor(
296379
// updated check, or an error.
297380
func (c *client) UpdateCheck(
298381
ctx context.Context,
299-
ID string, check Check,
382+
ID string,
383+
check Check,
300384
) (*Check, error) {
301-
// A nil value for a list will cause the backend to not update the value.
302-
// We must send empty lists instead.
303-
if check.Locations == nil {
304-
check.Locations = []string{}
305-
}
306-
if check.PrivateLocations == nil {
307-
check.PrivateLocations = &[]string{}
308-
}
309-
data, err := json.Marshal(check)
385+
payload := createCheckPayload(check)
386+
data, err := json.Marshal(payload)
310387
if err != nil {
311388
return nil, err
312389
}
@@ -381,26 +458,7 @@ func (c *client) UpdateTCPMonitor(
381458
ID string,
382459
monitor TCPMonitor,
383460
) (*TCPMonitor, error) {
384-
// A nil value for a list will cause the backend to not update the value.
385-
// We must send empty lists instead.
386-
if monitor.Locations == nil {
387-
monitor.Locations = []string{}
388-
}
389-
if monitor.PrivateLocations == nil {
390-
monitor.PrivateLocations = &[]string{}
391-
}
392-
payload := struct {
393-
TCPMonitor
394-
Type string `json:"checkType"`
395-
DoubleCheck bool `json:"doubleCheck"`
396-
}{
397-
TCPMonitor: monitor,
398-
// Unfortunately `checkType` is required for this endpoint, so sneak it in
399-
// using an anonymous struct.
400-
Type: "TCP",
401-
// Unfortunately, this will default to true if not set.
402-
DoubleCheck: false,
403-
}
461+
payload := createTCPMonitorPayload(monitor)
404462
data, err := json.Marshal(payload)
405463
if err != nil {
406464
return nil, err
@@ -430,28 +488,7 @@ func (c *client) UpdateURLMonitor(
430488
ID string,
431489
monitor URLMonitor,
432490
) (*URLMonitor, error) {
433-
// A nil value for a list will cause the backend to not update the value.
434-
// We must send empty lists instead.
435-
if monitor.Locations == nil {
436-
monitor.Locations = []string{}
437-
}
438-
if monitor.PrivateLocations == nil {
439-
monitor.PrivateLocations = &[]string{}
440-
}
441-
// Unfortunately `checkType` is required for this endpoint, so sneak it in
442-
// using an anonymous struct.
443-
payload := struct {
444-
URLMonitor
445-
Type string `json:"checkType"`
446-
Request Request `json:"request"`
447-
DoubleCheck bool `json:"doubleCheck"`
448-
}{
449-
URLMonitor: monitor,
450-
Type: "URL",
451-
Request: monitor.Request.toRequest(),
452-
// Unfortunately, this will default to true if not set.
453-
DoubleCheck: false,
454-
}
491+
payload := createURLMonitorPayload(monitor)
455492
data, err := json.Marshal(payload)
456493
if err != nil {
457494
return nil, err
@@ -638,13 +675,36 @@ func (c *client) GetURLMonitor(
638675
return &result, nil
639676
}
640677

678+
type groupPayload struct {
679+
Group
680+
}
681+
682+
func createGroupPayload(group Group) groupPayload {
683+
payload := groupPayload{
684+
Group: group,
685+
}
686+
687+
// A nil value for a list will cause the backend to not update the value.
688+
// We must send empty lists instead.
689+
if group.Locations == nil {
690+
payload.Locations = []string{}
691+
}
692+
693+
if group.PrivateLocations == nil {
694+
payload.PrivateLocations = &[]string{}
695+
}
696+
697+
return payload
698+
}
699+
641700
// CreateGroup creates a new check group with the specified details. It returns
642701
// the newly-created group, or an error.
643702
func (c *client) CreateGroup(
644703
ctx context.Context,
645704
group Group,
646705
) (*Group, error) {
647-
data, err := json.Marshal(group)
706+
payload := createGroupPayload(group)
707+
data, err := json.Marshal(payload)
648708
if err != nil {
649709
return nil, err
650710
}
@@ -702,15 +762,8 @@ func (c *client) UpdateGroup(
702762
ID int64,
703763
group Group,
704764
) (*Group, error) {
705-
// A nil value for a list will cause the backend to not update the value.
706-
// We must send empty lists instead.
707-
if group.Locations == nil {
708-
group.Locations = []string{}
709-
}
710-
if group.PrivateLocations == nil {
711-
group.PrivateLocations = &[]string{}
712-
}
713-
data, err := json.Marshal(group)
765+
payload := createGroupPayload(group)
766+
data, err := json.Marshal(payload)
714767
if err != nil {
715768
return nil, err
716769
}

0 commit comments

Comments
 (0)