diff --git a/cmd/get/get_alertsconditions.go b/cmd/get/get_alertsconditions.go index 7a92bbe..7e26930 100644 --- a/cmd/get/get_alertsconditions.go +++ b/cmd/get/get_alertsconditions.go @@ -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) @@ -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 { @@ -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 diff --git a/newrelic/alerts_conditions.go b/newrelic/alerts_conditions.go index 2b4ed52..637e71c 100644 --- a/newrelic/alerts_conditions.go +++ b/newrelic/alerts_conditions.go @@ -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" ) @@ -37,6 +38,7 @@ type AlertsConditionsService struct { *pluginsConditions *externalServiceConditions *syntheticsConditions + *locationConditions *nrqlConditions *infraConditions } @@ -53,6 +55,7 @@ type AlertsConditionList struct { *AlertsNRQLConditionList *AlertsPluginsConditionList *AlertsSyntheticsConditionList + *AlertsLocationConditionList *AlertsInfrastructureConditionList } @@ -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: diff --git a/newrelic/alerts_location_conditions.go b/newrelic/alerts_location_conditions.go new file mode 100644 index 0000000..567e0dd --- /dev/null +++ b/newrelic/alerts_location_conditions.go @@ -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 +} diff --git a/newrelic/alerts_nrql_conditions.go b/newrelic/alerts_nrql_conditions.go index 20bcc4b..1a19a20 100644 --- a/newrelic/alerts_nrql_conditions.go +++ b/newrelic/alerts_nrql_conditions.go @@ -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 { diff --git a/newrelic/base.go b/newrelic/base.go index 37c754a..653d400 100644 --- a/newrelic/base.go +++ b/newrelic/base.go @@ -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)