Skip to content

Commit

Permalink
Add full param options for monitors (#199)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Seanstoppable authored and Slavek Kabrda committed Sep 16, 2019
1 parent 5048796 commit d2641d2
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 25 deletions.
62 changes: 62 additions & 0 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
75 changes: 50 additions & 25 deletions monitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d2641d2

Please sign in to comment.