diff --git a/changes/issue-1373-add-status-to-fleetctl-get-command b/changes/issue-1373-add-status-to-fleetctl-get-command new file mode 100644 index 0000000000..9b93b93fed --- /dev/null +++ b/changes/issue-1373-add-status-to-fleetctl-get-command @@ -0,0 +1 @@ +* Change fleetctl get hosts to respond with host "status", "display_text", and "labels". Fixes issue 1373 diff --git a/cmd/fleetctl/get.go b/cmd/fleetctl/get.go index b52aabbf45..8e049489a3 100644 --- a/cmd/fleetctl/get.go +++ b/cmd/fleetctl/get.go @@ -10,6 +10,7 @@ import ( "gopkg.in/guregu/null.v3" "github.com/fleetdm/fleet/v4/server/fleet" + "github.com/fleetdm/fleet/v4/server/service" "github.com/ghodss/yaml" "github.com/olekukonko/tablewriter" "github.com/pkg/errors" @@ -120,7 +121,7 @@ func printSecret(c *cli.Context, secret *fleet.EnrollSecretSpec) error { return printSpec(c, spec) } -func printHost(c *cli.Context, host *fleet.Host) error { +func printHost(c *cli.Context, host *service.HostResponse) error { spec := specGeneric{ Kind: fleet.HostKind, Version: fleet.ApiVersion, @@ -586,7 +587,7 @@ func getHostsCommand() *cli.Command { if c.Bool(jsonFlagName) || c.Bool(yamlFlagName) { for _, host := range hosts { - err = printHost(c, host.Host) + err = printHost(c, &host) if err != nil { return err } diff --git a/cmd/fleetctl/get_test.go b/cmd/fleetctl/get_test.go index 2dbc16647f..7b36e0a3cd 100644 --- a/cmd/fleetctl/get_test.go +++ b/cmd/fleetctl/get_test.go @@ -159,3 +159,83 @@ spec: assert.Equal(t, expectedYaml, runAppForTest(t, []string{"get", "teams", "--yaml"})) assert.Equal(t, expectedJson, runAppForTest(t, []string{"get", "teams", "--json"})) } + +func TestGetHosts(t *testing.T) { + server, ds := runServerWithMockedDS(t) + defer server.Close() + + ds.ListHostsFunc = func(filter fleet.TeamFilter, opt fleet.HostListOptions) ([]*fleet.Host, error) { + hosts := []*fleet.Host{ + { + UpdateCreateTimestamps: fleet.UpdateCreateTimestamps{ + CreateTimestamp: fleet.CreateTimestamp{CreatedAt: time.Time{}}, + UpdateTimestamp: fleet.UpdateTimestamp{UpdatedAt: time.Time{}}, + }, + HostSoftware: fleet.HostSoftware{}, + DetailUpdatedAt: time.Time{}, + LabelUpdatedAt: time.Time{}, + LastEnrolledAt: time.Time{}, + SeenTime: time.Time{}, + ComputerName: "test_host", + Hostname: "test_host", + }, + } + return hosts, nil + } + + expectedText := `+------+-----------+----------+-----------------+--------+ +| UUID | HOSTNAME | PLATFORM | OSQUERY VERSION | STATUS | ++------+-----------+----------+-----------------+--------+ +| | test_host | | | mia | ++------+-----------+----------+-----------------+--------+ +` + + expectedYaml := `--- +apiVersion: v1 +kind: host +spec: + build: "" + code_name: "" + computer_name: test_host + config_tls_refresh: 0 + cpu_brand: "" + cpu_logical_cores: 0 + cpu_physical_cores: 0 + cpu_subtype: "" + cpu_type: "" + created_at: "0001-01-01T00:00:00Z" + detail_updated_at: "0001-01-01T00:00:00Z" + display_text: test_host + distributed_interval: 0 + hardware_model: "" + hardware_serial: "" + hardware_vendor: "" + hardware_version: "" + hostname: test_host + id: 0 + label_updated_at: "0001-01-01T00:00:00Z" + last_enrolled_at: "0001-01-01T00:00:00Z" + logger_tls_period: 0 + memory: 0 + os_version: "" + osquery_version: "" + pack_stats: null + platform: "" + platform_like: "" + primary_ip: "" + primary_mac: "" + refetch_requested: false + seen_time: "0001-01-01T00:00:00Z" + status: mia + team_id: null + team_name: null + updated_at: "0001-01-01T00:00:00Z" + uptime: 0 + uuid: "" +` + expectedJson := "{\"kind\":\"host\",\"apiVersion\":\"v1\",\"spec\":{\"created_at\":\"0001-01-01T00:00:00Z\",\"updated_at\":\"0001-01-01T00:00:00Z\",\"id\":0,\"detail_updated_at\":\"0001-01-01T00:00:00Z\",\"label_updated_at\":\"0001-01-01T00:00:00Z\",\"last_enrolled_at\":\"0001-01-01T00:00:00Z\",\"seen_time\":\"0001-01-01T00:00:00Z\",\"refetch_requested\":false,\"hostname\":\"test_host\",\"uuid\":\"\",\"platform\":\"\",\"osquery_version\":\"\",\"os_version\":\"\",\"build\":\"\",\"platform_like\":\"\",\"code_name\":\"\",\"uptime\":0,\"memory\":0,\"cpu_type\":\"\",\"cpu_subtype\":\"\",\"cpu_brand\":\"\",\"cpu_physical_cores\":0,\"cpu_logical_cores\":0,\"hardware_vendor\":\"\",\"hardware_model\":\"\",\"hardware_version\":\"\",\"hardware_serial\":\"\",\"computer_name\":\"test_host\",\"primary_ip\":\"\",\"primary_mac\":\"\",\"distributed_interval\":0,\"config_tls_refresh\":0,\"logger_tls_period\":0,\"team_id\":null,\"pack_stats\":null,\"team_name\":null,\"status\":\"mia\",\"display_text\":\"test_host\"}}\n" + + assert.Equal(t, expectedText, runAppForTest(t, []string{"get", "hosts"})) + assert.Equal(t, expectedYaml, runAppForTest(t, []string{"get", "hosts", "--yaml"})) + assert.Equal(t, expectedJson, runAppForTest(t, []string{"get", "hosts", "--json"})) +}