Skip to content

Commit 0e8bdff

Browse files
Add support for sorting toplist widget.
1 parent 996e233 commit 0e8bdff

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-0
lines changed

datadog/resource_datadog_dashboard.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8260,8 +8260,59 @@ func getToplistRequestSchema() map[string]*schema.Schema {
82608260
Schema: getWidgetRequestStyle(),
82618261
},
82628262
},
8263+
"sort": getToplistRequestSortSchema(),
82638264
}
82648265
}
8266+
8267+
func getToplistRequestSortSchema() *schema.Schema {
8268+
return &schema.Schema{
8269+
Description: "The controls for sorting the widget.",
8270+
Type: schema.TypeList,
8271+
Optional: true,
8272+
MaxItems: 1,
8273+
Elem: &schema.Resource{
8274+
Schema: map[string]*schema.Schema{
8275+
"count": {
8276+
Description: "The number of items to limit the widget to.",
8277+
Type: schema.TypeInt,
8278+
Optional: true,
8279+
},
8280+
"order_by": {
8281+
Description: "The array of items to sort the widget by in order.",
8282+
Type: schema.TypeList,
8283+
Optional: true,
8284+
Elem: &schema.Resource{
8285+
Schema: map[string]*schema.Schema{
8286+
"index": {
8287+
Description: "The index of the formula to sort by.",
8288+
Type: schema.TypeInt,
8289+
Optional: true,
8290+
},
8291+
"order": {
8292+
Description: "Widget sorting methods. Allowed enum values: `asc`, `desc`",
8293+
Type: schema.TypeString,
8294+
Required: true,
8295+
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewWidgetSortFromValue),
8296+
},
8297+
"type": {
8298+
Description: "Set the sort type to formula. Allowed enum values: `formula`",
8299+
Type: schema.TypeString,
8300+
Required: true,
8301+
ValidateDiagFunc: validators.ValidateStringEnumValue(datadogV1.FORMULATYPE_FORMULA, datadogV1.GROUPTYPE_GROUP),
8302+
},
8303+
"name": {
8304+
Description: "The name of the group.",
8305+
Type: schema.TypeString,
8306+
Optional: true,
8307+
},
8308+
},
8309+
},
8310+
},
8311+
},
8312+
},
8313+
}
8314+
}
8315+
82658316
func buildDatadogToplistRequests(terraformRequests *[]interface{}) *[]datadogV1.ToplistWidgetRequest {
82668317
datadogRequests := make([]datadogV1.ToplistWidgetRequest, len(*terraformRequests))
82678318
for i, r := range *terraformRequests {
@@ -8329,10 +8380,49 @@ func buildDatadogToplistRequests(terraformRequests *[]interface{}) *[]datadogV1.
83298380
datadogToplistRequest.Style = buildDatadogWidgetRequestStyle(v)
83308381
}
83318382
}
8383+
8384+
if v, ok := terraformRequest["sort"].([]interface{}); ok && len(v) > 0 {
8385+
sort := v[0].(map[string]interface{})
8386+
datadogToplistRequest.Sort = buildDatadogToplistSortBy(sort)
8387+
}
83328388
datadogRequests[i] = *datadogToplistRequest
83338389
}
83348390
return &datadogRequests
83358391
}
8392+
8393+
func buildDatadogToplistSortBy(terraformSort map[string]interface{}) *datadogV1.WidgetSortBy {
8394+
datadogSortBy := datadogV1.NewWidgetSortBy()
8395+
8396+
if count, ok := terraformSort["count"].(int); ok {
8397+
datadogSortBy.SetCount(int64(count))
8398+
}
8399+
8400+
if orderByList, ok := terraformSort["order_by"].([]interface{}); ok && len(orderByList) > 0 {
8401+
orderBy := make([]datadogV1.WidgetSortOrderBy, len(orderByList))
8402+
8403+
for i, item := range orderByList {
8404+
itemMap := item.(map[string]interface{})
8405+
order := itemMap["order"].(string)
8406+
widgetSortOrder := datadogV1.WidgetSort(order)
8407+
sortType := itemMap["type"].(string)
8408+
8409+
if sortType == string(datadogV1.FORMULATYPE_FORMULA) {
8410+
index := int64(itemMap["index"].(int))
8411+
formula := datadogV1.NewWidgetFormulaSort(index, widgetSortOrder, datadogV1.FORMULATYPE_FORMULA)
8412+
orderBy[i] = datadogV1.WidgetFormulaSortAsWidgetSortOrderBy(formula)
8413+
} else if sortType == string(datadogV1.GROUPTYPE_GROUP) {
8414+
name := itemMap["name"].(string)
8415+
group := datadogV1.NewWidgetGroupSort(name, widgetSortOrder, datadogV1.GROUPTYPE_GROUP)
8416+
orderBy[i] = datadogV1.WidgetGroupSortAsWidgetSortOrderBy(group)
8417+
}
8418+
}
8419+
8420+
datadogSortBy.SetOrderBy(orderBy)
8421+
}
8422+
8423+
return datadogSortBy
8424+
}
8425+
83368426
func buildTerraformToplistRequests(datadogToplistRequests *[]datadogV1.ToplistWidgetRequest) *[]map[string]interface{} {
83378427
terraformRequests := make([]map[string]interface{}, len(*datadogToplistRequests))
83388428
for i, datadogRequest := range *datadogToplistRequests {
@@ -8373,11 +8463,51 @@ func buildTerraformToplistRequests(datadogToplistRequests *[]datadogV1.ToplistWi
83738463
style := buildTerraformWidgetRequestStyle(*v)
83748464
terraformRequest["style"] = []map[string]interface{}{style}
83758465
}
8466+
if v, ok := datadogRequest.GetSortOk(); ok {
8467+
sort := buildTerraformToplistWidgetSort(*v)
8468+
terraformRequest["sort"] = []map[string]interface{}{sort}
8469+
}
83768470
terraformRequests[i] = terraformRequest
83778471
}
83788472
return &terraformRequests
83798473
}
83808474

