Skip to content

Commit 783b359

Browse files
committed
es version awareness ignore_missing_component_templates
1 parent 477cc2b commit 783b359

File tree

4 files changed

+111
-20
lines changed

4 files changed

+111
-20
lines changed

internal/elasticsearch/index/template.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ import (
1010
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
1111
"github.com/elastic/terraform-provider-elasticstack/internal/models"
1212
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
13+
"github.com/hashicorp/go-version"
1314
"github.com/hashicorp/terraform-plugin-log/tflog"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1718
)
1819

20+
var (
21+
MinSupportedIgnoreMissingComponentTemplateVersion = version.Must(version.NewVersion("8.7.0"))
22+
)
23+
1924
func ResourceTemplate() *schema.Resource {
2025
templateSchema := map[string]*schema.Schema{
2126
"id": {
@@ -219,6 +224,12 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta
219224
if diags.HasError() {
220225
return diags
221226
}
227+
228+
serverVersion, diags := client.ServerVersion(ctx)
229+
if diags.HasError() {
230+
return diags
231+
}
232+
222233
var indexTemplate models.IndexTemplate
223234
indexTemplate.Name = templateId
224235

@@ -230,13 +241,17 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta
230241
}
231242
indexTemplate.ComposedOf = compsOf
232243

233-
compsOfIgnore := make([]string, 0)
234244
if v, ok := d.GetOk("ignore_missing_component_templates"); ok {
245+
compsOfIgnore := make([]string, 0)
235246
for _, c := range v.([]interface{}) {
236247
compsOfIgnore = append(compsOfIgnore, c.(string))
237248
}
249+
250+
if len(compsOfIgnore) > 0 && serverVersion.LessThan(MinSupportedIgnoreMissingComponentTemplateVersion) {
251+
return diag.FromErr(fmt.Errorf("'ignore_missing_component_templates' is supported only for Elasticsearch v%s and above", MinSupportedIgnoreMissingComponentTemplateVersion.String()))
252+
}
253+
indexTemplate.IgnoreMissingComponentTemplates = compsOfIgnore
238254
}
239-
indexTemplate.IgnoreMissingComponentTemplates = compsOfIgnore
240255

241256
if v, ok := d.GetOk("data_stream"); ok {
242257
// 8.x workaround

internal/elasticsearch/index/template_data_source_test.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"testing"
66

77
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
8+
"github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/index"
9+
"github.com/elastic/terraform-provider-elasticstack/internal/versionutils"
810
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1012
)
1113

1214
func TestAccIndexTemplateDataSource(t *testing.T) {
1315
// generate a random role name
1416
templateName := "test-template-" + sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
17+
templateNameComponent := "test-template-" + sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
1518

1619
resource.Test(t, resource.TestCase{
1720
PreCheck: func() { acctest.PreCheck(t) },
@@ -22,11 +25,19 @@ func TestAccIndexTemplateDataSource(t *testing.T) {
2225
Check: resource.ComposeAggregateTestCheckFunc(
2326
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "name", templateName),
2427
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("tf-acc-%s-*", templateName)),
25-
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs@custom", templateName)),
26-
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs@custom", templateName)),
2728
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "priority", "100"),
2829
),
2930
},
31+
{
32+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion),
33+
Config: testAccIndexTemplateDataSourceWithIgnoreComponentConfig(templateNameComponent),
34+
Check: resource.ComposeAggregateTestCheckFunc(
35+
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "name", templateNameComponent),
36+
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("tf-acc-component-%s-*", templateNameComponent)),
37+
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)),
38+
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)),
39+
),
40+
},
3041
},
3142
})
3243
}
@@ -42,13 +53,30 @@ resource "elasticstack_elasticsearch_index_template" "test" {
4253
4354
priority = 100
4455
index_patterns = ["tf-acc-%s-*"]
45-
46-
composed_of = ["%s-logs@custom"]
47-
ignore_missing_component_templates = ["%s-logs@custom"]
4856
}
4957
5058
data "elasticstack_elasticsearch_index_template" "test" {
5159
name = elasticstack_elasticsearch_index_template.test.name
60+
}
61+
`, templateName, templateName)
62+
}
63+
64+
65+
func testAccIndexTemplateDataSourceWithIgnoreComponentConfig(templateName string) string {
66+
return fmt.Sprintf(`
67+
provider "elasticstack" {
68+
elasticsearch {}
69+
}
70+
71+
resource "elasticstack_elasticsearch_index_template" "test_component" {
72+
name = "%s"
73+
index_patterns = ["tf-acc-component-%s-*"]
74+
composed_of = ["%s-logscomponent@custom"]
75+
ignore_missing_component_templates = ["%s-logscomponent@custom"]
76+
}
77+
78+
data "elasticstack_elasticsearch_index_template" "test_component" {
79+
name = elasticstack_elasticsearch_index_template.test_component.name
5280
}
5381
`, templateName, templateName, templateName, templateName)
5482
}

internal/elasticsearch/index/template_test.go

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
88
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
9+
"github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/index"
10+
"github.com/elastic/terraform-provider-elasticstack/internal/versionutils"
911
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -14,6 +16,7 @@ import (
1416
func TestAccResourceIndexTemplate(t *testing.T) {
1517
// generate random template name
1618
templateName := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
19+
templateNameComponent := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
1720

1821
resource.Test(t, resource.TestCase{
1922
PreCheck: func() { acctest.PreCheck(t) },
@@ -25,8 +28,6 @@ func TestAccResourceIndexTemplate(t *testing.T) {
2528
Check: resource.ComposeTestCheckFunc(
2629
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "name", templateName),
2730
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("%s-logs-*", templateName)),
28-
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs@custom", templateName)),
29-
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs@custom", templateName)),
3031
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "priority", "42"),
3132
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "template.0.alias.#", "1"),
3233
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "name", fmt.Sprintf("%s-stream", templateName)),
@@ -38,13 +39,31 @@ func TestAccResourceIndexTemplate(t *testing.T) {
3839
Check: resource.ComposeTestCheckFunc(
3940
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "name", templateName),
4041
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("%s-logs-*", templateName)),
41-
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "composed_of.*", fmt.Sprintf("%s-logs-updated@custom", templateName)),
42-
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logs-updated@custom", templateName)),
4342
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test", "template.0.alias.#", "2"),
4443
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "name", fmt.Sprintf("%s-stream", templateName)),
4544
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test2", "data_stream.0.hidden", "false"),
4645
),
4746
},
47+
{
48+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion),
49+
Config: testAccResourceIndexTemplateCreateWithIgnoreComponent(templateNameComponent),
50+
Check: resource.ComposeTestCheckFunc(
51+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test_component", "name", templateNameComponent),
52+
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "index_patterns.*", fmt.Sprintf("%s-logscomponent-*", templateNameComponent)),
53+
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "composed_of.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)),
54+
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logscomponent@custom", templateNameComponent)),
55+
),
56+
},
57+
{
58+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(index.MinSupportedIgnoreMissingComponentTemplateVersion),
59+
Config: testAccResourceIndexTemplateUpdateWithIgnoreComponent(templateNameComponent),
60+
Check: resource.ComposeTestCheckFunc(
61+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_template.test_component", "name", templateNameComponent),
62+
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "index_patterns.*", fmt.Sprintf("%s-logscomponent-*", templateNameComponent)),
63+
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "composed_of.*", fmt.Sprintf("%s-logscomponent-updated@custom", templateNameComponent)),
64+
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_index_template.test_component", "ignore_missing_component_templates.*", fmt.Sprintf("%s-logscomponent-updated@custom", templateNameComponent)),
65+
),
66+
},
4867
},
4968
})
5069
}
@@ -61,9 +80,6 @@ resource "elasticstack_elasticsearch_index_template" "test" {
6180
priority = 42
6281
index_patterns = ["%s-logs-*"]
6382
64-
composed_of = ["%s-logs@custom"]
65-
ignore_missing_component_templates = ["%s-logs@custom"]
66-
6783
template {
6884
alias {
6985
name = "my_template_test"
@@ -83,7 +99,7 @@ resource "elasticstack_elasticsearch_index_template" "test2" {
8399
hidden = true
84100
}
85101
}
86-
`, name, name, name, name, name)
102+
`, name, name, name)
87103
}
88104

89105
func testAccResourceIndexTemplateUpdate(name string) string {
@@ -97,9 +113,6 @@ resource "elasticstack_elasticsearch_index_template" "test" {
97113
98114
index_patterns = ["%s-logs-*"]
99115
100-
composed_of = ["%s-logs-updated@custom"]
101-
ignore_missing_component_templates = ["%s-logs-updated@custom"]
102-
103116
template {
104117
alias {
105118
name = "my_template_test"
@@ -124,7 +137,42 @@ resource "elasticstack_elasticsearch_index_template" "test2" {
124137
125138
template {}
126139
}
127-
`, name, name, name, name, name)
140+
`, name, name, name)
141+
}
142+
143+
func testAccResourceIndexTemplateCreateWithIgnoreComponent(name string) string {
144+
return fmt.Sprintf(`
145+
provider "elasticstack" {
146+
elasticsearch {}
147+
}
148+
149+
resource "elasticstack_elasticsearch_index_template" "test_component" {
150+
name = "%s"
151+
index_patterns = ["%s-logscomponent-*"]
152+
153+
composed_of = ["%s-logscomponent@custom"]
154+
ignore_missing_component_templates = ["%s-logscomponent@custom"]
155+
}
156+
`, name, name, name, name)
157+
}
158+
159+
func testAccResourceIndexTemplateUpdateWithIgnoreComponent(name string) string {
160+
return fmt.Sprintf(`
161+
provider "elasticstack" {
162+
elasticsearch {}
163+
}
164+
165+
resource "elasticstack_elasticsearch_index_template" "test_component" {
166+
name = "%s"
167+
index_patterns = ["%s-logscomponent-*"]
168+
169+
composed_of = ["%s-logscomponent-updated@custom"]
170+
ignore_missing_component_templates = ["%s-logscomponent-updated@custom"]
171+
172+
template {
173+
}
174+
}
175+
`, name, name, name, name)
128176
}
129177

130178
func checkResourceIndexTemplateDestroy(s *terraform.State) error {

internal/models/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ type IndexTemplate struct {
167167
Create bool `json:"-"`
168168
Timeout string `json:"-"`
169169
ComposedOf []string `json:"composed_of"`
170-
IgnoreMissingComponentTemplates []string `json:"ignore_missing_component_templates"`
170+
IgnoreMissingComponentTemplates []string `json:"ignore_missing_component_templates,omitempty"`
171171
DataStream *DataStreamSettings `json:"data_stream,omitempty"`
172172
IndexPatterns []string `json:"index_patterns"`
173173
Meta map[string]interface{} `json:"_meta,omitempty"`

0 commit comments

Comments
 (0)