2016-10-03 03:14:35 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-02-18 20:52:43 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2016-10-03 03:14:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type getLabelRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
2016-11-03 01:17:23 +00:00
|
|
|
type labelResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
fleet.Label
|
2020-05-21 18:11:02 +00:00
|
|
|
DisplayText string `json:"display_text"`
|
|
|
|
Count int `json:"count"`
|
|
|
|
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 }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func labelResponseForLabel(ctx context.Context, svc fleet.Service, label *fleet.Label) (*labelResponse, error) {
|
2017-01-16 22:57:05 +00:00
|
|
|
return &labelResponse{
|
2020-05-21 18:11:02 +00:00
|
|
|
Label: *label,
|
|
|
|
DisplayText: label.Name,
|
|
|
|
Count: label.HostCount,
|
2017-01-16 22:57:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Label
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2017-02-14 22:28:34 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetLabelEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-10-03 03:14:35 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:09:08 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Create Label
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type createLabelRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
payload fleet.LabelPayload
|
2018-06-18 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type createLabelResponse struct {
|
|
|
|
Label labelResponse `json:"label"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r createLabelResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeCreateLabelEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-18 17:09:08 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(createLabelRequest)
|
|
|
|
|
|
|
|
label, err := svc.NewLabel(ctx, req.payload)
|
|
|
|
if err != nil {
|
|
|
|
return createLabelResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
labelResp, err := labelResponseForLabel(ctx, svc, label)
|
|
|
|
if err != nil {
|
|
|
|
return createLabelResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return createLabelResponse{Label: *labelResp}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modify Label
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyLabelRequest struct {
|
|
|
|
ID uint
|
2021-06-06 22:07:29 +00:00
|
|
|
payload fleet.ModifyLabelPayload
|
2018-06-18 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type modifyLabelResponse struct {
|
|
|
|
Label labelResponse `json:"label"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r modifyLabelResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeModifyLabelEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-18 17:09:08 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(modifyLabelRequest)
|
|
|
|
label, err := svc.ModifyLabel(ctx, req.ID, req.payload)
|
|
|
|
if err != nil {
|
|
|
|
return modifyLabelResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
labelResp, err := labelResponseForLabel(ctx, svc, label)
|
|
|
|
if err != nil {
|
|
|
|
return modifyLabelResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return modifyLabelResponse{Label: *labelResp}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-06-06 22:07:29 +00:00
|
|
|
ListOptions fleet.ListOptions
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeListLabelsEndpoint(svc fleet.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 02:19:54 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List Hosts in Label
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type listHostsInLabelRequest struct {
|
|
|
|
ID uint
|
2021-06-06 22:07:29 +00:00
|
|
|
ListOptions fleet.HostListOptions
|
2020-03-30 02:19:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeListHostsInLabelEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2020-03-30 02:19:54 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(listHostsInLabelRequest)
|
|
|
|
hosts, err := svc.ListHostsInLabel(ctx, req.ID, req.ListOptions)
|
|
|
|
if err != nil {
|
|
|
|
return listLabelsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hostResponses := make([]HostResponse, len(hosts))
|
|
|
|
for i, host := range hosts {
|
2021-05-26 01:53:22 +00:00
|
|
|
h, err := hostResponseForHost(ctx, svc, host)
|
2020-03-30 02:19:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return listHostsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hostResponses[i] = *h
|
|
|
|
}
|
|
|
|
return listHostsResponse{Hosts: hostResponses}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeDeleteLabelEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-10-03 03:14:35 +00:00
|
|
|
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
|
|
|
|
2018-06-25 20:56:59 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Label By ID
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteLabelByIDRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteLabelByIDResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteLabelByIDResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeDeleteLabelByIDEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-25 20:56:59 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteLabelByIDRequest)
|
|
|
|
err := svc.DeleteLabelByID(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteLabelByIDResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteLabelByIDResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 01:01:57 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Apply Label Specs
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type applyLabelSpecsRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Specs []*fleet.LabelSpec `json:"specs"`
|
2018-05-04 01:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type applyLabelSpecsResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r applyLabelSpecsResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeApplyLabelSpecsEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-05-04 01:01:57 +00:00
|
|
|
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 {
|
2021-06-06 22:07:29 +00:00
|
|
|
Specs []*fleet.LabelSpec `json:"specs"`
|
2018-05-04 01:01:57 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getLabelSpecsResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetLabelSpecsEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-05-04 01:01:57 +00:00
|
|
|
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 {
|
2021-06-06 22:07:29 +00:00
|
|
|
Spec *fleet.LabelSpec `json:"specs,omitempty"`
|
2018-05-08 01:54:29 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getLabelSpecResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetLabelSpecEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-05-08 01:54:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|