Skip to content

Commit

Permalink
query table widget - add text formats to request
Browse files Browse the repository at this point in the history
  • Loading branch information
hyungl committed Sep 19, 2024
1 parent 8ea5c87 commit 82c178c
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 2 deletions.
192 changes: 192 additions & 0 deletions datadog/resource_datadog_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4491,6 +4491,51 @@ func getQueryTableRequestSchema() map[string]*schema.Schema {
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewTableWidgetCellDisplayModeFromValue),
},
},
"text_formats": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"match": {
Type: schema.TypeMap,
MaxItems: 1,
Required: true,
Description: "Match rule for the table widget text format.",
Elem: &schema.Resource{
Schema: getTableWidgetTextFormatMatchSchema(),
},
},
"palette": {
Description: "The color palette to apply.",
Type: schema.TypeString,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewTableWidgetTextFormatPaletteFromValue),
Optional: true,
},
"replace": {
Type: schema.TypeMap,
MaxItems: 1,
Optional: true,
Description: "Match rule for the table widget text format.",
Elem: &schema.Resource{
Schema: getTableWidgetTextFormatReplaceSchema(),
},
},
"custom_bg_color": {
Description: "The custom color palette to apply to the background.",
Type: schema.TypeString,
Optional: true,
},
"custom_fg_color": {
Description: "The custom color palette to apply to the foreground text.",
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}
func buildDatadogQueryTableRequests(terraformRequests *[]interface{}) *[]datadogV1.TableWidgetRequest {
Expand Down Expand Up @@ -4581,6 +4626,9 @@ func buildDatadogQueryTableRequests(terraformRequests *[]interface{}) *[]datadog
}
datadogQueryTableRequest.CellDisplayMode = datadogCellDisplayMode
}
if v, ok := terraformRequest["text_formats"].([][]interface{}); ok && len(v) != 0 {
datadogQueryTableRequest.TextFormats = *buildDatadogQueryTableTextFormat(&v)
}
datadogRequests[i] = *datadogQueryTableRequest
}
return &datadogRequests
Expand Down Expand Up @@ -4641,11 +4689,155 @@ func buildTerraformQueryTableRequests(datadogQueryTableRequests *[]datadogV1.Tab
}
terraformRequest["cell_display_mode"] = terraformCellDisplayMode
}
if v, ok := datadogRequest.GetTextFormatsOk(); ok {
terraformTextFormats := buildTerraformQueryTableTextFormat(v)
terraformRequest["text_formats"] = terraformTextFormats
}
terraformRequests[i] = terraformRequest
}
return &terraformRequests
}

