fleet/server/service/logging_hosts.go
Zach Wasserman 6215acdd1b
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 13:05:02 -08:00

80 lines
1.5 KiB
Go

package service
import (
"context"
"time"
"github.com/fleetdm/fleet/server/kolide"
)
func (mw loggingMiddleware) ListHosts(ctx context.Context, opt kolide.HostListOptions) ([]*kolide.Host, error) {
var (
hosts []*kolide.Host
err error
)
defer func(begin time.Time) {
_ = mw.loggerDebug(err).Log(
"method", "ListHosts",
"err", err,
"took", time.Since(begin),
)
}(time.Now())
hosts, err = mw.Service.ListHosts(ctx, opt)
return hosts, err
}
func (mw loggingMiddleware) GetHost(ctx context.Context, id uint) (*kolide.HostDetail, error) {
var (
host *kolide.HostDetail
err error
)
defer func(begin time.Time) {
_ = mw.loggerDebug(err).Log(
"method", "GetHost",
"err", err,
"took", time.Since(begin),
)
}(time.Now())
host, err = mw.Service.GetHost(ctx, id)
return host, err
}
func (mw loggingMiddleware) GetHostSummary(ctx context.Context) (*kolide.HostSummary, error) {
var (
summary *kolide.HostSummary
err error
)
defer func(begin time.Time) {
_ = mw.loggerDebug(err).Log(
"method", "GetHostSummary",
"err", err,
"took", time.Since(begin),
)
}(time.Now())
summary, err = mw.Service.GetHostSummary(ctx)
return summary, err
}
func (mw loggingMiddleware) DeleteHost(ctx context.Context, id uint) error {
var (
err error
)
defer func(begin time.Time) {
_ = mw.loggerInfo(err).Log(
"method", "DeleteHost",
"err", err,
"took", time.Since(begin),
)
}(time.Now())
err = mw.Service.DeleteHost(ctx, id)
return err
}