2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getQueryRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type getQueryResponse struct {
|
2016-10-11 16:22:11 +00:00
|
|
|
Query *kolide.Query `json:"query,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getQueryResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeGetQueryEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getQueryRequest)
|
|
|
|
query, err := svc.GetQuery(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getQueryResponse{Err: err}, nil
|
|
|
|
}
|
2016-10-11 16:22:11 +00:00
|
|
|
return getQueryResponse{query, nil}, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-10-07 17:26:48 +00:00
|
|
|
// List Queries
|
2016-09-04 05:13:42 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-10-13 18:21:47 +00:00
|
|
|
type listQueriesRequest struct {
|
|
|
|
ListOptions kolide.ListOptions
|
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
type listQueriesResponse struct {
|
2016-10-11 16:22:11 +00:00
|
|
|
Queries []kolide.Query `json:"queries"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func (r listQueriesResponse) error() error { return r.Err }
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func makeListQueriesEndpoint(svc kolide.Service) endpoint.Endpoint {
|
2016-09-04 05:13:42 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2016-10-13 18:21:47 +00:00
|
|
|
req := request.(listQueriesRequest)
|
|
|
|
queries, err := svc.ListQueries(ctx, req.ListOptions)
|
2016-09-04 05:13:42 +00:00
|
|
|
if err != nil {
|
2016-10-07 17:26:48 +00:00
|
|
|
return listQueriesResponse{Err: err}, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
2016-10-07 17:26:48 +00:00
|
|
|
|
2016-10-11 16:22:11 +00:00
|
|
|
resp := listQueriesResponse{Queries: []kolide.Query{}}
|
2016-09-04 05:13:42 +00:00
|
|
|
for _, query := range queries {
|
2016-10-11 16:22:11 +00:00
|
|
|
resp.Queries = append(resp.Queries, *query)
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Create Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type createQueryRequest struct {
|
|
|
|
payload kolide.QueryPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type createQueryResponse struct {
|
2016-10-11 16:22:11 +00:00
|
|
|
Query *kolide.Query `json:"query,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r createQueryResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeCreateQueryEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(createQueryRequest)
|
|
|
|
query, err := svc.NewQuery(ctx, req.payload)
|
|
|
|
if err != nil {
|
|
|
|
return createQueryResponse{Err: err}, nil
|
|
|
|
}
|
2016-10-11 16:22:11 +00:00
|
|
|
return createQueryResponse{query, nil}, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modify Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyQueryRequest struct {
|
|
|
|
ID uint
|
|
|
|
payload kolide.QueryPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type modifyQueryResponse struct {
|
2016-10-11 16:22:11 +00:00
|
|
|
Query *kolide.Query `json:"query,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r modifyQueryResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeModifyQueryEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(modifyQueryRequest)
|
|
|
|
query, err := svc.ModifyQuery(ctx, req.ID, req.payload)
|
|
|
|
if err != nil {
|
|
|
|
return modifyQueryResponse{Err: err}, nil
|
|
|
|
}
|
2016-10-11 16:22:11 +00:00
|
|
|
return modifyQueryResponse{query, nil}, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Query
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteQueryRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteQueryResponse struct {
|
2016-09-06 15:02:11 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteQueryResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeDeleteQueryEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteQueryRequest)
|
|
|
|
err := svc.DeleteQuery(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteQueryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteQueryResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|