Skip to content

Commit 2dfed1b

Browse files
committed
fix slo compliance period validation
1 parent 52d0382 commit 2dfed1b

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

sumologic/resource_sumologic_slo.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
66
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
77
"log"
8+
"strings"
89
)
910

1011
const fieldNameWindowBasedEvaluation = `window_based_evaluation`
@@ -200,12 +201,9 @@ func resourceSumologicSLO() *schema.Resource {
200201
Required: true,
201202
},
202203
"size": {
203-
Type: schema.TypeString,
204-
Required: true,
205-
ValidateFunc: validation.StringInSlice([]string{
206-
"Week", "Month", "Quarter",
207-
"1d", "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d", "10d", "11d", "12d", "13d", "14d",
208-
}, false),
204+
Type: schema.TypeString,
205+
Required: true,
206+
ValidateFunc: validateSLOComplianceSize,
209207
},
210208
"start_from": {
211209
Type: schema.TypeString,
@@ -684,3 +682,35 @@ func verifySLOObject(slo SLOLibrarySLO) error {
684682
}
685683
return nil
686684
}
685+
686+
func validateSLOComplianceSize(i interface{}, k string) (warnings []string, errors []error) {
687+
v, ok := i.(string)
688+
if !ok {
689+
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
690+
return warnings, errors
691+
}
692+
693+
var validPeriods []string
694+
695+
calendarPeriods := []string{"Week", "Month", "Quarter"}
696+
for _, calendarPeriod := range calendarPeriods {
697+
validPeriods = append(validPeriods, calendarPeriod)
698+
}
699+
700+
minRollingDays := 1
701+
maxRollingDays := 90
702+
for days := minRollingDays; days <= maxRollingDays; days++ {
703+
validPeriods = append(validPeriods, fmt.Sprintf("%dd", days))
704+
}
705+
706+
for _, validPeriod := range validPeriods {
707+
if v == validPeriod {
708+
return warnings, errors
709+
}
710+
}
711+
712+
errorMessage := "compliance period must be " + strings.Join(calendarPeriods, ", ") + " or " +
713+
fmt.Sprintf("%dd...%dd", minRollingDays, maxRollingDays)
714+
715+
return warnings, append(errors, fmt.Errorf(errorMessage))
716+
}

0 commit comments

Comments
 (0)