From d2641d20dfaa3b084205718273f626b8b165fdbd Mon Sep 17 00:00:00 2001 From: Sean Smith Date: Mon, 16 Sep 2019 03:44:45 -0400 Subject: [PATCH] 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 --- datadog-accessors.go | 62 ++++++++++++++++++++++++++++++++++++ monitors.go | 75 +++++++++++++++++++++++++++++--------------- 2 files changed, 112 insertions(+), 25 deletions(-) diff --git a/datadog-accessors.go b/datadog-accessors.go index 3adeb4a..08cd44d 100644 --- a/datadog-accessors.go +++ b/datadog-accessors.go @@ -12197,6 +12197,68 @@ func (m *Monitor) SetType(v string) { m.Type = &v } +// GetName returns the Name field if non-nil, zero value otherwise. +func (m *MonitorQueryOpts) GetName() string { + if m == nil || m.Name == nil { + return "" + } + return *m.Name +} + +// GetNameOk returns a tuple with the Name field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (m *MonitorQueryOpts) GetNameOk() (string, bool) { + if m == nil || m.Name == nil { + return "", false + } + return *m.Name, true +} + +// HasName returns a boolean if a field has been set. +func (m *MonitorQueryOpts) HasName() bool { + if m != nil && m.Name != nil { + return true + } + + return false +} + +// SetName allocates a new m.Name and returns the pointer to it. +func (m *MonitorQueryOpts) SetName(v string) { + m.Name = &v +} + +// GetWithDowntimes returns the WithDowntimes field if non-nil, zero value otherwise. +func (m *MonitorQueryOpts) GetWithDowntimes() bool { + if m == nil || m.WithDowntimes == nil { + return false + } + return *m.WithDowntimes +} + +// GetWithDowntimesOk returns a tuple with the WithDowntimes field if it's non-nil, zero value otherwise +// and a boolean to check if the value has been set. +func (m *MonitorQueryOpts) GetWithDowntimesOk() (bool, bool) { + if m == nil || m.WithDowntimes == nil { + return false, false + } + return *m.WithDowntimes, true +} + +// HasWithDowntimes returns a boolean if a field has been set. +func (m *MonitorQueryOpts) HasWithDowntimes() bool { + if m != nil && m.WithDowntimes != nil { + return true + } + + return false +} + +// SetWithDowntimes allocates a new m.WithDowntimes and returns the pointer to it. +func (m *MonitorQueryOpts) SetWithDowntimes(v bool) { + m.WithDowntimes = &v +} + // GetEnd returns the End field if non-nil, zero value otherwise. func (m *MuteMonitorScope) GetEnd() int { if m == nil || m.End == nil { diff --git a/monitors.go b/monitors.go index 859e04e..d806579 100644 --- a/monitors.go +++ b/monitors.go @@ -182,33 +182,13 @@ func (client *Client) GetMonitor(id int) (*Monitor, error) { } // GetMonitorsByName retrieves monitors by name -func (self *Client) GetMonitorsByName(name string) ([]Monitor, error) { - var out reqMonitors - query, err := url.ParseQuery(fmt.Sprintf("name=%v", name)) - if err != nil { - return nil, err - } - - err = self.doJsonRequest("GET", fmt.Sprintf("/v1/monitor?%v", query.Encode()), nil, &out.Monitors) - if err != nil { - return nil, err - } - return out.Monitors, nil +func (client *Client) GetMonitorsByName(name string) ([]Monitor, error) { + return client.GetMonitorsWithOptions(MonitorQueryOpts{Name: &name}) } // GetMonitorsByTags retrieves monitors by a slice of tags -func (self *Client) GetMonitorsByTags(tags []string) ([]Monitor, error) { - var out reqMonitors - query, err := url.ParseQuery(fmt.Sprintf("monitor_tags=%v", strings.Join(tags, ","))) - if err != nil { - return nil, err - } - - err = self.doJsonRequest("GET", fmt.Sprintf("/v1/monitor?%v", query.Encode()), nil, &out.Monitors) - if err != nil { - return nil, err - } - return out.Monitors, nil +func (client *Client) GetMonitorsByTags(tags []string) ([]Monitor, error) { + return client.GetMonitorsWithOptions(MonitorQueryOpts{Tags: tags}) } // DeleteMonitor removes a monitor from the system @@ -219,8 +199,53 @@ func (client *Client) DeleteMonitor(id int) error { // GetMonitors returns a slice of all monitors func (client *Client) GetMonitors() ([]Monitor, error) { + return client.GetMonitorsWithOptions(MonitorQueryOpts{}) +} + +// MonitorQueryOpts contains the options supported by +// https://docs.datadoghq.com/api/?lang=bash#get-all-monitor-details +type MonitorQueryOpts struct { + GroupStates []string + Name *string + Tags []string + MonitorTags []string + WithDowntimes *bool +} + +// GetMonitorsWithOptions returns a slice of all monitors +// It supports all the options for querying +func (client *Client) GetMonitorsWithOptions(opts MonitorQueryOpts) ([]Monitor, error) { var out reqMonitors - if err := client.doJsonRequest("GET", "/v1/monitor", nil, &out.Monitors); err != nil { + var query []string + if len(opts.Tags) > 0 { + value := fmt.Sprintf("tags=%v", strings.Join(opts.Tags, ",")) + query = append(query, value) + } + + if len(opts.GroupStates) > 0 { + value := fmt.Sprintf("group_states=%v", strings.Join(opts.GroupStates, ",")) + query = append(query, value) + } + + if len(opts.MonitorTags) > 0 { + value := fmt.Sprintf("monitor_tags=%v", strings.Join(opts.MonitorTags, ",")) + query = append(query, value) + } + + if v, ok := opts.GetWithDowntimesOk(); ok { + query = append(query, fmt.Sprintf("with_downtimes=%t", v)) + } + + if v, ok := opts.GetNameOk(); ok { + query = append(query, fmt.Sprintf("name=%s", v)) + } + + queryString, err := url.ParseQuery(strings.Join(query, "&")) + if err != nil { + return nil, err + } + err = client.doJsonRequest("GET", fmt.Sprintf("/v1/monitor?%v", queryString.Encode()), nil, &out.Monitors) + if err != nil { return nil, err } return out.Monitors, nil