Add Import support for provider-datadog (#8324)

* Add import support
* Add import tests
* Fix tags/thresholds
* The type of the object is a float but the tests were using integers, so the
  tests were also adjusted to match.
* Fix Float formatting
* Provide thresholds as map[string]string to deal with formatting issues
* Adjust tests to deal with loss of trailing zeros on floats
This commit is contained in:
Kurt Scherer 2016-08-21 22:55:26 -05:00 committed by James Nugent
parent f0aff9ab49
commit faf43292c7
3 changed files with 104 additions and 25 deletions

View File

@ -0,0 +1,56 @@
package datadog
import (
"github.com/hashicorp/terraform/helper/resource"
"testing"
)
func TestDatadogMonitor_import(t *testing.T) {
resourceName := "datadog_monitor.foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDatadogMonitorDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckDatadogMonitorConfigImported,
},
resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
const testAccCheckDatadogMonitorConfigImported = `
resource "datadog_monitor" "foo" {
name = "name for monitor foo"
type = "metric alert"
message = "some message Notify: @hipchat-channel"
escalation_message = "the situation has escalated @pagerduty"
query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2.5"
thresholds {
ok = 1.5
warning = 2.3
critical = 2.5
}
notify_no_data = false
renotify_interval = 60
notify_audit = false
timeout_h = 60
include_tags = true
require_full_window = true
locked = false
tags {
"foo" = "bar"
"bar" = "baz"
}
}
`

View File

@ -18,6 +18,9 @@ func resourceDatadogMonitor() *schema.Resource {
Update: resourceDatadogMonitorUpdate, Update: resourceDatadogMonitorUpdate,
Delete: resourceDatadogMonitorDelete, Delete: resourceDatadogMonitorDelete,
Exists: resourceDatadogMonitorExists, Exists: resourceDatadogMonitorExists,
Importer: &schema.ResourceImporter{
State: resourceDatadogImport,
},
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": &schema.Schema{
@ -247,12 +250,30 @@ func resourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error
return err return err
} }
thresholds := make(map[string]string)
for k, v := range map[string]json.Number{
"ok": m.Options.Thresholds.Ok,
"warning": m.Options.Thresholds.Warning,
"critical": m.Options.Thresholds.Critical,
} {
s := v.String()
if s != "" {
thresholds[k] = s
}
}
tags := make(map[string]string)
for _, s := range m.Tags {
tag := strings.Split(s, ":")
tags[tag[0]] = tag[1]
}
log.Printf("[DEBUG] monitor: %v", m) log.Printf("[DEBUG] monitor: %v", m)
d.Set("name", m.Name) d.Set("name", m.Name)
d.Set("message", m.Message) d.Set("message", m.Message)
d.Set("query", m.Query) d.Set("query", m.Query)
d.Set("type", m.Type) d.Set("type", m.Type)
d.Set("thresholds", m.Options.Thresholds) d.Set("thresholds", thresholds)
d.Set("notify_no_data", m.Options.NotifyNoData) d.Set("notify_no_data", m.Options.NotifyNoData)
d.Set("no_data_timeframe", m.Options.NoDataTimeframe) d.Set("no_data_timeframe", m.Options.NoDataTimeframe)
d.Set("renotify_interval", m.Options.RenotifyInterval) d.Set("renotify_interval", m.Options.RenotifyInterval)
@ -261,7 +282,7 @@ func resourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error
d.Set("escalation_message", m.Options.EscalationMessage) d.Set("escalation_message", m.Options.EscalationMessage)
d.Set("silenced", m.Options.Silenced) d.Set("silenced", m.Options.Silenced)
d.Set("include_tags", m.Options.IncludeTags) d.Set("include_tags", m.Options.IncludeTags)
d.Set("tags", m.Tags) d.Set("tags", tags)
d.Set("require_full_window", m.Options.RequireFullWindow) d.Set("require_full_window", m.Options.RequireFullWindow)
d.Set("locked", m.Options.Locked) d.Set("locked", m.Options.Locked)
@ -370,3 +391,10 @@ func resourceDatadogMonitorDelete(d *schema.ResourceData, meta interface{}) erro
return nil return nil
} }
func resourceDatadogImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
if err := resourceDatadogMonitorRead(d, meta); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}

View File

@ -34,11 +34,9 @@ func TestAccDatadogMonitor_Basic(t *testing.T) {
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "renotify_interval", "60"), "datadog_monitor.foo", "renotify_interval", "60"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.ok", "0"), "datadog_monitor.foo", "thresholds.warning", "1.0"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.warning", "1"), "datadog_monitor.foo", "thresholds.critical", "2.0"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.critical", "2"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "require_full_window", "true"), "datadog_monitor.foo", "require_full_window", "true"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -78,11 +76,9 @@ func TestAccDatadogMonitor_Updated(t *testing.T) {
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "renotify_interval", "60"), "datadog_monitor.foo", "renotify_interval", "60"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.ok", "0"), "datadog_monitor.foo", "thresholds.warning", "1.0"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.warning", "1"), "datadog_monitor.foo", "thresholds.critical", "2.0"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.critical", "2"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "notify_audit", "false"), "datadog_monitor.foo", "notify_audit", "false"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -120,11 +116,11 @@ func TestAccDatadogMonitor_Updated(t *testing.T) {
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "renotify_interval", "40"), "datadog_monitor.foo", "renotify_interval", "40"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.ok", "0"), "datadog_monitor.foo", "thresholds.ok", "0.0"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.warning", "1"), "datadog_monitor.foo", "thresholds.warning", "1.0"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.critical", "3"), "datadog_monitor.foo", "thresholds.critical", "3.0"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "notify_audit", "true"), "datadog_monitor.foo", "notify_audit", "true"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -170,11 +166,11 @@ func TestAccDatadogMonitor_TrimWhitespace(t *testing.T) {
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "renotify_interval", "60"), "datadog_monitor.foo", "renotify_interval", "60"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.ok", "0"), "datadog_monitor.foo", "thresholds.ok", "0.0"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.warning", "1"), "datadog_monitor.foo", "thresholds.warning", "1.0"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"datadog_monitor.foo", "thresholds.critical", "2"), "datadog_monitor.foo", "thresholds.critical", "2.0"),
), ),
}, },
}, },
@ -210,9 +206,8 @@ resource "datadog_monitor" "foo" {
query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2" query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2"
thresholds { thresholds {
ok = 0 warning = "1.0"
warning = 1 critical = "2.0"
critical = 2
} }
notify_no_data = false notify_no_data = false
@ -240,9 +235,9 @@ resource "datadog_monitor" "foo" {
query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3" query = "avg(last_1h):avg:aws.ec2.cpu{environment:bar,host:bar} by {host} > 3"
thresholds { thresholds {
ok = 0 ok = "0.0"
warning = 1 warning = "1.0"
critical = 3 critical = "3.0"
} }
notify_no_data = true notify_no_data = true
@ -278,9 +273,9 @@ EOF
avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2 avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 2
EOF EOF
thresholds { thresholds {
ok = 0 ok = "0.0"
warning = 1 warning = "1.0"
critical = 2 critical = "2.0"
} }
notify_no_data = false notify_no_data = false