2016-10-06 00:10:44 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2016-12-06 17:37:22 +00:00
|
|
|
"time"
|
|
|
|
|
2016-10-06 00:10:44 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-12-06 17:37:22 +00:00
|
|
|
const (
|
|
|
|
// StatusOnline host is active
|
|
|
|
StatusOnline string = "online"
|
|
|
|
// StatusOffline no communication with host for OfflineDuration
|
|
|
|
StatusOffline string = "offline"
|
|
|
|
// StatusMIA no communition with host for MIADuration
|
|
|
|
StatusMIA string = "mia"
|
|
|
|
// OfflineDuration if a host hasn't been in communition for this
|
|
|
|
// period it is considered offline
|
|
|
|
OfflineDuration time.Duration = 30 * time.Minute
|
|
|
|
// OfflineDuration if a host hasn't been in communition for this
|
|
|
|
// period it is considered MIA
|
|
|
|
MIADuration time.Duration = 30 * 24 * time.Hour
|
|
|
|
)
|
|
|
|
|
2016-10-12 21:41:35 +00:00
|
|
|
type hostResponse struct {
|
|
|
|
kolide.Host
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
2016-10-06 00:10:44 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Host
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getHostRequest struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type getHostResponse struct {
|
2016-10-12 21:41:35 +00:00
|
|
|
Host *hostResponse `json:"host"`
|
|
|
|
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
|
|
|
|
}
|
2016-10-12 21:41:35 +00:00
|
|
|
return getHostResponse{&hostResponse{*host, svc.HostStatus(ctx, *host)}, nil}, nil
|
2016-10-06 00:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List Hosts
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
type listHostsRequest struct {
|
|
|
|
ListOptions kolide.ListOptions
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
type listHostsResponse struct {
|
2016-10-12 21:41:35 +00:00
|
|
|
Hosts []hostResponse `json:"hosts"`
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-12 21:41:35 +00:00
|
|
|
resp := listHostsResponse{Hosts: []hostResponse{}}
|
2016-10-06 00:10:44 +00:00
|
|
|
for _, host := range hosts {
|
2016-10-12 21:41:35 +00:00
|
|
|
resp.Hosts = append(resp.Hosts, hostResponse{*host, svc.HostStatus(ctx, *host)})
|
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
|
|
|
|
}
|
|
|
|
}
|