Skip to content

Commit 245e211

Browse files
ojongeriusstack72
authored andcommitted
provider/datadog: Allow tags to be configured for monitor resources. (hashicorp#8284)
1 parent d7e9a2e commit 245e211

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

builtin/providers/datadog/resource_datadog_monitor.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ func resourceDatadogMonitor() *schema.Resource {
114114
Type: schema.TypeBool,
115115
Optional: true,
116116
},
117+
"tags": &schema.Schema{
118+
Type: schema.TypeMap,
119+
Optional: true,
120+
Elem: &schema.Schema{
121+
Type: schema.TypeString,
122+
Elem: &schema.Schema{
123+
Type: schema.TypeString},
124+
},
125+
},
117126
},
118127
}
119128
}
@@ -179,6 +188,14 @@ func buildMonitorStruct(d *schema.ResourceData) *datadog.Monitor {
179188
Options: o,
180189
}
181190

191+
if attr, ok := d.GetOk("tags"); ok {
192+
s := make([]string, 0)
193+
for k, v := range attr.(map[string]interface{}) {
194+
s = append(s, fmt.Sprintf("%s:%s", k, v.(string)))
195+
}
196+
m.Tags = s
197+
}
198+
182199
return &m
183200
}
184201

@@ -244,6 +261,7 @@ func resourceDatadogMonitorRead(d *schema.ResourceData, meta interface{}) error
244261
d.Set("escalation_message", m.Options.EscalationMessage)
245262
d.Set("silenced", m.Options.Silenced)
246263
d.Set("include_tags", m.Options.IncludeTags)
264+
d.Set("tags", m.Tags)
247265
d.Set("require_full_window", m.Options.RequireFullWindow)
248266
d.Set("locked", m.Options.Locked)
249267

@@ -271,6 +289,14 @@ func resourceDatadogMonitorUpdate(d *schema.ResourceData, meta interface{}) erro
271289
m.Query = attr.(string)
272290
}
273291

292+
if attr, ok := d.GetOk("tags"); ok {
293+
s := make([]string, 0)
294+
for k, v := range attr.(map[string]interface{}) {
295+
s = append(s, fmt.Sprintf("%s:%s", k, v.(string)))
296+
}
297+
m.Tags = s
298+
}
299+
274300
o := datadog.Options{}
275301
if attr, ok := d.GetOk("thresholds"); ok {
276302
thresholds := attr.(map[string]interface{})

builtin/providers/datadog/resource_datadog_monitor_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func TestAccDatadogMonitor_Basic(t *testing.T) {
4343
"datadog_monitor.foo", "require_full_window", "true"),
4444
resource.TestCheckResourceAttr(
4545
"datadog_monitor.foo", "locked", "false"),
46+
resource.TestCheckResourceAttr(
47+
"datadog_monitor.foo", "tags.foo", "bar"),
48+
resource.TestCheckResourceAttr(
49+
"datadog_monitor.foo", "tags.bar", "baz"),
4650
),
4751
},
4852
},
@@ -89,6 +93,10 @@ func TestAccDatadogMonitor_Updated(t *testing.T) {
8993
"datadog_monitor.foo", "require_full_window", "true"),
9094
resource.TestCheckResourceAttr(
9195
"datadog_monitor.foo", "locked", "false"),
96+
resource.TestCheckResourceAttr(
97+
"datadog_monitor.foo", "tags.foo", "bar"),
98+
resource.TestCheckResourceAttr(
99+
"datadog_monitor.foo", "tags.bar", "baz"),
92100
),
93101
},
94102
resource.TestStep{
@@ -129,6 +137,10 @@ func TestAccDatadogMonitor_Updated(t *testing.T) {
129137
"datadog_monitor.foo", "require_full_window", "false"),
130138
resource.TestCheckResourceAttr(
131139
"datadog_monitor.foo", "locked", "true"),
140+
resource.TestCheckResourceAttr(
141+
"datadog_monitor.foo", "tags.baz", "qux"),
142+
resource.TestCheckResourceAttr(
143+
"datadog_monitor.foo", "tags.quux", "corge"),
132144
),
133145
},
134146
},
@@ -211,6 +223,10 @@ resource "datadog_monitor" "foo" {
211223
include_tags = true
212224
require_full_window = true
213225
locked = false
226+
tags {
227+
"foo" = "bar"
228+
"bar" = "baz"
229+
}
214230
}
215231
`
216232

@@ -241,6 +257,10 @@ resource "datadog_monitor" "foo" {
241257
silenced {
242258
"*" = 0
243259
}
260+
tags {
261+
"baz" = "qux"
262+
"quux" = "corge"
263+
}
244264
}
245265
`
246266

website/source/docs/providers/datadog/r/monitor.html.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ resource "datadog_monitor" "foo" {
3737
silenced {
3838
"*" = 0
3939
}
40+
tags {
41+
"foo" = "bar"
42+
"bar" = "baz"
43+
}
4044
}
4145
```
4246

@@ -78,6 +82,7 @@ The following arguments are supported:
7882
We highly recommend you set this to False for sparse metrics, otherwise some evaluations will be skipped.
7983
Default: True for "on average", "at all times" and "in total" aggregation. False otherwise.
8084
* `locked` (Optional) A boolean indicating whether changes to to this monitor should be restricted to the creator or admins. Defaults to False.
85+
* `tags` (Optional) A list of tags to associate with your monitor. This can help you categorize and filter monitors in the manage monitors page of the UI. Note: it's not currently possible to filter by these tags when querying via the API
8186

8287
To mute the alert completely:
8388

0 commit comments

Comments
 (0)