2016-12-13 22:22:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/server/fleet"
|
2021-05-17 17:29:50 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2016-12-13 22:22:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Scheduled Queries In Pack
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getScheduledQueriesInPackRequest struct {
|
|
|
|
ID uint
|
2021-06-06 22:07:29 +00:00
|
|
|
ListOptions fleet.ListOptions
|
2016-12-13 22:22:05 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:18:05 +00:00
|
|
|
type scheduledQueryResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
fleet.ScheduledQuery
|
2018-01-03 19:18:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 22:22:05 +00:00
|
|
|
type getScheduledQueriesInPackResponse struct {
|
|
|
|
Scheduled []scheduledQueryResponse `json:"scheduled"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getScheduledQueriesInPackResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetScheduledQueriesInPackEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-12-13 22:22:05 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getScheduledQueriesInPackRequest)
|
|
|
|
resp := getScheduledQueriesInPackResponse{Scheduled: []scheduledQueryResponse{}}
|
|
|
|
|
|
|
|
queries, err := svc.GetScheduledQueriesInPack(ctx, req.ID, req.ListOptions)
|
|
|
|
if err != nil {
|
|
|
|
return getScheduledQueriesInPackResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, q := range queries {
|
|
|
|
resp.Scheduled = append(resp.Scheduled, scheduledQueryResponse{
|
|
|
|
ScheduledQuery: *q,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 14:13:11 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Scheduled Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getScheduledQueryRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type getScheduledQueryResponse struct {
|
2018-06-22 00:06:44 +00:00
|
|
|
Scheduled *scheduledQueryResponse `json:"scheduled,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getScheduledQueryResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetScheduledQueryEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-15 14:13:11 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getScheduledQueryRequest)
|
|
|
|
|
|
|
|
sq, err := svc.GetScheduledQuery(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getScheduledQueryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return getScheduledQueryResponse{
|
2018-06-22 00:06:44 +00:00
|
|
|
Scheduled: &scheduledQueryResponse{
|
2018-06-15 14:13:11 +00:00
|
|
|
ScheduledQuery: *sq,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Schedule Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type scheduleQueryRequest struct {
|
|
|
|
PackID uint `json:"pack_id"`
|
|
|
|
QueryID uint `json:"query_id"`
|
|
|
|
Interval uint `json:"interval"`
|
|
|
|
Snapshot *bool `json:"snapshot"`
|
|
|
|
Removed *bool `json:"removed"`
|
|
|
|
Platform *string `json:"platform"`
|
|
|
|
Version *string `json:"version"`
|
|
|
|
Shard *uint `json:"shard"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type scheduleQueryResponse struct {
|
2018-06-22 00:06:44 +00:00
|
|
|
Scheduled *scheduledQueryResponse `json:"scheduled,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 00:06:44 +00:00
|
|
|
func (r scheduleQueryResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeScheduleQueryEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-15 14:13:11 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(scheduleQueryRequest)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
scheduled, err := svc.ScheduleQuery(ctx, &fleet.ScheduledQuery{
|
2018-06-15 14:13:11 +00:00
|
|
|
PackID: req.PackID,
|
|
|
|
QueryID: req.QueryID,
|
|
|
|
Interval: req.Interval,
|
|
|
|
Snapshot: req.Snapshot,
|
|
|
|
Removed: req.Removed,
|
|
|
|
Platform: req.Platform,
|
|
|
|
Version: req.Version,
|
|
|
|
Shard: req.Shard,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return scheduleQueryResponse{Err: err}, nil
|
|
|
|
}
|
2018-06-22 00:06:44 +00:00
|
|
|
return scheduleQueryResponse{Scheduled: &scheduledQueryResponse{
|
2018-06-15 14:13:11 +00:00
|
|
|
ScheduledQuery: *scheduled,
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modify Scheduled Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyScheduledQueryRequest struct {
|
|
|
|
ID uint
|
2021-06-06 22:07:29 +00:00
|
|
|
payload fleet.ScheduledQueryPayload
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type modifyScheduledQueryResponse struct {
|
2018-06-22 00:06:44 +00:00
|
|
|
Scheduled *scheduledQueryResponse `json:"scheduled,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r modifyScheduledQueryResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeModifyScheduledQueryEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-15 14:13:11 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(modifyScheduledQueryRequest)
|
|
|
|
|
|
|
|
sq, err := svc.ModifyScheduledQuery(ctx, req.ID, req.payload)
|
|
|
|
if err != nil {
|
|
|
|
return modifyScheduledQueryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return modifyScheduledQueryResponse{
|
2018-06-22 00:06:44 +00:00
|
|
|
Scheduled: &scheduledQueryResponse{
|
2018-06-15 14:13:11 +00:00
|
|
|
ScheduledQuery: *sq,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Scheduled Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteScheduledQueryRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteScheduledQueryResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteScheduledQueryResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeDeleteScheduledQueryEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-15 14:13:11 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteScheduledQueryRequest)
|
|
|
|
|
|
|
|
err := svc.DeleteScheduledQuery(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteScheduledQueryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return deleteScheduledQueryResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|