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 (
1416func 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
89105func 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
130178func checkResourceIndexTemplateDestroy (s * terraform.State ) error {
0 commit comments