2016-10-06 00:10:44 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-12-06 17:37:22 +00:00
|
|
|
"time"
|
|
|
|
|
2016-10-06 00:10:44 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/kolide"
|
2016-10-06 00:10:44 +00:00
|
|
|
)
|
|
|
|
|
2020-03-31 23:14:26 +00:00
|
|
|
// HostResponse is the response struct that contains the full host information
|
|
|
|
// along with the host online status and the "display text" to be used when
|
|
|
|
// rendering in the UI.
|
|
|
|
type HostResponse struct {
|
2016-10-12 21:41:35 +00:00
|
|
|
kolide.Host
|
2020-03-30 02:19:54 +00:00
|
|
|
Status kolide.HostStatus `json:"status"`
|
|
|
|
DisplayText string `json:"display_text"`
|
2017-01-10 05:05:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 23:14:26 +00:00
|
|
|
func hostResponseForHost(ctx context.Context, svc kolide.Service, host *kolide.Host) (*HostResponse, error) {
|
|
|
|
return &HostResponse{
|
2017-01-10 05:05:18 +00:00
|
|
|
Host: *host,
|
2017-04-18 17:39:50 +00:00
|
|
|
Status: host.Status(time.Now()),
|
2017-01-10 05:05:18 +00:00
|
|
|
DisplayText: host.HostName,
|
|
|
|
}, nil
|
2016-10-12 21:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 00:10:44 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Host
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getHostRequest struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type getHostResponse struct {
|
2020-03-31 23:14:26 +00:00
|
|
|
Host *HostResponse `json:"host"`
|
2016-10-12 21:41:35 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getHostResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeGetHostEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getHostRequest)
|
|
|
|
host, err := svc.GetHost(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getHostResponse{Err: err}, nil
|
|
|
|
}
|
2017-01-10 05:05:18 +00:00
|
|
|
|
2017-04-18 17:39:50 +00:00
|
|
|
resp, err := hostResponseForHost(ctx, svc, host)
|
2017-01-10 05:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return getHostResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:16:17 +00:00
|
|
|
return getHostResponse{
|
2017-01-10 05:05:18 +00:00
|
|
|
Host: resp,
|
2017-01-04 21:16:17 +00:00
|
|
|
}, nil
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List Hosts
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
type listHostsRequest struct {
|
2020-03-30 02:19:54 +00:00
|
|
|
ListOptions kolide.HostListOptions
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
type listHostsResponse struct {
|
2020-03-31 23:14:26 +00:00
|
|
|
Hosts []HostResponse `json:"hosts"`
|
2016-10-12 21:41:35 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func (r listHostsResponse) error() error { return r.Err }
|
2016-10-06 00:10:44 +00:00
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func makeListHostsEndpoint(svc kolide.Service) endpoint.Endpoint {
|
2016-10-06 00:10:44 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2016-10-13 18:21:47 +00:00
|
|
|
req := request.(listHostsRequest)
|
|
|
|
hosts, err := svc.ListHosts(ctx, req.ListOptions)
|
2016-10-06 00:10:44 +00:00
|
|
|
if err != nil {
|
2016-10-07 17:26:48 +00:00
|
|
|
return listHostsResponse{Err: err}, nil
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 23:14:26 +00:00
|
|
|
hostResponses := make([]HostResponse, len(hosts))
|
2017-01-10 05:05:18 +00:00
|
|
|
for i, host := range hosts {
|
2017-04-18 17:39:50 +00:00
|
|
|
h, err := hostResponseForHost(ctx, svc, host)
|
2017-01-10 05:05:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return listHostsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hostResponses[i] = *h
|
2017-01-04 21:16:17 +00:00
|
|
|
}
|
2017-01-10 05:05:18 +00:00
|
|
|
return listHostsResponse{Hosts: hostResponses}, nil
|
2017-01-04 21:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Host Summary
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getHostSummaryResponse struct {
|
|
|
|
kolide.HostSummary
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r getHostSummaryResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeGetHostSummaryEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
summary, err := svc.GetHostSummary(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return getHostSummaryResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := getHostSummaryResponse{
|
|
|
|
HostSummary: *summary,
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Host
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteHostRequest struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteHostResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteHostResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeDeleteHostEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deleteHostRequest)
|
|
|
|
err := svc.DeleteHost(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteHostResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteHostResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|