Skip to content

Commit

Permalink
Add support for Template Variable Presets for Boards (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackellenberger authored Feb 3, 2020
1 parent f811099 commit 261ab16
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ testrace:
go test -race $(TEST) $(TESTARGS)

fmt:
gofmt -w $(GOFMT_FILES)
gofmt -w -s $(GOFMT_FILES)

# vet runs the Go source code static analysis tool `vet` to find
# any common errors.
Expand Down
38 changes: 26 additions & 12 deletions boards.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,35 @@ import (
"fmt"
)

// Template variable preset represents a set of template variable values on a dashboard
// Not available to timeboards and screenboards
type TemplateVariablePreset struct {
Name *string `json:"name,omitempty"`
TemplateVariables []TemplateVariablePresetValue `json:"template_variables"`
}

// Template variable preset value represents the value for "name" template variable to assume
type TemplateVariablePresetValue struct {
Name *string `json:"name,omitempty"`
Value *string `json:"value,omitempty"`
}

// Board represents a user created dashboard. This is the full dashboard
// struct when we load a dashboard in detail.
type Board struct {
Title *string `json:"title"`
Widgets []BoardWidget `json:"widgets"`
LayoutType *string `json:"layout_type"`
Id *string `json:"id,omitempty"`
Description *string `json:"description,omitempty"`
TemplateVariables []TemplateVariable `json:"template_variables,omitempty"`
IsReadOnly *bool `json:"is_read_only,omitempty"`
NotifyList []string `json:"notify_list,omitempty"`
AuthorHandle *string `json:"author_handle,omitempty"`
Url *string `json:"url,omitempty"`
CreatedAt *string `json:"created_at,omitempty"`
ModifiedAt *string `json:"modified_at,omitempty"`
Title *string `json:"title"`
Widgets []BoardWidget `json:"widgets"`
LayoutType *string `json:"layout_type"`
Id *string `json:"id,omitempty"`
Description *string `json:"description,omitempty"`
TemplateVariables []TemplateVariable `json:"template_variables,omitempty"`
TemplateVariablePresets []TemplateVariablePreset `json:"template_variable_presets,omitempty"`
IsReadOnly *bool `json:"is_read_only,omitempty"`
NotifyList []string `json:"notify_list,omitempty"`
AuthorHandle *string `json:"author_handle,omitempty"`
Url *string `json:"url,omitempty"`
CreatedAt *string `json:"created_at,omitempty"`
ModifiedAt *string `json:"modified_at,omitempty"`
}

// BoardLite represents a simplify dashboard (without widgets, notify list, ...)
Expand Down
93 changes: 93 additions & 0 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20784,6 +20784,99 @@ func (t *TemplateVariable) SetPrefix(v string) {
t.Prefix = &v
}

// GetName returns the Name field if non-nil, zero value otherwise.
func (t *TemplateVariablePreset) GetName() string {
if t == nil || t.Name == nil {
return ""
}
return *t.Name
}

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

// HasName returns a boolean if a field has been set.
func (t *TemplateVariablePreset) HasName() bool {
if t != nil && t.Name != nil {
return true
}

return false
}

// SetName allocates a new t.Name and returns the pointer to it.
func (t *TemplateVariablePreset) SetName(v string) {
t.Name = &v
}

// GetName returns the Name field if non-nil, zero value otherwise.
func (t *TemplateVariablePresetValue) GetName() string {
if t == nil || t.Name == nil {
return ""
}
return *t.Name
}

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

// HasName returns a boolean if a field has been set.
func (t *TemplateVariablePresetValue) HasName() bool {
if t != nil && t.Name != nil {
return true
}

return false
}

// SetName allocates a new t.Name and returns the pointer to it.
func (t *TemplateVariablePresetValue) SetName(v string) {
t.Name = &v
}

// GetValue returns the Value field if non-nil, zero value otherwise.
func (t *TemplateVariablePresetValue) GetValue() string {
if t == nil || t.Value == nil {
return ""
}
return *t.Value
}

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

// HasValue returns a boolean if a field has been set.
func (t *TemplateVariablePresetValue) HasValue() bool {
if t != nil && t.Value != nil {
return true
}

return false
}

// SetValue allocates a new t.Value and returns the pointer to it.
func (t *TemplateVariablePresetValue) SetValue(v string) {
t.Value = &v
}

// GetCritical returns the Critical field if non-nil, zero value otherwise.
func (t *ThresholdCount) GetCritical() json.Number {
if t == nil || t.Critical == nil {
Expand Down
77 changes: 76 additions & 1 deletion integration/boards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ func TestOrderedBoardCreateAndDelete(t *testing.T) {
assertBoardEquals(t, actual, expected)
}

func TestBoardWithTemplateVarsAndPresets(t *testing.T) {
expected := getTestBoardWithTemplateVars()

// Create the dashboard and compare it
actual, err := client.CreateBoard(expected)
if err != nil {
t.Fatalf("Creating a dashboard failed when it shouldn't. (%s)", err)
}
defer cleanUpBoard(t, *actual.Id)
assertBoardEquals(t, actual, expected)

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

// Helpers for the tests

func getTestBoard() *datadog.Board {
Expand All @@ -37,6 +56,59 @@ func getTestBoard() *datadog.Board {
}
}

func getTestBoardWithTemplateVars() *datadog.Board {
return &datadog.Board{
Title: datadog.String("Test Dashboard"),
Widgets: createWidgets(),
LayoutType: datadog.String("ordered"),
Description: datadog.String("Test Dashboard description"),
IsReadOnly: datadog.Bool(false),
TemplateVariables: getTestTemplateVars(),
TemplateVariablePresets: getTestTemplateVarPresets(),
}
}

func getTestTemplateVars() []datadog.TemplateVariable {
return []datadog.TemplateVariable{
{
Name: datadog.String("Template Var 1"),
Prefix: datadog.String("var1"),
Default: datadog.String("value1"),
},
{
Name: datadog.String("Template Var 2"),
Prefix: datadog.String("var2"),
},
}
}

func getTestTemplateVarPresets() []datadog.TemplateVariablePreset {
return []datadog.TemplateVariablePreset{
{
Name: datadog.String("Preset 1"),
TemplateVariables: []datadog.TemplateVariablePresetValue{
{
Name: datadog.String("Template Var 1"),
Value: datadog.String("Preset 1 Var 1"),
},
{
Name: datadog.String("Template Var 2"),
Value: datadog.String("Preset 1 Var 2"),
},
},
},
{
Name: datadog.String("Preset 2"),
TemplateVariables: []datadog.TemplateVariablePresetValue{
{
Name: datadog.String("Template Var 1"),
Value: datadog.String("Preset 2 Var 1"),
},
},
},
}
}

func createWidgets() []datadog.BoardWidget {
widgets := []datadog.BoardWidget{}
widgets = append(widgets, createAlertGraph())
Expand Down Expand Up @@ -122,7 +194,10 @@ func assertBoardEquals(t *testing.T, actual, expected *datadog.Board) {
t.Errorf("Dashboard description does not match: %s != %s", *actual.Description, *expected.Description)
}
if len(actual.TemplateVariables) != len(expected.TemplateVariables) {
t.Errorf("Number of Dashboard template variables does not match: %d != %d", len(actual.TemplateVariables), len(expected.TemplateVariables))
t.Errorf("Number of template variables does not match: %d != %d", len(actual.TemplateVariables), len(expected.TemplateVariables))
}
if len(actual.TemplateVariablePresets) != len(expected.TemplateVariablePresets) {
t.Errorf("Number of template variables presets does not match: %d != %d", len(actual.TemplateVariablePresets), len(expected.TemplateVariablePresets))
}
if *actual.IsReadOnly != *expected.IsReadOnly {
t.Errorf("Dashboard description does not match: %v != %v", *actual.IsReadOnly, *expected.IsReadOnly)
Expand Down
2 changes: 1 addition & 1 deletion integration/dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestDashboardCreateAndDeleteAdvancesTimeseries(t *testing.T) {
t.Fatalf("Retrieving a dashboard failed when it shouldn't. (%s)", err)
}
assertDashboardEquals(t, actual, expected)

}

func TestDashboardUpdate(t *testing.T) {
Expand Down Expand Up @@ -195,6 +194,7 @@ func TestDashboardGetWithNewId(t *testing.T) {
assert.Contains(t, err.Error(), "unsupported id type")
}
}

func createTestDashboard(t *testing.T) *datadog.Dashboard {
board := getTestDashboard(createGraph)
board, err := client.CreateDashboard(board)
Expand Down

0 comments on commit 261ab16

Please sign in to comment.