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 23, 2024
1 parent 82c178c commit 5eef67b
Show file tree
Hide file tree
Showing 8 changed files with 545 additions and 311 deletions.
178 changes: 81 additions & 97 deletions datadog/resource_datadog_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -4690,118 +4695,97 @@ 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
}
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:
// 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{})
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
} else {
datadogTextFormatRule.Replace = nil
}
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)
} else {
datadogTextFormatRule.CustomBgColor = nil
}
if v, ok := terraformTextFormatRule["custom_fg_color"].(string); ok && len(v) != 0 {
datadogTextFormatRule.SetCustomFgColor(v)
} else {
datadogTextFormatRule.CustomFgColor = nil
}
datadogQueryTableTextFormat[i] = terraformTextFormatArray
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
}
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,
},
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{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-09-19T06:26:53.475609-04:00
2024-09-23T01:01:18.742561-04:00
Loading

0 comments on commit 5eef67b

Please sign in to comment.