Skip to content

Commit c0e10fb

Browse files
ojongeriusstack72
authored andcommitted
provider/datadog: Upgrade to Datadog API v2 (#12098)
* provider/datadog: Pulls v2 and removes v1 of library go-datadog-api. See zorkian/go-datadog-api#56 for context. * Fixes bug in backoff implementation that decreased performance significantly. * Uses pointers for field types, providing support of distinguishing between if a value is set, or the default value for that type is effective. * provider/datadog: Convert provider to use v2 of go-datadog-api. * provider/datadog: Update vendored library. * provider/datadog: Update dashboard resource to reflect API updates.
1 parent 9c13461 commit c0e10fb

33 files changed

+13094
-958
lines changed

builtin/providers/datadog/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package datadog
33
import (
44
"log"
55

6-
"github.com/zorkian/go-datadog-api"
6+
"gopkg.in/zorkian/go-datadog-api.v2"
77
)
88

99
// Config holds API and APP keys to authenticate to Datadog.

builtin/providers/datadog/resource_datadog_monitor.go

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99

1010
"github.com/hashicorp/terraform/helper/schema"
11-
"github.com/zorkian/go-datadog-api"
11+
"gopkg.in/zorkian/go-datadog-api.v2"
1212
)
1313

1414
func resourceDatadogMonitor() *schema.Resource {
@@ -137,17 +137,18 @@ func buildMonitorStruct(d *schema.ResourceData) *datadog.Monitor {
137137
var thresholds datadog.ThresholdCount
138138

139139
if r, ok := d.GetOk("thresholds.ok"); ok {
140-
thresholds.Ok = json.Number(r.(string))
140+
thresholds.SetOk(json.Number(r.(string)))
141141
}
142142
if r, ok := d.GetOk("thresholds.warning"); ok {
143-
thresholds.Warning = json.Number(r.(string))
143+
thresholds.SetWarning(json.Number(r.(string)))
144144
}
145145
if r, ok := d.GetOk("thresholds.critical"); ok {
146-
thresholds.Critical = json.Number(r.(string))
146+
thresholds.SetCritical(json.Number(r.(string)))
147147
}
148148

149149
o := datadog.Options{
150-
Thresholds: thresholds,
150+
Thresholds: &thresholds,
151+
NotifyNoData: datadog.Bool(d.Get("notify_no_data").(bool)),
151152
}
152153
if attr, ok := d.GetOk("silenced"); ok {
153154
s := make(map[string]int)
@@ -158,42 +159,42 @@ func buildMonitorStruct(d *schema.ResourceData) *datadog.Monitor {
158159
o.Silenced = s
159160
}
160161
if attr, ok := d.GetOk("notify_no_data"); ok {
161-
o.NotifyNoData = attr.(bool)
162+
o.SetNotifyNoData(attr.(bool))
162163
}
163164
if attr, ok := d.GetOk("new_host_delay"); ok {
164-
o.NewHostDelay = datadog.Int(attr.(int))
165+
o.SetNewHostDelay(attr.(int))
165166
}
166167
if attr, ok := d.GetOk("no_data_timeframe"); ok {
167168
o.NoDataTimeframe = datadog.NoDataTimeframe(attr.(int))
168169
}
169170
if attr, ok := d.GetOk("renotify_interval"); ok {
170-
o.RenotifyInterval = attr.(int)
171+
o.SetRenotifyInterval(attr.(int))
171172
}
172173
if attr, ok := d.GetOk("notify_audit"); ok {
173-
o.NotifyAudit = attr.(bool)
174+
o.SetNotifyAudit(attr.(bool))
174175
}
175176
if attr, ok := d.GetOk("timeout_h"); ok {
176-
o.TimeoutH = attr.(int)
177+
o.SetTimeoutH(attr.(int))
177178
}
178179
if attr, ok := d.GetOk("escalation_message"); ok {
179-
o.EscalationMessage = attr.(string)
180+
o.SetEscalationMessage(attr.(string))
180181
}
181182
if attr, ok := d.GetOk("include_tags"); ok {
182-
o.IncludeTags = attr.(bool)
183+
o.SetIncludeTags(attr.(bool))
183184
}
184185
if attr, ok := d.GetOk("require_full_window"); ok {
185-
o.RequireFullWindow = attr.(bool)
186+
o.SetRequireFullWindow(attr.(bool))
186187
}
187188
if attr, ok := d.GetOk("locked"); ok {
188-
o.Locked = attr.(bool)
189+
o.SetLocked(attr.(bool))
189190
}
190191

191192
m := datadog.Monitor{
192-
Type: d.Get("type").(string),
193-
Query: d.Get("query").(string),
194-
Name: d.Get("name").(string),
195-
Message: d.Get("message").(string),
196-
Options: o,
193+
Type: datadog.String(d.Get("type").(string)),
194+
Query: datadog.String(d.Get("query").(string)),
195+
Name: datadog.String(d.Get("name").(string)),
196+
Message: datadog.String(d.Get("message").(string)),
197+
Options: &o,
197198
}
198199

199200
if attr, ok := d.GetOk("tags"); ok {
@@ -237,7 +238,7 @@ func resourceDatadogMonitorCreate(d *schema.ResourceData, meta interface{}) erro
237238
return fmt.Errorf("error updating monitor: %s", err.Error())
238239
}
239240

240-
d.SetId(strconv.Itoa(m.Id))
241+
d.SetId(strconv.Itoa(m.GetId()))
241242

242243
return nil
243244
}
@@ -257,9 +258,9 @@ func resourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error
257258

258259
thresholds := make(map[string]string)
259260
for k, v := range map[string]json.Number{
260-
"ok": m.Options.Thresholds.Ok,
261-
"warning": m.Options.Thresholds.Warning,
262-
"critical": m.Options.Thresholds.Critical,
261+
"ok": m.Options.Thresholds.GetOk(),
262+
"warning": m.Options.Thresholds.GetWarning(),
263+
"critical": m.Options.Thresholds.GetCritical(),
263264
} {
264265
s := v.String()
265266
if s != "" {
@@ -273,24 +274,24 @@ func resourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error
273274
}
274275

275276
log.Printf("[DEBUG] monitor: %v", m)
276-
d.Set("name", m.Name)
277-
d.Set("message", m.Message)
278-
d.Set("query", m.Query)
279-
d.Set("type", m.Type)
277+
d.Set("name", m.GetName())
278+
d.Set("message", m.GetMessage())
279+
d.Set("query", m.GetQuery())
280+
d.Set("type", m.GetType())
280281
d.Set("thresholds", thresholds)
281282

282-
d.Set("new_host_delay", m.Options.NewHostDelay)
283-
d.Set("notify_no_data", m.Options.NotifyNoData)
283+
d.Set("new_host_delay", m.Options.GetNewHostDelay())
284+
d.Set("notify_no_data", m.Options.GetNotifyNoData())
284285
d.Set("no_data_timeframe", m.Options.NoDataTimeframe)
285-
d.Set("renotify_interval", m.Options.RenotifyInterval)
286-
d.Set("notify_audit", m.Options.NotifyAudit)
287-
d.Set("timeout_h", m.Options.TimeoutH)
288-
d.Set("escalation_message", m.Options.EscalationMessage)
286+
d.Set("renotify_interval", m.Options.GetRenotifyInterval())
287+
d.Set("notify_audit", m.Options.GetNotifyAudit())
288+
d.Set("timeout_h", m.Options.GetTimeoutH())
289+
d.Set("escalation_message", m.Options.GetEscalationMessage())
289290
d.Set("silenced", m.Options.Silenced)
290-
d.Set("include_tags", m.Options.IncludeTags)
291+
d.Set("include_tags", m.Options.GetIncludeTags())
291292
d.Set("tags", tags)
292-
d.Set("require_full_window", m.Options.RequireFullWindow)
293-
d.Set("locked", m.Options.Locked)
293+
d.Set("require_full_window", m.Options.GetRequireFullWindow()) // TODO Is this one of those options that we neeed to check?
294+
d.Set("locked", m.Options.GetLocked())
294295

295296
return nil
296297
}
@@ -305,15 +306,15 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro
305306
return err
306307
}
307308

308-
m.Id = i
309+
m.Id = datadog.Int(i)
309310
if attr, ok := d.GetOk("name"); ok {
310-
m.Name = attr.(string)
311+
m.SetName(attr.(string))
311312
}
312313
if attr, ok := d.GetOk("message"); ok {
313-
m.Message = attr.(string)
314+
m.SetMessage(attr.(string))
314315
}
315316
if attr, ok := d.GetOk("query"); ok {
316-
m.Query = attr.(string)
317+
m.SetQuery(attr.(string))
317318
}
318319

319320
if attr, ok := d.GetOk("tags"); ok {
@@ -324,40 +325,43 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro
324325
m.Tags = s
325326
}
326327

327-
o := datadog.Options{}
328+
o := datadog.Options{
329+
NotifyNoData: datadog.Bool(d.Get("notify_no_data").(bool)),
330+
}
328331
if attr, ok := d.GetOk("thresholds"); ok {
329332
thresholds := attr.(map[string]interface{})
333+
o.Thresholds = &datadog.ThresholdCount{} // TODO: This is a little annoying..
330334
if thresholds["ok"] != nil {
331-
o.Thresholds.Ok = json.Number(thresholds["ok"].(string))
335+
o.Thresholds.SetOk(json.Number(thresholds["ok"].(string)))
332336
}
333337
if thresholds["warning"] != nil {
334-
o.Thresholds.Warning = json.Number(thresholds["warning"].(string))
338+
o.Thresholds.SetWarning(json.Number(thresholds["warning"].(string)))
335339
}
336340
if thresholds["critical"] != nil {
337-
o.Thresholds.Critical = json.Number(thresholds["critical"].(string))
341+
o.Thresholds.SetCritical(json.Number(thresholds["critical"].(string)))
338342
}
339343
}
340344

341345
if attr, ok := d.GetOk("notify_no_data"); ok {
342-
o.NotifyNoData = attr.(bool)
346+
o.SetNotifyNoData(attr.(bool))
343347
}
344348
if attr, ok := d.GetOk("new_host_delay"); ok {
345-
o.NewHostDelay = datadog.Int(attr.(int))
349+
o.SetNewHostDelay(attr.(int))
346350
}
347351
if attr, ok := d.GetOk("no_data_timeframe"); ok {
348352
o.NoDataTimeframe = datadog.NoDataTimeframe(attr.(int))
349353
}
350354
if attr, ok := d.GetOk("renotify_interval"); ok {
351-
o.RenotifyInterval = attr.(int)
355+
o.SetRenotifyInterval(attr.(int))
352356
}
353357
if attr, ok := d.GetOk("notify_audit"); ok {
354-
o.NotifyAudit = attr.(bool)
358+
o.SetNotifyAudit(attr.(bool))
355359
}
356360
if attr, ok := d.GetOk("timeout_h"); ok {
357-
o.TimeoutH = attr.(int)
361+
o.SetTimeoutH(attr.(int))
358362
}
359363
if attr, ok := d.GetOk("escalation_message"); ok {
360-
o.EscalationMessage = attr.(string)
364+
o.SetEscalationMessage(attr.(string))
361365
}
362366
if attr, ok := d.GetOk("silenced"); ok {
363367
// TODO: this is not very defensive, test if we can fail non int input
@@ -368,16 +372,16 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro
368372
o.Silenced = s
369373
}
370374
if attr, ok := d.GetOk("include_tags"); ok {
371-
o.IncludeTags = attr.(bool)
375+
o.SetIncludeTags(attr.(bool))
372376
}
373377
if attr, ok := d.GetOk("require_full_window"); ok {
374-
o.RequireFullWindow = attr.(bool)
378+
o.SetRequireFullWindow(attr.(bool))
375379
}
376380
if attr, ok := d.GetOk("locked"); ok {
377-
o.Locked = attr.(bool)
381+
o.SetLocked(attr.(bool))
378382
}
379383

380-
m.Options = o
384+
m.Options = &o
381385

382386
if err = client.UpdateMonitor(m); err != nil {
383387
return fmt.Errorf("error updating monitor: %s", err.Error())

builtin/providers/datadog/resource_datadog_monitor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform/helper/resource"
1010
"github.com/hashicorp/terraform/terraform"
11-
"github.com/zorkian/go-datadog-api"
11+
"gopkg.in/zorkian/go-datadog-api.v2"
1212
)
1313

1414
func TestAccDatadogMonitor_Basic(t *testing.T) {

0 commit comments

Comments
 (0)