Skip to content

Commit c242820

Browse files
committed
Addressing PR comments 2
1 parent 2cc2382 commit c242820

File tree

9 files changed

+179
-151
lines changed

9 files changed

+179
-151
lines changed

internal/kibana/alerting.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
1010
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana"
11-
validation_utils "github.com/elastic/terraform-provider-elasticstack/internal/kibana/utils"
11+
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/validators"
1212
"github.com/elastic/terraform-provider-elasticstack/internal/models"
1313
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
1414
"github.com/hashicorp/go-version"
@@ -72,7 +72,7 @@ func ResourceAlertingRule() *schema.Resource {
7272
Description: "The check interval, which specifies how frequently the rule conditions are checked. The interval must be specified in seconds, minutes, hours or days.",
7373
Type: schema.TypeString,
7474
Required: true,
75-
ValidateFunc: validation_utils.StringIsAlertingDurationSDKV2(),
75+
ValidateFunc: validators.StringIsAlertingDurationSDKV2(),
7676
},
7777
"actions": {
7878
Description: "An action that runs under defined conditions.",
@@ -121,7 +121,7 @@ func ResourceAlertingRule() *schema.Resource {
121121
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.",
122122
Type: schema.TypeString,
123123
Optional: true,
124-
ValidateFunc: validation_utils.StringIsAlertingDurationSDKV2(),
124+
ValidateFunc: validators.StringIsAlertingDurationSDKV2(),
125125
},
126126
},
127127
},
@@ -199,7 +199,7 @@ func ResourceAlertingRule() *schema.Resource {
199199
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.",
200200
Type: schema.TypeString,
201201
Optional: true,
202-
ValidateFunc: validation_utils.StringIsAlertingDurationSDKV2(),
202+
ValidateFunc: validators.StringIsAlertingDurationSDKV2(),
203203
},
204204
"scheduled_task_id": {
205205
Description: "ID of the scheduled task that will execute the alert.",

internal/kibana/maintenance_window/read.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func (r *MaintenanceWindowResource) Read(ctx context.Context, req resource.ReadR
1212
var stateModel MaintenanceWindowModel
1313

1414
req.State.GetAttribute(ctx, path.Root("id"), &stateModel.ID)
15+
req.State.GetAttribute(ctx, path.Root("space_id"), &stateModel.SpaceID)
1516

1617
serverVersion, sdkDiags := r.client.ServerVersion(ctx)
1718
if sdkDiags.HasError() {

internal/kibana/maintenance_window/schema.go

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

6-
validation_utils "github.com/elastic/terraform-provider-elasticstack/internal/kibana/utils"
6+
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/validators"
77
"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
88
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
99
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
@@ -58,14 +58,14 @@ func (r *MaintenanceWindowResource) Schema(_ context.Context, _ resource.SchemaR
5858
Description: "The start date and time of the schedule, provided in ISO 8601 format and set to the UTC timezone. For example: `2025-03-12T12:00:00.000Z`.",
5959
Required: true,
6060
Validators: []validator.String{
61-
validation_utils.StringIsISO8601{},
61+
validators.StringIsISO8601{},
6262
},
6363
},
6464
"duration": schema.StringAttribute{
6565
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`.",
6666
Required: true,
6767
Validators: []validator.String{
68-
validation_utils.StringIsAlertingDuration{},
68+
validators.StringIsAlertingDuration{},
6969
},
7070
},
7171
"timezone": schema.StringAttribute{
@@ -81,14 +81,14 @@ func (r *MaintenanceWindowResource) Schema(_ context.Context, _ resource.SchemaR
8181
Description: "The end date and time of the schedule, provided in ISO 8601 format and set to the UTC timezone. For example: `2025-03-12T12:00:00.000Z`.",
8282
Optional: true,
8383
Validators: []validator.String{
84-
validation_utils.StringIsISO8601{},
84+
validators.StringIsISO8601{},
8585
},
8686
},
8787
"every": schema.StringAttribute{
8888
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`.",
8989
Optional: true,
9090
Validators: []validator.String{
91-
validation_utils.StringIsMaintenanceWindowIntervalFrequency{},
91+
validators.StringIsMaintenanceWindowIntervalFrequency{},
9292
},
9393
},
9494
"occurrences": schema.Int32Attribute{
@@ -104,7 +104,7 @@ func (r *MaintenanceWindowResource) Schema(_ context.Context, _ resource.SchemaR
104104
Optional: true,
105105
Validators: []validator.List{
106106
listvalidator.ValueStringsAre(
107-
validation_utils.StringIsMaintenanceWindowOnWeekDay{},
107+
validators.StringIsMaintenanceWindowOnWeekDay{},
108108
),
109109
},
110110
},

internal/kibana/utils/validators.go

Lines changed: 0 additions & 140 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package validators
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"regexp"
7+
8+
"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"
11+
)
12+
13+
var alertingDurationPattern = "^[1-9][0-9]*(?:d|h|m|s)$"
14+
15+
func StringMatchesAlertingDurationRegex(s string) (matched bool, err error) {
16+
return regexp.MatchString(alertingDurationPattern, s)
17+
}
18+
19+
type StringIsAlertingDuration struct{}
20+
21+
func (s StringIsAlertingDuration) Description(_ context.Context) string {
22+
return "a valid alerting duration in seconds (s), minutes (m), hours (h), or days (d)"
23+
}
24+
25+
func (s StringIsAlertingDuration) MarkdownDescription(ctx context.Context) string {
26+
return s.Description(ctx)
27+
}
28+
29+
func (s StringIsAlertingDuration) ValidateString(_ context.Context, req validator.StringRequest, resp *validator.StringResponse) {
30+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
31+
return
32+
}
33+
34+
if matched, err := StringMatchesAlertingDurationRegex(req.ConfigValue.ValueString()); err != nil || !matched {
35+
resp.Diagnostics.AddAttributeError(
36+
req.Path,
37+
"expected value to be a valid alerting duration",
38+
fmt.Sprintf("This value must be a valid alerting duration in seconds (s), minutes (m), hours (h), or days (d) %s", err),
39+
)
40+
return
41+
}
42+
}
43+
44+
// Avoid lint error on deprecated SchemaValidateFunc usage.
45+
//
46+
//nolint:staticcheck
47+
func StringIsAlertingDurationSDKV2() schema.SchemaValidateFunc {
48+
r := regexp.MustCompile(alertingDurationPattern)
49+
return validation.StringMatch(r, "string is not a valid Alerting duration in seconds (s), minutes (m), hours (h), or days (d)")
50+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package validators
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"regexp"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
9+
)
10+
11+
func StringMatchesISO8601Regex(s string) (matched bool, err error) {
12+
pattern := `(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))`
13+
return regexp.MatchString(pattern, s)
14+
}
15+
16+
type StringIsISO8601 struct{}
17+
18+
func (s StringIsISO8601) Description(_ context.Context) string {
19+
return "a valid ISO8601 date and time formatted string"
20+
}
21+
22+
func (s StringIsISO8601) MarkdownDescription(ctx context.Context) string {
23+
return s.Description(ctx)
24+
}
25+
26+
func (s StringIsISO8601) ValidateString(_ context.Context, req validator.StringRequest, resp *validator.StringResponse) {
27+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
28+
return
29+
}
30+
31+
if matched, err := StringMatchesISO8601Regex(req.ConfigValue.ValueString()); err != nil || !matched {
32+
resp.Diagnostics.AddAttributeError(
33+
req.Path,
34+
"expected value to be a valid ISO8601 string",
35+
fmt.Sprintf("This value must be a valid ISO8601 date and time formatted string %s", err),
36+
)
37+
return
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package validators
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"regexp"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
9+
)
10+
11+
func StringMatchesIntervalFrequencyRegex(s string) (matched bool, err error) {
12+
pattern := `^[1-9][0-9]*(?:d|w|M|y)$`
13+
return regexp.MatchString(pattern, s)
14+
}
15+
16+
type StringIsMaintenanceWindowIntervalFrequency struct{}
17+
18+
func (s StringIsMaintenanceWindowIntervalFrequency) Description(_ context.Context) string {
19+
return "a valid interval/frequency. Allowed values are in the `<integer><unit>` format. `<unit>` is one of `d`, `w`, `M`, or `y` for days, weeks, months, years. For example: `15d`, `2w`, `3m`, `1y`."
20+
}
21+
22+
func (s StringIsMaintenanceWindowIntervalFrequency) MarkdownDescription(ctx context.Context) string {
23+
return s.Description(ctx)
24+
}
25+
26+
func (s StringIsMaintenanceWindowIntervalFrequency) ValidateString(_ context.Context, req validator.StringRequest, resp *validator.StringResponse) {
27+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
28+
return
29+
}
30+
31+
if matched, err := StringMatchesIntervalFrequencyRegex(req.ConfigValue.ValueString()); err != nil || !matched {
32+
resp.Diagnostics.AddAttributeError(
33+
req.Path,
34+
"expected value to be a valid interval/frequency",
35+
fmt.Sprintf("This value must be a valid interval/frequency. Allowed values are in the `<integer><unit>` format. `<unit>` is one of `d`, `w`, `M`, or `y` for days, weeks, months, years. For example: `15d`, `2w`, `3m`, `1y`. %s", err),
36+
)
37+
return
38+
}
39+
}

0 commit comments

Comments
 (0)