Skip to content

Commit

Permalink
Add 'get hosts' command to fleetctl (kolide#1977)
Browse files Browse the repository at this point in the history
This commit:
 - adds a new sub-command for fetching hosts to `fleetctl get` command.

Why?
 - this allows for listing of all hosts via the fleetctl interface.
 There may be additional attributes of the host that we'd like to
 display, but this should be a good start.

Closes kolide#1962
  • Loading branch information
blaedj authored Jan 3, 2019
1 parent 759a69b commit be85377
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/fleetctl/fleetctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
getPacksCommand(),
getLabelsCommand(),
getOptionsCommand(),
getHostsCommand(),
getEnrollSecretCommand(),
},
},
Expand Down
46 changes: 46 additions & 0 deletions cmd/fleetctl/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,49 @@ func getEnrollSecretCommand() cli.Command {
},
}
}

func getHostsCommand() cli.Command {
return cli.Command{
Name: "hosts",
Aliases: []string{"host", "h"},
Usage: "List information about one or more hosts",
Flags: []cli.Flag{
configFlag(),
contextFlag(),
},
Action: func(c *cli.Context) error {
fleet, err := clientFromCLI(c)
if err != nil {
return err
}

hosts, err := fleet.GetHosts()
if err != nil {
return errors.Wrap(err, "could not list hosts")
}

if len(hosts) == 0 {
fmt.Println("no hosts found")
return nil
}

data := [][]string{}

for _, host := range hosts {
data = append(data, []string{
host.Host.UUID,
host.DisplayText,
host.Host.Platform,
host.Status,
})
}

table := defaultTable()
table.SetHeader([]string{"uuid", "hostname", "platform", "status"})
table.AppendBulk(data)
table.Render()

return nil
},
}
}
35 changes: 35 additions & 0 deletions server/service/client_hosts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package service

import (
"encoding/json"
"net/http"

"github.com/pkg/errors"
)

// GetHosts retrieves the list of all Hosts
func (c *Client) GetHosts() ([]hostResponse, error) {
response, err := c.AuthenticatedDo("GET", "/api/v1/kolide/hosts", nil)
if err != nil {
return nil, errors.Wrap(err, "GET /api/v1/kolide/hosts")
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return nil, errors.Errorf(
"get hosts received status %d %s",
response.StatusCode,
extractServerErrorText(response.Body),
)
}
var responseBody listHostsResponse
err = json.NewDecoder(response.Body).Decode(&responseBody)
if err != nil {
return nil, errors.Wrap(err, "decode list hosts response")
}
if responseBody.Err != nil {
return nil, errors.Errorf("list hosts: %s", responseBody.Err)
}

return responseBody.Hosts, nil
}

0 comments on commit be85377

Please sign in to comment.