@@ -182,33 +182,13 @@ func (client *Client) GetMonitor(id int) (*Monitor, error) {
182
182
}
183
183
184
184
// GetMonitorsByName retrieves monitors by name
185
- func (self * Client ) GetMonitorsByName (name string ) ([]Monitor , error ) {
186
- var out reqMonitors
187
- query , err := url .ParseQuery (fmt .Sprintf ("name=%v" , name ))
188
- if err != nil {
189
- return nil , err
190
- }
191
-
192
- err = self .doJsonRequest ("GET" , fmt .Sprintf ("/v1/monitor?%v" , query .Encode ()), nil , & out .Monitors )
193
- if err != nil {
194
- return nil , err
195
- }
196
- return out .Monitors , nil
185
+ func (client * Client ) GetMonitorsByName (name string ) ([]Monitor , error ) {
186
+ return client .GetMonitorsWithOptions (MonitorQueryOpts {Name : & name })
197
187
}
198
188
199
189
// GetMonitorsByTags retrieves monitors by a slice of tags
200
- func (self * Client ) GetMonitorsByTags (tags []string ) ([]Monitor , error ) {
201
- var out reqMonitors
202
- query , err := url .ParseQuery (fmt .Sprintf ("monitor_tags=%v" , strings .Join (tags , "," )))
203
- if err != nil {
204
- return nil , err
205
- }
206
-
207
- err = self .doJsonRequest ("GET" , fmt .Sprintf ("/v1/monitor?%v" , query .Encode ()), nil , & out .Monitors )
208
- if err != nil {
209
- return nil , err
210
- }
211
- return out .Monitors , nil
190
+ func (client * Client ) GetMonitorsByTags (tags []string ) ([]Monitor , error ) {
191
+ return client .GetMonitorsWithOptions (MonitorQueryOpts {Tags : tags })
212
192
}
213
193
214
194
// DeleteMonitor removes a monitor from the system
@@ -219,8 +199,53 @@ func (client *Client) DeleteMonitor(id int) error {
219
199
220
200
// GetMonitors returns a slice of all monitors
221
201
func (client * Client ) GetMonitors () ([]Monitor , error ) {
202
+ return client .GetMonitorsWithOptions (MonitorQueryOpts {})
203
+ }
204
+
205
+ // MonitorQueryOpts contains the options supported by
206
+ // https://docs.datadoghq.com/api/?lang=bash#get-all-monitor-details
207
+ type MonitorQueryOpts struct {
208
+ GroupStates []string
209
+ Name * string
210
+ Tags []string
211
+ MonitorTags []string
212
+ WithDowntimes * bool
213
+ }
214
+
215
+ // GetMonitorsWithOptions returns a slice of all monitors
216
+ // It supports all the options for querying
217
+ func (client * Client ) GetMonitorsWithOptions (opts MonitorQueryOpts ) ([]Monitor , error ) {
222
218
var out reqMonitors
223
- if err := client .doJsonRequest ("GET" , "/v1/monitor" , nil , & out .Monitors ); err != nil {
219
+ var query []string
220
+ if len (opts .Tags ) > 0 {
221
+ value := fmt .Sprintf ("tags=%v" , strings .Join (opts .Tags , "," ))
222
+ query = append (query , value )
223
+ }
224
+
225
+ if len (opts .GroupStates ) > 0 {
226
+ value := fmt .Sprintf ("group_states=%v" , strings .Join (opts .GroupStates , "," ))
227
+ query = append (query , value )
228
+ }
229
+
230
+ if len (opts .MonitorTags ) > 0 {
231
+ value := fmt .Sprintf ("monitor_tags=%v" , strings .Join (opts .MonitorTags , "," ))
232
+ query = append (query , value )
233
+ }
234
+
235
+ if v , ok := opts .GetWithDowntimesOk (); ok {
236
+ query = append (query , fmt .Sprintf ("with_downtimes=%t" , v ))
237
+ }
238
+
239
+ if v , ok := opts .GetNameOk (); ok {
240
+ query = append (query , fmt .Sprintf ("name=%s" , v ))
241
+ }
242
+
243
+ queryString , err := url .ParseQuery (strings .Join (query , "&" ))
244
+ if err != nil {
245
+ return nil , err
246
+ }
247
+ err = client .doJsonRequest ("GET" , fmt .Sprintf ("/v1/monitor?%v" , queryString .Encode ()), nil , & out .Monitors )
248
+ if err != nil {
224
249
return nil , err
225
250
}
226
251
return out .Monitors , nil
0 commit comments