Skip to content

Commit f278a16

Browse files
authored
Merge pull request #544 from SumoLogic/validate_monitors_time_range
Improve time_range validation for sumologic_monitor
2 parents e2812b3 + 43d4346 commit f278a16

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

sumologic/resource_sumologic_monitors_library_monitor.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ var logsStaticTriggerConditionSchema = map[string]*schema.Schema{
456456
Optional: true,
457457
},
458458
"critical": nestedWithAtleastOneOfKeys(true, schemaMap{
459-
"time_range": &timeRangeSchema,
459+
"time_range": &timeRangeWithAllowedValuesSchema,
460460
"alert": nested(false, schemaMap{
461461
"threshold": &thresholdSchema,
462462
"threshold_type": &thresholdTypeSchema,
@@ -468,7 +468,7 @@ var logsStaticTriggerConditionSchema = map[string]*schema.Schema{
468468
}),
469469
}, logStaticConditionCriticalOrWarningAtleastOneKeys),
470470
"warning": nestedWithAtleastOneOfKeys(true, schemaMap{
471-
"time_range": &timeRangeSchema,
471+
"time_range": &timeRangeWithAllowedValuesSchema,
472472
"alert": nested(false, schemaMap{
473473
"threshold": &thresholdSchema,
474474
"threshold_type": &thresholdTypeSchema,
@@ -483,7 +483,7 @@ var logsStaticTriggerConditionSchema = map[string]*schema.Schema{
483483

484484
var metricsStaticTriggerConditionSchema = map[string]*schema.Schema{
485485
"critical": nestedWithAtleastOneOfKeys(true, schemaMap{
486-
"time_range": &timeRangeSchema,
486+
"time_range": &timeRangeWithAllowedValuesSchema,
487487
"occurrence_type": &occurrenceTypeSchema,
488488
"alert": nested(false, schemaMap{
489489
"threshold": &thresholdSchema,
@@ -498,7 +498,7 @@ var metricsStaticTriggerConditionSchema = map[string]*schema.Schema{
498498
}),
499499
}, metricsStaticConditionCriticalOrWarningAtleastOneKeys),
500500
"warning": nestedWithAtleastOneOfKeys(true, schemaMap{
501-
"time_range": &timeRangeSchema,
501+
"time_range": &timeRangeWithAllowedValuesSchema,
502502
"occurrence_type": &occurrenceTypeSchema,
503503
"alert": nested(false, schemaMap{
504504
"threshold": &thresholdSchema,
@@ -553,11 +553,11 @@ var metricsOutlierTriggerConditionSchema = map[string]*schema.Schema{
553553
}
554554

555555
var logsMissingDataTriggerConditionSchema = map[string]*schema.Schema{
556-
"time_range": &timeRangeSchema,
556+
"time_range": &timeRangeWithAllowedValuesSchema,
557557
}
558558

559559
var metricsMissingDataTriggerConditionSchema = map[string]*schema.Schema{
560-
"time_range": &timeRangeSchema,
560+
"time_range": &timeRangeWithAllowedValuesSchema,
561561
"trigger_source": {
562562
Type: schema.TypeString,
563563
Required: true,
@@ -610,7 +610,7 @@ func getBurnRateSchema(triggerType string) *schema.Schema {
610610
Required: true,
611611
ValidateFunc: validation.FloatAtLeast(0),
612612
},
613-
"time_range": &timeRangeSchema,
613+
"time_range": &timeRangeWithFormatSchema,
614614
},
615615
},
616616
}
@@ -652,10 +652,16 @@ var consecutiveSchema = schema.Schema{
652652
ValidateFunc: validation.IntAtLeast(1),
653653
}
654654

655-
var timeRangeSchema = schema.Schema{
655+
var timeRangeWithFormatSchema = schema.Schema{
656656
Type: schema.TypeString,
657657
Required: true,
658-
ValidateFunc: timeRangeValidation,
658+
ValidateFunc: timeRangeFormatValidation,
659+
DiffSuppressFunc: SuppressEquivalentTimeDiff(false),
660+
}
661+
var timeRangeWithAllowedValuesSchema = schema.Schema{
662+
Type: schema.TypeString,
663+
Required: true,
664+
ValidateFunc: timeRangeAllowedValuesValidation,
659665
DiffSuppressFunc: SuppressEquivalentTimeDiff(false),
660666
}
661667

@@ -677,14 +683,22 @@ func getSloBurnRateTimeRangeSchema(triggerType string) *schema.Schema {
677683
return &schema.Schema{
678684
Type: schema.TypeString,
679685
Optional: true,
680-
ValidateFunc: timeRangeValidation,
686+
ValidateFunc: timeRangeFormatValidation,
681687
DiffSuppressFunc: SuppressEquivalentTimeDiff(false),
682688
RequiredWith: []string{requiredWith},
683689
ConflictsWith: []string{conflictsWith},
684690
}
685691
}
686692

687-
var timeRangeValidation = validation.StringMatch(regexp.MustCompile(`^-?(\d)+[smhd]$`), "Time range must be in the format '-?\\d+[smhd]'. Examples: -15m, 1d, etc.")
693+
var allowedTimeRanges = []string{
694+
"2m", "5m", "10m", "15m", "30m", "60m", "180m", "360m", "720m", "1440m",
695+
"-2m", "-5m", "-10m", "-15m", "-30m", "-60m", "-180m", "-360m", "-720m", "-1440m",
696+
"1h", "3h", "6h", "12h", "24h",
697+
"-1h", "-3h", "-6h", "-12h", "-24h",
698+
"-1d", "1d",
699+
}
700+
var timeRangeAllowedValuesValidation = validation.StringInSlice(allowedTimeRanges, false)
701+
var timeRangeFormatValidation = validation.StringMatch(regexp.MustCompile(`^-?(\d)+[smhd]$`), "Time range must be in the format '-?\\d+[smhd]'. Examples: -15m, 1d, etc.")
688702

689703
var resolutionWindowSchema = schema.Schema{
690704
Type: schema.TypeString,

0 commit comments

Comments
 (0)