Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[resource_datadog_dashboard] add support for toplist widget styles in dashboards #2566

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions datadog/resource_datadog_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7793,6 +7793,14 @@ func getToplistDefinitionSchema() map[string]*schema.Schema {
Schema: getWidgetCustomLinkSchema(),
},
},
"style": {
Description: "The style of the widget",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: getToplistWidgetStyleSchema(),
},
},
}
}
func buildDatadogToplistDefinition(terraformDefinition map[string]interface{}) *datadogV1.ToplistWidgetDefinition {
Expand All @@ -7818,8 +7826,42 @@ func buildDatadogToplistDefinition(terraformDefinition map[string]interface{}) *
if v, ok := terraformDefinition["custom_link"].([]interface{}); ok && len(v) > 0 {
datadogDefinition.SetCustomLinks(*buildDatadogWidgetCustomLinks(&v))
}

if style, ok := terraformDefinition["style"].([]interface{}); ok && len(style) > 0 {
if v, ok := style[0].(map[string]interface{}); ok && len(v) > 0 {
datadogDefinition.SetStyle(buildDatadogToplistStyle(v))
}
}
return datadogDefinition
}

func buildDatadogToplistStyle(terraformToplistStyle map[string]interface{}) datadogV1.ToplistWidgetStyle {
datadogToplistStyle := datadogV1.NewToplistWidgetStyleWithDefaults()

if display, ok := terraformToplistStyle["display"].([]interface{}); ok && len(display) > 0 {
if v, ok := display[0].(map[string]interface{}); ok && len(v) > 0 {
if t, ok := v["type"].(string); ok && len(t) != 0 {
if t == "stacked" {
datadogToplistStyle.SetDisplay(datadogV1.ToplistWidgetDisplay{
ToplistWidgetStacked: datadogV1.NewToplistWidgetStacked(
datadogV1.TOPLISTWIDGETLEGEND_AUTOMATIC,
datadogV1.TOPLISTWIDGETSTACKEDTYPE_STACKED,
),
})
} else if t == "flat" {
datadogToplistStyle.SetDisplay(datadogV1.ToplistWidgetDisplay{
ToplistWidgetFlat: datadogV1.NewToplistWidgetFlatWithDefaults(),
})
}
}
}
}
if palette, ok := terraformToplistStyle["palette"].(string); ok && len(palette) != 0 {
datadogToplistStyle.SetPalette(palette)
}
return *datadogToplistStyle
}

func buildTerraformToplistDefinition(datadogDefinition *datadogV1.ToplistWidgetDefinition) map[string]interface{} {
terraformDefinition := map[string]interface{}{}
// Required params
Expand All @@ -7841,6 +7883,9 @@ func buildTerraformToplistDefinition(datadogDefinition *datadogV1.ToplistWidgetD
if v, ok := datadogDefinition.GetCustomLinksOk(); ok {
terraformDefinition["custom_link"] = buildTerraformWidgetCustomLinks(v)
}
if v, ok := datadogDefinition.GetStyleOk(); ok {
terraformDefinition["style"] = buildTerraformToplistWidgetStyle(v)
}
return terraformDefinition
}

Expand Down Expand Up @@ -7993,6 +8038,27 @@ func buildTerraformToplistRequests(datadogToplistRequests *[]datadogV1.ToplistWi
return &terraformRequests
}

func buildTerraformToplistWidgetStyle(datadogToplistStyle *datadogV1.ToplistWidgetStyle) *[]map[string]interface{} {
terraformStyles := make([]map[string]interface{}, 1)
terraformStyle := map[string]interface{}{}
if display, ok := datadogToplistStyle.GetDisplayOk(); ok {
terraformDisplays := make([]map[string]interface{}, 1)
terraformDisplay := map[string]interface{}{}
if display.ToplistWidgetStacked != nil {
terraformDisplay["type"] = datadogV1.TOPLISTWIDGETSTACKEDTYPE_STACKED
} else if display.ToplistWidgetFlat != nil {
terraformDisplay["type"] = datadogV1.TOPLISTWIDGETFLATTYPE_FLAT
}
terraformDisplays[0] = terraformDisplay
terraformStyle["display"] = terraformDisplays
}
if palette, ok := datadogToplistStyle.GetPaletteOk(); ok {
terraformStyle["palette"] = palette
}
terraformStyles[0] = terraformStyle
return &terraformStyles
}

//
// Trace Service Widget Definition helpers
//
Expand Down Expand Up @@ -9150,6 +9216,38 @@ func getWidgetCustomLinkSchema() map[string]*schema.Schema {
}
}

