fleet/server/service/service_targets.go
Martin Angers 4f4185372d
Add support for context in datastore/mysql layer (#1962)
This is just to pass down the context to the datastore layer, it doesn't
use it just yet - this will be in a follow-up PR.
2021-09-14 08:11:07 -04:00

85 lines
2.0 KiB
Go

package service
import (
"context"
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
"github.com/fleetdm/fleet/v4/server/fleet"
)
func (svc Service) SearchTargets(ctx context.Context, matchQuery string, queryID *uint, targets fleet.HostTargets) (*fleet.TargetSearchResults, error) {
if err := svc.authz.Authorize(ctx, &fleet.Target{}, fleet.ActionRead); err != nil {
return nil, err
}
vc, ok := viewer.FromContext(ctx)
if !ok {
return nil, fleet.ErrNoContext
}
includeObserver := false
if queryID != nil {
query, err := svc.ds.Query(ctx, *queryID)
if err != nil {
return nil, err
}
includeObserver = query.ObserverCanRun
}
filter := fleet.TeamFilter{User: vc.User, IncludeObserver: includeObserver}
results := &fleet.TargetSearchResults{}
hosts, err := svc.ds.SearchHosts(ctx, 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(ctx, filter, matchQuery, targets.LabelIDs...)
if err != nil {
return nil, err
}
results.Labels = labels
teams, err := svc.ds.SearchTeams(ctx, 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 fleet.HostTargets) (*fleet.TargetMetrics, error) {
if err := svc.authz.Authorize(ctx, &fleet.Target{}, fleet.ActionRead); err != nil {
return nil, err
}
vc, ok := viewer.FromContext(ctx)
if !ok {
return nil, fleet.ErrNoContext
}
includeObserver := false
if queryID != nil {
query, err := svc.ds.Query(ctx, *queryID)
if err != nil {
return nil, err
}
includeObserver = query.ObserverCanRun
}
filter := fleet.TeamFilter{User: vc.User, IncludeObserver: includeObserver}
metrics, err := svc.ds.CountHostsInTargets(ctx, filter, targets, svc.clock.Now())
if err != nil {
return nil, err
}
return &metrics, nil
}