Skip to content

Commit

Permalink
Don't allow empty content in note widget (#607)
Browse files Browse the repository at this point in the history
If the note_definition of a widget is empty, it will fail JSON
serialization. Let's make sure it fails validation instead.

Closes #574

Co-authored-by: Jiri Kuncar <jiri.kuncar@datadoghq.com>
  • Loading branch information
therve and jirikuncar authored Jul 30, 2020
1 parent d02d292 commit dcdb66c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions datadog/resource_datadog_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func resourceDatadogDashboard() *schema.Resource {
Expand Down Expand Up @@ -2905,8 +2906,9 @@ func buildTerraformManageStatusDefinition(datadogDefinition datadogV1.MonitorSum
func getNoteDefinitionSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"background_color": {
Type: schema.TypeString,
Expand Down
34 changes: 34 additions & 0 deletions datadog/resource_datadog_dashboard_note_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package datadog

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

// JSON export used as test scenario
Expand Down Expand Up @@ -62,6 +65,21 @@ resource "datadog_dashboard" "note_dashboard" {
}
`

const datadogDashboardNoteConfigNoContent = `
resource "datadog_dashboard" "note_dashboard" {
title = "Acceptance Test Notes Widget Dashboard"
description = "Created using the Datadog provider in Terraform"
layout_type = "ordered"
is_read_only = "true"
widget {
note_definition {
content = ""
}
}
}
`

var datadogDashboardNoteAsserts = []string{
"description = Created using the Datadog provider in Terraform",
"widget.0.note_definition.0.content = This is a note widget",
Expand All @@ -83,3 +101,19 @@ func TestAccDatadogDashboardNote(t *testing.T) {
func TestAccDatadogDashboardNote_import(t *testing.T) {
testAccDatadogDashboardWidgetUtil_import(t, datadogDashboardNoteConfig, "datadog_dashboard.note_dashboard")
}

func TestAccDatadogDashboardNoteContentError(t *testing.T) {
accProviders, cleanup := testAccProviders(t, initRecorder(t))
defer cleanup(t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: accProviders,
Steps: []resource.TestStep{
{
Config: datadogDashboardNoteConfigNoContent,
ExpectError: regexp.MustCompile("expected \"widget.0.note_definition.0.content\" to not be an empty string"),
},
},
})
}

0 comments on commit dcdb66c

Please sign in to comment.