Skip to content

Commit

Permalink
Merge pull request #31 from gailtang/master
Browse files Browse the repository at this point in the history
  • Loading branch information
wentao-zh authored May 16, 2021
2 parents c3acfab + 0ae45b2 commit 16e18f7
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 7 deletions.
25 changes: 25 additions & 0 deletions cmd/get/get_alertsconditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ var alertsconditionsCmd = &cobra.Command{
return
}
alertsConditionList = list
// location_failure_conditions uses different methods for pagination,
// if a page requested doesn't exist, it returns the first page instead of empty entires.
// use seperate logic to get location_failure_conditions, instead of adding it in ListAll
list, resp, err := client.AlertsConditions.List(context.Background(), conditionsOptions, newrelic.ConditionLocation)
if err == nil && resp.StatusCode >= 200 && resp.StatusCode <= 299 {
alertsConditionList.AlertsLocationConditionList = list.AlertsLocationConditionList
}
}
} else {
list, err := client.AlertsConditions.ListAll(context.Background(), conditionsOptions)
Expand All @@ -113,6 +120,13 @@ var alertsconditionsCmd = &cobra.Command{
return
}
alertsConditionList = list
// location_failure_conditions uses different methods for pagination,
// if a page requested doesn't exist, it returns the first page instead of empty entires.
// use seperate logic to get location_failure_conditions, instead of adding it in ListAll
list, resp, err := client.AlertsConditions.List(context.Background(), conditionsOptions, newrelic.ConditionLocation)
if err == nil && resp.StatusCode >= 200 && resp.StatusCode <= 299 {
alertsConditionList.AlertsLocationConditionList = list.AlertsLocationConditionList
}
}

if err != nil {
Expand Down Expand Up @@ -179,6 +193,17 @@ func GetAllConditionsByAlertPolicyID(id int64) (*newrelic.AlertsConditionList, e
}
}

// location_failure_conditions uses different methods for pagination,
// if a page requested doesn't exist, it returns the first page instead of empty entires.
// use seperate logic to get location_failure_conditions
list, resp, err := client.AlertsConditions.List(context.Background(), conditionsOptions, newrelic.ConditionLocation)
if err != nil || resp.StatusCode >= 400 {
ret := tracker.ToReturnValue(false, tracker.OPERATION_NAME_GET_CONDITIONS_BY_POLICY_ID, fmt.Errorf("%v.Response: %v. Error: %v.", newrelic.ConditionLocation, resp, err), tracker.ERR_REST_CALL, "")
return nil, err, ret
}
allList.AlertsLocationConditionList = &newrelic.AlertsLocationConditionList{}
allList.AlertsLocationConditions = append(allList.AlertsLocationConditions, list.AlertsLocationConditions...)

ret := tracker.ToReturnValue(true, tracker.OPERATION_NAME_GET_CONDITIONS_BY_POLICY_ID, nil, nil, "")

return allList, err, ret
Expand Down
5 changes: 5 additions & 0 deletions newrelic/alerts_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
ConditionPlugins ConditionCategory = "plugins_condition"
ConditionExternalService ConditionCategory = "external_service_condition"
ConditionSynthetics ConditionCategory = "synthetics_condition"
ConditionLocation ConditionCategory = "location_condition"
ConditionNRQL ConditionCategory = "nrql_condition"
ConditionInfrastructure ConditionCategory = "infrastructure_condition"
)
Expand All @@ -37,6 +38,7 @@ type AlertsConditionsService struct {
*pluginsConditions
*externalServiceConditions
*syntheticsConditions
*locationConditions
*nrqlConditions
*infraConditions
}
Expand All @@ -53,6 +55,7 @@ type AlertsConditionList struct {
*AlertsNRQLConditionList
*AlertsPluginsConditionList
*AlertsSyntheticsConditionList
*AlertsLocationConditionList
*AlertsInfrastructureConditionList
}

Expand Down Expand Up @@ -125,6 +128,8 @@ func (s *AlertsConditionsService) listByCategory(cat ConditionCategory) func(ctx
return s.pluginsConditions.listAll
case ConditionSynthetics:
return s.syntheticsConditions.listAll
case ConditionLocation:
return s.locationConditions.listAll
case ConditionInfrastructure:
return s.infraConditions.listAll
default:
Expand Down
59 changes: 59 additions & 0 deletions newrelic/alerts_location_conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2017-2018 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package newrelic

import (
"context"
)

type locationConditions service

type LocationTerm struct {
Priority *string `json:"priority,omitempty"`
Threshold *int64 `json:"threshold,omitempty"`
}

type AlertsLocationConditionList struct {
AlertsLocationConditions []*AlertsLocationCondition `json:"location_failure_conditions,omitempty"`
}

type AlertsLocationCondition struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
RunbookURL *string `json:"runbook_url,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Entities []*string `json:"entities,omitempty"`
Terms []*LocationTerm `json:"terms,omitempty"`
TimeLimit *int64 `json:"violation_time_limit_seconds,omitempty"`
}

func (s *locationConditions) listAll(ctx context.Context, list *AlertsConditionList, opt *AlertsConditionsOptions) (*Response, error) {
u, err := addOptions("alerts_location_failure_conditions/policies/"+opt.PolicyIDOptions+".json", opt)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}

list.AlertsLocationConditionList = new(AlertsLocationConditionList)
resp, err := s.client.Do(ctx, req, list.AlertsLocationConditionList)
if err != nil {
return resp, err
}
return resp, nil
}
24 changes: 17 additions & 7 deletions newrelic/alerts_nrql_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ type AlertsNRQLConditionList struct {
}

type AlertsNRQLCondition struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
RunbookURL *string `json:"runbook_url,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Terms []*AlertsConditionTerm `json:"terms,omitempty"`
ValueFunction *string `json:"value_function,omitempty"`
NRQL *AlertsNRQLConditionNRQL `json:"nrql,omitempty"`
Type *string `json:"type,omitempty"`
ViolationTimeLimitSeconds *int64 `json:"violation_time_limit_seconds"`
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
RunbookURL *string `json:"runbook_url,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Terms []*AlertsConditionTerm `json:"terms,omitempty"`
ValueFunction *string `json:"value_function,omitempty"`
NRQL *AlertsNRQLConditionNRQL `json:"nrql,omitempty"`
Signal *AlertsNRQLConditionSignal `json:"signal,omitempty"`
}

type AlertsNRQLConditionSignal struct {
AggregationWindow *string `json:"aggregation_window,omitempty"`
EvaluationOffset *string `json:"evaluation_offset,omitempty"`
FillOption *string `json:"fill_option,omitempty"`
FillValue *string `json:"fill_value,omitempty"`
}

type AlertsNRQLConditionNRQL struct {
Expand Down
1 change: 1 addition & 0 deletions newrelic/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func NewClient(httpClient *http.Client, endpointType string) *Client {
c.AlertsConditions.pluginsConditions = (*pluginsConditions)(&c.common)
c.AlertsConditions.externalServiceConditions = (*externalServiceConditions)(&c.common)
c.AlertsConditions.syntheticsConditions = (*syntheticsConditions)(&c.common)
c.AlertsConditions.locationConditions = (*locationConditions)(&c.common)
c.AlertsConditions.nrqlConditions = (*nrqlConditions)(&c.common)
c.AlertsConditions.infraConditions = (*infraConditions)(&c.common)

Expand Down

0 comments on commit 16e18f7

Please sign in to comment.