11package datadog
22
33import (
4+ "context"
45 "fmt"
56 "sort"
67 "strconv"
@@ -9,13 +10,14 @@ import (
910 "github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
1011
1112 datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
12- "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
13+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1315)
1416
1517func dataSourceDatadogMonitor () * schema.Resource {
1618 return & schema.Resource {
1719 Description : "Use this data source to retrieve information about an existing monitor for use in other resources." ,
18- Read : dataSourceDatadogMonitorRead ,
20+ ReadContext : dataSourceDatadogMonitorRead ,
1921 Schema : map [string ]* schema.Schema {
2022 "name_filter" : {
2123 Description : "A monitor name to limit the search." ,
@@ -63,45 +65,10 @@ func dataSourceDatadogMonitor() *schema.Resource {
6365 },
6466
6567 // Options
66- "thresholds" : {
67- Description : "Alert thresholds of the monitor." ,
68- Deprecated : "Define `monitor_thresholds` list with one element instead." ,
69- Type : schema .TypeMap ,
70- Computed : true ,
71- Elem : & schema.Resource {
72- Schema : map [string ]* schema.Schema {
73- "ok" : {
74- Type : schema .TypeFloat ,
75- Computed : true ,
76- },
77- "warning" : {
78- Type : schema .TypeFloat ,
79- Computed : true ,
80- },
81- "critical" : {
82- Type : schema .TypeFloat ,
83- Computed : true ,
84- },
85- "unknown" : {
86- Type : schema .TypeFloat ,
87- Computed : true ,
88- },
89- "warning_recovery" : {
90- Type : schema .TypeFloat ,
91- Computed : true ,
92- },
93- "critical_recovery" : {
94- Type : schema .TypeFloat ,
95- Computed : true ,
96- },
97- },
98- },
99- },
10068 "monitor_thresholds" : {
10169 Description : "Alert thresholds of the monitor." ,
10270 Type : schema .TypeList ,
10371 Computed : true ,
104- MaxItems : 1 ,
10572 Elem : & schema.Resource {
10673 Schema : map [string ]* schema.Schema {
10774 "ok" : {
@@ -131,28 +98,9 @@ func dataSourceDatadogMonitor() *schema.Resource {
13198 },
13299 },
133100 },
134- "threshold_windows" : {
135- Description : "Mapping containing `recovery_window` and `trigger_window` values, e.g. `last_15m`. This is only used by anomaly monitors." ,
136- Deprecated : "Define `monitor_threshold_windows` list with one element instead." ,
137- Type : schema .TypeMap ,
138- Computed : true ,
139- Elem : & schema.Resource {
140- Schema : map [string ]* schema.Schema {
141- "recovery_window" : {
142- Type : schema .TypeString ,
143- Computed : true ,
144- },
145- "trigger_window" : {
146- Type : schema .TypeString ,
147- Computed : true ,
148- },
149- },
150- },
151- },
152101 "monitor_threshold_windows" : {
153102 Description : "Mapping containing `recovery_window` and `trigger_window` values, e.g. `last_15m`. This is only used by anomaly monitors." ,
154103 Type : schema .TypeList ,
155- MaxItems : 1 ,
156104 Computed : true ,
157105 Elem : & schema.Resource {
158106 Schema : map [string ]* schema.Schema {
@@ -240,7 +188,7 @@ func dataSourceDatadogMonitor() *schema.Resource {
240188 }
241189}
242190
243- func dataSourceDatadogMonitorRead (d * schema.ResourceData , meta interface {}) error {
191+ func dataSourceDatadogMonitorRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
244192 providerConf := meta .(* ProviderConfiguration )
245193 datadogClientV1 := providerConf .DatadogClientV1
246194 authV1 := providerConf .AuthV1
@@ -258,13 +206,13 @@ func dataSourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) erro
258206
259207 monitors , _ , err := datadogClientV1 .MonitorsApi .ListMonitors (authV1 , * optionalParams )
260208 if err != nil {
261- return utils .TranslateClientError (err , "error querying monitors" )
209+ return utils .TranslateClientErrorDiag (err , "error querying monitors" )
262210 }
263211 if len (monitors ) > 1 {
264- return fmt .Errorf ("your query returned more than one result, please try a more specific search criteria" )
212+ return diag .Errorf ("your query returned more than one result, please try a more specific search criteria" )
265213 }
266214 if len (monitors ) == 0 {
267- return fmt .Errorf ("your query returned no result, please try a less specific search criteria" )
215+ return diag .Errorf ("your query returned no result, please try a less specific search criteria" )
268216 }
269217
270218 m := monitors [0 ]
@@ -301,9 +249,7 @@ func dataSourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) erro
301249 }
302250
303251 var tags []string
304- for _ , s := range m .GetTags () {
305- tags = append (tags , s )
306- }
252+ tags = append (tags , m .GetTags ()... )
307253 sort .Strings (tags )
308254
309255 d .SetId (strconv .FormatInt (m .GetId (), 10 ))
@@ -312,19 +258,17 @@ func dataSourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) erro
312258 d .Set ("query" , m .GetQuery ())
313259 d .Set ("type" , m .GetType ())
314260
315- d .Set ("thresholds" , thresholds )
316261 if err := d .Set ("monitor_thresholds" , []interface {}{thresholds }); err != nil {
317- return err
262+ return diag . FromErr ( err )
318263 }
319- d .Set ("threshold_windows" , thresholdWindows )
320264 if err := d .Set ("monitor_threshold_windows" , []interface {}{thresholdWindows }); err != nil {
321- return err
265+ return diag . FromErr ( err )
322266 }
323267
324268 d .Set ("new_host_delay" , m .Options .GetNewHostDelay ())
325269 d .Set ("evaluation_delay" , m .Options .GetEvaluationDelay ())
326270 d .Set ("notify_no_data" , m .Options .GetNotifyNoData ())
327- d .Set ("no_data_timeframe" , m .Options .NoDataTimeframe )
271+ d .Set ("no_data_timeframe" , m .Options .GetNoDataTimeframe () )
328272 d .Set ("renotify_interval" , m .Options .GetRenotifyInterval ())
329273 d .Set ("notify_audit" , m .Options .GetNotifyAudit ())
330274 d .Set ("timeout_h" , m .Options .GetTimeoutH ())
0 commit comments