8475+
func buildTerraformToplistWidgetSort(datadogSort datadogV1.WidgetSortBy) map[string]interface{} {
8476+
terraformSort := make(map[string]interface{})
8477+
8478+
if count, ok := datadogSort.GetCountOk(); ok {
8479+
terraformSort["count"] = int(*count)
8480+
}
8481+
8482+
if orderByList, ok := datadogSort.GetOrderByOk(); ok && len(*orderByList) > 0 {
8483+
terraformOrderBy := make([]map[string]interface{}, len(*orderByList))
8484+
8485+
for i, item := range *orderByList {
8486+
orderByItem := make(map[string]interface{})
8487+
8488+
if formula := item.WidgetFormulaSort; formula != nil {
8489+
orderByItem["type"] = string(formula.GetType())
8490+
orderByItem["order"] = string(formula.GetOrder())
8491+
if index, ok := formula.GetIndexOk(); ok {
8492+
orderByItem["index"] = int(*index)
8493+
}
8494+
} else if group := item.WidgetGroupSort; group != nil {
8495+
orderByItem["type"] = string(group.GetType())
8496+
orderByItem["order"] = string(group.GetOrder())
8497+
if name, ok := group.GetNameOk(); ok {
8498+
orderByItem["name"] = *name
8499+
}
8500+
}
8501+
8502+
terraformOrderBy[i] = orderByItem
8503+
}
8504+
8505+
terraformSort["order_by"] = terraformOrderBy
8506+
}
8507+
8508+
return terraformSort
8509+
}
8510+
83818511
func buildTerraformToplistWidgetStyle(datadogToplistStyle *datadogV1.ToplistWidgetStyle) *[]map[string]interface{} {
83828512
terraformStyles := make([]map[string]interface{}, 1)
83838513
terraformStyle := map[string]interface{}{}

docs/resources/dashboard.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12854,6 +12854,7 @@ Optional:
1285412854
- `query` (Block List) (see [below for nested schema](#nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--query))
1285512855
- `rum_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--rum_query))
1285612856
- `security_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--security_query))
12857+
- `sort` (Block List, Max: 1) The controls for sorting the widget. (see [below for nested schema](#nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--sort))
1285712858
- `style` (Block List, Max: 1) Define request for the widget's style. (see [below for nested schema](#nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--style))
1285812859

1285912860
<a id="nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--apm_query"></a>
@@ -13501,6 +13502,29 @@ Optional:
1350113502

1350213503

1350313504

13505+
<a id="nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--sort"></a>
13506+
### Nested Schema for `widget.group_definition.widget.split_graph_definition.source_widget_definition.toplist_definition.request.sort`
13507+
13508+
Optional:
13509+
13510+
- `count` (Number) The number of items to limit the widget to.
13511+
- `order_by` (Block List) The array of items to sort the widget by in order. (see [below for nested schema](#nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--sort--order_by))
13512+
13513+
<a id="nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--sort--order_by"></a>
13514+
### Nested Schema for `widget.group_definition.widget.split_graph_definition.source_widget_definition.toplist_definition.request.sort.order_by`
13515+
13516+
Required:
13517+
13518+
- `order` (String) Widget sorting methods. Allowed enum values: `asc`, `desc` Valid values are `asc`, `desc`.
13519+
- `type` (String) Set the sort type to formula. Allowed enum values: `formula` Valid values are `formula`, `group`.
13520+
13521+
Optional:
13522+
13523+
- `index` (Number) The index of the formula to sort by.
13524+
- `name` (String) The name of the group.
13525+
13526+
13527+
1350413528
<a id="nestedblock--widget--group_definition--widget--split_graph_definition--source_widget_definition--toplist_definition--request--style"></a>
1350513529
### Nested Schema for `widget.group_definition.widget.split_graph_definition.source_widget_definition.toplist_definition.request.style`
1350613530

@@ -15533,6 +15557,7 @@ Optional:
1553315557
- `query` (Block List) (see [below for nested schema](#nestedblock--widget--group_definition--widget--toplist_definition--request--query))
1553415558
- `rum_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--group_definition--widget--toplist_definition--request--rum_query))
1553515559
- `security_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--group_definition--widget--toplist_definition--request--security_query))
15560+
- `sort` (Block List, Max: 1) The controls for sorting the widget. (see [below for nested schema](#nestedblock--widget--group_definition--widget--toplist_definition--request--sort))
1553615561
- `style` (Block List, Max: 1) Define request for the widget's style. (see [below for nested schema](#nestedblock--widget--group_definition--widget--toplist_definition--request--style))
1553715562

1553815563
<a id="nestedblock--widget--group_definition--widget--toplist_definition--request--apm_query"></a>
@@ -16180,6 +16205,29 @@ Optional:
1618016205

1618116206

1618216207

16208+
<a id="nestedblock--widget--group_definition--widget--toplist_definition--request--sort"></a>
16209+
### Nested Schema for `widget.group_definition.widget.toplist_definition.request.sort`
16210+
16211+
Optional:
16212+
16213+
- `count` (Number) The number of items to limit the widget to.
16214+
- `order_by` (Block List) The array of items to sort the widget by in order. (see [below for nested schema](#nestedblock--widget--group_definition--widget--toplist_definition--request--sort--order_by))
16215+
16216+
<a id="nestedblock--widget--group_definition--widget--toplist_definition--request--sort--order_by"></a>
16217+
### Nested Schema for `widget.group_definition.widget.toplist_definition.request.sort.order_by`
16218+
16219+
Required:
16220+
16221+
- `order` (String) Widget sorting methods. Allowed enum values: `asc`, `desc` Valid values are `asc`, `desc`.
16222+
- `type` (String) Set the sort type to formula. Allowed enum values: `formula` Valid values are `formula`, `group`.
16223+
16224+
Optional:
16225+
16226+
- `index` (Number) The index of the formula to sort by.
16227+
- `name` (String) The name of the group.
16228+
16229+
16230+
1618316231
<a id="nestedblock--widget--group_definition--widget--toplist_definition--request--style"></a>
1618416232
### Nested Schema for `widget.group_definition.widget.toplist_definition.request.style`
1618516233

@@ -25482,6 +25530,7 @@ Optional:
2548225530
- `query` (Block List) (see [below for nested schema](#nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--query))
2548325531
- `rum_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--rum_query))
2548425532
- `security_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--security_query))
25533+
- `sort` (Block List, Max: 1) The controls for sorting the widget. (see [below for nested schema](#nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--sort))
2548525534
- `style` (Block List, Max: 1) Define request for the widget's style. (see [below for nested schema](#nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--style))
2548625535

2548725536
<a id="nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--apm_query"></a>
@@ -26129,6 +26178,29 @@ Optional:
2612926178

2613026179

2613126180

26181+
<a id="nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--sort"></a>
26182+
### Nested Schema for `widget.split_graph_definition.source_widget_definition.toplist_definition.request.sort`
26183+
26184+
Optional:
26185+
26186+
- `count` (Number) The number of items to limit the widget to.
26187+
- `order_by` (Block List) The array of items to sort the widget by in order. (see [below for nested schema](#nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--sort--order_by))
26188+
26189+
<a id="nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--sort--order_by"></a>
26190+
### Nested Schema for `widget.split_graph_definition.source_widget_definition.toplist_definition.request.sort.order_by`
26191+
26192+
Required:
26193+
26194+
- `order` (String) Widget sorting methods. Allowed enum values: `asc`, `desc` Valid values are `asc`, `desc`.
26195+
- `type` (String) Set the sort type to formula. Allowed enum values: `formula` Valid values are `formula`, `group`.
26196+
26197+
Optional:
26198+
26199+
- `index` (Number) The index of the formula to sort by.
26200+
- `name` (String) The name of the group.
26201+
26202+
26203+
2613226204
<a id="nestedblock--widget--split_graph_definition--source_widget_definition--toplist_definition--request--style"></a>
2613326205
### Nested Schema for `widget.split_graph_definition.source_widget_definition.toplist_definition.request.style`
2613426206

@@ -28161,6 +28233,7 @@ Optional:
2816128233
- `query` (Block List) (see [below for nested schema](#nestedblock--widget--toplist_definition--request--query))
2816228234
- `rum_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--rum_query))
2816328235
- `security_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--security_query))
28236+
- `sort` (Block List, Max: 1) The controls for sorting the widget. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--sort))
2816428237
- `style` (Block List, Max: 1) Define request for the widget's style. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--style))
2816528238

2816628239
<a id="nestedblock--widget--toplist_definition--request--apm_query"></a>
@@ -28808,6 +28881,29 @@ Optional:
2880828881

2880928882

2881028883

28884+
<a id="nestedblock--widget--toplist_definition--request--sort"></a>
28885+
### Nested Schema for `widget.toplist_definition.request.sort`
28886+
28887+
Optional:
28888+
28889+
- `count` (Number) The number of items to limit the widget to.
28890+
- `order_by` (Block List) The array of items to sort the widget by in order. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--sort--order_by))
28891+
28892+
<a id="nestedblock--widget--toplist_definition--request--sort--order_by"></a>
28893+
### Nested Schema for `widget.toplist_definition.request.sort.order_by`
28894+
28895+
Required:
28896+
28897+
- `order` (String) Widget sorting methods. Allowed enum values: `asc`, `desc` Valid values are `asc`, `desc`.
28898+
- `type` (String) Set the sort type to formula. Allowed enum values: `formula` Valid values are `formula`, `group`.
28899+
28900+
Optional:
28901+
28902+
- `index` (Number) The index of the formula to sort by.
28903+
- `name` (String) The name of the group.
28904+
28905+
28906+
2881128907
<a id="nestedblock--widget--toplist_definition--request--style"></a>
2881228908
### Nested Schema for `widget.toplist_definition.request.style`
2881328909

docs/resources/powerpack.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7102,6 +7102,7 @@ Optional:
71027102
- `query` (Block List) (see [below for nested schema](#nestedblock--widget--toplist_definition--request--query))
71037103
- `rum_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--rum_query))
71047104
- `security_query` (Block List, Max: 1) The query to use for this widget. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--security_query))
7105+
- `sort` (Block List, Max: 1) The controls for sorting the widget. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--sort))
71057106
- `style` (Block List, Max: 1) Define request for the widget's style. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--style))
71067107

71077108
<a id="nestedblock--widget--toplist_definition--request--apm_query"></a>
@@ -7749,6 +7750,29 @@ Optional:
77497750

77507751

77517752

7753+
<a id="nestedblock--widget--toplist_definition--request--sort"></a>
7754+
### Nested Schema for `widget.toplist_definition.request.sort`
7755+
7756+
Optional:
7757+
7758+
- `count` (Number) The number of items to limit the widget to.
7759+
- `order_by` (Block List) The array of items to sort the widget by in order. (see [below for nested schema](#nestedblock--widget--toplist_definition--request--sort--order_by))
7760+
7761+
<a id="nestedblock--widget--toplist_definition--request--sort--order_by"></a>
7762+
### Nested Schema for `widget.toplist_definition.request.sort.order_by`
7763+
7764+
Required:
7765+
7766+
- `order` (String) Widget sorting methods. Allowed enum values: `asc`, `desc` Valid values are `asc`, `desc`.
7767+
- `type` (String) Set the sort type to formula. Allowed enum values: `formula` Valid values are `formula`, `group`.
7768+
7769+
Optional:
7770+
7771+
- `index` (Number) The index of the formula to sort by.
7772+
- `name` (String) The name of the group.
7773+
7774+
7775+
77527776
<a id="nestedblock--widget--toplist_definition--request--style"></a>
77537777
### Nested Schema for `widget.toplist_definition.request.style`
77547778

0 commit comments

Comments
 (0)