diff --git a/datadog/resource_datadog_dashboard.go b/datadog/resource_datadog_dashboard.go index 53c5d37489..a2161f49bd 100644 --- a/datadog/resource_datadog_dashboard.go +++ b/datadog/resource_datadog_dashboard.go @@ -4626,8 +4626,13 @@ func buildDatadogQueryTableRequests(terraformRequests *[]interface{}) *[]datadog } datadogQueryTableRequest.CellDisplayMode = datadogCellDisplayMode } - if v, ok := terraformRequest["text_formats"].([][]interface{}); ok && len(v) != 0 { - datadogQueryTableRequest.TextFormats = *buildDatadogQueryTableTextFormat(&v) + if v, ok := terraformRequest["text_formats"].([]interface{}); ok && len(v) != 0 { + datadogQueryTableRequest.TextFormats = make([][]datadogV1.TableWidgetTextFormatRule, len(v)) + for i, textFormat := range v { + if w, ok := textFormat.([]interface{}); ok { + datadogQueryTableRequest.TextFormats[i] = *buildDatadogQueryTableTextFormat(&w) + } + } } datadogRequests[i] = *datadogQueryTableRequest } @@ -4690,7 +4695,11 @@ func buildTerraformQueryTableRequests(datadogQueryTableRequests *[]datadogV1.Tab terraformRequest["cell_display_mode"] = terraformCellDisplayMode } if v, ok := datadogRequest.GetTextFormatsOk(); ok { - terraformTextFormats := buildTerraformQueryTableTextFormat(v) + terraformTextFormats := make([][]map[string]interface{}, len(*v)) + for i, textFormat := range *v { + test := buildTerraformQueryTableTextFormat(&textFormat) + terraformTextFormats[i] = *test + } terraformRequest["text_formats"] = terraformTextFormats } terraformRequests[i] = terraformRequest @@ -4699,110 +4708,84 @@ func buildTerraformQueryTableRequests(datadogQueryTableRequests *[]datadogV1.Tab } // 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: +func buildDatadogQueryTableTextFormat(terraformQueryTableTextFormat *[]interface{}) *[]datadogV1.TableWidgetTextFormatRule { + datadogQueryTableTextFormat := make([]datadogV1.TableWidgetTextFormatRule, len(*terraformQueryTableTextFormat)) + for j, textFormatRule := range *terraformQueryTableTextFormat { + 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"].(map[string]interface{}); ok { + if w, ok := v["type"].(string); ok && len(w) != 0 { + switch w { + case "all": + datadogReplace := datadogV1.NewTableWidgetTextFormatReplaceAll(datadogV1.TABLEWIDGETTEXTFORMATREPLACEALLTYPE_ALL, v["with"].(string)) + datadogTextFormatRule.SetReplace(datadogV1.TableWidgetTextFormatReplaceAllAsTableWidgetTextFormatReplace(datadogReplace)) + case "substring": + datadogReplace := datadogV1.NewTableWidgetTextFormatReplaceSubstring(v["substring"].(string), datadogV1.TABLEWIDGETTEXTFORMATREPLACESUBSTRINGTYPE_SUBSTRING, v["with"].(string)) + datadogTextFormatRule.SetReplace(datadogV1.TableWidgetTextFormatReplaceSubstringAsTableWidgetTextFormatReplace(datadogReplace)) } } - 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 + if v, ok := terraformTextFormatRule["palette"].(string); ok && len(v) != 0 { + datadogTextFormatRule.SetPalette(datadogV1.TableWidgetTextFormatPalette(v)) + } else { + datadogTextFormatRule.Palette = nil + } + 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("Hi") + datadogTextFormatRule.SetCustomFgColor(v) + } else { + datadogTextFormatRule.CustomFgColor = nil + } + datadogQueryTableTextFormat[j] = *datadogTextFormatRule } 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 +func buildTerraformQueryTableTextFormat(datadogQueryTableTextFormat *[]datadogV1.TableWidgetTextFormatRule) *[]map[string]interface{} { + terraformQueryTableTextFormat := make([]map[string]interface{}, len(*datadogQueryTableTextFormat)) + for i, datadogQueryTableTextFormatRule := range *datadogQueryTableTextFormat { + terraformQueryTableTextFormatRule := map[string]interface{}{} + // Required params + match := make(map[string]interface{}) + match["type"] = datadogQueryTableTextFormatRule.GetMatch().Type + match["value"] = datadogQueryTableTextFormatRule.GetMatch().Value + terraformQueryTableTextFormatRule["match"] = match + // Optional params + if v, ok := datadogQueryTableTextFormatRule.GetReplaceOk(); ok { + if v.TableWidgetTextFormatReplaceAll != nil { + replace := make(map[string]interface{}) + replace["type"] = v.TableWidgetTextFormatReplaceAll.Type + replace["with"] = v.TableWidgetTextFormatReplaceAll.With + terraformQueryTableTextFormatRule["replace"] = replace } - if v, ok := datadogQueryTableTextFormatRule.GetCustomFgColorOk(); ok { - terraformQueryTableTextFormatRule["custom_fg_color"] = v + if v.TableWidgetTextFormatReplaceSubstring != nil { + replace := make(map[string]interface{}) + replace["type"] = v.TableWidgetTextFormatReplaceSubstring.Type + replace["with"] = v.TableWidgetTextFormatReplaceSubstring.With + replace["substring"] = v.TableWidgetTextFormatReplaceSubstring.Substring + terraformQueryTableTextFormatRule["replace"] = replace } - terraformQueryTableTextFormatRuleArray[j] = terraformQueryTableTextFormatRule } - terraformQueryTableTextFormats[i] = terraformQueryTableTextFormatRuleArray + 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 + } + terraformQueryTableTextFormat[i] = terraformQueryTableTextFormatRule } - return &terraformQueryTableTextFormats + return &terraformQueryTableTextFormat } // 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": { diff --git a/datadog/tests/cassettes/TestAccDatadogDashboardQueryTableWithTextFormats.freeze b/datadog/tests/cassettes/TestAccDatadogDashboardQueryTableWithTextFormats.freeze index 04be590ab3..4bacbc3e2f 100644 --- a/datadog/tests/cassettes/TestAccDatadogDashboardQueryTableWithTextFormats.freeze +++ b/datadog/tests/cassettes/TestAccDatadogDashboardQueryTableWithTextFormats.freeze @@ -1 +1 @@ -2024-09-19T06:26:53.475609-04:00 \ No newline at end of file +2024-09-20T05:49:48.27432-04:00 \ No newline at end of file diff --git a/datadog/tests/cassettes/TestAccDatadogDashboardQueryTableWithTextFormats_import.freeze b/datadog/tests/cassettes/TestAccDatadogDashboardQueryTableWithTextFormats_import.freeze index 0cb4c52f49..4b3984f1b7 100644 --- a/datadog/tests/cassettes/TestAccDatadogDashboardQueryTableWithTextFormats_import.freeze +++ b/datadog/tests/cassettes/TestAccDatadogDashboardQueryTableWithTextFormats_import.freeze @@ -1 +1 @@ -2024-09-19T06:27:09.039894-04:00 \ No newline at end of file +2024-09-20T05:49:52.828285-04:00 \ No newline at end of file diff --git a/datadog/tests/resource_datadog_dashboard_query_table_test.go b/datadog/tests/resource_datadog_dashboard_query_table_test.go index d38c8c80a0..f4c68e3f3e 100644 --- a/datadog/tests/resource_datadog_dashboard_query_table_test.go +++ b/datadog/tests/resource_datadog_dashboard_query_table_test.go @@ -229,7 +229,7 @@ resource "datadog_dashboard" "query_table_dashboard" { widget { query_table_definition { title_size = "16" - title = "system.cpu.user, system.load.1" + title = "this is hyung" title_align = "right" live_span = "1d" request { @@ -245,19 +245,23 @@ resource "datadog_dashboard" "query_table_dashboard" { type = "is" value = "test" } - palette = "custom_bg" - }, - { - match = { - type = "is" - value = "test" - } + palette = "black_on_light_yellow" + replace = { + type = "all" + with = "test" + }, + custom_bg_color = null + custom_fg_color = null }], [{ match = { type = "is" - value = "test" + value = "versus" } + palette = null + replace = { + }, + custom_bg_color = null }] ] } @@ -365,7 +369,6 @@ var datadogDashboardQueryTableFormulaAsserts = []string{ 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", @@ -374,16 +377,8 @@ var datadogDashboardQueryTableWithTextFormatsAsserts = []string{ "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 = this is hyung", "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) {