Skip to content

Commit e694471

Browse files
tobioadcoelho
andauthored
Alerting fields should be Elastic durations, not Golang durations (#846)
* Alerting fields should be Elastic durations, not Golang durations * Changelog * PR feedback * Ok golangci-lint * Update internal/kibana/alerting.go Co-authored-by: Antonio <[email protected]> --------- Co-authored-by: Antonio <[email protected]>
1 parent 1fa3e9b commit e694471

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

CHANGELOG.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
## [Unreleased]
22

3-
### Changes
4-
3+
- Fix validation of `throttle`, and `interval` attributes in `elasticstack_kibana_alerting_rule` allowing all Elastic duration values ([#846](https://github.com./elastic/terraform-provider-elasticstack/pull/846))
54
- Fix boolean setting parsing for `elasticstack_elasticsearch_indices` data source. ([#842](https://github.com./elastic/terraform-provider-elasticstack/pull/842))
65

76
## [0.11.9] - 2024-10-14

internal/fleet/integration_policy/resource_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package integration_policy_test
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"regexp"
78
"testing"
@@ -12,13 +13,23 @@ import (
1213
"github.com./elastic/terraform-provider-elasticstack/internal/utils"
1314
"github.com./elastic/terraform-provider-elasticstack/internal/versionutils"
1415
"github.com./hashicorp/go-version"
16+
"github.com./hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1517
sdkacctest "github.com./hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1618
"github.com./hashicorp/terraform-plugin-sdk/v2/helper/resource"
1719
"github.com./hashicorp/terraform-plugin-sdk/v2/terraform"
20+
"github.com./stretchr/testify/require"
1821
)
1922

2023
var minVersionIntegrationPolicy = version.Must(version.NewVersion("8.10.0"))
2124

25+
func TestJsonTypes(t *testing.T) {
26+
mapBytes, err := json.Marshal(map[string]string{})
27+
require.NoError(t, err)
28+
equal, diags := jsontypes.NewNormalizedValue(`{"a": "b"}`).StringSemanticEquals(context.Background(), jsontypes.NewNormalizedValue(string(mapBytes)))
29+
require.Empty(t, diags)
30+
require.False(t, equal)
31+
}
32+
2233
func TestAccResourceIntegrationPolicy(t *testing.T) {
2334
policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
2435

internal/kibana/alerting.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"regexp"
78
"strings"
89

910
"github.com./elastic/terraform-provider-elasticstack/internal/clients"
@@ -21,6 +22,14 @@ var frequencyMinSupportedVersion = version.Must(version.NewVersion("8.6.0"))
2122
var alertsFilterMinSupportedVersion = version.Must(version.NewVersion("8.9.0"))
2223
var alertDelayMinSupportedVersion = version.Must(version.NewVersion("8.13.0"))
2324

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+
2433
func ResourceAlertingRule() *schema.Resource {
2534
apikeySchema := map[string]*schema.Schema{
2635
"rule_id": {
@@ -71,7 +80,7 @@ func ResourceAlertingRule() *schema.Resource {
7180
Description: "The check interval, which specifies how frequently the rule conditions are checked. The interval must be specified in seconds, minutes, hours or days.",
7281
Type: schema.TypeString,
7382
Required: true,
74-
ValidateFunc: utils.StringIsDuration,
83+
ValidateFunc: stringIsAlertingDuration(),
7584
},
7685
"actions": {
7786
Description: "An action that runs under defined conditions.",
@@ -120,7 +129,7 @@ func ResourceAlertingRule() *schema.Resource {
120129
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.",
121130
Type: schema.TypeString,
122131
Optional: true,
123-
ValidateFunc: utils.StringIsDuration,
132+
ValidateFunc: stringIsAlertingDuration(),
124133
},
125134
},
126135
},
@@ -198,7 +207,7 @@ func ResourceAlertingRule() *schema.Resource {
198207
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.",
199208
Type: schema.TypeString,
200209
Optional: true,
201-
ValidateFunc: utils.StringIsDuration,
210+
ValidateFunc: stringIsAlertingDuration(),
202211
},
203212
"scheduled_task_id": {
204213
Description: "ID of the scheduled task that will execute the alert.",

internal/kibana/alerting_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestAccResourceAlertingRule(t *testing.T) {
5252
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "consumer", "alerts"),
5353
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "notify_when", "onActiveAlert"),
5454
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_type_id", ".index-threshold"),
55-
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "interval", "10m"),
55+
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "interval", "1d"),
5656
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "enabled", "false"),
5757
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "tags.0", "first"),
5858
resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "tags.1", "second"),
@@ -244,7 +244,7 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" {
244244
termField = "name"
245245
})
246246
rule_type_id = ".index-threshold"
247-
interval = "10m"
247+
interval = "1d"
248248
enabled = false
249249
tags = ["first", "second"]
250250
}
@@ -439,7 +439,7 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" {
439439
days = [1,2,3]
440440
timezone = "Africa/Accra"
441441
hours_start = "01:00"
442-
hours_end = "07:00"
442+
hours_end = "07:00"
443443
}
444444
}
445445
}
@@ -514,7 +514,7 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" {
514514
days = [7]
515515
timezone = "Pacific/Honolulu"
516516
hours_start = "02:00"
517-
hours_end = "03:00"
517+
hours_end = "03:00"
518518
}
519519
}
520520
}

0 commit comments

Comments
 (0)