// Widget Text Format helpers
func buildDatadogQueryTableTextFormat(terraformQueryTableTextFormat *[][]interface{}) *[][]datadogV1.TableWidgetTextFormatRule {
datadogQueryTableTextFormat := make([][]datadogV1.TableWidgetTextFormatRule, len(*terraformQueryTableTextFormat))
for i, textFormatArray := range *terraformQueryTableTextFormat {
terraformTextFormatArray := make([]datadogV1.TableWidgetTextFormatRule, len(textFormatArray))
for j, textFormatRule := range textFormatArray {
terraformTextFormatRule := textFormatRule.(map[string]interface{})
terraformTextFormatMatch := terraformTextFormatRule["match"].(map[string]interface{})
datadogMatch := datadogV1.NewTableWidgetTextFormatMatch(datadogV1.TableWidgetTextFormatMatchType(terraformTextFormatMatch["type"].(string)), terraformTextFormatMatch["value"].(string))
datadogTextFormatRule := datadogV1.NewTableWidgetTextFormatRule(*datadogMatch)
// Optional
if v, ok := terraformTextFormatRule["replace"].(interface{}); ok {
switch v.(type) {
case datadogV1.TableWidgetTextFormatReplaceAll:
datadogReplace := v.(datadogV1.TableWidgetTextFormatReplaceAll)
datadogTextFormatRule.SetReplace(datadogV1.TableWidgetTextFormatReplaceAllAsTableWidgetTextFormatReplace(&datadogReplace))
case datadogV1.TableWidgetTextFormatReplaceSubstring:
datadogReplace := v.(datadogV1.TableWidgetTextFormatReplaceSubstring)
datadogTextFormatRule.SetReplace(datadogV1.TableWidgetTextFormatReplaceSubstringAsTableWidgetTextFormatReplace(&datadogReplace))
default:
}
}
if v, ok := terraformTextFormatRule["palette"].(string); ok && len(v) != 0 {
datadogTextFormatRule.SetPalette(datadogV1.TableWidgetTextFormatPalette(v))
}
if v, ok := terraformTextFormatRule["custom_bg_color"].(string); ok && len(v) != 0 {
datadogTextFormatRule.SetCustomBgColor(v)
}
if v, ok := terraformTextFormatRule["custom_fg_color"].(string); ok && len(v) != 0 {
datadogTextFormatRule.SetCustomFgColor(v)
}
terraformTextFormatArray[j] = *datadogTextFormatRule
}
datadogQueryTableTextFormat[i] = terraformTextFormatArray
}
return &datadogQueryTableTextFormat
}
func buildTerraformQueryTableTextFormat(datadogQueryTableTextFormats *[][]datadogV1.TableWidgetTextFormatRule) *[][]map[string]interface{} {
terraformQueryTableTextFormats := make([][]map[string]interface{}, len(*datadogQueryTableTextFormats))
for i, datadogQueryTableTextFormatRuleArray := range *datadogQueryTableTextFormats {
terraformQueryTableTextFormatRuleArray := make([]map[string]interface{}, len(datadogQueryTableTextFormatRuleArray))
for j, datadogQueryTableTextFormatRule := range datadogQueryTableTextFormatRuleArray {
terraformQueryTableTextFormatRule := map[string]interface{}{}
// Required params
terraformQueryTableTextFormatRule["match"] = datadogQueryTableTextFormatRule.GetMatch()
// Optional params
if v, ok := datadogQueryTableTextFormatRule.GetReplaceOk(); ok {
terraformQueryTableTextFormatRule["replace"] = v
}
if v, ok := datadogQueryTableTextFormatRule.GetPaletteOk(); ok {
terraformQueryTableTextFormatRule["palette"] = v
}
if v, ok := datadogQueryTableTextFormatRule.GetCustomBgColorOk(); ok {
terraformQueryTableTextFormatRule["custom_bg_color"] = v
}
if v, ok := datadogQueryTableTextFormatRule.GetCustomFgColorOk(); ok {
terraformQueryTableTextFormatRule["custom_fg_color"] = v
}
terraformQueryTableTextFormatRuleArray[j] = terraformQueryTableTextFormatRule
}
terraformQueryTableTextFormats[i] = terraformQueryTableTextFormatRuleArray
}
return &terraformQueryTableTextFormats
}

// Table Widget Text Format helpers

func getQueryTableFormatRuleSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"match": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Match rule for the table widget text format.",
Elem: &schema.Resource{
Schema: getTableWidgetTextFormatMatchSchema(),
},
},
"palette": {
Description: "The color palette to apply.",
Type: schema.TypeString,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewTableWidgetTextFormatPaletteFromValue),
Required: true,
},
"replace": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "Match rule for the table widget text format.",
Elem: &schema.Resource{
Schema: getTableWidgetTextFormatReplaceSchema(),
},
},
"custom_bg_color": {
Description: "The custom color palette to apply to the background.",
Type: schema.TypeString,
Optional: true,
},
"custom_fg_color": {
Description: "The custom color palette to apply to the foreground text.",
Type: schema.TypeString,
Optional: true,
},
}
}
func getTableWidgetTextFormatMatchSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"type": {
Description: "Match or compare option.",
Type: schema.TypeString,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewTableWidgetTextFormatMatchTypeFromValue),
Optional: true,
},
"value": {
Description: "Table Widget Match String.",
Type: schema.TypeString,
Optional: true,
},
}
}
func getTableWidgetTextFormatReplaceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"type": {
Description: "Table widget text format replace all type.",
Type: schema.TypeString,
Optional: true,
},
"with": {
Description: "Table Widget Match String.",
Type: schema.TypeString,
Optional: true,
},
"substring": {
Description: "Text that will be replaced.",
Type: schema.TypeString,
Optional: true,
},
}
}

