From ef63b9b5a17040a9ceeb119f35a2a33734d1dc38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Wed, 16 Jul 2025 06:57:16 +0200 Subject: [PATCH 01/11] Adding ignore_missing_component_templates https://github.com/elastic/terraform-provider-elasticstack/issues/631 --- .../elasticsearch_index_template.md | 1 + .../resources/elasticsearch_index_template.md | 1 + internal/elasticsearch/index/template.go | 20 ++++++++++++++++++ .../index/template_data_source.go | 8 +++++++ internal/models/models.go | 21 ++++++++++--------- 5 files changed, 41 insertions(+), 10 deletions(-) diff --git a/docs/data-sources/elasticsearch_index_template.md b/docs/data-sources/elasticsearch_index_template.md index c1363c3cd..9beebbb90 100644 --- a/docs/data-sources/elasticsearch_index_template.md +++ b/docs/data-sources/elasticsearch_index_template.md @@ -42,6 +42,7 @@ output "template" { - `composed_of` (List of String) An ordered list of component template names. - `data_stream` (List of Object) If this object is included, the template is used to create data streams and their backing indices. Supports an empty object. (see [below for nested schema](#nestedatt--data_stream)) - `id` (String) Internal identifier of the resource +- `ignore_missing_component_templates` (List of String) A list of component template names that are ignored if missing. - `index_patterns` (Set of String) Array of wildcard (*) expressions used to match the names of data streams and indices during creation. - `metadata` (String) Optional user metadata about the index template. - `priority` (Number) Priority to determine index template precedence when a new data stream or index is created. diff --git a/docs/resources/elasticsearch_index_template.md b/docs/resources/elasticsearch_index_template.md index 17cc732de..08715c32e 100644 --- a/docs/resources/elasticsearch_index_template.md +++ b/docs/resources/elasticsearch_index_template.md @@ -58,6 +58,7 @@ resource "elasticstack_elasticsearch_index_template" "my_data_stream" { - `composed_of` (List of String) An ordered list of component template names. - `data_stream` (Block List, Max: 1) If this object is included, the template is used to create data streams and their backing indices. Supports an empty object. (see [below for nested schema](#nestedblock--data_stream)) - `elasticsearch_connection` (Block List, Max: 1, Deprecated) Elasticsearch connection configuration block. This property will be removed in a future provider version. Configure the Elasticsearch connection via the provider configuration instead. (see [below for nested schema](#nestedblock--elasticsearch_connection)) +- `ignore_missing_component_templates` (List of String) A list of component template names that are ignored if missing. - `metadata` (String) Optional user metadata about the index template. - `priority` (Number) Priority to determine index template precedence when a new data stream or index is created. - `template` (Block List, Max: 1) Template to be applied. It may optionally include an aliases, mappings, lifecycle, or settings configuration. (see [below for nested schema](#nestedblock--template)) diff --git a/internal/elasticsearch/index/template.go b/internal/elasticsearch/index/template.go index d5a70cfc9..4e73a516e 100644 --- a/internal/elasticsearch/index/template.go +++ b/internal/elasticsearch/index/template.go @@ -38,6 +38,15 @@ func ResourceTemplate() *schema.Resource { Type: schema.TypeString, }, }, + "ignore_missing_component_templates": { + Description: "A list of component template names that are ignored if missing.", + Type: schema.TypeList, + Optional: true, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "data_stream": { Description: "If this object is included, the template is used to create data streams and their backing indices. Supports an empty object.", Type: schema.TypeList, @@ -221,6 +230,14 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta } indexTemplate.ComposedOf = compsOf + compsOfIgnore := make([]string, 0) + if v, ok := d.GetOk("ignore_missing_component_templates"); ok { + for _, c := range v.([]interface{}) { + compsOfIgnore = append(compsOfIgnore, c.(string)) + } + } + indexTemplate.IgnoreMissingComponentTemplates = compsOfIgnore + if v, ok := d.GetOk("data_stream"); ok { // 8.x workaround hasAllowCustomRouting := false @@ -371,6 +388,9 @@ func resourceIndexTemplateRead(ctx context.Context, d *schema.ResourceData, meta if err := d.Set("composed_of", tpl.IndexTemplate.ComposedOf); err != nil { return diag.FromErr(err) } + if err := d.Set("ignore_missing_component_templates", tpl.IndexTemplate.IgnoreMissingComponentTemplates); err != nil { + return diag.FromErr(err) + } if stream := tpl.IndexTemplate.DataStream; stream != nil { ds := make([]interface{}, 1) dSettings := make(map[string]interface{}) diff --git a/internal/elasticsearch/index/template_data_source.go b/internal/elasticsearch/index/template_data_source.go index 8d1630268..14bc02c37 100644 --- a/internal/elasticsearch/index/template_data_source.go +++ b/internal/elasticsearch/index/template_data_source.go @@ -29,6 +29,14 @@ func DataSourceTemplate() *schema.Resource { Type: schema.TypeString, }, }, + "ignore_missing_component_templates": { + Description: "A list of component template names that are ignored if missing.", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, "data_stream": { Description: "If this object is included, the template is used to create data streams and their backing indices. Supports an empty object.", Type: schema.TypeList, diff --git a/internal/models/models.go b/internal/models/models.go index 3cc9c1e18..7dc2c1046 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -163,16 +163,17 @@ type Application struct { } type IndexTemplate struct { - Name string `json:"-"` - Create bool `json:"-"` - Timeout string `json:"-"` - ComposedOf []string `json:"composed_of"` - DataStream *DataStreamSettings `json:"data_stream,omitempty"` - IndexPatterns []string `json:"index_patterns"` - Meta map[string]interface{} `json:"_meta,omitempty"` - Priority *int `json:"priority,omitempty"` - Template *Template `json:"template,omitempty"` - Version *int `json:"version,omitempty"` + Name string `json:"-"` + Create bool `json:"-"` + Timeout string `json:"-"` + ComposedOf []string `json:"composed_of"` + IgnoreMissingComponentTemplates []string `json:"ignore_missing_component_templates"` + DataStream *DataStreamSettings `json:"data_stream,omitempty"` + IndexPatterns []string `json:"index_patterns"` + Meta map[string]interface{} `json:"_meta,omitempty"` + Priority *int `json:"priority,omitempty"` + Template *Template `json:"template,omitempty"` + Version *int `json:"version,omitempty"` } type DataStreamSettings struct { From 277c649746d165bf24acf40d06a4ef45c2acad42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Wed, 16 Jul 2025 07:29:43 +0200 Subject: [PATCH 02/11] Trigger workflow --- internal/elasticsearch/index/template.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/elasticsearch/index/template.go b/internal/elasticsearch/index/template.go index 4e73a516e..3fe55bf43 100644 --- a/internal/elasticsearch/index/template.go +++ b/internal/elasticsearch/index/template.go @@ -39,7 +39,7 @@ func ResourceTemplate() *schema.Resource { }, }, "ignore_missing_component_templates": { - Description: "A list of component template names that are ignored if missing.", + Description: "A list of component template names that are ignored if missing. ", Type: schema.TypeList, Optional: true, Computed: true, From 29b0af7bf63e27d01c216faf3d34dbbc80df7b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Wed, 16 Jul 2025 07:30:41 +0200 Subject: [PATCH 03/11] Reverting trigger change --- internal/elasticsearch/index/template.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/elasticsearch/index/template.go b/internal/elasticsearch/index/template.go index 3fe55bf43..4e73a516e 100644 --- a/internal/elasticsearch/index/template.go +++ b/internal/elasticsearch/index/template.go @@ -39,7 +39,7 @@ func ResourceTemplate() *schema.Resource { }, }, "ignore_missing_component_templates": { - Description: "A list of component template names that are ignored if missing. ", + Description: "A list of component template names that are ignored if missing.", Type: schema.TypeList, Optional: true, Computed: true, From 51bce1ccedf1b7b33ec7656954930d8441a4626f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Fri, 18 Jul 2025 20:40:10 +0200 Subject: [PATCH 04/11] Adding acceptance tests on index template for parameter ignore_missing_component_templates --- .../index/template_data_source_test.go | 7 ++++++- internal/elasticsearch/index/template_test.go | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/elasticsearch/index/template_data_source_test.go b/internal/elasticsearch/index/template_data_source_test.go index c37ee5797..046863734 100644 --- a/internal/elasticsearch/index/template_data_source_test.go +++ b/internal/elasticsearch/index/template_data_source_test.go @@ -22,6 +22,8 @@ func TestAccIndexTemplateDataSource(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "name", templateName), resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("tf-acc-%s-*", templateName)), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs@custom", templateName)), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs@custom", templateName)), resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "priority", "100"), ), }, @@ -40,10 +42,13 @@ resource "elasticstack_elasticsearch_index_template" "test" { priority = 100 index_patterns = ["tf-acc-%s-*"] + + composed_of = ["%s-logs@custom"] + ignore_missing_component_templates = ["%s-logs@custom"] } data "elasticstack_elasticsearch_index_template" "test" { name = elasticstack_elasticsearch_index_template.test.name } - `, templateName, templateName) + `, templateName, templateName, templateName, templateName) } diff --git a/internal/elasticsearch/index/template_test.go b/internal/elasticsearch/index/template_test.go index e0d3ce7a4..137d9de7a 100644 --- a/internal/elasticsearch/index/template_test.go +++ b/internal/elasticsearch/index/template_test.go @@ -25,6 +25,8 @@ func TestAccResourceIndexTemplate(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "name", templateName), resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("%s-logs-*", templateName)), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs@custom", templateName)), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs@custom", templateName)), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "priority", "42"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "template.0.alias.#", "1"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "name", fmt.Sprintf("%s-stream", templateName)), @@ -36,6 +38,8 @@ func TestAccResourceIndexTemplate(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "name", templateName), resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("%s-logs-*", templateName)), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs-updated@custom", templateName)), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs-updated@custom", templateName)), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "template.0.alias.#", "2"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "name", fmt.Sprintf("%s-stream", templateName)), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "data_stream.0.hidden", "false"), @@ -57,6 +61,9 @@ resource "elasticstack_elasticsearch_index_template" "test" { priority = 42 index_patterns = ["%s-logs-*"] + composed_of = ["%s-logs@custom"] + ignore_missing_component_templates = ["%s-logs@custom"] + template { alias { name = "my_template_test" @@ -76,7 +83,7 @@ resource "elasticstack_elasticsearch_index_template" "test2" { hidden = true } } - `, name, name, name) + `, name, name, name, name, name) } func testAccResourceIndexTemplateUpdate(name string) string { @@ -90,6 +97,9 @@ resource "elasticstack_elasticsearch_index_template" "test" { index_patterns = ["%s-logs-*"] + composed_of = ["%s-logs-updated@custom"] + ignore_missing_component_templates = ["%s-logs-updated@custom"] + template { alias { name = "my_template_test" @@ -114,7 +124,7 @@ resource "elasticstack_elasticsearch_index_template" "test2" { template {} } - `, name, name, name) + `, name, name, name, name, name) } func checkResourceIndexTemplateDestroy(s *terraform.State) error { From f8c7073e0bd9e24a2d0d049f023657f5ee543ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Fri, 18 Jul 2025 20:40:23 +0200 Subject: [PATCH 05/11] Updating changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2e78b535..2609275dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [Unreleased] - Add support for `timeslice_metric_indicator` in `elasticstack_kibana_slo` ([#1195](https://github.com/elastic/terraform-provider-elasticstack/pull/1195)) +- Add `ignore_missing_component_templates` to `elasticstack_elasticsearch_index_template` ([#1206](https://github.com/elastic/terraform-provider-elasticstack/pull/1206)) ## [0.11.16] - 2025-07-09 From a4fc369d802773189d3755e66cc9188456a5d719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Fri, 18 Jul 2025 20:50:36 +0200 Subject: [PATCH 06/11] Handling inconsistent tabs vs spaces --- internal/elasticsearch/index/template_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/elasticsearch/index/template_test.go b/internal/elasticsearch/index/template_test.go index 137d9de7a..9e5b31de2 100644 --- a/internal/elasticsearch/index/template_test.go +++ b/internal/elasticsearch/index/template_test.go @@ -61,8 +61,8 @@ resource "elasticstack_elasticsearch_index_template" "test" { priority = 42 index_patterns = ["%s-logs-*"] - composed_of = ["%s-logs@custom"] - ignore_missing_component_templates = ["%s-logs@custom"] + composed_of = ["%s-logs@custom"] + ignore_missing_component_templates = ["%s-logs@custom"] template { alias { From 477cc2b23e48db0186a83f18abaedf47008bc47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Fri, 18 Jul 2025 20:52:44 +0200 Subject: [PATCH 07/11] Handling inconsistent tabs vs spaces --- internal/elasticsearch/index/template_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/elasticsearch/index/template_test.go b/internal/elasticsearch/index/template_test.go index 9e5b31de2..bcb8bec3c 100644 --- a/internal/elasticsearch/index/template_test.go +++ b/internal/elasticsearch/index/template_test.go @@ -97,8 +97,8 @@ resource "elasticstack_elasticsearch_index_template" "test" { index_patterns = ["%s-logs-*"] - composed_of = ["%s-logs-updated@custom"] - ignore_missing_component_templates = ["%s-logs-updated@custom"] + composed_of = ["%s-logs-updated@custom"] + ignore_missing_component_templates = ["%s-logs-updated@custom"] template { alias { From 783b35966e7cfe1b4f26b462f8d7a00583d9d8ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Mon, 21 Jul 2025 14:09:54 +0200 Subject: [PATCH 08/11] es version awareness ignore_missing_component_templates --- internal/elasticsearch/index/template.go | 19 ++++- .../index/template_data_source_test.go | 38 ++++++++-- internal/elasticsearch/index/template_test.go | 72 +++++++++++++++---- internal/models/models.go | 2 +- 4 files changed, 111 insertions(+), 20 deletions(-) diff --git a/internal/elasticsearch/index/template.go b/internal/elasticsearch/index/template.go index 4e73a516e..30fecfa24 100644 --- a/internal/elasticsearch/index/template.go +++ b/internal/elasticsearch/index/template.go @@ -10,12 +10,17 @@ import ( "github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch" "github.com/elastic/terraform-provider-elasticstack/internal/models" "github.com/elastic/terraform-provider-elasticstack/internal/utils" + "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) +var ( + MinSupportedIgnoreMissingComponentTemplateVersion = version.Must(version.NewVersion("8.7.0")) +) + func ResourceTemplate() *schema.Resource { templateSchema := map[string]*schema.Schema{ "id": { @@ -219,6 +224,12 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta if diags.HasError() { return diags } + + serverVersion, diags := client.ServerVersion(ctx) + if diags.HasError() { + return diags + } + var indexTemplate models.IndexTemplate indexTemplate.Name = templateId @@ -230,13 +241,17 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta } indexTemplate.ComposedOf = compsOf - compsOfIgnore := make([]string, 0) if v, ok := d.GetOk("ignore_missing_component_templates"); ok { + compsOfIgnore := make([]string, 0) for _, c := range v.([]interface{}) { compsOfIgnore = append(compsOfIgnore, c.(string)) } + + if len(compsOfIgnore) > 0 && serverVersion.LessThan(MinSupportedIgnoreMissingComponentTemplateVersion) { + return diag.FromErr(fmt.Errorf("'ignore_missing_component_templates' is supported only for Elasticsearch v%s and above", MinSupportedIgnoreMissingComponentTemplateVersion.String())) + } + indexTemplate.IgnoreMissingComponentTemplates = compsOfIgnore } - indexTemplate.IgnoreMissingComponentTemplates = compsOfIgnore if v, ok := d.GetOk("data_stream"); ok { // 8.x workaround diff --git a/internal/elasticsearch/index/template_data_source_test.go b/internal/elasticsearch/index/template_data_source_test.go index 046863734..72dc0d7b4 100644 --- a/internal/elasticsearch/index/template_data_source_test.go +++ b/internal/elasticsearch/index/template_data_source_test.go @@ -5,6 +5,8 @@ import ( "testing" "github.com/elastic/terraform-provider-elasticstack/internal/acctest" + "github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/index" + "github.com/elastic/terraform-provider-elasticstack/internal/versionutils" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) @@ -12,6 +14,7 @@ import ( func TestAccIndexTemplateDataSource(t *testing.T) { // generate a random role name templateName := "test-template-" + sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) + templateNameComponent := "test-template-" + sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) resource.Test(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(t) }, @@ -22,11 +25,19 @@ func TestAccIndexTemplateDataSource(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "name", templateName), resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("tf-acc-%s-*", templateName)), - resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs@custom", templateName)), - resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs@custom", templateName)), resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "priority", "100"), ), }, + { + SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion), + Config: testAccIndexTemplateDataSourceWithIgnoreComponentConfig(templateNameComponent), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "name", templateNameComponent), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("tf-acc-component-%s-*", templateNameComponent)), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)), + ), + }, }, }) } @@ -42,13 +53,30 @@ resource "elasticstack_elasticsearch_index_template" "test" { priority = 100 index_patterns = ["tf-acc-%s-*"] - - composed_of = ["%s-logs@custom"] - ignore_missing_component_templates = ["%s-logs@custom"] } data "elasticstack_elasticsearch_index_template" "test" { name = elasticstack_elasticsearch_index_template.test.name +} + `, templateName, templateName) +} + + +func testAccIndexTemplateDataSourceWithIgnoreComponentConfig(templateName string) string { + return fmt.Sprintf(` +provider "elasticstack" { + elasticsearch {} +} + +resource "elasticstack_elasticsearch_index_template" "test_component" { + name = "%s" + index_patterns = ["tf-acc-component-%s-*"] + composed_of = ["%s-logscomponent@custom"] + ignore_missing_component_templates = ["%s-logscomponent@custom"] +} + +data "elasticstack_elasticsearch_index_template" "test_component" { + name = elasticstack_elasticsearch_index_template.test_component.name } `, templateName, templateName, templateName, templateName) } diff --git a/internal/elasticsearch/index/template_test.go b/internal/elasticsearch/index/template_test.go index bcb8bec3c..39f8debb2 100644 --- a/internal/elasticsearch/index/template_test.go +++ b/internal/elasticsearch/index/template_test.go @@ -6,6 +6,8 @@ import ( "github.com/elastic/terraform-provider-elasticstack/internal/acctest" "github.com/elastic/terraform-provider-elasticstack/internal/clients" + "github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/index" + "github.com/elastic/terraform-provider-elasticstack/internal/versionutils" 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" @@ -14,6 +16,7 @@ import ( func TestAccResourceIndexTemplate(t *testing.T) { // generate random template name templateName := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) + templateNameComponent := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) resource.Test(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(t) }, @@ -25,8 +28,6 @@ func TestAccResourceIndexTemplate(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "name", templateName), resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("%s-logs-*", templateName)), - resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs@custom", templateName)), - resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs@custom", templateName)), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "priority", "42"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "template.0.alias.#", "1"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "name", fmt.Sprintf("%s-stream", templateName)), @@ -38,13 +39,31 @@ func TestAccResourceIndexTemplate(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "name", templateName), resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("%s-logs-*", templateName)), - resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs-updated@custom", templateName)), - resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs-updated@custom", templateName)), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "template.0.alias.#", "2"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "name", fmt.Sprintf("%s-stream", templateName)), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "data_stream.0.hidden", "false"), ), }, + { + SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion), + Config: testAccResourceIndexTemplateCreateWithIgnoreComponent(templateNameComponent), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test_component", "name", templateNameComponent), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "index_patterns.*", fmt.Sprintf("%s-logscomponent-*", templateNameComponent)), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "composed_of.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)), + ), + }, + { + SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion), + Config: testAccResourceIndexTemplateUpdateWithIgnoreComponent(templateNameComponent), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test_component", "name", templateNameComponent), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "index_patterns.*", fmt.Sprintf("%s-logscomponent-*", templateNameComponent)), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "composed_of.*", fmt.Sprintf("%s-logscomponent-updated@custom", templateNameComponent)), + resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logscomponent-updated@custom", templateNameComponent)), + ), + }, }, }) } @@ -61,9 +80,6 @@ resource "elasticstack_elasticsearch_index_template" "test" { priority = 42 index_patterns = ["%s-logs-*"] - composed_of = ["%s-logs@custom"] - ignore_missing_component_templates = ["%s-logs@custom"] - template { alias { name = "my_template_test" @@ -83,7 +99,7 @@ resource "elasticstack_elasticsearch_index_template" "test2" { hidden = true } } - `, name, name, name, name, name) + `, name, name, name) } func testAccResourceIndexTemplateUpdate(name string) string { @@ -97,9 +113,6 @@ resource "elasticstack_elasticsearch_index_template" "test" { index_patterns = ["%s-logs-*"] - composed_of = ["%s-logs-updated@custom"] - ignore_missing_component_templates = ["%s-logs-updated@custom"] - template { alias { name = "my_template_test" @@ -124,7 +137,42 @@ resource "elasticstack_elasticsearch_index_template" "test2" { template {} } - `, name, name, name, name, name) + `, name, name, name) +} + +func testAccResourceIndexTemplateCreateWithIgnoreComponent(name string) string { + return fmt.Sprintf(` +provider "elasticstack" { + elasticsearch {} +} + +resource "elasticstack_elasticsearch_index_template" "test_component" { + name = "%s" + index_patterns = ["%s-logscomponent-*"] + + composed_of = ["%s-logscomponent@custom"] + ignore_missing_component_templates = ["%s-logscomponent@custom"] +} + `, name, name, name, name) +} + +func testAccResourceIndexTemplateUpdateWithIgnoreComponent(name string) string { + return fmt.Sprintf(` +provider "elasticstack" { + elasticsearch {} +} + +resource "elasticstack_elasticsearch_index_template" "test_component" { + name = "%s" + index_patterns = ["%s-logscomponent-*"] + + composed_of = ["%s-logscomponent-updated@custom"] + ignore_missing_component_templates = ["%s-logscomponent-updated@custom"] + + template { + } +} + `, name, name, name, name) } func checkResourceIndexTemplateDestroy(s *terraform.State) error { diff --git a/internal/models/models.go b/internal/models/models.go index 7dc2c1046..5608df749 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -167,7 +167,7 @@ type IndexTemplate struct { Create bool `json:"-"` Timeout string `json:"-"` ComposedOf []string `json:"composed_of"` - IgnoreMissingComponentTemplates []string `json:"ignore_missing_component_templates"` + IgnoreMissingComponentTemplates []string `json:"ignore_missing_component_templates,omitempty"` DataStream *DataStreamSettings `json:"data_stream,omitempty"` IndexPatterns []string `json:"index_patterns"` Meta map[string]interface{} `json:"_meta,omitempty"` From 5375c57d9eb37d6e814728871e648a625685bfdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Mon, 21 Jul 2025 15:13:13 +0200 Subject: [PATCH 09/11] Formatting --- internal/elasticsearch/index/template_data_source_test.go | 3 +-- internal/elasticsearch/index/template_test.go | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/elasticsearch/index/template_data_source_test.go b/internal/elasticsearch/index/template_data_source_test.go index 72dc0d7b4..441168df6 100644 --- a/internal/elasticsearch/index/template_data_source_test.go +++ b/internal/elasticsearch/index/template_data_source_test.go @@ -30,7 +30,7 @@ func TestAccIndexTemplateDataSource(t *testing.T) { }, { SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion), - Config: testAccIndexTemplateDataSourceWithIgnoreComponentConfig(templateNameComponent), + Config: testAccIndexTemplateDataSourceWithIgnoreComponentConfig(templateNameComponent), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "name", templateNameComponent), resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("tf-acc-component-%s-*", templateNameComponent)), @@ -61,7 +61,6 @@ data "elasticstack_elasticsearch_index_template" "test" { `, templateName, templateName) } - func testAccIndexTemplateDataSourceWithIgnoreComponentConfig(templateName string) string { return fmt.Sprintf(` provider "elasticstack" { diff --git a/internal/elasticsearch/index/template_test.go b/internal/elasticsearch/index/template_test.go index 39f8debb2..7e0ffd8f2 100644 --- a/internal/elasticsearch/index/template_test.go +++ b/internal/elasticsearch/index/template_test.go @@ -46,7 +46,7 @@ func TestAccResourceIndexTemplate(t *testing.T) { }, { SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion), - Config: testAccResourceIndexTemplateCreateWithIgnoreComponent(templateNameComponent), + Config: testAccResourceIndexTemplateCreateWithIgnoreComponent(templateNameComponent), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test_component", "name", templateNameComponent), resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "index_patterns.*", fmt.Sprintf("%s-logscomponent-*", templateNameComponent)), @@ -56,7 +56,7 @@ func TestAccResourceIndexTemplate(t *testing.T) { }, { SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion), - Config: testAccResourceIndexTemplateUpdateWithIgnoreComponent(templateNameComponent), + Config: testAccResourceIndexTemplateUpdateWithIgnoreComponent(templateNameComponent), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test_component", "name", templateNameComponent), resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "index_patterns.*", fmt.Sprintf("%s-logscomponent-*", templateNameComponent)), From 40de8282f143274a2deeb844a78d6a1427038424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hvals=C3=B8e?= Date: Mon, 21 Jul 2025 15:13:37 +0200 Subject: [PATCH 10/11] Updating ressource ref from data source test --- internal/elasticsearch/index/template_data_source_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/elasticsearch/index/template_data_source_test.go b/internal/elasticsearch/index/template_data_source_test.go index 441168df6..6cc4aa0cf 100644 --- a/internal/elasticsearch/index/template_data_source_test.go +++ b/internal/elasticsearch/index/template_data_source_test.go @@ -32,10 +32,10 @@ func TestAccIndexTemplateDataSource(t *testing.T) { SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion), Config: testAccIndexTemplateDataSourceWithIgnoreComponentConfig(templateNameComponent), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "name", templateNameComponent), - resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("tf-acc-component-%s-*", templateNameComponent)), - resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)), - resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)), + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test_component", "name", templateNameComponent), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test_component", "index_patterns.*", fmt.Sprintf("tf-acc-component-%s-*", templateNameComponent)), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test_component", "composed_of.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test_component", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)), ), }, }, From a4587b5be37aecdd779d29bde2f99aac8dd4b100 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Tue, 22 Jul 2025 17:50:08 +1000 Subject: [PATCH 11/11] Update Changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d00b300fb..88d7acee1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## [Unreleased] +- Add `ignore_missing_component_templates` to `elasticstack_elasticsearch_index_template` ([#1206](https://github.com/elastic/terraform-provider-elasticstack/pull/1206)) + ## [0.11.17] - 2025-07-21 - Add `elasticstack_apm_agent_configuration` resource ([#1196](https://github.com/elastic/terraform-provider-elasticstack/pull/1196)) @@ -7,7 +9,6 @@ - Add `elasticstack_elasticsearch_ingest_processor_reroute` data source ([#678](https://github.com/elastic/terraform-provider-elasticstack/issues/678)) - Add support for `supports_agentless` to `elasticstack_fleet_agent_policy` ([#1197](https://github.com/elastic/terraform-provider-elasticstack/pull/1197)) - Ignore `master_timeout` when targeting Serverless projects ([#1207](https://github.com/elastic/terraform-provider-elasticstack/pull/1207)) -- Add `ignore_missing_component_templates` to `elasticstack_elasticsearch_index_template` ([#1206](https://github.com/elastic/terraform-provider-elasticstack/pull/1206)) ## [0.11.16] - 2025-07-09