Skip to content

Commit dc324c0

Browse files
authored
Merge pull request #186 from mnaboka/mnaboka/add-missing-response-fields
Add missing dashboard fields
2 parents b4afa10 + 3d10cf3 commit dc324c0

File tree

4 files changed

+622
-4
lines changed

4 files changed

+622
-4
lines changed

dashboards.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,27 @@ type Dashboard struct {
162162
// DashboardLite represents a user created dashboard. This is the mini
163163
// struct when we load the summaries.
164164
type DashboardLite struct {
165-
Id *int `json:"id,string,omitempty"` // TODO: Remove ',string'.
166-
Resource *string `json:"resource,omitempty"`
167-
Description *string `json:"description,omitempty"`
168-
Title *string `json:"title,omitempty"`
165+
Id *int `json:"id,string,omitempty"` // TODO: Remove ',string'.
166+
Resource *string `json:"resource,omitempty"`
167+
Description *string `json:"description,omitempty"`
168+
Title *string `json:"title,omitempty"`
169+
ReadOnly *bool `json:"read_only,omitempty"`
170+
Created *string `json:"created,omitempty"`
171+
Modified *string `json:"modified,omitempty"`
172+
CreatedBy *CreatedBy `json:"created_by,omitempty"`
173+
}
174+
175+
// CreatedBy represents a field from DashboardLite.
176+
type CreatedBy struct {
177+
Disabled *bool `json:"disabled,omitempty"`
178+
Handle *string `json:"handle,omitempty"`
179+
Name *string `json:"name,omitempty"`
180+
IsAdmin *bool `json:"is_admin,omitempty"`
181+
Role *string `json:"role,omitempty"`
182+
AccessRole *string `json:"access_role,omitempty"`
183+
Verified *bool `json:"verified,omitempty"`
184+
Email *string `json:"email,omitempty"`
185+
Icon *string `json:"icon,omitempty"`
169186
}
170187

171188
// reqGetDashboards from /api/v1/dash

dashboards_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package datadog
22

33
import (
44
"encoding/json"
5+
"io/ioutil"
6+
"net/http"
7+
"net/http/httptest"
58
"testing"
69

710
"github.com/stretchr/testify/assert"
@@ -75,3 +78,154 @@ func (suite *YAxisTestSuite) TestYAxisJSONUnmarshalAutoMinMax() {
7578
func TestYAxisTestSuite(t *testing.T) {
7679
suite.Run(t, new(YAxisTestSuite))
7780
}
81+
82+
func TestDashboardGetters(t *testing.T) {
83+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84+
response, err := ioutil.ReadFile("./tests/fixtures/dashboards_response.json")
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
w.Write(response)
89+
}))
90+
defer ts.Close()
91+
92+
datadogClient := Client{
93+
baseUrl: ts.URL,
94+
HttpClient: http.DefaultClient,
95+
}
96+
97+
dashboards, err := datadogClient.GetDashboards()
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
102+
if len(dashboards) != 2 {
103+
t.Fatalf("expect response with 2 dashboards. Got %d", len(dashboards))
104+
}
105+
106+
d1 := dashboards[0]
107+
108+
expectedID := 123
109+
if id := d1.GetId(); id != expectedID {
110+
t.Fatalf("expect ID %d. Got %d", expectedID, id)
111+
}
112+
113+
expectedTitle := "Dashboard 1"
114+
if title := d1.GetTitle(); title != expectedTitle {
115+
t.Fatalf("expect title %s. Got %s", expectedTitle, title)
116+
}
117+
118+
expectedDescription := "created by user1"
119+
if description := d1.GetDescription(); description != expectedDescription {
120+
t.Fatalf("expect description %s. Got %s", expectedDescription, description)
121+
}
122+
123+
expectedCreateTime := "2018-10-05T12:32:01.000000+00:00"
124+
createTime, ok := d1.GetCreatedOk()
125+
if !ok {
126+
t.Fatalf("expect to have a created field")
127+
}
128+
129+
if createTime != expectedCreateTime {
130+
t.Fatalf("expect create time %s. Got %s", expectedCreateTime, createTime)
131+
}
132+
133+
expectedReadOnly := false
134+
readOnly, ok := d1.GetReadOnlyOk()
135+
if !ok {
136+
t.Fatalf("expect to have a read_only field")
137+
}
138+
139+
if readOnly != expectedReadOnly {
140+
t.Fatalf("expect read_only %v. Got %v", expectedReadOnly, readOnly)
141+
}
142+
143+
expectedModified := "2018-09-11T06:38:09.000000+00:00"
144+
modified, ok := d1.GetModifiedOk()
145+
if !ok {
146+
t.Fatalf("expect to have a modified field")
147+
}
148+
149+
if modified != expectedModified {
150+
t.Fatalf("expect modified %s. Got %s", expectedModified, modified)
151+
}
152+
153+
createdBy, ok := d1.GetCreatedByOk()
154+
if !ok {
155+
t.Fatal("expect created_by field to exist")
156+
}
157+
158+
validateCreatedBy(t, &createdBy)
159+
}
160+
161+
func validateCreatedBy(t *testing.T, cb *CreatedBy) {
162+
expectedAccessRole := "adm"
163+
accessRole, ok := cb.GetAccessRoleOk()
164+
if !ok {
165+
t.Fatal("expect to have access_role field")
166+
}
167+
168+
if accessRole != expectedAccessRole {
169+
t.Fatalf("expect access_role %s. Got %s", expectedAccessRole, accessRole)
170+
}
171+
172+
expectedDisabled := true
173+
disabled, ok := cb.GetDisabledOk()
174+
if !ok {
175+
t.Fatal("expect to have disabled field")
176+
}
177+
178+
if disabled != expectedDisabled {
179+
t.Fatalf("expect disabled %v. Got %v", expectedDisabled, disabled)
180+
}
181+
182+
expectedEmail := "[email protected]"
183+
email, ok := cb.GetEmailOk()
184+
if !ok {
185+
t.Fatal("expect to have email field")
186+
}
187+
188+
if email != expectedEmail {
189+
t.Fatalf("expect email %s. Got %s", expectedEmail, email)
190+
}
191+
192+
expectedHandle := "[email protected]"
193+
handle, ok := cb.GetHandleOk()
194+
if !ok {
195+
t.Fatal("expect to have handle field")
196+
}
197+
198+
if handle != expectedHandle {
199+
t.Fatalf("expect handle %s. Got %s", expectedHandle, handle)
200+
}
201+
202+
expectedIcon := "https://pics.site.com/123.jpg"
203+
icon, ok := cb.GetIconOk()
204+
if !ok {
205+
t.Fatal("expect to have icon field")
206+
}
207+
208+
if icon != expectedIcon {
209+
t.Fatalf("expect icon %s. Got %s", expectedIcon, icon)
210+
}
211+
212+
expectedName := "John Doe"
213+
name, ok := cb.GetNameOk()
214+
if !ok {
215+
t.Fatal("expect to have name field")
216+
}
217+
218+
if name != expectedName {
219+
t.Fatalf("expect name %s. Got %s", expectedName, name)
220+
}
221+
222+
expectedVerified := true
223+
verified, ok := cb.GetVerifiedOk()
224+
if !ok {
225+
t.Fatal("expect to have verified field")
226+
}
227+
228+
if verified != expectedVerified {
229+
t.Fatalf("expect verify %v. Got %v", expectedVerified, verified)
230+
}
231+
}

0 commit comments

Comments
 (0)