//
// Scatterplot Widget Definition helpers
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-09-19T06:26:53.475609-04:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
version: 2
interactions: []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-09-19T06:27:09.039894-04:00
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
version: 2
interactions: []
79 changes: 79 additions & 0 deletions datadog/tests/resource_datadog_dashboard_query_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,54 @@ resource "datadog_dashboard" "query_table_dashboard" {
}
`

const datadogDashboardQueryTableConfigWithTextFormats = `
resource "datadog_dashboard" "query_table_dashboard" {
title = "{{uniq}}"
description = "Created using the Datadog provider in Terraform"
layout_type = "ordered"
is_read_only = "true"
widget {
query_table_definition {
title_size = "16"
title = "system.cpu.user, system.load.1"
title_align = "right"
live_span = "1d"
request {
aggregator = "max"
q = "avg:system.cpu.user{account:prod} by {service, team}"
alias = "cpu user"
limit = 25
order = "desc"
cell_display_mode = ["number"]
text_formats = [
[{
match = {
type = "is"
value = "test"
}
palette = "custom_bg"
},
{
match = {
type = "is"
value = "test"
}
}],
[{
match = {
type = "is"
value = "test"
}
}]
]
}
has_search_bar = "auto"
}
}
}
`

var datadogDashboardQueryTableAsserts = []string{
"widget.0.query_table_definition.0.live_span = 1d",
"widget.0.query_table_definition.0.request.1.order =",
Expand Down Expand Up @@ -315,6 +363,29 @@ var datadogDashboardQueryTableFormulaAsserts = []string{
"widget.2.query_table_definition.0.request.0.query.0.apm_resource_stats_query.0.primary_tag_value = abc",
}

var datadogDashboardQueryTableWithTextFormatsAsserts = []string{
"widget.0.query_table_definition.0.live_span = 1d",
"widget.0.query_table_definition.0.request.0.conditional_formats.0.timeframe =",
"widget.0.query_table_definition.0.request.0.q = avg:system.cpu.user{account:prod} by {service, team}",
"title = {{uniq}}",
"widget.0.query_table_definition.0.request.0.aggregator = max",
"widget.0.query_table_definition.0.request.0.order = desc",
"widget.0.query_table_definition.0.request.0.alias = cpu user",
"is_read_only = true",
"widget.0.query_table_definition.0.request.0.limit = 25",
"layout_type = ordered",
"widget.0.query_table_definition.0.title = system.cpu.user, system.load.1",
"widget.0.query_table_definition.0.title_align = right",
"widget.1.query_table_definition.0.request.0.apm_stats_query.0.service = service",
"widget.1.query_table_definition.0.request.0.apm_stats_query.0.env = env",
"widget.1.query_table_definition.0.request.0.apm_stats_query.0.primary_tag = tag:*",
"widget.1.query_table_definition.0.request.0.apm_stats_query.0.name = name",
"widget.1.query_table_definition.0.request.0.apm_stats_query.0.row_type = resource",
"widget.0.query_table_definition.0.request.0.cell_display_mode.0 = number",
"widget.0.query_table_definition.0.has_search_bar = auto",
"widget.1.query_table_definition.0.has_search_bar = never",
}

func TestAccDatadogDashboardQueryTable(t *testing.T) {
testAccDatadogDashboardWidgetUtil(t, datadogDashboardQueryTableConfig, "datadog_dashboard.query_table_dashboard", datadogDashboardQueryTableAsserts)
}
Expand All @@ -330,3 +401,11 @@ func TestAccDatadogDashboardQueryTableFormula(t *testing.T) {
func TestAccDatadogDashboardQueryTableFormula_import(t *testing.T) {
testAccDatadogDashboardWidgetUtilImport(t, datadogDashboardQueryTableFormulaConfig, "datadog_dashboard.query_table_dashboard")
}

func TestAccDatadogDashboardQueryTableWithTextFormats(t *testing.T) {
testAccDatadogDashboardWidgetUtil(t, datadogDashboardQueryTableConfigWithTextFormats, "datadog_dashboard.query_table_dashboard", datadogDashboardQueryTableWithTextFormatsAsserts)
}

func TestAccDatadogDashboardQueryTableWithTextFormats_import(t *testing.T) {
testAccDatadogDashboardWidgetUtilImport(t, datadogDashboardQueryTableConfigWithTextFormats, "datadog_dashboard.query_table_dashboard")
}
Loading

0 comments on commit 82c178c

Please sign in to comment.