Skip to content

Commit

Permalink
refactor and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hyungl committed Sep 20, 2024
1 parent 82c178c commit 7b41249
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 115 deletions.
172 changes: 78 additions & 94 deletions datadog/resource_datadog_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ func getSplitGraphSourceWidgetSchema() map[string]*schema.Schema {
}
}
func buildDatadogWidgets(terraformWidgets *[]interface{}) (*[]datadogV1.Widget, error) {
fmt.Print("being used?")
datadogWidgets := make([]datadogV1.Widget, len(*terraformWidgets))
for i, terraformWidget := range *terraformWidgets {
if terraformWidget == nil {
Expand Down Expand Up @@ -4626,8 +4627,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
}
Expand Down Expand Up @@ -4690,7 +4696,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
Expand All @@ -4699,110 +4709,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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-09-19T06:26:53.475609-04:00
2024-09-20T05:49:48.27432-04:00
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-09-19T06:27:09.039894-04:00
2024-09-20T05:49:52.828285-04:00
33 changes: 14 additions & 19 deletions datadog/tests/resource_datadog_dashboard_query_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}]
]
}
Expand Down Expand Up @@ -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",
Expand All @@ -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) {
Expand Down

0 comments on commit 7b41249

Please sign in to comment.