fleet/server/service/endpoint_hosts.go

280 lines
7.8 KiB
Go
Raw Normal View History

package service
import (
"context"
"time"
2021-06-26 04:46:51 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
"github.com/go-kit/kit/endpoint"
)
// 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 {
*fleet.Host
Status fleet.HostStatus `json:"status"`
DisplayText string `json:"display_text"`
Labels []fleet.Label `json:"labels,omitempty"`
}
func hostResponseForHost(ctx context.Context, svc fleet.Service, host *fleet.Host) (*HostResponse, error) {
return &HostResponse{
Host: host,
Status: host.Status(time.Now()),
DisplayText: host.Hostname,
}, nil
}
// HostDetailResponse is the response struct that contains the full host information
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
// with the HostDetail details.
type HostDetailResponse struct {
fleet.HostDetail
Status fleet.HostStatus `json:"status"`
DisplayText string `json:"display_text"`
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
}
func hostDetailResponseForHost(ctx context.Context, svc fleet.Service, host *fleet.HostDetail) (*HostDetailResponse, error) {
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
return &HostDetailResponse{
HostDetail: *host,
Status: host.Status(time.Now()),
DisplayText: host.Hostname,
}, nil
}
////////////////////////////////////////////////////////////////////////////////
// Get Host
////////////////////////////////////////////////////////////////////////////////
type getHostRequest struct {
ID uint `json:"id"`
}
type getHostResponse struct {
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
Host *HostDetailResponse `json:"host"`
Err error `json:"error,omitempty"`
}
func (r getHostResponse) error() error { return r.Err }
func makeGetHostEndpoint(svc fleet.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
}
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
resp, err := hostDetailResponseForHost(ctx, svc, host)
if err != nil {
return getHostResponse{Err: err}, nil
}
return getHostResponse{
Host: resp,
}, nil
}
}
////////////////////////////////////////////////////////////////////////////////
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
// Get Host By Identifier
////////////////////////////////////////////////////////////////////////////////
type hostByIdentifierRequest struct {
Identifier string `json:"identifier"`
}
func makeHostByIdentifierEndpoint(svc fleet.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(hostByIdentifierRequest)
host, err := svc.HostByIdentifier(ctx, req.Identifier)
if err != nil {
return getHostResponse{Err: err}, nil
}
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
resp, err := hostDetailResponseForHost(ctx, svc, host)
if err != nil {
return getHostResponse{Err: err}, nil
}
return getHostResponse{
Host: resp,
}, nil
}
}
////////////////////////////////////////////////////////////////////////////////
// List Hosts
////////////////////////////////////////////////////////////////////////////////
type listHostsRequest struct {
ListOptions fleet.HostListOptions
}
2016-10-07 17:26:48 +00:00
type listHostsResponse struct {
Hosts []HostResponse `json:"hosts"`
Err error `json:"error,omitempty"`
}
2016-10-07 17:26:48 +00:00
func (r listHostsResponse) error() error { return r.Err }
func makeListHostsEndpoint(svc fleet.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listHostsRequest)
hosts, err := svc.ListHosts(ctx, req.ListOptions)
if err != nil {
2016-10-07 17:26:48 +00:00
return listHostsResponse{Err: err}, nil
}
hostResponses := make([]HostResponse, len(hosts))
for i, host := range hosts {
h, err := hostResponseForHost(ctx, svc, host)
if err != nil {
return listHostsResponse{Err: err}, nil
}
hostResponses[i] = *h
}
return listHostsResponse{Hosts: hostResponses}, nil
}
}
////////////////////////////////////////////////////////////////////////////////
// Get Host Summary
////////////////////////////////////////////////////////////////////////////////
type getHostSummaryResponse struct {
fleet.HostSummary
Err error `json:"error,omitempty"`
}
func (r getHostSummaryResponse) error() error { return r.Err }
func makeGetHostSummaryEndpoint(svc fleet.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,
}
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 fleet.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
}
}
////////////////////////////////////////////////////////////////////////////////
// Add Hosts to Team
////////////////////////////////////////////////////////////////////////////////
type addHostsToTeamRequest struct {
TeamID *uint `json:"team_id"`
HostIDs []uint `json:"hosts"`
}
type addHostsToTeamResponse struct {
Err error `json:"error,omitempty"`
}
func (r addHostsToTeamResponse) error() error { return r.Err }
func makeAddHostsToTeamEndpoint(svc fleet.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(addHostsToTeamRequest)
err := svc.AddHostsToTeam(ctx, req.TeamID, req.HostIDs)
if err != nil {
return addHostsToTeamResponse{Err: err}, nil
}
return addHostsToTeamResponse{}, err
}
}
2021-05-19 17:16:54 +00:00
////////////////////////////////////////////////////////////////////////////////
// Add Hosts to Team by Filter
////////////////////////////////////////////////////////////////////////////////
type addHostsToTeamByFilterRequest struct {
TeamID *uint `json:"team_id"`
Filters struct {
MatchQuery string `json:"query"`
Status fleet.HostStatus `json:"status"`
LabelID *uint `json:"label_id"`
} `json:"filters"`
}
type addHostsToTeamByFilterResponse struct {
Err error `json:"error,omitempty"`
}
func (r addHostsToTeamByFilterResponse) error() error { return r.Err }
func makeAddHostsToTeamByFilterEndpoint(svc fleet.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(addHostsToTeamByFilterRequest)
listOpt := fleet.HostListOptions{
ListOptions: fleet.ListOptions{
MatchQuery: req.Filters.MatchQuery,
},
StatusFilter: req.Filters.Status,
}
err := svc.AddHostsToTeamByFilter(ctx, req.TeamID, listOpt, req.Filters.LabelID)
if err != nil {
return addHostsToTeamByFilterResponse{Err: err}, nil
}
return addHostsToTeamByFilterResponse{}, err
}
}
////////////////////////////////////////////////////////////////////////////////
// Refetch Host
////////////////////////////////////////////////////////////////////////////////
type refetchHostRequest struct {
ID uint `json:"id"`
}
type refetchHostResponse struct {
Err error `json:"error,omitempty"`
}
func (r refetchHostResponse) error() error { return r.Err }
func makeRefetchHostEndpoint(svc fleet.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(refetchHostRequest)
err := svc.RefetchHost(ctx, req.ID)
if err != nil {
return refetchHostResponse{Err: err}, nil
}
return refetchHostResponse{}, nil
}
}