Skip to content

[datadog_monitor] Adds the monitor ID as an available filter #2228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions datadog/data_source_datadog_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func dataSourceDatadogMonitor() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"monitor_id": {
Description: "The ID of the monitor.",
Type: schema.TypeString,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

// Computed values
"name": {
Expand Down Expand Up @@ -327,6 +333,9 @@ func dataSourceDatadogMonitorRead(ctx context.Context, d *schema.ResourceData, m
}

monitors, httpresp, err := apiInstances.GetMonitorsApiV1().ListMonitors(auth, *optionalParams)
if v, ok := d.GetOk("monitor_id"); ok {
monitors = fitlerByMonitorId(fmt.Sprint(v), monitors)
}
if len(monitors) > 1 {
return diag.Errorf("your query returned more than one result, please try a more specific search criteria")
}
Expand Down Expand Up @@ -466,6 +475,19 @@ func dataSourceDatadogMonitorRead(ctx context.Context, d *schema.ResourceData, m
return nil
}

func fitlerByMonitorId(monitorId string, monitors []datadogV1.Monitor) []datadogV1.Monitor {
var filteredMonitiors []datadogV1.Monitor

for index, monitor := range monitors {
currentMonitorId := fmt.Sprint(monitor.GetId())

if currentMonitorId == monitorId {
filteredMonitiors = append(filteredMonitiors, monitors[index])
}
}
return filteredMonitiors
}

func expandStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data "datadog_monitor" "test" {

### Optional

- `monitor_id` (String) The ID of the monitor.
- `monitor_tags_filter` (List of String) A list of monitor tags to limit the search. This filters on the tags set on the monitor itself.
- `name_filter` (String) A monitor name to limit the search.
- `tags_filter` (List of String) A list of tags to limit the search. This filters on the monitor scope.
Expand Down