|
5 | 5 | "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
6 | 6 | "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
7 | 7 | "log" |
| 8 | + "strings" |
8 | 9 | ) |
9 | 10 |
|
10 | 11 | const fieldNameWindowBasedEvaluation = `window_based_evaluation` |
@@ -200,12 +201,9 @@ func resourceSumologicSLO() *schema.Resource { |
200 | 201 | Required: true, |
201 | 202 | }, |
202 | 203 | "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, |
209 | 207 | }, |
210 | 208 | "start_from": { |
211 | 209 | Type: schema.TypeString, |
@@ -684,3 +682,36 @@ func verifySLOObject(slo SLOLibrarySLO) error { |
684 | 682 | } |
685 | 683 | return nil |
686 | 684 | } |
| 685 | + |
| 686 | +// lintignore:V013 |
| 687 | +func validateSLOComplianceSize(i interface{}, k string) (warnings []string, errors []error) { |
| 688 | + v, ok := i.(string) |
| 689 | + if !ok { |
| 690 | + errors = append(errors, fmt.Errorf("expected type of %s to be string", k)) |
| 691 | + return warnings, errors |
| 692 | + } |
| 693 | + |
| 694 | + var validPeriods []string |
| 695 | + |
| 696 | + calendarPeriods := []string{"Week", "Month", "Quarter"} |
| 697 | + for _, calendarPeriod := range calendarPeriods { |
| 698 | + validPeriods = append(validPeriods, calendarPeriod) |
| 699 | + } |
| 700 | + |
| 701 | + minRollingDays := 1 |
| 702 | + maxRollingDays := 90 |
| 703 | + for days := minRollingDays; days <= maxRollingDays; days++ { |
| 704 | + validPeriods = append(validPeriods, fmt.Sprintf("%dd", days)) |
| 705 | + } |
| 706 | + |
| 707 | + for _, validPeriod := range validPeriods { |
| 708 | + if v == validPeriod { |
| 709 | + return warnings, errors |
| 710 | + } |
| 711 | + } |
| 712 | + |
| 713 | + errorMessage := "compliance period must be " + strings.Join(calendarPeriods, ", ") + " or " + |
| 714 | + fmt.Sprintf("%dd...%dd", minRollingDays, maxRollingDays) |
| 715 | + |
| 716 | + return warnings, append(errors, fmt.Errorf(errorMessage)) |
| 717 | +} |
0 commit comments