2016-10-03 03:14:35 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/kolide"
|
2016-10-03 03:14:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type getLabelRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
2016-11-03 01:17:23 +00:00
|
|
|
type labelResponse struct {
|
|
|
|
kolide.Label
|
2016-12-06 17:37:22 +00:00
|
|
|
DisplayText string `json:"display_text"`
|
|
|
|
Count uint `json:"count"`
|
|
|
|
Online uint `json:"online"`
|
|
|
|
Offline uint `json:"offline"`
|
|
|
|
MissingInAction uint `json:"missing_in_action"`
|
2017-01-23 15:37:09 +00:00
|
|
|
HostIDs []uint `json:"host_ids"`
|
2016-11-03 01:17:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
type getLabelResponse struct {
|
2016-11-03 01:17:23 +00:00
|
|
|
Label labelResponse `json:"label"`
|
2016-10-11 16:22:11 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getLabelResponse) error() error { return r.Err }
|
|
|
|
|
2017-01-16 22:57:05 +00:00
|
|
|
func labelResponseForLabel(ctx context.Context, svc kolide.Service, label *kolide.Label) (*labelResponse, error) {
|
|
|
|
metrics, err := svc.CountHostsInTargets(ctx, nil, []uint{label.ID})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hosts, err := svc.HostIDsForLabel(label.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &labelResponse{
|
|
|
|
*label,
|
|
|
|
label.Name,
|
|
|
|
metrics.TotalHosts,
|
|
|
|
metrics.OnlineHosts,
|
|
|
|
metrics.OfflineHosts,
|
|
|
|
metrics.MissingInActionHosts,
|
|
|
|
hosts,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Label
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2017-02-14 22:28:34 +00:00
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
func makeGetLabelEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getLabelRequest)
|
|
|
|
label, err := svc.GetLabel(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getLabelResponse{Err: err}, nil
|
|
|
|
}
|
2017-01-16 22:57:05 +00:00
|
|
|
resp, err := labelResponseForLabel(ctx, svc, label)
|
2016-11-03 01:17:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return getLabelResponse{Err: err}, nil
|
|
|
|
}
|
2017-01-16 22:57:05 +00:00
|
|
|
return getLabelResponse{Label: *resp}, nil
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-10-07 17:26:48 +00:00
|
|
|
// List Labels
|
2016-10-03 03:14:35 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
type listLabelsRequest struct {
|
|
|
|
ListOptions kolide.ListOptions
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
type listLabelsResponse struct {
|
2016-11-03 01:17:23 +00:00
|
|
|
Labels []labelResponse `json:"labels"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func (r listLabelsResponse) error() error { return r.Err }
|
2016-10-03 03:14:35 +00:00
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func makeListLabelsEndpoint(svc kolide.Service) endpoint.Endpoint {
|
2016-10-03 03:14:35 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2016-10-13 18:21:47 +00:00
|
|
|
req := request.(listLabelsRequest)
|
|
|
|
labels, err := svc.ListLabels(ctx, req.ListOptions)
|
2016-10-03 03:14:35 +00:00
|
|
|
if err != nil {
|
2016-10-07 17:26:48 +00:00
|
|
|
return listLabelsResponse{Err: err}, nil
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
2016-10-07 17:26:48 +00:00
|
|
|
|
2016-11-03 01:17:23 +00:00
|
|
|
resp := listLabelsResponse{}
|
2016-10-03 03:14:35 +00:00
|
|
|
for _, label := range labels {
|
2017-01-16 22:57:05 +00:00
|
|
|
labelResp, err := labelResponseForLabel(ctx, svc, label)
|
2016-11-03 01:17:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return listLabelsResponse{Err: err}, nil
|
|
|
|
}
|
2017-01-16 22:57:05 +00:00
|
|
|
resp.Labels = append(resp.Labels, *labelResp)
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Label
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteLabelRequest struct {
|
2018-05-04 18:05:55 +00:00
|
|
|
Name string
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type deleteLabelResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteLabelResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeDeleteLabelEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteLabelRequest)
|
2018-05-04 18:05:55 +00:00
|
|
|
err := svc.DeleteLabel(ctx, req.Name)
|
2016-10-03 03:14:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return deleteLabelResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteLabelResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 01:01:57 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Apply Label Specs
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type applyLabelSpecsRequest struct {
|
|
|
|
Specs []*kolide.LabelSpec `json:"specs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type applyLabelSpecsResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r applyLabelSpecsResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeApplyLabelSpecsEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(applyLabelSpecsRequest)
|
|
|
|
err := svc.ApplyLabelSpecs(ctx, req.Specs)
|
|
|
|
if err != nil {
|
|
|
|
return applyLabelSpecsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return applyLabelSpecsResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Label Specs
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getLabelSpecsResponse struct {
|
|
|
|
Specs []*kolide.LabelSpec `json:"specs"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getLabelSpecsResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeGetLabelSpecsEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
specs, err := svc.GetLabelSpecs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return getLabelSpecsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return getLabelSpecsResponse{Specs: specs}, nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-08 01:54:29 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Label Spec
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getLabelSpecResponse struct {
|
|
|
|
Spec *kolide.LabelSpec `json:"specs,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getLabelSpecResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeGetLabelSpecEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getGenericSpecRequest)
|
|
|
|
spec, err := svc.GetLabelSpec(ctx, req.Name)
|
|
|
|
if err != nil {
|
|
|
|
return getLabelSpecResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return getLabelSpecResponse{Spec: spec}, nil
|
|
|
|
}
|
|
|
|
}
|