Skip to content

Commit cc910f2

Browse files
author
Chan Gao
committed
Infra alerts
1 parent 1f7e66c commit cc910f2

11 files changed

Lines changed: 316 additions & 129 deletions

cmd/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func init() {
3333
CreateCmd.PersistentFlags().StringP("file", "f", "", "Filename to create resource with, yaml/json format is supported.")
3434
CreateCmd.MarkPersistentFlagRequired("file")
3535

36-
CreateCmd.PersistentFlags().StringP("type-condition", "t", "default", "Alert condition type. Only used for 'alertsconditions' command. default|synthetics|ext|plugin|nrql are supported")
36+
CreateCmd.PersistentFlags().StringP("type-condition", "t", "default", "Alert condition type. Only used for 'alertsconditions' command. default|synthetics|ext|plugin|nrql|infrastructure are supported")
3737

3838
CreateCmd.PersistentFlags().StringP("output", "o", "json", "Output format. json/yaml are supported")
3939

cmd/create/create_alertsconditions.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var alertsconditionsCmd = &cobra.Command{
3333
Use: "alertsconditions",
3434
Short: "Create alerts_conditions from a file.",
3535
Aliases: []string{"ac", "alertcondition", "alertscondition"},
36-
Example: "nr create alertsconditions -f <example.yaml>",
36+
Example: "nr create alertsconditions <policy id> -f <example.yaml>",
3737
Args: func(cmd *cobra.Command, args []string) error {
3838
if len(args) != 1 {
3939
var err = fmt.Errorf("length of [flags] should be 1 instead of %d", len(args))
@@ -82,7 +82,8 @@ var alertsconditionsCmd = &cobra.Command{
8282
var cat newrelic.ConditionCategory
8383
var ac = new(newrelic.AlertsConditionEntity)
8484

85-
if conditionType == "plugins" {
85+
switch conditionType {
86+
case "plugins":
8687
cat = newrelic.ConditionPlugins
8788
var ace = new(newrelic.AlertsPluginsConditionEntity)
8889
err = decorder.Decode(ace)
@@ -99,7 +100,7 @@ var alertsconditionsCmd = &cobra.Command{
99100
ac.AlertsPluginsConditionEntity = ace
100101

101102
cat = newrelic.ConditionPlugins
102-
} else if conditionType == "synthetics" {
103+
case "synthetics":
103104
var ace = new(newrelic.AlertsSyntheticsConditionEntity)
104105
err = decorder.Decode(ace)
105106
if err != nil {
@@ -115,7 +116,7 @@ var alertsconditionsCmd = &cobra.Command{
115116
ac.AlertsSyntheticsConditionEntity = ace
116117

117118
cat = newrelic.ConditionSynthetics
118-
} else if conditionType == "ext" {
119+
case "ext":
119120
var ace = new(newrelic.AlertsExternalServiceConditionEntity)
120121
err = decorder.Decode(ace)
121122
if err != nil {
@@ -131,7 +132,7 @@ var alertsconditionsCmd = &cobra.Command{
131132
ac.AlertsExternalServiceConditionEntity = ace
132133

133134
cat = newrelic.ConditionExternalService
134-
} else if conditionType == "nrql" {
135+
case "nrql":
135136
var ace = new(newrelic.AlertsNRQLConditionEntity)
136137
err = decorder.Decode(ace)
137138
if err != nil {
@@ -147,7 +148,23 @@ var alertsconditionsCmd = &cobra.Command{
147148
ac.AlertsNRQLConditionEntity = ace
148149

149150
cat = newrelic.ConditionNRQL
150-
} else {
151+
case "infrastructure":
152+
var ace = new(newrelic.AlertsInfrastructureConditionEntity)
153+
err = decorder.Decode(ace)
154+
if err != nil {
155+
fmt.Printf("Unable to decode for infrastructure type condition %q: %v\n", file, err)
156+
os.Exit(1)
157+
return
158+
}
159+
if reflect.DeepEqual(new(newrelic.AlertsInfrastructureConditionEntity), ace) {
160+
fmt.Printf("Error validating for infrastructure type condition %q.\n", file)
161+
os.Exit(1)
162+
return
163+
}
164+
ac.AlertsInfrastructureConditionEntity = ace
165+
166+
cat = newrelic.ConditionInfrastructure
167+
default:
151168
var ace = new(newrelic.AlertsDefaultConditionEntity)
152169
err = decorder.Decode(ace)
153170
if err != nil {

cmd/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func init() {
3030

3131
// Cobra supports Persistent Flags which will work for this command
3232
// and all subcommands, e.g.:
33-
DeleteCmd.PersistentFlags().StringP("type-condition", "t", "default", "Alert condition type. Only used for 'alertsconditions' command. default|synthetics|ext|plugin|nrql are supported")
33+
DeleteCmd.PersistentFlags().StringP("type-condition", "t", "default", "Alert condition type. Only used for 'alertsconditions' command. default|synthetics|ext|plugin|nrql|infrastructure are supported")
3434

3535
DeleteCmd.PersistentFlags().StringP("output", "o", "json", "Output format. json/yaml are supported")
3636
// DeleteCmd.PersistentFlags().String("foo", "", "A help for foo")

cmd/delete/delete_alertsconditions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ var alertsconditionsCmd = &cobra.Command{
7171
cat = newrelic.ConditionExternalService
7272
} else if conditionType == "nrql" {
7373
cat = newrelic.ConditionNRQL
74+
} else if conditionType == "infrastructure" {
75+
cat = newrelic.ConditionInfrastructure
7476
} else {
7577
cat = newrelic.ConditionDefault
7678
}

cmd/get/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func init() {
3333
// GetCmd.PersistentFlags().String("foo", "", "A help for foo")
3434
GetCmd.PersistentFlags().StringP("output", "o", "table", "Output format. table/json/yaml are supported")
3535

36-
GetCmd.PersistentFlags().StringP("type-condition", "t", "all", "Alert condition type. Only used for 'alertsconditions' command. all|conditions|synthetics|ext|plugin|nrql are supported")
36+
GetCmd.PersistentFlags().StringP("type-condition", "t", "all", "Alert condition type. Only used for 'alertsconditions' command. all|conditions|synthetics|ext|plugin|nrql|infrastructure are supported")
3737
// Cobra supports local flags which will only run when this command
3838
// is called directly, e.g.:
3939
// GetCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

cmd/get/get_alertsconditions.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var alertsconditionsCmd = &cobra.Command{
7373
os.Exit(1)
7474
return
7575
}
76-
if conditionType == "plugins" || conditionType == "synthetics" || conditionType == "ext" || conditionType == "nrql" || conditionType == "conditions" {
76+
if conditionType == "plugins" || conditionType == "synthetics" || conditionType == "ext" || conditionType == "nrql" || conditionType == "conditions" || conditionType == "infrastructure" {
7777
var cat newrelic.ConditionCategory
7878
switch conditionType {
7979
case "plugins":
@@ -84,6 +84,8 @@ var alertsconditionsCmd = &cobra.Command{
8484
cat = newrelic.ConditionExternalService
8585
case "nrql":
8686
cat = newrelic.ConditionNRQL
87+
case "infrastructure":
88+
cat = newrelic.ConditionInfrastructure
8789
default:
8890
cat = newrelic.ConditionDefault
8991
}
@@ -195,6 +197,7 @@ func GetConditionsByAlertPolicyIDAndConditionType(id int64, cat newrelic.Conditi
195197
alertsConditionList.AlertsNRQLConditionList = &newrelic.AlertsNRQLConditionList{}
196198
alertsConditionList.AlertsPluginsConditionList = &newrelic.AlertsPluginsConditionList{}
197199
alertsConditionList.AlertsSyntheticsConditionList = &newrelic.AlertsSyntheticsConditionList{}
200+
alertsConditionList.AlertsInfrastructureConditionList = &newrelic.AlertsInfrastructureConditionList{}
198201

199202
var conditionsOptions *newrelic.AlertsConditionsOptions
200203
conditionsOptions = new(newrelic.AlertsConditionsOptions)
@@ -284,6 +287,12 @@ func IsConditionNameExists(alertPolicyID int64, condtionName string, cat newreli
284287
return true, *condition.ID, err, ret
285288
}
286289
}
290+
} else if cat == newrelic.ConditionInfrastructure {
291+
for _, condition := range list.AlertsInfrastructureConditions {
292+
if *condition.Name == condtionName {
293+
return true, *condition.ID, err, ret
294+
}
295+
}
287296
}
288297

289298
return false, -1, err, ret

cmd/update/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func init() {
3636

3737
UpdateCmd.PersistentFlags().StringP("output", "o", "json", "Output format. json/yaml are supported")
3838

39-
UpdateCmd.PersistentFlags().StringP("type-condition", "t", "default", "Alert condition type. Only used for 'alertsconditions' command. default|synthetics|ext|plugin|nrql are supported")
39+
UpdateCmd.PersistentFlags().StringP("type-condition", "t", "default", "Alert condition type. Only used for 'alertsconditions' command. default|synthetics|ext|plugin|nrql|infrastructure are supported")
4040

4141
UpdateCmd.PersistentFlags().StringP("script-file", "s", "", "Synthetics monitor javascript file name. Only used for 'monitor' command.")
4242
// Cobra supports local flags which will only run when this command

0 commit comments

Comments
 (0)