From 3dddf1341cecac1bbfc09c4ae9c6d05904c27004 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Tue, 15 Oct 2024 21:30:42 +1100 Subject: [PATCH 1/5] Alerting fields should be Elastic durations, not Golang durations --- internal/kibana/alerting.go | 6 +++--- internal/kibana/alerting_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index e92b7b568..1076d0020 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -71,7 +71,7 @@ func ResourceAlertingRule() *schema.Resource { Description: "The check interval, which specifies how frequently the rule conditions are checked. The interval must be specified in seconds, minutes, hours or days.", Type: schema.TypeString, Required: true, - ValidateFunc: utils.StringIsDuration, + ValidateFunc: utils.StringIsElasticDuration, }, "actions": { Description: "An action that runs under defined conditions.", @@ -120,7 +120,7 @@ func ResourceAlertingRule() *schema.Resource { 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.", Type: schema.TypeString, Optional: true, - ValidateFunc: utils.StringIsDuration, + ValidateFunc: utils.StringIsElasticDuration, }, }, }, @@ -198,7 +198,7 @@ func ResourceAlertingRule() *schema.Resource { 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.", Type: schema.TypeString, Optional: true, - ValidateFunc: utils.StringIsDuration, + ValidateFunc: utils.StringIsElasticDuration, }, "scheduled_task_id": { Description: "ID of the scheduled task that will execute the alert.", diff --git a/internal/kibana/alerting_test.go b/internal/kibana/alerting_test.go index 0c0f3d37c..3f894fb25 100644 --- a/internal/kibana/alerting_test.go +++ b/internal/kibana/alerting_test.go @@ -52,7 +52,7 @@ func TestAccResourceAlertingRule(t *testing.T) { resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "consumer", "alerts"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "notify_when", "onActiveAlert"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "rule_type_id", ".index-threshold"), - resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "interval", "10m"), + resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "interval", "1d"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "enabled", "false"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "tags.0", "first"), resource.TestCheckResourceAttr("elasticstack_kibana_alerting_rule.test_rule", "tags.1", "second"), @@ -244,7 +244,7 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { termField = "name" }) rule_type_id = ".index-threshold" - interval = "10m" + interval = "1d" enabled = false tags = ["first", "second"] } @@ -439,7 +439,7 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { days = [1,2,3] timezone = "Africa/Accra" hours_start = "01:00" - hours_end = "07:00" + hours_end = "07:00" } } } @@ -514,7 +514,7 @@ resource "elasticstack_kibana_alerting_rule" "test_rule" { days = [7] timezone = "Pacific/Honolulu" hours_start = "02:00" - hours_end = "03:00" + hours_end = "03:00" } } } From 5405b81cdfd510e6b69bc10030df49f186ebfe00 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Tue, 15 Oct 2024 21:40:57 +1100 Subject: [PATCH 2/5] Changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1f622804..a9e9f6b85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## [Unreleased] +- 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)) + ## [0.11.9] - 2024-10-14 ### Breaking changes From c26ef6b2a14b103b9b8bb4d25f45219ce364a4c4 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Wed, 16 Oct 2024 10:11:25 +1100 Subject: [PATCH 3/5] PR feedback --- internal/fleet/integration_policy/resource_test.go | 11 +++++++++++ internal/kibana/alerting.go | 13 ++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/internal/fleet/integration_policy/resource_test.go b/internal/fleet/integration_policy/resource_test.go index 6d4d3c41d..45acedbc1 100644 --- a/internal/fleet/integration_policy/resource_test.go +++ b/internal/fleet/integration_policy/resource_test.go @@ -2,6 +2,7 @@ package integration_policy_test import ( "context" + "encoding/json" "fmt" "regexp" "testing" @@ -12,13 +13,23 @@ import ( "github.com/elastic/terraform-provider-elasticstack/internal/utils" "github.com/elastic/terraform-provider-elasticstack/internal/versionutils" "github.com/hashicorp/go-version" + "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/stretchr/testify/require" ) var minVersionIntegrationPolicy = version.Must(version.NewVersion("8.10.0")) +func TestJsonTypes(t *testing.T) { + mapBytes, err := json.Marshal(map[string]string{}) + require.NoError(t, err) + equal, diags := jsontypes.NewNormalizedValue(`{"a": "b"}`).StringSemanticEquals(context.Background(), jsontypes.NewNormalizedValue(string(mapBytes))) + require.Empty(t, diags) + require.False(t, equal) +} + func TestAccResourceIntegrationPolicy(t *testing.T) { policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum) diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index 1076d0020..15d5596dd 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "regexp" "strings" "github.com/elastic/terraform-provider-elasticstack/internal/clients" @@ -21,6 +22,12 @@ var frequencyMinSupportedVersion = version.Must(version.NewVersion("8.6.0")) var alertsFilterMinSupportedVersion = version.Must(version.NewVersion("8.9.0")) var alertDelayMinSupportedVersion = version.Must(version.NewVersion("8.13.0")) +//nolint:staticcheck Avoid lint error on deprecated SchemaValidateFunc usage. +func stringIsAlertingDuration() schema.SchemaValidateFunc { + r := regexp.MustCompile(`^[0-9]+(?:\.[0-9]+)?(?:d|h|m|s)$`) + return validation.StringMatch(r, "string is not a valid Alerting duration in seconds (s), minutes (m), hours (h), or days (d)") +} + func ResourceAlertingRule() *schema.Resource { apikeySchema := map[string]*schema.Schema{ "rule_id": { @@ -71,7 +78,7 @@ func ResourceAlertingRule() *schema.Resource { Description: "The check interval, which specifies how frequently the rule conditions are checked. The interval must be specified in seconds, minutes, hours or days.", Type: schema.TypeString, Required: true, - ValidateFunc: utils.StringIsElasticDuration, + ValidateFunc: stringIsAlertingDuration(), }, "actions": { Description: "An action that runs under defined conditions.", @@ -120,7 +127,7 @@ func ResourceAlertingRule() *schema.Resource { 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.", Type: schema.TypeString, Optional: true, - ValidateFunc: utils.StringIsElasticDuration, + ValidateFunc: stringIsAlertingDuration(), }, }, }, @@ -198,7 +205,7 @@ func ResourceAlertingRule() *schema.Resource { 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.", Type: schema.TypeString, Optional: true, - ValidateFunc: utils.StringIsElasticDuration, + ValidateFunc: stringIsAlertingDuration(), }, "scheduled_task_id": { Description: "ID of the scheduled task that will execute the alert.", From 74b356f5f4b3311b04428603f75c930fa246f063 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Wed, 16 Oct 2024 10:58:41 +1100 Subject: [PATCH 4/5] Ok golangci-lint --- internal/kibana/alerting.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index 15d5596dd..dbb799d4e 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -22,7 +22,9 @@ var frequencyMinSupportedVersion = version.Must(version.NewVersion("8.6.0")) var alertsFilterMinSupportedVersion = version.Must(version.NewVersion("8.9.0")) var alertDelayMinSupportedVersion = version.Must(version.NewVersion("8.13.0")) -//nolint:staticcheck Avoid lint error on deprecated SchemaValidateFunc usage. +// Avoid lint error on deprecated SchemaValidateFunc usage. +// +//nolint:staticcheck func stringIsAlertingDuration() schema.SchemaValidateFunc { r := regexp.MustCompile(`^[0-9]+(?:\.[0-9]+)?(?:d|h|m|s)$`) return validation.StringMatch(r, "string is not a valid Alerting duration in seconds (s), minutes (m), hours (h), or days (d)") From 259a0e393bf78eef32d69db1a39a544fec13b60c Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Wed, 16 Oct 2024 22:14:42 +1100 Subject: [PATCH 5/5] Update internal/kibana/alerting.go Co-authored-by: Antonio --- internal/kibana/alerting.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/kibana/alerting.go b/internal/kibana/alerting.go index dbb799d4e..06e4639a2 100644 --- a/internal/kibana/alerting.go +++ b/internal/kibana/alerting.go @@ -26,7 +26,7 @@ var alertDelayMinSupportedVersion = version.Must(version.NewVersion("8.13.0")) // //nolint:staticcheck func stringIsAlertingDuration() schema.SchemaValidateFunc { - r := regexp.MustCompile(`^[0-9]+(?:\.[0-9]+)?(?:d|h|m|s)$`) + r := regexp.MustCompile(`^[1-9][0-9]*(?:d|h|m|s)$`) return validation.StringMatch(r, "string is not a valid Alerting duration in seconds (s), minutes (m), hours (h), or days (d)") }