fleet/server/service/service_agent_options.go
Martavis Parker 5b2cac31d9
Agent options added to organization settings (#1120)
* #511 refactored update options - new params & ts

* updated server to include agent_options for read and update

* added agent options form to org settings

* #511 finished connecting agent form to server

* #511 fixing api to save/read agent options

* #511 linted

* #511 fixed reading & updating agent options

* #511 api fixes to support agent options

* #511 removed log

* Fix json.RawMessage pointers in tests

Co-authored-by: Zach Wasserman <zach@fleetdm.com>
2021-06-17 13:47:15 -07:00

43 lines
1.2 KiB
Go

package service
import (
"context"
"encoding/json"
"github.com/fleetdm/fleet/server/fleet"
"github.com/pkg/errors"
)
func (svc *Service) AgentOptionsForHost(ctx context.Context, host *fleet.Host) (json.RawMessage, error) {
// If host has a team and team has non-empty options, prioritize that.
if host.TeamID != nil {
team, err := svc.ds.Team(*host.TeamID)
if err != nil {
return nil, errors.Wrap(err, "load team for host")
}
if team.AgentOptions != nil && len(*team.AgentOptions) > 0 {
var options fleet.AgentOptions
if err := json.Unmarshal(*team.AgentOptions, &options); err != nil {
return nil, errors.Wrap(err, "unmarshal team agent options")
}
return options.ForPlatform(host.Platform), nil
}
}
// Otherwise return the appropriate override for global options.
appConfig, err := svc.ds.AppConfig()
if err != nil {
return nil, errors.Wrap(err, "load global agent options")
}
var options fleet.AgentOptions
if appConfig.AgentOptions != nil {
if err := json.Unmarshal(*appConfig.AgentOptions, &options); err != nil {
return nil, errors.Wrap(err, "unmarshal global agent options")
}
}
return options.ForPlatform(host.Platform), nil
}