Skip to content

Commit

Permalink
fix: always send empty [] for locations (public/private) even when no…
Browse files Browse the repository at this point in the history
…t 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.
  • Loading branch information
sorccu committed Feb 3, 2025
1 parent d19fe3b commit 50161a0
Showing 1 changed file with 27 additions and 56 deletions.
83 changes: 27 additions & 56 deletions checkly.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,7 @@ func (c *client) Create(
ctx context.Context,
check Check,
) (*Check, error) {
data, err := json.Marshal(check)
if err != nil {
return nil, err
}
status, res, err := c.apiCall(
ctx,
http.MethodPost,
withAutoAssignAlertsFlag("checks"),
data,
)
if err != nil {
return nil, err
}
if status != http.StatusCreated {
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
}
var result Check
if err = json.NewDecoder(strings.NewReader(res)).Decode(&result); err != nil {
return nil, fmt.Errorf("decoding error for data %s: %v", res, err)
}
return &result, nil
return c.CreateCheck(ctx, check)
}

// Update updates an existing check with the specified details. It returns the
Expand All @@ -98,28 +78,7 @@ func (c *client) Update(
ctx context.Context,
ID string, check Check,
) (*Check, error) {
data, err := json.Marshal(check)
if err != nil {
return nil, err
}
status, res, err := c.apiCall(
ctx,
http.MethodPut,
withAutoAssignAlertsFlag(fmt.Sprintf("checks/%s", ID)),
data,
)
if err != nil {
return nil, err
}
if status != http.StatusOK {
return nil, fmt.Errorf("unexpected response status %d: %q", status, res)
}
var result Check
err = json.NewDecoder(strings.NewReader(res)).Decode(&result)
if err != nil {
return nil, fmt.Errorf("decoding error for data %s: %v", res, err)
}
return &result, nil
return c.UpdateCheck(ctx, ID, check)
}

// Delete deletes the check with the specified ID.
Expand All @@ -130,19 +89,7 @@ func (c *client) Delete(
ctx context.Context,
ID string,
) error {
status, res, err := c.apiCall(
ctx,
http.MethodDelete,
fmt.Sprintf("checks/%s", ID),
nil,
)
if err != nil {
return err
}
if status != http.StatusNoContent {
return fmt.Errorf("unexpected response status %d: %q", status, res)
}
return nil
return c.DeleteCheck(ctx, ID)
}

// Get takes the ID of an existing check, and returns the check parameters, or
Expand Down Expand Up @@ -278,6 +225,14 @@ func (c *client) UpdateCheck(
ctx context.Context,
ID string, check Check,
) (*Check, error) {
// A nil value for a list will cause the backend to not update the value.
// We must send empty lists instead.
if check.Locations == nil {
check.Locations = []string{}
}
if check.PrivateLocations == nil {
check.PrivateLocations = &[]string{}
}
data, err := json.Marshal(check)
if err != nil {
return nil, err
Expand Down Expand Up @@ -337,6 +292,14 @@ func (c *client) UpdateTCPCheck(
ID string,
check TCPCheck,
) (*TCPCheck, error) {
// A nil value for a list will cause the backend to not update the value.
// We must send empty lists instead.
if check.Locations == nil {
check.Locations = []string{}
}
if check.PrivateLocations == nil {
check.PrivateLocations = &[]string{}
}
// Unfortunately `checkType` is required for this endpoint, so sneak it in
// using an anonymous struct.
payload := struct {
Expand Down Expand Up @@ -532,6 +495,14 @@ func (c *client) UpdateGroup(
ID int64,
group Group,
) (*Group, error) {
// A nil value for a list will cause the backend to not update the value.
// We must send empty lists instead.
if group.Locations == nil {
group.Locations = []string{}
}
if group.PrivateLocations == nil {
group.PrivateLocations = &[]string{}
}
data, err := json.Marshal(group)
if err != nil {
return nil, err
Expand Down

0 comments on commit 50161a0

Please sign in to comment.