From d0ce49abc107b72e5b29ccdebac9075970e38c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20FORTUNATO?= Date: Fri, 2 Aug 2019 13:32:07 +0200 Subject: [PATCH] feat: add get all dashboards support (New dashboards API) (#255) --- boards.go | 28 +++ boards_test.go | 33 ++++ datadog-accessors.go | 279 ++++++++++++++++++++++++++++ tests/fixtures/boards_response.json | 26 +++ 4 files changed, 366 insertions(+) create mode 100644 boards_test.go create mode 100644 tests/fixtures/boards_response.json diff --git a/boards.go b/boards.go index ae8e4ba..d96cc34 100644 --- a/boards.go +++ b/boards.go @@ -29,6 +29,24 @@ type Board struct { ModifiedAt *string `json:"modified_at,omitempty"` } +// BoardLite represents a simplify dashboard (without widgets, notify list, ...) +// It's used when we load all boards. +type BoardLite struct { + Title *string `json:"title,omitempty"` + Description *string `json:"description,omitempty"` + LayoutType *string `json:"layout_type,omitempty"` + Id *string `json:"id,omitempty"` + Url *string `json:"url,omitempty"` + AuthorHandle *string `json:"author_handle,omitempty"` + IsReadOnly *bool `json:"is_read_only,omitempty"` + CreatedAt *string `json:"created_at,omitempty"` + ModifiedAt *string `json:"modified_at,omitempty"` +} + +type reqGetBoards struct { + Boards []BoardLite `json:"dashboards,omitempty"` +} + // GetBoard returns a single dashboard created on this account. func (client *Client) GetBoard(id string) (*Board, error) { var board Board @@ -57,3 +75,13 @@ func (client *Client) CreateBoard(board *Board) (*Board, error) { func (client *Client) UpdateBoard(board *Board) error { return client.doJsonRequest("PUT", fmt.Sprintf("/v1/dashboard/%s", *board.Id), board, nil) } + +// GetBoards returns all Dashboards. +func (client *Client) GetBoards() ([]BoardLite, error) { + var out reqGetBoards + if err := client.doJsonRequest("GET", "/v1/dashboard", nil, &out); err != nil { + return nil, err + } + + return out.Boards, nil +} diff --git a/boards_test.go b/boards_test.go new file mode 100644 index 0000000..24205d9 --- /dev/null +++ b/boards_test.go @@ -0,0 +1,33 @@ +package datadog + +import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/stretchr/testify/require" +) + +func TestGetBoards(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + response, err := ioutil.ReadFile("./tests/fixtures/boards_response.json") + if err != nil { + t.Fatal(err) + } + w.Write(response) + })) + defer ts.Close() + + datadogClient := Client{ + baseUrl: ts.URL, + HttpClient: http.DefaultClient, + } + + boards, err := datadogClient.GetBoards() + require.NoError(t, err) + + assert.Len(t, boards, 2) +} diff --git a/datadog-accessors.go b/datadog-accessors.go index 22e8f4d..da637a4 100644 --- a/datadog-accessors.go +++ b/datadog-accessors.go @@ -1533,6 +1533,285 @@ func (b *Board) SetUrl(v string) { b.Url = &v } +// GetAuthorHandle returns the AuthorHandle field if non-nil, zero value otherwise. +func (b *BoardLite) GetAuthorHandle() string { + if b == nil || b.AuthorHandle == nil { + return "" + } + return *b.AuthorHandle +} + +// GetAuthorHandleOk returns a tuple with the AuthorHandle field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetAuthorHandleOk() (string, bool) { + if b == nil || b.AuthorHandle == nil { + return "", false + } + return *b.AuthorHandle, true +} + +// HasAuthorHandle returns a boolean if a field has been set. +func (b *BoardLite) HasAuthorHandle() bool { + if b != nil && b.AuthorHandle != nil { + return true + } + + return false +} + +// SetAuthorHandle allocates a new b.AuthorHandle and returns the pointer to it. +func (b *BoardLite) SetAuthorHandle(v string) { + b.AuthorHandle = &v +} + +// GetCreatedAt returns the CreatedAt field if non-nil, zero value otherwise. +func (b *BoardLite) GetCreatedAt() string { + if b == nil || b.CreatedAt == nil { + return "" + } + return *b.CreatedAt +} + +// GetCreatedAtOk returns a tuple with the CreatedAt field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetCreatedAtOk() (string, bool) { + if b == nil || b.CreatedAt == nil { + return "", false + } + return *b.CreatedAt, true +} + +// HasCreatedAt returns a boolean if a field has been set. +func (b *BoardLite) HasCreatedAt() bool { + if b != nil && b.CreatedAt != nil { + return true + } + + return false +} + +// SetCreatedAt allocates a new b.CreatedAt and returns the pointer to it. +func (b *BoardLite) SetCreatedAt(v string) { + b.CreatedAt = &v +} + +// GetDescription returns the Description field if non-nil, zero value otherwise. +func (b *BoardLite) GetDescription() string { + if b == nil || b.Description == nil { + return "" + } + return *b.Description +} + +// GetDescriptionOk returns a tuple with the Description field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetDescriptionOk() (string, bool) { + if b == nil || b.Description == nil { + return "", false + } + return *b.Description, true +} + +// HasDescription returns a boolean if a field has been set. +func (b *BoardLite) HasDescription() bool { + if b != nil && b.Description != nil { + return true + } + + return false +} + +// SetDescription allocates a new b.Description and returns the pointer to it. +func (b *BoardLite) SetDescription(v string) { + b.Description = &v +} + +// GetId returns the Id field if non-nil, zero value otherwise. +func (b *BoardLite) GetId() string { + if b == nil || b.Id == nil { + return "" + } + return *b.Id +} + +// GetIdOk returns a tuple with the Id field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetIdOk() (string, bool) { + if b == nil || b.Id == nil { + return "", false + } + return *b.Id, true +} + +// HasId returns a boolean if a field has been set. +func (b *BoardLite) HasId() bool { + if b != nil && b.Id != nil { + return true + } + + return false +} + +// SetId allocates a new b.Id and returns the pointer to it. +func (b *BoardLite) SetId(v string) { + b.Id = &v +} + +// GetIsReadOnly returns the IsReadOnly field if non-nil, zero value otherwise. +func (b *BoardLite) GetIsReadOnly() bool { + if b == nil || b.IsReadOnly == nil { + return false + } + return *b.IsReadOnly +} + +// GetIsReadOnlyOk returns a tuple with the IsReadOnly field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetIsReadOnlyOk() (bool, bool) { + if b == nil || b.IsReadOnly == nil { + return false, false + } + return *b.IsReadOnly, true +} + +// HasIsReadOnly returns a boolean if a field has been set. +func (b *BoardLite) HasIsReadOnly() bool { + if b != nil && b.IsReadOnly != nil { + return true + } + + return false +} + +// SetIsReadOnly allocates a new b.IsReadOnly and returns the pointer to it. +func (b *BoardLite) SetIsReadOnly(v bool) { + b.IsReadOnly = &v +} + +// GetLayoutType returns the LayoutType field if non-nil, zero value otherwise. +func (b *BoardLite) GetLayoutType() string { + if b == nil || b.LayoutType == nil { + return "" + } + return *b.LayoutType +} + +// GetLayoutTypeOk returns a tuple with the LayoutType field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetLayoutTypeOk() (string, bool) { + if b == nil || b.LayoutType == nil { + return "", false + } + return *b.LayoutType, true +} + +// HasLayoutType returns a boolean if a field has been set. +func (b *BoardLite) HasLayoutType() bool { + if b != nil && b.LayoutType != nil { + return true + } + + return false +} + +// SetLayoutType allocates a new b.LayoutType and returns the pointer to it. +func (b *BoardLite) SetLayoutType(v string) { + b.LayoutType = &v +} + +// GetModifiedAt returns the ModifiedAt field if non-nil, zero value otherwise. +func (b *BoardLite) GetModifiedAt() string { + if b == nil || b.ModifiedAt == nil { + return "" + } + return *b.ModifiedAt +} + +// GetModifiedAtOk returns a tuple with the ModifiedAt field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetModifiedAtOk() (string, bool) { + if b == nil || b.ModifiedAt == nil { + return "", false + } + return *b.ModifiedAt, true +} + +// HasModifiedAt returns a boolean if a field has been set. +func (b *BoardLite) HasModifiedAt() bool { + if b != nil && b.ModifiedAt != nil { + return true + } + + return false +} + +// SetModifiedAt allocates a new b.ModifiedAt and returns the pointer to it. +func (b *BoardLite) SetModifiedAt(v string) { + b.ModifiedAt = &v +} + +// GetTitle returns the Title field if non-nil, zero value otherwise. +func (b *BoardLite) GetTitle() string { + if b == nil || b.Title == nil { + return "" + } + return *b.Title +} + +// GetTitleOk returns a tuple with the Title field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetTitleOk() (string, bool) { + if b == nil || b.Title == nil { + return "", false + } + return *b.Title, true +} + +// HasTitle returns a boolean if a field has been set. +func (b *BoardLite) HasTitle() bool { + if b != nil && b.Title != nil { + return true + } + + return false +} + +// SetTitle allocates a new b.Title and returns the pointer to it. +func (b *BoardLite) SetTitle(v string) { + b.Title = &v +} + +// GetUrl returns the Url field if non-nil, zero value otherwise. +func (b *BoardLite) GetUrl() string { + if b == nil || b.Url == nil { + return "" + } + return *b.Url +} + +// GetUrlOk returns a tuple with the Url field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (b *BoardLite) GetUrlOk() (string, bool) { + if b == nil || b.Url == nil { + return "", false + } + return *b.Url, true +} + +// HasUrl returns a boolean if a field has been set. +func (b *BoardLite) HasUrl() bool { + if b != nil && b.Url != nil { + return true + } + + return false +} + +// SetUrl allocates a new b.Url and returns the pointer to it. +func (b *BoardLite) SetUrl(v string) { + b.Url = &v +} + // GetId returns the Id field if non-nil, zero value otherwise. func (b *BoardWidget) GetId() int { if b == nil || b.Id == nil { diff --git a/tests/fixtures/boards_response.json b/tests/fixtures/boards_response.json new file mode 100644 index 0000000..2665d77 --- /dev/null +++ b/tests/fixtures/boards_response.json @@ -0,0 +1,26 @@ +{ + "dashboards": [ + { + "created_at": "2019-02-28T17:12:59.327729+00:00", + "is_read_only": false, + "description": "My Screenboard Description", + "title": "My Screenboard", + "url": "/dashboard/grh-zkj-h2c/my-screenboard", + "layout_type": "free", + "modified_at": "2019-03-13T19:09:40.930616+00:00", + "author_handle":"user@domain.com", + "id": "grh-zkj-h2c" + }, + { + "created_at": "2019-02-28T17:45:11.535045+00:00", + "is_read_only": false, + "description": "My Timeboard Description", + "title": "My Timeboard", + "url": "/dashboard/4qf-77b-uhc/my-timeboard", + "layout_type": "ordered", + "modified_at": "2019-03-11T14:22:03.131495+00:00", + "author_handle":"user@domain.com", + "id": "4qf-77b-uhc" + } + ] +}