fleet/server/service/service_packs.go
Mike Arpaia f109b14f9d Moving query attributes from the query object to the pack-query relationship (#559)
* Moving query attributes from the query object to the pack-query relationship

* some additional tests

* http request parsing test

* QueryOptions in new test_util code

* initial scaffolding of new request structures

* service and datastore

* test outline

* l2 merge conflict scrub

* service tests for scheduled query service

* service and datastore tests

* most endpoints and transports

* order of values are not deterministic with inmem

* transport tests

* rename PackQuery to ScheduledQuery

* removing existing implementation of adding queries to packs

* accounting for the new argument to NewQuery

* fix alignment in sql query

* removing underscore

* add removed to the datastore

* removed differential from the schema
2016-12-13 14:22:05 -08:00

168 lines
3.5 KiB
Go

package service
import (
"github.com/kolide/kolide-ose/server/contexts/viewer"
"github.com/kolide/kolide-ose/server/kolide"
"golang.org/x/net/context"
)
func (svc service) ListPacks(ctx context.Context, opt kolide.ListOptions) ([]*kolide.Pack, error) {
return svc.ds.ListPacks(opt)
}
func (svc service) GetPack(ctx context.Context, id uint) (*kolide.Pack, error) {
return svc.ds.Pack(id)
}
func (svc service) NewPack(ctx context.Context, p kolide.PackPayload) (*kolide.Pack, error) {
var pack kolide.Pack
if p.Name != nil {
pack.Name = *p.Name
}
if p.Description != nil {
pack.Description = *p.Description
}
if p.Platform != nil {
pack.Platform = *p.Platform
}
if p.Disabled != nil {
pack.Disabled = *p.Disabled
}
vc, ok := viewer.FromContext(ctx)
if ok {
if createdBy := vc.UserID(); createdBy != uint(0) {
pack.CreatedBy = createdBy
}
}
_, err := svc.ds.NewPack(&pack)
if err != nil {
return nil, err
}
return &pack, nil
}
func (svc service) ModifyPack(ctx context.Context, id uint, p kolide.PackPayload) (*kolide.Pack, error) {
pack, err := svc.ds.Pack(id)
if err != nil {
return nil, err
}
if p.Name != nil {
pack.Name = *p.Name
}
if p.Description != nil {
pack.Description = *p.Description
}
if p.Platform != nil {
pack.Platform = *p.Platform
}
if p.Disabled != nil {
pack.Disabled = *p.Disabled
}
err = svc.ds.SavePack(pack)
if err != nil {
return nil, err
}
return pack, err
}
func (svc service) DeletePack(ctx context.Context, id uint) error {
return svc.ds.DeletePack(id)
}
func (svc service) AddLabelToPack(ctx context.Context, lid, pid uint) error {
return svc.ds.AddLabelToPack(lid, pid)
}
func (svc service) ListLabelsForPack(ctx context.Context, pid uint) ([]*kolide.Label, error) {
labels, err := svc.ds.ListLabelsForPack(pid)
if err != nil {
return nil, err
}
return labels, nil
}
func (svc service) RemoveLabelFromPack(ctx context.Context, lid, pid uint) error {
pack, err := svc.ds.Pack(pid)
if err != nil {
return err
}
label, err := svc.ds.Label(lid)
if err != nil {
return err
}
err = svc.ds.RemoveLabelFromPack(label, pack)
if err != nil {
return err
}
return nil
}
func (svc service) ListHostsInPack(ctx context.Context, pid uint, opt kolide.ListOptions) ([]*kolide.Host, error) {
return svc.ds.ListHostsInPack(pid, opt)
}
func (svc service) ListPacksForHost(ctx context.Context, hid uint) ([]*kolide.Pack, error) {
packs := []*kolide.Pack{}
// we will need to give some subset of packs to this host based on the
// labels which this host is known to belong to
allPacks, err := svc.ds.ListPacks(kolide.ListOptions{})
if err != nil {
return nil, err
}
// pull the labels that this host belongs to
labels, err := svc.ds.ListLabelsForHost(hid)
if err != nil {
return nil, err
}
// in order to use o(1) array indexing in an o(n) loop vs a o(n^2) double
// for loop iteration, we must create the array which may be indexed below
labelIDs := map[uint]bool{}
for _, label := range labels {
labelIDs[label.ID] = true
}
for _, pack := range allPacks {
// don't include packs which have been disabled
if pack.Disabled {
continue
}
// for each pack, we must know what labels have been assigned to that
// pack
labelsForPack, err := svc.ds.ListLabelsForPack(pack.ID)
if err != nil {
return nil, err
}
// o(n) iteration to determine whether or not a pack is enabled
// in this case, n is len(labelsForPack)
for _, label := range labelsForPack {
if labelIDs[label.ID] {
packs = append(packs, pack)
break
}
}
}
return packs, nil
}