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

88 lines
2.9 KiB
Go

package service
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"github.com/fleetdm/fleet/v4/server/fleet"
)
// ListTeams retrieves the list of teams.
func (c *Client) ListTeams(query string) ([]fleet.Team, error) {
verb, path := "GET", "/api/latest/fleet/teams"
var responseBody listTeamsResponse
err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query)
if err != nil {
return nil, err
}
return responseBody.Teams, nil
}
// CreateTeam creates a new team.
func (c *Client) CreateTeam(teamPayload fleet.TeamPayload) (*fleet.Team, error) {
req := createTeamRequest{
TeamPayload: teamPayload,
}
verb, path := "POST", "/api/latest/fleet/teams"
var responseBody teamResponse
err := c.authenticatedRequest(req, verb, path, &responseBody)
if err != nil {
return nil, err
}
return responseBody.Team, nil
}
func (c *Client) GetTeam(teamID uint) (*fleet.Team, error) {
verb, path := "GET", fmt.Sprintf("/api/latest/fleet/teams/%d", teamID)
var responseBody getTeamResponse
if err := c.authenticatedRequest(getTeamRequest{}, verb, path, &responseBody); err != nil {
return nil, err
}
return responseBody.Team, nil
}
// DeleteTeam deletes a team.
func (c *Client) DeleteTeam(teamID uint) error {
verb, path := "DELETE", "/api/latest/fleet/teams/"+strconv.FormatUint(uint64(teamID), 10)
var responseBody deleteTeamResponse
return c.authenticatedRequest(nil, verb, path, &responseBody)
}
// ApplyTeams sends the list of Teams to be applied to the
// Fleet instance.
func (c *Client) ApplyTeams(specs []json.RawMessage, opts fleet.ApplySpecOptions) (map[string]uint, error) {
verb, path := "POST", "/api/latest/fleet/spec/teams"
var responseBody applyTeamSpecsResponse
err := c.authenticatedRequestWithQuery(map[string]interface{}{"specs": specs}, verb, path, &responseBody, opts.RawQuery())
if err != nil {
return nil, err
}
return responseBody.TeamIDsByName, nil
}
// ApplyTeamProfiles sends the list of profiles to be applied for the specified
// team.
func (c *Client) ApplyTeamProfiles(tmName string, profiles []fleet.MDMProfileBatchPayload, opts fleet.ApplySpecOptions) error {
verb, path := "POST", "/api/latest/fleet/mdm/profiles/batch"
query, err := url.ParseQuery(opts.RawQuery())
if err != nil {
return err
}
query.Add("team_name", tmName)
return c.authenticatedRequestWithQuery(map[string]interface{}{"profiles": profiles}, verb, path, nil, query.Encode())
}
// ApplyTeamScripts sends the list of scripts to be applied for the specified
// team.
func (c *Client) ApplyTeamScripts(tmName string, scripts []fleet.ScriptPayload, opts fleet.ApplySpecOptions) error {
verb, path := "POST", "/api/latest/fleet/scripts/batch"
query, err := url.ParseQuery(opts.RawQuery())
if err != nil {
return err
}
query.Add("team_name", tmName)
return c.authenticatedRequestWithQuery(map[string]interface{}{"scripts": scripts}, verb, path, nil, query.Encode())
}