fleet/server/service/client_policies.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

61 lines
2.0 KiB
Go

package service
import (
"fmt"
"github.com/fleetdm/fleet/v4/server/fleet"
)
func (c *Client) CreateGlobalPolicy(name, query, description, resolution, platform string) error {
req := globalPolicyRequest{
Name: name,
Query: query,
Description: description,
Resolution: resolution,
Platform: platform,
}
verb, path := "POST", "/api/latest/fleet/global/policies"
var responseBody globalPolicyResponse
return c.authenticatedRequest(req, verb, path, &responseBody)
}
// ApplyPolicies sends the list of Policies to be applied to the
// Fleet instance.
func (c *Client) ApplyPolicies(specs []*fleet.PolicySpec) error {
req := applyPolicySpecsRequest{Specs: specs}
verb, path := "POST", "/api/latest/fleet/spec/policies"
var responseBody applyPolicySpecsResponse
return c.authenticatedRequest(req, verb, path, &responseBody)
}
// GetPolicies retrieves the list of Policies. Inherited policies are excluded.
func (c *Client) GetPolicies(teamID *uint) ([]*fleet.Policy, error) {
verb, path := "GET", ""
if teamID != nil {
path = fmt.Sprintf("/api/latest/fleet/teams/%d/policies", *teamID)
} else {
path = "/api/latest/fleet/policies"
}
// The response body also works for listTeamPoliciesResponse because they contain some of the same members.
var responseBody listGlobalPoliciesResponse
err := c.authenticatedRequest(nil, verb, path, &responseBody)
if err != nil {
return nil, err
}
return responseBody.Policies, nil
}
// DeletePolicies deletes several policies.
func (c *Client) DeletePolicies(teamID *uint, IDs []uint) error {
verb, path := "POST", ""
req := deleteTeamPoliciesRequest{IDs: IDs}
if teamID != nil {
path = fmt.Sprintf("/api/latest/fleet/teams/%d/policies/delete", *teamID)
req.TeamID = *teamID
} else {
path = "/api/latest/fleet/policies/delete"
}
// The response body also works for deleteTeamPoliciesResponse because they contain some of the same members.
var responseBody deleteGlobalPoliciesResponse
return c.authenticatedRequest(req, verb, path, &responseBody)
}