Skip to content

use the new hosts search endpoint #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9066,6 +9066,37 @@ func (h *HeatmapRequest) SetStyle(v WidgetRequestStyle) {
h.Style = &v
}

// GetAwsID returns the AwsID field if non-nil, zero value otherwise.
func (h *Host) GetAwsID() string {
if h == nil || h.AwsID == nil {
return ""
}
return *h.AwsID
}

// GetAwsIDOk returns a tuple with the AwsID field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (h *Host) GetAwsIDOk() (string, bool) {
if h == nil || h.AwsID == nil {
return "", false
}
return *h.AwsID, true
}

// HasAwsID returns a boolean if a field has been set.
func (h *Host) HasAwsID() bool {
if h != nil && h.AwsID != nil {
return true
}

return false
}

// SetAwsID allocates a new h.AwsID and returns the pointer to it.
func (h *Host) SetAwsID(v string) {
h.AwsID = &v
}

// GetEndTime returns the EndTime field if non-nil, zero value otherwise.
func (h *HostActionMute) GetEndTime() string {
if h == nil || h.EndTime == nil {
Expand Down
74 changes: 74 additions & 0 deletions hosts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package datadog

import (
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)

type HostActionResp struct {
Action string `json:"action"`
Hostname string `json:"hostname"`
Expand Down Expand Up @@ -48,3 +56,69 @@ func (client *Client) GetHostTotals() (*HostTotalsResp, error) {
}
return &out, nil
}

// HostSearchResp defines response to GET /v1/hosts.
type HostSearchResp struct {
ExactTotalMatching bool `json:"exact_total_matching"`
TotalMatching int `json:"total_matching"`
TotalReturned int `json:"total_returned"`
HostList []Host `json:"host_list"`
}

type Host struct {
LastReportedTime int64 `json:"last_reported_time"`
Name string `json:"name"`
IsMuted bool `json:"is_muted"`
MuteTimeout int `json:"mute_timeout"`
Apps []string `json:"apps"`
TagsBySource map[string][]string `json:"tags_by_source"`
Up bool `json:"up"`
Metrics map[string]float64 `json:"metrics"`
Sources []string `json:"sources"`
Meta map[string]interface{} `json:"meta"`
HostName string `json:"host_name"`
AwsID *string `json:"aws_id"`
ID int64 `json:"id"`
Aliases []string `json:"aliases"`
}

type HostSearchRequest struct {
Filter string
SortField string
SortDirection string
Start int
Count int
FromTs time.Time
}

// GetHosts searches through the hosts facet, returning matching hosts.
func (client *Client) GetHosts(req HostSearchRequest) (*HostSearchResp, error) {
v := url.Values{}

if req.Filter != "" {
v.Add("filter", req.Filter)
}
if req.SortField != "" {
v.Add("sort_field", req.SortField)
}
if req.SortDirection != "" {
v.Add("sort_dir", req.SortDirection)
}
if req.Start >= 0 {
v.Add("start", strconv.Itoa(req.Start))
}
if req.Count >= 0 {
v.Add("count", strconv.Itoa(req.Count))
}
if !req.FromTs.IsZero() {
v.Add("from", fmt.Sprintf("%d", req.FromTs.Unix()))
}

var out HostSearchResp
uri := "/v1/hosts?" + v.Encode()
if err := client.doJsonRequest(http.MethodGet, uri, nil, &out); err != nil {
return nil, err
}

return &out, nil
}
31 changes: 31 additions & 0 deletions hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,34 @@ func TestGetHostTotals(t *testing.T) {
assert.Equal(t, *res.TotalActive, 1)
assert.Equal(t, *res.TotalUp, 2)
}

func TestGetHosts(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response, err := ioutil.ReadFile("./tests/fixtures/hosts/get_hosts_response.json")
if err != nil {
t.Fatal(err)
}
w.Write(response)
}))
defer ts.Close()

datadogClient := Client{
baseUrl: ts.URL,
HttpClient: http.DefaultClient,
}

searchRequest := HostSearchRequest{
SortField: "cpu",
SortDirection: "desc",
Start: 0,
Count: 100,
}
res, err := datadogClient.GetHosts(searchRequest)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, res.TotalReturned, 3)
assert.Equal(t, res.TotalMatching, 3)
assert.Len(t, res.HostList, 3)
}
Loading