Skip to content

Commit

Permalink
Merge pull request DataDog#202 from bkabrda/monitor-muting-unmuting
Browse files Browse the repository at this point in the history
Fix and provide better docs for (un)muting monitors. Fixes DataDog#181
  • Loading branch information
Slavek Kabrda authored May 13, 2019
2 parents 7dc2225 + 1e10131 commit 6c05c40
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 5 deletions.
4 changes: 2 additions & 2 deletions datadog/resource_datadog_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro
if _, ok := d.GetOk("silenced"); ok && !silenced {
// This means the monitor must be manually unmuted since the API
// wouldn't do it automatically when `silenced` is just missing
retval = client.UnmuteMonitor(*m.Id)
d.Set("silenced", nil)
retval = client.UnmuteMonitorScopes(*m.Id, &datadog.UnmuteMonitorScopes{AllScopes: datadog.Bool(true)})
d.Set("silenced", map[string]int{})
}

return retval
Expand Down
60 changes: 60 additions & 0 deletions datadog/resource_datadog_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,34 @@ func TestAccDatadogMonitor_ThresholdWindows(t *testing.T) {
})
}

func TestAccDatadogMonitor_MuteUnmuteSpecificScopes(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDatadogMonitorDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckDatadogMonitorConfigMuteSpecificScopes,
Check: resource.ComposeTestCheckFunc(
testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "silenced.%", "1"),
resource.TestCheckResourceAttr(
"datadog_monitor.foo", "silenced.host:myserver", "0"),
),
},
{
Config: testAccCheckDatadogMonitorConfigUnmuteSpecificScopes,
Check: resource.ComposeTestCheckFunc(
testAccCheckDatadogMonitorExists("datadog_monitor.foo"),
resource.TestCheckNoResourceAttr(
"datadog_monitor.foo", "silenced"),
),
},
},
})
}

func testAccCheckDatadogMonitorDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*datadog.Client)

Expand Down Expand Up @@ -886,6 +914,38 @@ resource "datadog_monitor" "foo" {
}
`

const testAccCheckDatadogMonitorConfigMuteSpecificScopes = `
resource "datadog_monitor" "foo" {
name = "foo"
type = "metric alert"
message = "test"
query = "avg(last_5m):max:system.load.1{*} by {host} > 100"
thresholds = {
critical = 100
}
silenced = {
"host:myserver" = 0
}
}
`

const testAccCheckDatadogMonitorConfigUnmuteSpecificScopes = `
resource "datadog_monitor" "foo" {
name = "foo"
type = "metric alert"
message = "test"
query = "avg(last_5m):max:system.load.1{*} by {host} > 100"
thresholds = {
critical = 100
}
}
`

func destroyHelper(s *terraform.State, client *datadog.Client) error {
for _, r := range s.RootModule().Resources {
i, _ := strconv.Atoi(r.Primary.ID)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/terraform-providers/terraform-provider-aws v1.29.0 // indirect
github.com/terraform-providers/terraform-provider-template v1.0.0 // indirect
github.com/terraform-providers/terraform-provider-tls v1.2.0 // indirect
github.com/zorkian/go-datadog-api v2.20.1-0.20190430091414-fcf4c3b6edfd+incompatible
github.com/zorkian/go-datadog-api v2.20.1-0.20190513084440-9d8b2d52bc3d+incompatible
gopkg.in/vmihailenco/msgpack.v2 v2.9.1 // indirect
labix.org/v2/mgo v0.0.0-20140701140051-000000000287 // indirect
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ github.com/zorkian/go-datadog-api v2.20.0+incompatible h1:zfITezz+b9lZuYghMXTdAX
github.com/zorkian/go-datadog-api v2.20.0+incompatible/go.mod h1:PkXwHX9CUQa/FpB9ZwAD45N1uhCW4MT/Wj7m36PbKss=
github.com/zorkian/go-datadog-api v2.20.1-0.20190430091414-fcf4c3b6edfd+incompatible h1:nBa/g0we0spNByi38CWZNBn7sCs+vfPrWPQKzPSbnVo=
github.com/zorkian/go-datadog-api v2.20.1-0.20190430091414-fcf4c3b6edfd+incompatible/go.mod h1:PkXwHX9CUQa/FpB9ZwAD45N1uhCW4MT/Wj7m36PbKss=
github.com/zorkian/go-datadog-api v2.20.1-0.20190513084440-9d8b2d52bc3d+incompatible h1:aPFEeTnGh8pTfVwnb9EJhKJulkdYN9ObEe7e/eSG3UE=
github.com/zorkian/go-datadog-api v2.20.1-0.20190513084440-9d8b2d52bc3d+incompatible/go.mod h1:PkXwHX9CUQa/FpB9ZwAD45N1uhCW4MT/Wj7m36PbKss=
go.opencensus.io v0.18.0 h1:Mk5rgZcggtbvtAun5aJzAtjKKN/t0R3jJPlWILlv938=
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down
124 changes: 124 additions & 0 deletions vendor/github.com/zorkian/go-datadog-api/datadog-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/zorkian/go-datadog-api/monitors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ github.com/zclconf/go-cty/cty/gocty
github.com/zclconf/go-cty/cty/set
github.com/zclconf/go-cty/cty/function
github.com/zclconf/go-cty/cty/function/stdlib
# github.com/zorkian/go-datadog-api v2.20.1-0.20190430091414-fcf4c3b6edfd+incompatible
# github.com/zorkian/go-datadog-api v2.20.1-0.20190513084440-9d8b2d52bc3d+incompatible
github.com/zorkian/go-datadog-api
# golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734
golang.org/x/crypto/openpgp
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/downtime.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The following arguments are supported:
* `until_occurrences` - (Optional) How many times the downtime will be rescheduled. `until_occurrences` and `until_date` are mutually exclusive.
* `until_date` - (Optional) The date at which the recurrence should end as a POSIX timestamp. `until_occurrences` and `until_date` are mutually exclusive.
* `message` - (Optional) A message to include with notifications for this downtime.
* `monitor_id` - (Optional) Reference to which monitor this downtime is applied. When scheduling downtime for a given monitor, datadog changes `silenced` property of the monitor to match the `end` POSIX timestamp.
* `monitor_id` - (Optional) Reference to which monitor this downtime is applied. When scheduling downtime for a given monitor, datadog changes `silenced` property of the monitor to match the `end` POSIX timestamp. **Note:** this will effectively change the `silenced` attribute of the referenced monitor. If that monitor is also tracked by Terraform and you don't want it to be unmuted on the next `terraform apply`, see [details](/docs/providers/datadog/r/monitor.html#silencing-by-hand-and-by-downtimes) in the monitor resource documentation.

## Attributes Reference

Expand Down
19 changes: 19 additions & 0 deletions website/docs/r/monitor.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ The following arguments are supported:

silenced = ${map("role:${var:role}", 0)}

## Silencing by Hand and by Downtimes

There are two ways how to silence a single monitor:

* Mute it by hand
* Create a Downtime

Both of these actions add a new value to the `silenced` map. This can be problematic if the `silenced` attribute doesn't contain them in your Terraform, as they would be removed on next `terraform apply` invocation. In order to prevent that from happening, you can add following to your monitor:

```
lifecycle {
ignore_changes = ["silenced"]
}
```

The above will make sure that any changes to the `silenced` attribute are ignored.

This issue doesn't apply to multi-monitor downtimes (those that don't contain `monitor_id`), as these don't influence contents of the `silenced` attribute.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit 6c05c40

Please sign in to comment.