Skip to content

Commit 6549a9f

Browse files
committed
Moving validation around.
1 parent d10d44c commit 6549a9f

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

internal/kibana/alerting.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"regexp"
87
"strings"
98

109
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
1110
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana"
11+
validation_utils "github.com/elastic/terraform-provider-elasticstack/internal/kibana/utils"
1212
"github.com/elastic/terraform-provider-elasticstack/internal/models"
1313
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
1414
"github.com/hashicorp/go-version"
@@ -22,14 +22,6 @@ var frequencyMinSupportedVersion = version.Must(version.NewVersion("8.6.0"))
2222
var alertsFilterMinSupportedVersion = version.Must(version.NewVersion("8.9.0"))
2323
var alertDelayMinSupportedVersion = version.Must(version.NewVersion("8.13.0"))
2424

25-
// Avoid lint error on deprecated SchemaValidateFunc usage.
26-
//
27-
//nolint:staticcheck
28-
func stringIsAlertingDuration() schema.SchemaValidateFunc {
29-
r := regexp.MustCompile(`^[1-9][0-9]*(?:d|h|m|s)$`)
30-
return validation.StringMatch(r, "string is not a valid Alerting duration in seconds (s), minutes (m), hours (h), or days (d)")
31-
}
32-
3325
func ResourceAlertingRule() *schema.Resource {
3426
apikeySchema := map[string]*schema.Schema{
3527
"rule_id": {
@@ -80,7 +72,7 @@ func ResourceAlertingRule() *schema.Resource {
8072
Description: "The check interval, which specifies how frequently the rule conditions are checked. The interval must be specified in seconds, minutes, hours or days.",
8173
Type: schema.TypeString,
8274
Required: true,
83-
ValidateFunc: stringIsAlertingDuration(),
75+
ValidateFunc: validation_utils.StringIsAlertingDurationSDKV2(),
8476
},
8577
"actions": {
8678
Description: "An action that runs under defined conditions.",
@@ -129,7 +121,7 @@ func ResourceAlertingRule() *schema.Resource {
129121
Description: "Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values.",
130122
Type: schema.TypeString,
131123
Optional: true,
132-
ValidateFunc: stringIsAlertingDuration(),
124+
ValidateFunc: validation_utils.StringIsAlertingDurationSDKV2(),
133125
},
134126
},
135127
},
@@ -207,7 +199,7 @@ func ResourceAlertingRule() *schema.Resource {
207199
Description: "Deprecated in 8.13.0. Defines how often an alert generates repeated actions. This custom action interval must be specified in seconds, minutes, hours, or days. For example, 10m or 1h. This property is applicable only if `notify_when` is `onThrottleInterval`. NOTE: This is a rule level property; if you update the rule in Kibana, it is automatically changed to use action-specific `throttle` values.",
208200
Type: schema.TypeString,
209201
Optional: true,
210-
ValidateFunc: stringIsAlertingDuration(),
202+
ValidateFunc: validation_utils.StringIsAlertingDurationSDKV2(),
211203
},
212204
"scheduled_task_id": {
213205
Description: "ID of the scheduled task that will execute the alert.",

internal/kibana/maintenance_window/schema.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package maintenance_window
33
import (
44
"context"
55

6+
validation_utils "github.com/elastic/terraform-provider-elasticstack/internal/kibana/utils"
67
"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
78
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
89
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
@@ -61,7 +62,7 @@ func (r *MaintenanceWindowResource) Schema(_ context.Context, _ resource.SchemaR
6162
Description: "The duration of the schedule. It allows values in `<integer><unit>` format. `<unit>` is one of `d`, `h`, `m`, or `s` for hours, minutes, seconds. For example: `1d`, `5h`, `30m`, `5000s`.",
6263
Required: true,
6364
Validators: []validator.String{
64-
StringIsAlertingDuration{},
65+
validation_utils.StringIsAlertingDuration{},
6566
},
6667
},
6768
"timezone": schema.StringAttribute{
@@ -81,7 +82,7 @@ func (r *MaintenanceWindowResource) Schema(_ context.Context, _ resource.SchemaR
8182
Description: "The duration of the schedule. It allows values in `<integer><unit>` format. `<unit>` is one of `d`, `h`, `m`, or `s` for hours, minutes, seconds. For example: `1d`, `5h`, `30m`, `5000s`.",
8283
Optional: true,
8384
Validators: []validator.String{
84-
StringIsMaintenanceWindowIntervalFrequency{},
85+
validation_utils.StringIsMaintenanceWindowIntervalFrequency{},
8586
},
8687
},
8788
"occurrences": schema.Int32Attribute{
@@ -97,7 +98,7 @@ func (r *MaintenanceWindowResource) Schema(_ context.Context, _ resource.SchemaR
9798
Optional: true,
9899
Validators: []validator.List{
99100
listvalidator.ValueStringsAre(
100-
StringIsMaintenanceWindowOnWeekDay{},
101+
validation_utils.StringIsMaintenanceWindowOnWeekDay{},
101102
),
102103
},
103104
},

internal/kibana/maintenance_window/validators.go renamed to internal/kibana/utils/validators.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package maintenance_window
1+
package validation_utils
22

33
import (
44
"context"
55
"fmt"
66
"regexp"
77

88
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
911
)
1012

1113
func StringMatchesIntervalFrequencyRegex(s string) (matched bool, err error) {
@@ -68,10 +70,10 @@ func (s StringIsMaintenanceWindowOnWeekDay) ValidateString(_ context.Context, re
6870
}
6971
}
7072

71-
func StringMatchesAlertingDurationRegex(s string) (matched bool, err error) {
72-
pattern := "^[1-9][0-9]*(?:d|h|m|s)$"
73+
var alertingDurationPattern = "^[1-9][0-9]*(?:d|h|m|s)$"
7374

74-
return regexp.MatchString(pattern, s)
75+
func StringMatchesAlertingDurationRegex(s string) (matched bool, err error) {
76+
return regexp.MatchString(alertingDurationPattern, s)
7577
}
7678

7779
type StringIsAlertingDuration struct{}
@@ -98,3 +100,11 @@ func (s StringIsAlertingDuration) ValidateString(_ context.Context, req validato
98100
return
99101
}
100102
}
103+
104+
// Avoid lint error on deprecated SchemaValidateFunc usage.
105+
//
106+
//nolint:staticcheck
107+
func StringIsAlertingDurationSDKV2() schema.SchemaValidateFunc {
108+
r := regexp.MustCompile(alertingDurationPattern)
109+
return validation.StringMatch(r, "string is not a valid Alerting duration in seconds (s), minutes (m), hours (h), or days (d)")
110+
}

internal/kibana/maintenance_window/validators_test.go renamed to internal/kibana/utils/validators_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package maintenance_window
1+
package validation_utils
22

33
import (
44
"reflect"

0 commit comments

Comments
 (0)