Skip to content

Commit d26fa36

Browse files
committed
use the new hosts search endpoint
1 parent 1ece346 commit d26fa36

File tree

4 files changed

+689
-0
lines changed

4 files changed

+689
-0
lines changed

datadog-accessors.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9066,6 +9066,37 @@ func (h *HeatmapRequest) SetStyle(v WidgetRequestStyle) {
90669066
h.Style = &v
90679067
}
90689068

9069+
// GetAwsID returns the AwsID field if non-nil, zero value otherwise.
9070+
func (h *Host) GetAwsID() string {
9071+
if h == nil || h.AwsID == nil {
9072+
return ""
9073+
}
9074+
return *h.AwsID
9075+
}
9076+
9077+
// GetAwsIDOk returns a tuple with the AwsID field if it's non-nil, zero value otherwise
9078+
// and a boolean to check if the value has been set.
9079+
func (h *Host) GetAwsIDOk() (string, bool) {
9080+
if h == nil || h.AwsID == nil {
9081+
return "", false
9082+
}
9083+
return *h.AwsID, true
9084+
}
9085+
9086+
// HasAwsID returns a boolean if a field has been set.
9087+
func (h *Host) HasAwsID() bool {
9088+
if h != nil && h.AwsID != nil {
9089+
return true
9090+
}
9091+
9092+
return false
9093+
}
9094+
9095+
// SetAwsID allocates a new h.AwsID and returns the pointer to it.
9096+
func (h *Host) SetAwsID(v string) {
9097+
h.AwsID = &v
9098+
}
9099+
90699100
// GetEndTime returns the EndTime field if non-nil, zero value otherwise.
90709101
func (h *HostActionMute) GetEndTime() string {
90719102
if h == nil || h.EndTime == nil {

hosts.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package datadog
22

3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/url"
7+
"strconv"
8+
"time"
9+
)
10+
311
type HostActionResp struct {
412
Action string `json:"action"`
513
Hostname string `json:"hostname"`
@@ -48,3 +56,69 @@ func (client *Client) GetHostTotals() (*HostTotalsResp, error) {
4856
}
4957
return &out, nil
5058
}
59+
60+
// HostSearchResp defines response to GET /v1/hosts.
61+
type HostSearchResp struct {
62+
ExactTotalMatching bool `json:"exact_total_matching"`
63+
TotalMatching int `json:"total_matching"`
64+
TotalReturned int `json:"total_returned"`
65+
HostList []Host `json:"host_list"`
66+
}
67+
68+
type Host struct {
69+
LastReportedTime int64 `json:"last_reported_time"`
70+
Name string `json:"name"`
71+
IsMuted bool `json:"is_muted"`
72+
MuteTimeout int `json:"mute_timeout"`
73+
Apps []string `json:"apps"`
74+
TagsBySource map[string][]string `json:"tags_by_source"`
75+
Up bool `json:"up"`
76+
Metrics map[string]float64 `json:"metrics"`
77+
Sources []string `json:"sources"`
78+
Meta map[string]interface{} `json:"meta"`
79+
HostName string `json:"host_name"`
80+
AwsID *string `json:"aws_id"`
81+
ID int64 `json:"id"`
82+
Aliases []string `json:"aliases"`
83+
}
84+
85+
type HostSearchRequest struct {
86+
Filter string
87+
SortField string
88+
SortDirection string
89+
Start int
90+
Count int
91+
FromTs time.Time
92+
}
93+
94+
// GetHosts searches through the hosts facet, returning matching hosts.
95+
func (client *Client) GetHosts(req HostSearchRequest) (*HostSearchResp, error) {
96+
v := url.Values{}
97+
98+
if req.Filter != "" {
99+
v.Add("filter", req.Filter)
100+
}
101+
if req.SortField != "" {
102+
v.Add("sort_field", req.SortField)
103+
}
104+
if req.SortDirection != "" {
105+
v.Add("sort_dir", req.SortDirection)
106+
}
107+
if req.Start >= 0 {
108+
v.Add("start", strconv.Itoa(req.Start))
109+
}
110+
if req.Count >= 0 {
111+
v.Add("count", strconv.Itoa(req.Count))
112+
}
113+
if !req.FromTs.IsZero() {
114+
v.Add("from", fmt.Sprintf("%d", req.FromTs.Unix()))
115+
}
116+
117+
var out HostSearchResp
118+
uri := "/v1/hosts?" + v.Encode()
119+
if err := client.doJsonRequest(http.MethodGet, uri, nil, &out); err != nil {
120+
return nil, err
121+
}
122+
123+
return &out, nil
124+
}

hosts_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,34 @@ func TestGetHostTotals(t *testing.T) {
3232
assert.Equal(t, *res.TotalActive, 1)
3333
assert.Equal(t, *res.TotalUp, 2)
3434
}
35+
36+
func TestGetHosts(t *testing.T) {
37+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38+
response, err := ioutil.ReadFile("./tests/fixtures/hosts/get_hosts_response.json")
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
w.Write(response)
43+
}))
44+
defer ts.Close()
45+
46+
datadogClient := Client{
47+
baseUrl: ts.URL,
48+
HttpClient: http.DefaultClient,
49+
}
50+
51+
searchRequest := HostSearchRequest{
52+
SortField: "cpu",
53+
SortDirection: "desc",
54+
Start: 0,
55+
Count: 100,
56+
}
57+
res, err := datadogClient.GetHosts(searchRequest)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
62+
assert.Equal(t, res.TotalReturned, 3)
63+
assert.Equal(t, res.TotalMatching, 3)
64+
assert.Len(t, res.HostList, 3)
65+
}

0 commit comments

Comments
 (0)