Skip to content

Commit 697a7f4

Browse files
jasonrhodestobio
andauthored
Add optional preventInitialBackfill for SLO API (#1071)
* Add optional preventInitialBackfill for SLO API Docs for this option are here: https://www.elastic.co/docs/api/doc/serverless/operation/operation-createsloop#operation-createsloop-body-application-json-settings-preventinitialbackfill * Correct new field to use snake_case * Adds further settings management I think the generated settings model found at generated/slo/model_settings.go is still missing a final component here, but that file is generated by OpenAPI so we probably have a change to make in our open API docs, or we need to re-run the generation script there? * Remove unused function * Changelog * make docs-generate * Acceptance test * Tidy up the version check * make lint --------- Co-authored-by: Toby Brain <[email protected]>
1 parent 385595f commit 697a7f4

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Migrate `elasticstack_kibana_action_connector` to the Terraform plugin framework ([#1269](https://github.com/elastic/terraform-provider-elasticstack/pull/1269))
1414
- Migrate `elasticstack_elasticsearch_security_role_mapping` resource and data source to Terraform Plugin Framework ([#1279](https://github.com/elastic/terraform-provider-elasticstack/pull/1279))
1515
- Add support for `inactivity_timeout` in `elasticstack_fleet_agent_policy` ([#641](https://github.com/elastic/terraform-provider-elasticstack/issues/641))
16+
- Add support for `prevent_initial_backfill` to `elasticstack_kibana_slo` ([#1071](https://github.com/elastic/terraform-provider-elasticstack/pull/1071))
1617
- [Refactor] Regenerate the SLO client using the current OpenAPI spec ([#1303](https://github.com/elastic/terraform-provider-elasticstack/pull/1303))
1718
- Add support for `data_view_id` in the `elasticstack_kibana_slo` resource ([#1305](https://github.com/elastic/terraform-provider-elasticstack/pull/1305))
1819
- Add support for `unenrollment_timeout` in `elasticstack_fleet_agent_policy` ([#1169](https://github.com/elastic/terraform-provider-elasticstack/issues/1169))

docs/resources/kibana_slo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ Optional:
442442
Optional:
443443

444444
- `frequency` (String)
445+
- `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
445446
- `sync_delay` (String)
446447

447448

internal/kibana/slo.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
)
1818

1919
var (
20-
SLOSupportsMultipleGroupByMinVersion = version.Must(version.NewVersion("8.14.0"))
21-
SLOSupportsDataViewIDMinVersion = version.Must(version.NewVersion("8.15.0"))
20+
SLOSupportsMultipleGroupByMinVersion = version.Must(version.NewVersion("8.14.0"))
21+
SLOSupportsPreventInitialBackfillMinVersion = version.Must(version.NewVersion("8.15.0"))
22+
SLOSupportsDataViewIDMinVersion = version.Must(version.NewVersion("8.15.0"))
2223
)
2324

2425
func ResourceSlo() *schema.Resource {
@@ -565,6 +566,11 @@ func getSchema() map[string]*schema.Schema {
565566
Optional: true,
566567
Computed: true,
567568
},
569+
"prevent_initial_backfill": {
570+
Description: "Prevents the underlying ES transform from attempting to backfill data on start, which can sometimes be resource-intensive or time-consuming and unnecessary",
571+
Type: schema.TypeBool,
572+
Optional: true,
573+
},
568574
},
569575
},
570576
},
@@ -824,8 +830,9 @@ func getSloFromResourceData(d *schema.ResourceData) (models.Slo, diag.Diagnostic
824830
}
825831

826832
settings := slo.Settings{
827-
SyncDelay: getOrNil[string]("settings.0.sync_delay", d),
828-
Frequency: getOrNil[string]("settings.0.frequency", d),
833+
SyncDelay: getOrNil[string]("settings.0.sync_delay", d),
834+
Frequency: getOrNil[string]("settings.0.frequency", d),
835+
PreventInitialBackfill: getOrNil[bool]("settings.0.prevent_initial_backfill", d),
829836
}
830837

831838
budgetingMethod := slo.BudgetingMethod(d.Get("budgeting_method").(string))
@@ -877,6 +884,13 @@ func resourceSloCreate(ctx context.Context, d *schema.ResourceData, meta interfa
877884
return diags
878885
}
879886

887+
// Version check for prevent_initial_backfill
888+
if slo.Settings.PreventInitialBackfill != nil {
889+
if !serverVersion.GreaterThanOrEqual(SLOSupportsPreventInitialBackfillMinVersion) {
890+
return diag.Errorf("The 'prevent_initial_backfill' setting requires Elastic Stack version %s or higher.", SLOSupportsPreventInitialBackfillMinVersion)
891+
}
892+
}
893+
880894
// Version check for data_view_id support
881895
if !serverVersion.GreaterThanOrEqual(SLOSupportsDataViewIDMinVersion) {
882896
// Check all indicator types that support data_view_id
@@ -919,6 +933,13 @@ func resourceSloUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
919933
return diags
920934
}
921935

936+
// Version check for prevent_initial_backfill
937+
if slo.Settings.PreventInitialBackfill != nil {
938+
if !serverVersion.GreaterThanOrEqual(SLOSupportsPreventInitialBackfillMinVersion) {
939+
return diag.Errorf("The 'prevent_initial_backfill' setting requires Elastic Stack version %s or higher.", SLOSupportsPreventInitialBackfillMinVersion)
940+
}
941+
}
942+
922943
// Version check for data_view_id support
923944
if !serverVersion.GreaterThanOrEqual(SLOSupportsDataViewIDMinVersion) {
924945
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
11491170

11501171
if err := d.Set("settings", []interface{}{
11511172
map[string]interface{}{
1152-
"sync_delay": s.Settings.SyncDelay,
1153-
"frequency": s.Settings.Frequency,
1173+
"sync_delay": s.Settings.SyncDelay,
1174+
"frequency": s.Settings.Frequency,
1175+
"prevent_initial_backfill": s.Settings.PreventInitialBackfill,
11541176
},
11551177
}); err != nil {
11561178
return diag.FromErr(err)

internal/kibana/slo_test.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,32 @@ func TestAccResourceSloGroupBy(t *testing.T) {
332332
})
333333
}
334334

335+
func TestAccResourceSloPreventInitialBackfill(t *testing.T) {
336+
sloName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
337+
resource.Test(t, resource.TestCase{
338+
PreCheck: func() { acctest.PreCheck(t) },
339+
CheckDestroy: checkResourceSloDestroy,
340+
ProtoV6ProviderFactories: acctest.Providers,
341+
Steps: []resource.TestStep{
342+
{
343+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(kibanaresource.SLOSupportsPreventInitialBackfillMinVersion),
344+
Config: getSLOConfig(sloVars{
345+
name: sloName,
346+
indicatorType: "metric_custom_indicator",
347+
settingsEnabled: true,
348+
groupBy: []string{"some.field", "some.other.field"},
349+
includePreventInitialBackfill: true,
350+
}),
351+
Check: resource.ComposeTestCheckFunc(
352+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "metric_custom_indicator.0.index", "my-index-"+sloName),
353+
354+
resource.TestCheckResourceAttr("elasticstack_kibana_slo.test_slo", "settings.0.prevent_initial_backfill", "true"),
355+
),
356+
},
357+
},
358+
})
359+
}
360+
335361
func TestAccResourceSlo_timeslice_metric_indicator_basic(t *testing.T) {
336362
sloName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
337363
resource.Test(t, resource.TestCase{
@@ -793,24 +819,30 @@ func checkResourceSloDestroy(s *terraform.State) error {
793819
}
794820

795821
type sloVars struct {
796-
name string
797-
indicatorType string
798-
settingsEnabled bool
799-
tags []string
800-
groupBy []string
801-
useSingleElementGroupBy bool
802-
includeDataViewID bool
822+
name string
823+
indicatorType string
824+
settingsEnabled bool
825+
tags []string
826+
groupBy []string
827+
useSingleElementGroupBy bool
828+
includeDataViewID bool
829+
includePreventInitialBackfill bool
803830
}
804831

805832
func getSLOConfig(vars sloVars) string {
806833
var settings string
807834
if vars.settingsEnabled {
808-
settings = `
835+
preventInitialBackfill := ""
836+
if vars.includePreventInitialBackfill {
837+
preventInitialBackfill = "prevent_initial_backfill = true"
838+
}
839+
settings = fmt.Sprintf(`
809840
settings {
810841
sync_delay = "5m"
811842
frequency = "5m"
843+
%s
812844
}
813-
`
845+
`, preventInitialBackfill)
814846
} else {
815847
settings = ""
816848
}

0 commit comments

Comments
 (0)