diff --git a/CHANGELOG.md b/CHANGELOG.md index a98032f68..3f27ecf86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Migrate `elasticstack_kibana_action_connector` to the Terraform plugin framework ([#1269](https://github.com/elastic/terraform-provider-elasticstack/pull/1269)) - Migrate `elasticstack_elasticsearch_security_role_mapping` resource and data source to Terraform Plugin Framework ([#1279](https://github.com/elastic/terraform-provider-elasticstack/pull/1279)) - Add support for `inactivity_timeout` in `elasticstack_fleet_agent_policy` ([#641](https://github.com/elastic/terraform-provider-elasticstack/issues/641)) +- Add support for `prevent_initial_backfill` to `elasticstack_kibana_slo` ([#1071](https://github.com/elastic/terraform-provider-elasticstack/pull/1071)) - [Refactor] Regenerate the SLO client using the current OpenAPI spec ([#1303](https://github.com/elastic/terraform-provider-elasticstack/pull/1303)) - Add support for `data_view_id` in the `elasticstack_kibana_slo` resource ([#1305](https://github.com/elastic/terraform-provider-elasticstack/pull/1305)) - Add support for `unenrollment_timeout` in `elasticstack_fleet_agent_policy` ([#1169](https://github.com/elastic/terraform-provider-elasticstack/issues/1169)) diff --git a/docs/resources/kibana_slo.md b/docs/resources/kibana_slo.md index 61add57c0..788bdf740 100644 --- a/docs/resources/kibana_slo.md +++ b/docs/resources/kibana_slo.md @@ -442,6 +442,7 @@ Optional: Optional: - `frequency` (String) +- `prevent_initial_backfill` (Boolean) Prevents the underlying ES transform from attempting to backfill data on start, which can sometimes be resource-intensive or time-consuming and unnecessary - `sync_delay` (String) diff --git a/internal/kibana/slo.go b/internal/kibana/slo.go index 53dc3d19c..fb83c4a72 100644 --- a/internal/kibana/slo.go +++ b/internal/kibana/slo.go @@ -17,8 +17,9 @@ import ( ) var ( - SLOSupportsMultipleGroupByMinVersion = version.Must(version.NewVersion("8.14.0")) - SLOSupportsDataViewIDMinVersion = version.Must(version.NewVersion("8.15.0")) + SLOSupportsMultipleGroupByMinVersion = version.Must(version.NewVersion("8.14.0")) + SLOSupportsPreventInitialBackfillMinVersion = version.Must(version.NewVersion("8.15.0")) + SLOSupportsDataViewIDMinVersion = version.Must(version.NewVersion("8.15.0")) ) func ResourceSlo() *schema.Resource { @@ -565,6 +566,11 @@ func getSchema() map[string]*schema.Schema { Optional: true, Computed: true, }, + "prevent_initial_backfill": { + Description: "Prevents the underlying ES transform from attempting to backfill data on start, which can sometimes be resource-intensive or time-consuming and unnecessary", + Type: schema.TypeBool, + Optional: true, + }, }, }, }, @@ -824,8 +830,9 @@ func getSloFromResourceData(d *schema.ResourceData) (models.Slo, diag.Diagnostic } settings := slo.Settings{ - SyncDelay: getOrNil[string]("settings.0.sync_delay", d), - Frequency: getOrNil[string]("settings.0.frequency", d), + SyncDelay: getOrNil[string]("settings.0.sync_delay", d), + Frequency: getOrNil[string]("settings.0.frequency", d), + PreventInitialBackfill: getOrNil[bool]("settings.0.prevent_initial_backfill", d), } budgetingMethod := slo.BudgetingMethod(d.Get("budgeting_method").(string)) @@ -877,6 +884,13 @@ func resourceSloCreate(ctx context.Context, d *schema.ResourceData, meta interfa return diags } + // Version check for prevent_initial_backfill + if slo.Settings.PreventInitialBackfill != nil { + if !serverVersion.GreaterThanOrEqual(SLOSupportsPreventInitialBackfillMinVersion) { + return diag.Errorf("The 'prevent_initial_backfill' setting requires Elastic Stack version %s or higher.", SLOSupportsPreventInitialBackfillMinVersion) + } + } + // Version check for data_view_id support if !serverVersion.GreaterThanOrEqual(SLOSupportsDataViewIDMinVersion) { // Check all indicator types that support data_view_id @@ -919,6 +933,13 @@ func resourceSloUpdate(ctx context.Context, d *schema.ResourceData, meta interfa return diags } + // Version check for prevent_initial_backfill + if slo.Settings.PreventInitialBackfill != nil { + if !serverVersion.GreaterThanOrEqual(SLOSupportsPreventInitialBackfillMinVersion) { + return diag.Errorf("The 'prevent_initial_backfill' setting requires Elastic Stack version %s or higher.", SLOSupportsPreventInitialBackfillMinVersion) + } + } + // Version check for data_view_id support if !serverVersion.GreaterThanOrEqual(SLOSupportsDataViewIDMinVersion) { for _, indicatorType := range []string{"metric_custom_indicator", "histogram_custom_indicator", "kql_custom_indicator", "timeslice_metric_indicator"} { @@ -1149,8 +1170,9 @@ func resourceSloRead(ctx context.Context, d *schema.ResourceData, meta interface if err := d.Set("settings", []interface{}{ map[string]interface{}{ - "sync_delay": s.Settings.SyncDelay, - "frequency": s.Settings.Frequency, + "sync_delay": s.Settings.SyncDelay, + "frequency": s.Settings.Frequency, + "prevent_initial_backfill": s.Settings.PreventInitialBackfill, }, }); err != nil { return diag.FromErr(err) diff --git a/internal/kibana/slo_test.go b/internal/kibana/slo_test.go index 2f1216bed..d2c0a5b0c 100644 --- a/internal/kibana/slo_test.go +++ b/internal/kibana/slo_test.go @@ -332,6 +332,32 @@ func TestAccResourceSloGroupBy(t *testing.T) { }) } +func TestAccResourceSloPreventInitialBackfill(t *testing.T) { + sloName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum) + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + CheckDestroy: checkResourceSloDestroy, + ProtoV6ProviderFactories: acctest.Providers, + Steps: []resource.TestStep{ + { + SkipFunc: versionutils.CheckIfVersionIsUnsupported(kibanaresource.SLOSupportsPreventInitialBackfillMinVersion), + Config: getSLOConfig(sloVars{ + name: sloName, + indicatorType: "metric_custom_indicator", + settingsEnabled: true, + groupBy: []string{"some.field", "some.other.field"}, + includePreventInitialBackfill: true, + }), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.index", "my-index-"+sloName), + + resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "settings.0.prevent_initial_backfill", "true"), + ), + }, + }, + }) +} + func TestAccResourceSlo_timeslice_metric_indicator_basic(t *testing.T) { sloName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum) resource.Test(t, resource.TestCase{ @@ -793,24 +819,30 @@ func checkResourceSloDestroy(s *terraform.State) error { } type sloVars struct { - name string - indicatorType string - settingsEnabled bool - tags []string - groupBy []string - useSingleElementGroupBy bool - includeDataViewID bool + name string + indicatorType string + settingsEnabled bool + tags []string + groupBy []string + useSingleElementGroupBy bool + includeDataViewID bool + includePreventInitialBackfill bool } func getSLOConfig(vars sloVars) string { var settings string if vars.settingsEnabled { - settings = ` + preventInitialBackfill := "" + if vars.includePreventInitialBackfill { + preventInitialBackfill = "prevent_initial_backfill = true" + } + settings = fmt.Sprintf(` settings { sync_delay = "5m" frequency = "5m" + %s } - ` + `, preventInitialBackfill) } else { settings = "" }