fleet/server/service/service_targets.go
Zach Wasserman 417ef2c9b6
Refactor teams service methods (#910)
- Move team-related service methods to `ee/server/service`.
- Instantiate different service on startup based on license key.
- Refactor service errors into separate package.
- Add support for running E2E tests in both Core and Basic tiers.
2021-05-31 17:07:51 -07:00

77 lines
1.7 KiB
Go

package service
import (
"context"
"github.com/fleetdm/fleet/server/contexts/viewer"
"github.com/fleetdm/fleet/server/kolide"
)
func (svc Service) SearchTargets(ctx context.Context, matchQuery string, queryID *uint, targets kolide.HostTargets) (*kolide.TargetSearchResults, error) {
vc, ok := viewer.FromContext(ctx)
if !ok {
return nil, kolide.ErrNoContext
}
includeObserver := false
if queryID != nil {
query, err := svc.ds.Query(*queryID)
if err != nil {
return nil, err
}
includeObserver = query.ObserverCanRun
}
filter := kolide.TeamFilter{User: vc.User, IncludeObserver: includeObserver}
results := &kolide.TargetSearchResults{}
hosts, err := svc.ds.SearchHosts(filter, matchQuery, targets.HostIDs...)
if err != nil {
return nil, err
}
for _, h := range hosts {
results.Hosts = append(results.Hosts, h)
}
labels, err := svc.ds.SearchLabels(filter, matchQuery, targets.LabelIDs...)
if err != nil {
return nil, err
}
results.Labels = labels
teams, err := svc.ds.SearchTeams(filter, matchQuery, targets.TeamIDs...)
if err != nil {
return nil, err
}
results.Teams = teams
return results, nil
}
func (svc Service) CountHostsInTargets(ctx context.Context, queryID *uint, targets kolide.HostTargets) (*kolide.TargetMetrics, error) {
vc, ok := viewer.FromContext(ctx)
if !ok {
return nil, kolide.ErrNoContext
}
includeObserver := false
if queryID != nil {
query, err := svc.ds.Query(*queryID)
if err != nil {
return nil, err
}
includeObserver = query.ObserverCanRun
}
filter := kolide.TeamFilter{User: vc.User, IncludeObserver: includeObserver}
metrics, err := svc.ds.CountHostsInTargets(filter, targets, svc.clock.Now())
if err != nil {
return nil, err
}
return &metrics, nil
}