// Toplist Widget Style helpers

func getToplistWidgetStyleSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"display": {
Description: "The display mode for the widget.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: getWidgetDisplaySchema(),
},
},
"palette": {
Description: "The color palette for the widget.",
Type: schema.TypeString,
Optional: true,
},
}
}

// Widget Display helper

func getWidgetDisplaySchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"type": {
Description: "The display type for the widget.",
Type: schema.TypeString,
Required: true,
},
}
}

func buildDatadogTimeseriesBackground(terraformTimeseriesBackground map[string]interface{}) *datadogV1.TimeseriesBackground {
datadogTimeseriesBackground := &datadogV1.TimeseriesBackground{}
if v, ok := terraformTimeseriesBackground["type"].(string); ok && len(v) != 0 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-09-08T22:06:13.597382-04:00
215 changes: 215 additions & 0 deletions datadog/tests/cassettes/TestAccDatadogDashboardTopListWithStyle.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
---
version: 2
interactions:
- id: 0
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 1453
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: |
{"description":"Created using the Datadog provider in Terraform","id":"","is_read_only":true,"layout_type":"ordered","notify_list":[],"tags":[],"template_variable_presets":[],"template_variables":[],"title":"tf-TestAccDatadogDashboardTopListWithStyle-local-1725847573","widgets":[{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":"\u003e","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"type":"flat"}},"time":{"live_span":"1w"},"title":"Toplist with flat style","title_align":"right","title_size":"16","type":"toplist"}},{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":"\u003e","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"legend":"automatic","type":"stacked"},"palette":"datadog16"},"time":{"live_span":"1w"},"title":"Toplist with stacked style","title_align":"right","title_size":"16","type":"toplist"}}]}
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
url: https://api.datadoghq.com/api/v1/dashboard
method: POST
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: |
{"id":"xr5-ybv-k7p","title":"tf-TestAccDatadogDashboardTopListWithStyle-local-1725847573","description":"Created using the Datadog provider in Terraform","author_handle":"[email protected]","author_name":null,"layout_type":"ordered","url":"/dashboard/xr5-ybv-k7p/tf-testaccdatadogdashboardtoplistwithstyle-local-1725847573","is_read_only":true,"template_variables":[],"widgets":[{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":">","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"type":"flat"}},"time":{"live_span":"1w"},"title":"Toplist with flat style","title_align":"right","title_size":"16","type":"toplist"},"id":6874455526830069},{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":">","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"legend":"automatic","type":"stacked"},"palette":"datadog16"},"time":{"live_span":"1w"},"title":"Toplist with stacked style","title_align":"right","title_size":"16","type":"toplist"},"id":4085304145087860}],"notify_list":[],"created_at":"2024-09-09T02:06:15.930563+00:00","modified_at":"2024-09-09T02:06:15.930563+00:00","template_variable_presets":[],"tags":[]}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: 394.23525ms
- id: 1
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/dashboard/xr5-ybv-k7p
method: GET
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: |
{"id":"xr5-ybv-k7p","title":"tf-TestAccDatadogDashboardTopListWithStyle-local-1725847573","description":"Created using the Datadog provider in Terraform","author_handle":"[email protected]","author_name":null,"layout_type":"ordered","url":"/dashboard/xr5-ybv-k7p/tf-testaccdatadogdashboardtoplistwithstyle-local-1725847573","is_read_only":true,"template_variables":[],"widgets":[{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":">","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"type":"flat"}},"time":{"live_span":"1w"},"title":"Toplist with flat style","title_align":"right","title_size":"16","type":"toplist"},"id":6874455526830069},{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":">","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"legend":"automatic","type":"stacked"},"palette":"datadog16"},"time":{"live_span":"1w"},"title":"Toplist with stacked style","title_align":"right","title_size":"16","type":"toplist"},"id":4085304145087860}],"notify_list":[],"created_at":"2024-09-09T02:06:15.930563+00:00","modified_at":"2024-09-09T02:06:15.930563+00:00","template_variable_presets":[],"tags":[]}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: 97.972417ms
- id: 2
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/dashboard/xr5-ybv-k7p
method: GET
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: |
{"id":"xr5-ybv-k7p","title":"tf-TestAccDatadogDashboardTopListWithStyle-local-1725847573","description":"Created using the Datadog provider in Terraform","author_handle":"[email protected]","author_name":null,"layout_type":"ordered","url":"/dashboard/xr5-ybv-k7p/tf-testaccdatadogdashboardtoplistwithstyle-local-1725847573","is_read_only":true,"template_variables":[],"widgets":[{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":">","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"type":"flat"}},"time":{"live_span":"1w"},"title":"Toplist with flat style","title_align":"right","title_size":"16","type":"toplist"},"id":6874455526830069},{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":">","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"legend":"automatic","type":"stacked"},"palette":"datadog16"},"time":{"live_span":"1w"},"title":"Toplist with stacked style","title_align":"right","title_size":"16","type":"toplist"},"id":4085304145087860}],"notify_list":[],"created_at":"2024-09-09T02:06:15.930563+00:00","modified_at":"2024-09-09T02:06:15.930563+00:00","template_variable_presets":[],"tags":[]}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: 126.133375ms
- id: 3
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/dashboard/xr5-ybv-k7p
method: GET
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: |
{"id":"xr5-ybv-k7p","title":"tf-TestAccDatadogDashboardTopListWithStyle-local-1725847573","description":"Created using the Datadog provider in Terraform","author_handle":"[email protected]","author_name":null,"layout_type":"ordered","url":"/dashboard/xr5-ybv-k7p/tf-testaccdatadogdashboardtoplistwithstyle-local-1725847573","is_read_only":true,"template_variables":[],"widgets":[{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":">","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"type":"flat"}},"time":{"live_span":"1w"},"title":"Toplist with flat style","title_align":"right","title_size":"16","type":"toplist"},"id":6874455526830069},{"definition":{"custom_links":[{"label":"Test Custom Link label","link":"https://app.datadoghq.com/dashboard/lists"},{"is_hidden":true,"link":"https://app.datadoghq.com/dashboard/lists","override_label":"logs"}],"requests":[{"conditional_formats":[{"comparator":">","hide_value":false,"palette":"white_on_red","value":15000}],"q":"top(avg:system.cpu.user{*} by {service,app_id}, 10, 'sum', 'desc')"}],"style":{"display":{"legend":"automatic","type":"stacked"},"palette":"datadog16"},"time":{"live_span":"1w"},"title":"Toplist with stacked style","title_align":"right","title_size":"16","type":"toplist"},"id":4085304145087860}],"notify_list":[],"created_at":"2024-09-09T02:06:15.930563+00:00","modified_at":"2024-09-09T02:06:15.930563+00:00","template_variable_presets":[],"tags":[]}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: 113.903708ms
- id: 4
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/dashboard/xr5-ybv-k7p
method: DELETE
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: |
{"deleted_dashboard_id":"xr5-ybv-k7p"}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: 170.26275ms
- id: 5
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v1/dashboard/xr5-ybv-k7p
method: GET
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: '{"errors":["Dashboard with ID xr5-ybv-k7p not found"]}'
headers:
Content-Type:
- application/json
status: 404 Not Found
code: 404
duration: 86.552916ms
Loading
Loading