@@ -149,12 +149,41 @@ func (c *client) CreateCheck(
149
149
return c .createCheck (ctx , check , endpoint )
150
150
}
151
151
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
+
152
180
func (c * client ) createCheck (
153
181
ctx context.Context ,
154
182
check Check ,
155
183
endpoint string ,
156
184
) (* Check , error ) {
157
- data , err := json .Marshal (check )
185
+ payload := createCheckPayload (check )
186
+ data , err := json .Marshal (payload )
158
187
if err != nil {
159
188
return nil , err
160
189
}
@@ -220,18 +249,45 @@ func (c *client) CreateTCPCheck(
220
249
return c .CreateTCPMonitor (ctx , check )
221
250
}
222
251
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 {
231
261
TCPMonitor : monitor ,
262
+ // Unfortunately `checkType` is required for this endpoint.
263
+ Type : "TCP" ,
232
264
// Unfortunately, this will default to true if not set.
233
265
DoubleCheck : false ,
234
266
}
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 )
235
291
data , err := json .Marshal (payload )
236
292
if err != nil {
237
293
return nil , err
@@ -255,20 +311,47 @@ func (c *client) CreateTCPMonitor(
255
311
return & result , nil
256
312
}
257
313
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 {
267
324
URLMonitor : monitor ,
268
- Request : monitor .Request .toRequest (),
325
+ // Unfortunately `checkType` is required for this endpoint.
326
+ Type : "URL" ,
327
+ Request : monitor .Request .toRequest (),
269
328
// Unfortunately, this will default to true if not set.
270
329
DoubleCheck : false ,
271
330
}
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 )
272
355
data , err := json .Marshal (payload )
273
356
if err != nil {
274
357
return nil , err
@@ -296,17 +379,11 @@ func (c *client) CreateURLMonitor(
296
379
// updated check, or an error.
297
380
func (c * client ) UpdateCheck (
298
381
ctx context.Context ,
299
- ID string , check Check ,
382
+ ID string ,
383
+ check Check ,
300
384
) (* 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 )
310
387
if err != nil {
311
388
return nil , err
312
389
}
@@ -381,26 +458,7 @@ func (c *client) UpdateTCPMonitor(
381
458
ID string ,
382
459
monitor TCPMonitor ,
383
460
) (* 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 )
404
462
data , err := json .Marshal (payload )
405
463
if err != nil {
406
464
return nil , err
@@ -430,28 +488,7 @@ func (c *client) UpdateURLMonitor(
430
488
ID string ,
431
489
monitor URLMonitor ,
432
490
) (* 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 )
455
492
data , err := json .Marshal (payload )
456
493
if err != nil {
457
494
return nil , err
@@ -638,13 +675,36 @@ func (c *client) GetURLMonitor(
638
675
return & result , nil
639
676
}
640
677
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
+
641
700
// CreateGroup creates a new check group with the specified details. It returns
642
701
// the newly-created group, or an error.
643
702
func (c * client ) CreateGroup (
644
703
ctx context.Context ,
645
704
group Group ,
646
705
) (* Group , error ) {
647
- data , err := json .Marshal (group )
706
+ payload := createGroupPayload (group )
707
+ data , err := json .Marshal (payload )
648
708
if err != nil {
649
709
return nil , err
650
710
}
@@ -702,15 +762,8 @@ func (c *client) UpdateGroup(
702
762
ID int64 ,
703
763
group Group ,
704
764
) (* 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 )
714
767
if err != nil {
715
768
return nil , err
716
769
}
0 commit comments