Skip to content
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

[dashboards] Support getting dashboards by string id #225

Merged
merged 5 commits into from
Apr 15, 2019
Merged
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
11 changes: 9 additions & 2 deletions dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ type TemplateVariable struct {
// struct when we load a dashboard in detail.
type Dashboard struct {
Id *int `json:"id,omitempty"`
NewId *string `json:"new_id,omitempty"`
Description *string `json:"description,omitempty"`
Title *string `json:"title,omitempty"`
Graphs []Graph `json:"graphs,omitempty"`
Expand Down Expand Up @@ -210,9 +211,15 @@ type DashboardConditionalFormat struct {
}

// GetDashboard returns a single dashboard created on this account.
func (client *Client) GetDashboard(id int) (*Dashboard, error) {
func (client *Client) GetDashboard(id interface{}) (*Dashboard, error) {

stringId, err := GetStringId(id)
if err != nil {
return nil, err
}

var out reqGetDashboard
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/dash/%d", id), nil, &out); err != nil {
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/dash/%s", stringId), nil, &out); err != nil {
return nil, err
}
return out.Dashboard, nil
Expand Down
62 changes: 62 additions & 0 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,37 @@ func (d *Dashboard) SetId(v int) {
d.Id = &v
}

// GetNewId returns the NewId field if non-nil, zero value otherwise.
func (d *Dashboard) GetNewId() string {
if d == nil || d.NewId == nil {
return ""
}
return *d.NewId
}

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

// HasNewId returns a boolean if a field has been set.
func (d *Dashboard) HasNewId() bool {
if d != nil && d.NewId != nil {
return true
}

return false
}

// SetNewId allocates a new d.NewId and returns the pointer to it.
func (d *Dashboard) SetNewId(v string) {
d.NewId = &v
}

// GetReadOnly returns the ReadOnly field if non-nil, zero value otherwise.
func (d *Dashboard) GetReadOnly() bool {
if d == nil || d.ReadOnly == nil {
Expand Down Expand Up @@ -6461,6 +6492,37 @@ func (s *Screenboard) SetId(v int) {
s.Id = &v
}

// GetNewId returns the NewId field if non-nil, zero value otherwise.
func (s *Screenboard) GetNewId() string {
if s == nil || s.NewId == nil {
return ""
}
return *s.NewId
}

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

// HasNewId returns a boolean if a field has been set.
func (s *Screenboard) HasNewId() bool {
if s != nil && s.NewId != nil {
return true
}

return false
}

// SetNewId allocates a new s.NewId and returns the pointer to it.
func (s *Screenboard) SetNewId(v string) {
s.NewId = &v
}

// GetReadOnly returns the ReadOnly field if non-nil, zero value otherwise.
func (s *Screenboard) GetReadOnly() bool {
if s == nil || s.ReadOnly == nil {
Expand Down
22 changes: 22 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,25 @@ func getTestMonitor() *datadog.Monitor {
Tags: make([]string, 0),
}
}

func TestHelperGetStringId(t *testing.T) {
// Assert GetStringId returned the id without a change if it is a string
id, err := datadog.GetStringId("abc-xyz-123")
assert.Equal(t, err, nil)
assert.Equal(t, id, "abc-xyz-123")

// Assert GetStringId returned the id as a string if it is an integer
id, err = datadog.GetStringId(123)
assert.Equal(t, err, nil)
assert.Equal(t, id, "123")

// Assert GetStringId returned an error if the id type is boolean
_, err = datadog.GetStringId(true)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "unsupported id type")

// Assert GetStringId returned an error if the id type is float64
_, err = datadog.GetStringId(5.2)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "unsupported id type")
}
21 changes: 20 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

package datadog

import "encoding/json"
import (
"encoding/json"
"errors"
"strconv"
)

// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
Expand Down Expand Up @@ -79,3 +83,18 @@ func GetPrecision(v *PrecisionT) (PrecisionT, bool) {

return PrecisionT(""), false
}

// GetStringId is a helper routine that allows screenboards and timeboards to be retrieved
// by either the legacy numerical format or the new string format.
// It returns the id as is if it is a string, converts it to a string if it is an integer.
// It return an error if the type is neither string or an integer
func GetStringId(id interface{}) (string, error) {
switch v := id.(type) {
case int:
return strconv.Itoa(v), nil
case string:
return v, nil
default:
return "", errors.New("unsupported id type")
}
}
46 changes: 46 additions & 0 deletions integration/dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/zorkian/go-datadog-api"
)

Expand Down Expand Up @@ -111,6 +112,51 @@ func TestDashboardCreateWithCustomGraph(t *testing.T) {
assertDashboardEquals(t, actual, expected)
}

func TestDashboardGetWithNewId(t *testing.T) {
expected := getTestDashboard(createGraph)
// create the dashboard and compare it
actual, err := client.CreateDashboard(expected)
if err != nil {
t.Fatalf("Creating a dashboard failed when it shouldn't. (%s)", err)
}

defer cleanUpDashboard(t, *actual.Id)

assertDashboardEquals(t, actual, expected)

// try to fetch it freshly using the new id format and compare it again
actualWithNewId, err := client.GetDashboard(*actual.NewId)
if err != nil {
t.Fatalf("Retrieving a dashboard failed when it shouldn't. (%s)", err)
}
assertDashboardEquals(t, actualWithNewId, expected)

// the ids are equal whether fetching using the old or the new id
assert.Equal(t, *actualWithNewId.Id, *actual.Id)
enbashi marked this conversation as resolved.
Show resolved Hide resolved

// try to fetch it freshly using a string, but with a wrong value
actual, err = client.GetDashboard("random_string")
if assert.NotNil(t, err) {
// it should not fail because of the id format
assert.NotContains(t, err.Error(), "unsupported id type")
assert.Contains(t, err.Error(), "404")
}

// try to fetch it freshly using a boolean
actual, err = client.GetDashboard(true)
if assert.NotNil(t, err) {
// it should fail because of the id format
assert.Contains(t, err.Error(), "unsupported id type")
}

// try to fetch it freshly using a float64
actual, err = client.GetDashboard(5.5)
if assert.NotNil(t, err) {
// it should fail because of the id format
assert.Contains(t, err.Error(), "unsupported id type")
}
}

func getTestDashboard(createGraph func() []datadog.Graph) *datadog.Dashboard {
return &datadog.Dashboard{
Title: datadog.String("___Test-Board___"),
Expand Down
55 changes: 55 additions & 0 deletions integration/screenboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integration
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/zorkian/go-datadog-api"
)

Expand Down Expand Up @@ -90,6 +91,60 @@ func TestScreenboardGet(t *testing.T) {
}
}

func TestScreenboardGetWithNewId(t *testing.T) {
expected := getTestScreenboard()
// create the screenboard and compare it
actual, err := client.CreateScreenboard(expected)
if err != nil {
t.Fatalf("Creating a screenboard failed when it shouldn't. (%s)", err)
}

defer cleanUpScreenboard(t, *actual.Id)

assertScreenboardEquals(t, actual, expected)

// try to fetch it freshly and compare it again
actual, err = client.GetScreenboard(*actual.Id)
if err != nil {
t.Fatalf("Retrieving a screenboard failed when it shouldn't. (%s)", err)
}

assertScreenboardEquals(t, actual, expected)

// try to fetch it freshly using the new id format and compare it again
actualWithNewId, err := client.GetScreenboard(*actual.NewId)
if err != nil {
t.Fatalf("Retrieving a screenboard failed when it shouldn't. (%s)", err)
}
assertScreenboardEquals(t, actualWithNewId, expected)

// the ids are equal whether fetching using the old or the new id
assert.Equal(t, *actualWithNewId.Id, *actual.Id)

// try to fetch it freshly using a string, but with a wrong value
actual, err = client.GetScreenboard("random_string")
if assert.NotNil(t, err) {
// it should not fail because of the id format
assert.NotContains(t, err.Error(), "unsupported id type")
assert.Contains(t, err.Error(), "404")
}

// try to fetch it freshly using a boolean
actual, err = client.GetScreenboard(true)
if assert.NotNil(t, err) {
// it should fail because of the id format
assert.Contains(t, err.Error(), "unsupported id type")
}

// try to fetch it freshly using a float64
actual, err = client.GetScreenboard(5.5)
if assert.NotNil(t, err) {
// it should fail because of the id format
assert.Contains(t, err.Error(), "unsupported id type")
}

}

func getTestScreenboard() *datadog.Screenboard {
return &datadog.Screenboard{
Title: datadog.String("___Test-Board___"),
Expand Down
10 changes: 8 additions & 2 deletions screenboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// struct when we load a screenboard in detail.
type Screenboard struct {
Id *int `json:"id,omitempty"`
NewId *string `json:"new_id,omitempty"`
Title *string `json:"board_title,omitempty"`
Height *int `json:"height,omitempty"`
Width *int `json:"width,omitempty"`
Expand All @@ -39,9 +40,14 @@ type reqGetScreenboards struct {
}

// GetScreenboard returns a single screenboard created on this account.
func (client *Client) GetScreenboard(id int) (*Screenboard, error) {
func (client *Client) GetScreenboard(id interface{}) (*Screenboard, error) {
stringId, err := GetStringId(id)
if err != nil {
return nil, err
}

out := &Screenboard{}
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/screen/%d", id), nil, out); err != nil {
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/screen/%s", stringId), nil, out); err != nil {
return nil, err
}
return out, nil
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/dashboards_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"title": "Dashboard 1",
"created": "2018-10-05T12:32:01.000000+00:00",
"id": "123",
"new_id": "abc-xyz-111",
"created_by": {
"disabled": true,
"handle": "[email protected]",
Expand All @@ -27,6 +28,7 @@
"title": "Dashboard 2",
"created": "2018-01-01T00:00:00.000000+00:00",
"id": "1234",
"new_id": "abc-xyz-222",
"created_by": {
"disabled": false,
"handle": "[email protected]",
Expand All @@ -41,4 +43,4 @@
"modified": "2018-09-11T06:38:09.000000+00:00"
}
]
}
}