fleet/server/service/client_queries.go
Victor Lyuboslavsky e4d5e27dd9
fleetctl gitops (#16535)
Add `fleetctl gitops` command for #13643 

Code review video:
https://www.loom.com/share/7941c51c709b44ccafd618dd05837d99?sid=27b923d7-1393-4396-bac7-30616b2d6de9

fleet-gitops PR that also needs review:
https://github.com/fleetdm/fleet-gitops/pull/26

Working global/team gitops configs that can be used for testing:
https://github.com/fleetdm/fleet-gitops/tree/victor/fixing-configs

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
2024-02-09 13:34:57 -06:00

63 lines
2.0 KiB
Go

package service
import (
"fmt"
"net/url"
"github.com/fleetdm/fleet/v4/server/fleet"
)
// ApplyQueries sends the list of Queries to be applied (upserted) to the
// Fleet instance.
func (c *Client) ApplyQueries(specs []*fleet.QuerySpec) error {
req := applyQuerySpecsRequest{Specs: specs}
verb, path := "POST", "/api/latest/fleet/spec/queries"
var responseBody applyQuerySpecsResponse
return c.authenticatedRequest(req, verb, path, &responseBody)
}
// GetQuerySpec returns the query spec of a query by its team+name.
func (c *Client) GetQuerySpec(teamID *uint, name string) (*fleet.QuerySpec, error) {
verb, path := "GET", "/api/latest/fleet/spec/queries/"+url.PathEscape(name)
query := url.Values{}
if teamID != nil {
query.Set("team_id", fmt.Sprint(*teamID))
}
var responseBody getQuerySpecResponse
err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query.Encode())
return responseBody.Spec, err
}
// GetQueries retrieves the list of all Queries.
func (c *Client) GetQueries(teamID *uint, name *string) ([]fleet.Query, error) {
verb, path := "GET", "/api/latest/fleet/queries"
query := url.Values{}
if teamID != nil {
query.Set("team_id", fmt.Sprint(*teamID))
}
if name != nil {
query.Set("query", *name)
}
var responseBody listQueriesResponse
err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query.Encode())
if err != nil {
return nil, err
}
return responseBody.Queries, nil
}
// DeleteQuery deletes the query with the matching name.
func (c *Client) DeleteQuery(name string) error {
verb, path := "DELETE", "/api/latest/fleet/queries/"+url.PathEscape(name)
var responseBody deleteQueryResponse
return c.authenticatedRequest(nil, verb, path, &responseBody)
}
// DeleteQueries deletes several queries.
func (c *Client) DeleteQueries(IDs []uint) error {
req := deleteQueriesRequest{IDs: IDs}
verb, path := "POST", "/api/latest/fleet/queries/delete"
var responseBody deleteQueriesResponse
return c.authenticatedRequest(req, verb, path, &responseBody)
}