Files
fleet/server/service/client_policies.go
Lucas Manuel Rodriguez 305886fe86 Move policy request and response types to server/fleet/ package (#43068)
For #36087

## Testing

- [x] QA'd all new/changed functionality manually
2026-04-07 11:04:08 -03:00

62 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 := fleet.GlobalPolicyRequest{
Name: name,
Query: query,
Description: description,
Resolution: resolution,
Platform: platform,
}
verb, path := "POST", "/api/latest/fleet/global/policies"
var responseBody fleet.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 := fleet.ApplyPolicySpecsRequest{Specs: specs}
verb, path := "POST", "/api/latest/fleet/spec/policies"
var responseBody fleet.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/fleets/%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 fleet.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 := fleet.DeleteTeamPoliciesRequest{IDs: ids}
if teamID != nil {
path = fmt.Sprintf("/api/latest/fleet/fleets/%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 fleet.DeleteGlobalPoliciesResponse
return c.authenticatedRequest(req, verb, path, &responseBody)
}