mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 21:47:20 +00:00
Resolves #40396. No changes file because there should be no user visible changes. ## Testing - [x] QA'd all new/changed functionality manually ## fleetd/orbit/Fleet Desktop - [x] Verified compatibility with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)) - [X] Verified that fleetd runs on macOS, Linux and Windows - [X] Verified auto-update works from the released version of component to the new version (see [tools/tuf/test](../tools/tuf/test/README.md))
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// TriggerCronSchedule attempts to trigger an ad-hoc run of the named cron schedule.
|
|
func (c *Client) TriggerCronSchedule(name string) error {
|
|
verb, path := http.MethodPost, "/api/latest/fleet/trigger"
|
|
|
|
query := url.Values{}
|
|
query.Set("name", name)
|
|
|
|
response, err := c.AuthenticatedDo(verb, path, query.Encode(), nil)
|
|
if err != nil {
|
|
return fmt.Errorf("%s %s: %s", verb, path, err)
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
switch response.StatusCode {
|
|
case http.StatusConflict:
|
|
msg, err := extractServerErrMsg(verb, path, response)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return conflictErr{Msg: msg}
|
|
case http.StatusNotFound:
|
|
msg, err := extractServerErrMsg(verb, path, response)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ¬FoundErr{Msg: msg}
|
|
default:
|
|
return c.ParseResponse(verb, path, response, nil)
|
|
}
|
|
}
|
|
|
|
func extractServerErrMsg(verb string, path string, res *http.Response) (string, error) {
|
|
var decoded serverError
|
|
if err := json.NewDecoder(res.Body).Decode(&decoded); err != nil {
|
|
return "", fmt.Errorf("%s %s: decode server error: %s", verb, path, err)
|
|
}
|
|
if len(decoded.Errors) > 0 {
|
|
return decoded.Errors[0].Reason, nil
|
|
}
|
|
return decoded.Message, nil
|
|
}
|