Skip to content

Commit 50161a0

Browse files
committed
fix: always send empty [] for locations (public/private) even when not set
If the backend does not receive a value, or receives a null value, it will not update the database. This is an issue for many other values too, but as an API level fix will be needed, for now this patch only addresses the fields we've received reports about. To make the patch a little easier, Create now calls CreateCheck, Update UpdateCheck and Delete DeleteCheck. The latter two were already equivalent. Create and CreateCheck had some differences (mainly that that former would send the request to /v1/checks instead of /v1/checks/<type>), but given how CreateCheck covers all check types, there should be no difference.
1 parent d19fe3b commit 50161a0

File tree

1 file changed

+27
-56
lines changed

1 file changed

+27
-56
lines changed

checkly.go

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,7 @@ func (c *client) Create(
6666
ctx context.Context,
6767
check Check,
6868
) (*Check, error) {
69-
data, err := json.Marshal(check)
70-
if err != nil {
71-
return nil, err
72-
}
73-
status, res, err := c.apiCall(
74-
ctx,
75-
http.MethodPost,
76-
withAutoAssignAlertsFlag("checks"),
77-
data,
78-
)
79-
if err != nil {
80-
return nil, err
81-
}
82-
if status != http.StatusCreated {
83-
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
84-
}
85-
var result Check
86-
if err = json.NewDecoder(strings.NewReader(res)).Decode(&result); err != nil {
87-
return nil, fmt.Errorf("decoding error for data %s: %v", res, err)
88-
}
89-
return &result, nil
69+
return c.CreateCheck(ctx, check)
9070
}
9171

9272
// Update updates an existing check with the specified details. It returns the
@@ -98,28 +78,7 @@ func (c *client) Update(
9878
ctx context.Context,
9979
ID string, check Check,
10080
) (*Check, error) {
101-
data, err := json.Marshal(check)
102-
if err != nil {
103-
return nil, err
104-
}
105-
status, res, err := c.apiCall(
106-
ctx,
107-
http.MethodPut,
108-
withAutoAssignAlertsFlag(fmt.Sprintf("checks/%s", ID)),
109-
data,
110-
)
111-
if err != nil {
112-
return nil, err
113-
}
114-
if status != http.StatusOK {
115-
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
116-
}
117-
var result Check
118-
err = json.NewDecoder(strings.NewReader(res)).Decode(&result)
119-
if err != nil {
120-
return nil, fmt.Errorf("decoding error for data %s: %v", res, err)
121-
}
122-
return &result, nil
81+
return c.UpdateCheck(ctx, ID, check)
12382
}
12483

12584
// Delete deletes the check with the specified ID.
@@ -130,19 +89,7 @@ func (c *client) Delete(
13089
ctx context.Context,
13190
ID string,
13291
) error {
133-
status, res, err := c.apiCall(
134-
ctx,
135-
http.MethodDelete,
136-
fmt.Sprintf("checks/%s", ID),
137-
nil,
138-
)
139-
if err != nil {
140-
return err
141-
}
142-
if status != http.StatusNoContent {
143-
return fmt.Errorf("unexpected response status %d: %q", status, res)
144-
}
145-
return nil
92+
return c.DeleteCheck(ctx, ID)
14693
}
14794

14895
// Get takes the ID of an existing check, and returns the check parameters, or
@@ -278,6 +225,14 @@ func (c *client) UpdateCheck(
278225
ctx context.Context,
279226
ID string, check Check,
280227
) (*Check, error) {
228+
// A nil value for a list will cause the backend to not update the value.
229+
// We must send empty lists instead.
230+
if check.Locations == nil {
231+
check.Locations = []string{}
232+
}
233+
if check.PrivateLocations == nil {
234+
check.PrivateLocations = &[]string{}
235+
}
281236
data, err := json.Marshal(check)
282237
if err != nil {
283238
return nil, err
@@ -337,6 +292,14 @@ func (c *client) UpdateTCPCheck(
337292
ID string,
338293
check TCPCheck,
339294
) (*TCPCheck, error) {
295+
// A nil value for a list will cause the backend to not update the value.
296+
// We must send empty lists instead.
297+
if check.Locations == nil {
298+
check.Locations = []string{}
299+
}
300+
if check.PrivateLocations == nil {
301+
check.PrivateLocations = &[]string{}
302+
}
340303
// Unfortunately `checkType` is required for this endpoint, so sneak it in
341304
// using an anonymous struct.
342305
payload := struct {
@@ -532,6 +495,14 @@ func (c *client) UpdateGroup(
532495
ID int64,
533496
group Group,
534497
) (*Group, error) {
498+
// A nil value for a list will cause the backend to not update the value.
499+
// We must send empty lists instead.
500+
if group.Locations == nil {
501+
group.Locations = []string{}
502+
}
503+
if group.PrivateLocations == nil {
504+
group.PrivateLocations = &[]string{}
505+
}
535506
data, err := json.Marshal(group)
536507
if err != nil {
537508
return nil, err

0 commit comments

Comments
 (0)