Skip to content

Commit

Permalink
[datadog_dashboard] Add support for text_formats in query table widge…
Browse files Browse the repository at this point in the history
…t requests (#2587)

* [terraform][query table widget] Update description for sub-string field

* refactor and bug fix

* Add changes to docs

* Add changes to docs for unrelated files

* add intermediate schema key

* [terraform][query table widget]Make some fields required

* [terraform][query table widget] Description changes

* [terraform][query table widget] Doc changes

* [terraform][query table widget] Fix bug for palette field, add tests

* [terraform][query table widget] Allow for empty text_formats block

* [terraform][query table widget] Update description for sub-string field

---------

Co-authored-by: Sherzod Karimov <[email protected]>
  • Loading branch information
hyungl and skarimo authored Oct 3, 2024
1 parent 37b25e6 commit e012111
Show file tree
Hide file tree
Showing 9 changed files with 957 additions and 2 deletions.
188 changes: 188 additions & 0 deletions datadog/resource_datadog_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4491,6 +4491,58 @@ func getQueryTableRequestSchema() map[string]*schema.Schema {
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewTableWidgetCellDisplayModeFromValue),
},
},
"text_formats": {
Description: "Text formats define how to format text in table widget content. Multiple `text_formats` blocks are allowed using the structure below. This resource is in beta and is subject to change.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"text_format": {
Description: "The text format to apply to the items in a table widget column.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"match": {
Type: schema.TypeList,
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.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 buildDatadogQueryTableRequests(terraformRequests *[]interface{}) *[]datadogV1.TableWidgetRequest {
Expand Down Expand Up @@ -4581,6 +4633,19 @@ func buildDatadogQueryTableRequests(terraformRequests *[]interface{}) *[]datadog
}
datadogQueryTableRequest.CellDisplayMode = datadogCellDisplayMode
}
if v, ok := terraformRequest["text_formats"].([]interface{}); ok && len(v) != 0 {
datadogQueryTableRequest.TextFormats = make([][]datadogV1.TableWidgetTextFormatRule, len(v))
for i, w := range v {
if c, ok := w.(map[string]interface{}); ok {
if textFormat, ok := c["text_format"].([]interface{}); ok && len(textFormat) > 0 {
datadogQueryTableRequest.TextFormats[i] = *buildDatadogQueryTableTextFormat(&textFormat)
}
} else {
datadogQueryTableRequest.TextFormats[i] = []datadogV1.TableWidgetTextFormatRule{}
}
}
}

datadogRequests[i] = *datadogQueryTableRequest
}
return &datadogRequests
Expand Down Expand Up @@ -4641,11 +4706,134 @@ func buildTerraformQueryTableRequests(datadogQueryTableRequests *[]datadogV1.Tab
}
terraformRequest["cell_display_mode"] = terraformCellDisplayMode
}
if v, ok := datadogRequest.GetTextFormatsOk(); ok {
terraformTextFormats := make([]map[string][]map[string]interface{}, len(*v))
for i, textFormat := range *v {
test := buildTerraformQueryTableTextFormat(&textFormat)
terraformTextFormats[i] = make(map[string][]map[string]interface{})
terraformTextFormats[i]["text_format"] = *test
}
terraformRequest["text_formats"] = terraformTextFormats
}
terraformRequests[i] = terraformRequest
}
return &terraformRequests
}

// Query Table Widget Text Format Helpers
func buildDatadogQueryTableTextFormat(terraformQueryTableTextFormat *[]interface{}) *[]datadogV1.TableWidgetTextFormatRule {
datadogQueryTableTextFormat := make([]datadogV1.TableWidgetTextFormatRule, len(*terraformQueryTableTextFormat))
for j, textFormatRule := range *terraformQueryTableTextFormat {
terraformTextFormatRule := textFormatRule.(map[string]interface{})

match, _ := terraformTextFormatRule["match"].([]interface{})
terraformTextFormatMatch := match[0].(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 && len(v) > 0 {
if replace, ok := v[0].(map[string]interface{}); ok {
if w, ok := replace["type"].(string); ok && len(w) != 0 {
switch w {
case "all":
datadogReplace := datadogV1.NewTableWidgetTextFormatReplaceAll(datadogV1.TABLEWIDGETTEXTFORMATREPLACEALLTYPE_ALL, replace["with"].(string))
datadogTextFormatRule.SetReplace(datadogV1.TableWidgetTextFormatReplaceAllAsTableWidgetTextFormatReplace(datadogReplace))
case "substring":
datadogReplace := datadogV1.NewTableWidgetTextFormatReplaceSubstring(replace["substring"].(string), datadogV1.TABLEWIDGETTEXTFORMATREPLACESUBSTRINGTYPE_SUBSTRING, replace["with"].(string))
datadogTextFormatRule.SetReplace(datadogV1.TableWidgetTextFormatReplaceSubstringAsTableWidgetTextFormatReplace(datadogReplace))
}
}
}
}
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(v)
}
datadogQueryTableTextFormat[j] = *datadogTextFormatRule
}
return &datadogQueryTableTextFormat
}
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"] = []interface{}{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"] = []interface{}{replace}
}
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"] = []interface{}{replace}
}
}
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 &terraformQueryTableTextFormat
}
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),
Required: true,
},
"value": {
Description: "Table Widget Match String.",
Type: schema.TypeString,
Required: true,
},
}
}
func getTableWidgetTextFormatReplaceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"type": {
Description: "Table widget text format replace all type.",
Type: schema.TypeString,
Required: true,
},
"with": {
Description: "Table Widget Match String.",
Type: schema.TypeString,
Required: true,
},
"substring": {
Description: "Text that will be replaced. Must be used with type `substring`.",
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-10-03T10:24:53.730398-04:00
Loading

0 comments on commit e012111

Please sign in to comment.