2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-07 20:22:31 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/contexts/viewer"
|
2016-09-26 18:48:55 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
2016-09-04 05:13:42 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
func (svc service) ListQueries(ctx context.Context, opt kolide.ListOptions) ([]*kolide.Query, error) {
|
2016-10-14 15:59:27 +00:00
|
|
|
return svc.ds.ListQueries(opt)
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (svc service) GetQuery(ctx context.Context, id uint) (*kolide.Query, error) {
|
|
|
|
return svc.ds.Query(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc service) NewQuery(ctx context.Context, p kolide.QueryPayload) (*kolide.Query, error) {
|
2016-12-01 21:21:27 +00:00
|
|
|
query := &kolide.Query{Saved: true}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
if p.Name != nil {
|
|
|
|
query.Name = *p.Name
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:05:02 +00:00
|
|
|
if p.Description != nil {
|
|
|
|
query.Description = *p.Description
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
if p.Query != nil {
|
|
|
|
query.Query = *p.Query
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Interval != nil {
|
|
|
|
query.Interval = *p.Interval
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Snapshot != nil {
|
|
|
|
query.Snapshot = *p.Snapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Differential != nil {
|
|
|
|
query.Differential = *p.Differential
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Platform != nil {
|
|
|
|
query.Platform = *p.Platform
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Version != nil {
|
|
|
|
query.Version = *p.Version
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:22:31 +00:00
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if ok {
|
|
|
|
query.AuthorID = vc.UserID()
|
|
|
|
}
|
|
|
|
|
2016-10-31 22:00:04 +00:00
|
|
|
query, err := svc.ds.NewQuery(query)
|
2016-09-04 05:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-10-31 22:00:04 +00:00
|
|
|
return query, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (svc service) ModifyQuery(ctx context.Context, id uint, p kolide.QueryPayload) (*kolide.Query, error) {
|
|
|
|
query, err := svc.ds.Query(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Name != nil {
|
|
|
|
query.Name = *p.Name
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:05:02 +00:00
|
|
|
if p.Description != nil {
|
|
|
|
query.Description = *p.Description
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
if p.Query != nil {
|
|
|
|
query.Query = *p.Query
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Interval != nil {
|
|
|
|
query.Interval = *p.Interval
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Snapshot != nil {
|
|
|
|
query.Snapshot = *p.Snapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Differential != nil {
|
|
|
|
query.Differential = *p.Differential
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Platform != nil {
|
|
|
|
query.Platform = *p.Platform
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Version != nil {
|
|
|
|
query.Version = *p.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.ds.SaveQuery(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return query, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc service) DeleteQuery(ctx context.Context, id uint) error {
|
|
|
|
query, err := svc.ds.Query(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.ds.DeleteQuery(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|