Skip to content

Commit d2641d2

Browse files
SeanstoppableSlavek Kabrda
authored andcommitted
Add full param options for monitors (#199)
Add a more generic function that will let people mix and match Have existing functions wrap this better function Resolves the rest of state support in #164
1 parent 5048796 commit d2641d2

File tree

2 files changed

+112
-25
lines changed

2 files changed

+112
-25
lines changed

datadog-accessors.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12197,6 +12197,68 @@ func (m *Monitor) SetType(v string) {
1219712197
m.Type = &v
1219812198
}
1219912199

12200+
// GetName returns the Name field if non-nil, zero value otherwise.
12201+
func (m *MonitorQueryOpts) GetName() string {
12202+
if m == nil || m.Name == nil {
12203+
return ""
12204+
}
12205+
return *m.Name
12206+
}
12207+
12208+
// GetNameOk returns a tuple with the Name field if it's non-nil, zero value otherwise
12209+
// and a boolean to check if the value has been set.
12210+
func (m *MonitorQueryOpts) GetNameOk() (string, bool) {
12211+
if m == nil || m.Name == nil {
12212+
return "", false
12213+
}
12214+
return *m.Name, true
12215+
}
12216+
12217+
// HasName returns a boolean if a field has been set.
12218+
func (m *MonitorQueryOpts) HasName() bool {
12219+
if m != nil && m.Name != nil {
12220+
return true
12221+
}
12222+
12223+
return false
12224+
}
12225+
12226+
// SetName allocates a new m.Name and returns the pointer to it.
12227+
func (m *MonitorQueryOpts) SetName(v string) {
12228+
m.Name = &v
12229+
}
12230+
12231+
// GetWithDowntimes returns the WithDowntimes field if non-nil, zero value otherwise.
12232+
func (m *MonitorQueryOpts) GetWithDowntimes() bool {
12233+
if m == nil || m.WithDowntimes == nil {
12234+
return false
12235+
}
12236+
return *m.WithDowntimes
12237+
}
12238+
12239+
// GetWithDowntimesOk returns a tuple with the WithDowntimes field if it's non-nil, zero value otherwise
12240+
// and a boolean to check if the value has been set.
12241+
func (m *MonitorQueryOpts) GetWithDowntimesOk() (bool, bool) {
12242+
if m == nil || m.WithDowntimes == nil {
12243+
return false, false
12244+
}
12245+
return *m.WithDowntimes, true
12246+
}
12247+
12248+
// HasWithDowntimes returns a boolean if a field has been set.
12249+
func (m *MonitorQueryOpts) HasWithDowntimes() bool {
12250+
if m != nil && m.WithDowntimes != nil {
12251+
return true
12252+
}
12253+
12254+
return false
12255+
}
12256+
12257+
// SetWithDowntimes allocates a new m.WithDowntimes and returns the pointer to it.
12258+
func (m *MonitorQueryOpts) SetWithDowntimes(v bool) {
12259+
m.WithDowntimes = &v
12260+
}
12261+
1220012262
// GetEnd returns the End field if non-nil, zero value otherwise.
1220112263
func (m *MuteMonitorScope) GetEnd() int {
1220212264
if m == nil || m.End == nil {

monitors.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -182,33 +182,13 @@ func (client *Client) GetMonitor(id int) (*Monitor, error) {
182182
}
183183

184184
// 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})
197187
}
198188

199189
// 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})
212192
}
213193

214194
// DeleteMonitor removes a monitor from the system
@@ -219,8 +199,53 @@ func (client *Client) DeleteMonitor(id int) error {
219199

220200
// GetMonitors returns a slice of all monitors
221201
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) {
222218
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 {
224249
return nil, err
225250
}
226251
return out.Monitors, nil

0 commit comments

Comments
 (0)