2018-05-07 19:44:40 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-07-25 00:17:20 +00:00
|
|
|
"fmt"
|
2018-05-07 19:44:40 +00:00
|
|
|
"net/url"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2018-05-07 19:44:40 +00:00
|
|
|
)
|
|
|
|
|
2018-05-09 00:10:01 +00:00
|
|
|
// ApplyQueries sends the list of Queries to be applied (upserted) to the
|
2018-05-07 19:44:40 +00:00
|
|
|
// Fleet instance.
|
2021-06-06 22:07:29 +00:00
|
|
|
func (c *Client) ApplyQueries(specs []*fleet.QuerySpec) error {
|
2018-05-07 19:44:40 +00:00
|
|
|
req := applyQuerySpecsRequest{Specs: specs}
|
2022-04-05 15:35:53 +00:00
|
|
|
verb, path := "POST", "/api/latest/fleet/spec/queries"
|
2018-05-07 19:44:40 +00:00
|
|
|
var responseBody applyQuerySpecsResponse
|
2022-01-24 19:40:51 +00:00
|
|
|
return c.authenticatedRequest(req, verb, path, &responseBody)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 00:17:20 +00:00
|
|
|
// GetQuerySpec returns the query spec of a query by its team+name.
|
|
|
|
func (c *Client) GetQuerySpec(teamID *uint, name string) (*fleet.QuerySpec, error) {
|
2022-04-05 15:35:53 +00:00
|
|
|
verb, path := "GET", "/api/latest/fleet/spec/queries/"+url.PathEscape(name)
|
2023-07-25 00:17:20 +00:00
|
|
|
query := url.Values{}
|
|
|
|
if teamID != nil {
|
|
|
|
query.Set("team_id", fmt.Sprint(*teamID))
|
|
|
|
}
|
2018-05-09 00:10:01 +00:00
|
|
|
var responseBody getQuerySpecResponse
|
2023-07-25 00:17:20 +00:00
|
|
|
err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query.Encode())
|
2022-01-24 19:40:51 +00:00
|
|
|
return responseBody.Spec, err
|
2018-05-09 00:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetQueries retrieves the list of all Queries.
|
2023-12-15 18:55:39 +00:00
|
|
|
func (c *Client) GetQueries(teamID *uint, name *string) ([]fleet.Query, error) {
|
2023-04-26 14:38:20 +00:00
|
|
|
verb, path := "GET", "/api/latest/fleet/queries"
|
2023-07-25 00:17:20 +00:00
|
|
|
query := url.Values{}
|
|
|
|
if teamID != nil {
|
|
|
|
query.Set("team_id", fmt.Sprint(*teamID))
|
|
|
|
}
|
2023-12-15 18:55:39 +00:00
|
|
|
if name != nil {
|
|
|
|
query.Set("query", *name)
|
|
|
|
}
|
2023-04-26 14:38:20 +00:00
|
|
|
var responseBody listQueriesResponse
|
2023-07-25 00:17:20 +00:00
|
|
|
err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query.Encode())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return responseBody.Queries, nil
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteQuery deletes the query with the matching name.
|
|
|
|
func (c *Client) DeleteQuery(name string) error {
|
2022-04-05 15:35:53 +00:00
|
|
|
verb, path := "DELETE", "/api/latest/fleet/queries/"+url.PathEscape(name)
|
2018-05-07 19:44:40 +00:00
|
|
|
var responseBody deleteQueryResponse
|
2022-01-24 19:40:51 +00:00
|
|
|
return c.authenticatedRequest(nil, verb, path, &responseBody)
|
2021-10-25 12:17:34 +00:00
|
|
|
}
|