@@ -80,40 +80,96 @@ func TestAccResourceAliasDataStream(t *testing.T) {
8080
8181func testAccResourceAliasCreate (aliasName , indexName string ) string {
8282 return fmt .Sprintf (`
83+ provider "elasticstack" {
84+ elasticsearch {}
85+ }
86+
87+ # Create index with mappings to avoid alias management
8388resource "elasticstack_elasticsearch_index" "test_index" {
84- name = "%s"
89+ name = "%s"
90+ deletion_protection = false
91+
92+ mappings = jsonencode({
93+ properties = {
94+ title = {
95+ type = "text"
96+ }
97+ }
98+ })
8599}
86100
87101resource "elasticstack_elasticsearch_alias" "test_alias" {
88102 name = "%s"
89103 indices = [elasticstack_elasticsearch_index.test_index.name]
104+
105+ depends_on = [elasticstack_elasticsearch_index.test_index]
90106}
91107 ` , indexName , aliasName )
92108}
93109
94110func testAccResourceAliasUpdate (aliasName , indexName , indexName2 string ) string {
95111 return fmt .Sprintf (`
112+ provider "elasticstack" {
113+ elasticsearch {}
114+ }
115+
96116resource "elasticstack_elasticsearch_index" "test_index" {
97- name = "%s"
117+ name = "%s"
118+ deletion_protection = false
119+
120+ mappings = jsonencode({
121+ properties = {
122+ title = {
123+ type = "text"
124+ }
125+ }
126+ })
98127}
99128
100129resource "elasticstack_elasticsearch_index" "test_index2" {
101- name = "%s"
130+ name = "%s"
131+ deletion_protection = false
132+
133+ mappings = jsonencode({
134+ properties = {
135+ title = {
136+ type = "text"
137+ }
138+ }
139+ })
102140}
103141
104142resource "elasticstack_elasticsearch_alias" "test_alias" {
105143 name = "%s"
106144 indices = [elasticstack_elasticsearch_index.test_index.name, elasticstack_elasticsearch_index.test_index2.name]
107145 is_write_index = true
108146 routing = "test-routing"
147+
148+ depends_on = [elasticstack_elasticsearch_index.test_index, elasticstack_elasticsearch_index.test_index2]
109149}
110150 ` , indexName , indexName2 , aliasName )
111151}
112152
113153func testAccResourceAliasWithFilter (aliasName , indexName string ) string {
114154 return fmt .Sprintf (`
155+ provider "elasticstack" {
156+ elasticsearch {}
157+ }
158+
115159resource "elasticstack_elasticsearch_index" "test_index" {
116- name = "%s"
160+ name = "%s"
161+ deletion_protection = false
162+
163+ mappings = jsonencode({
164+ properties = {
165+ title = {
166+ type = "text"
167+ }
168+ status = {
169+ type = "keyword"
170+ }
171+ }
172+ })
117173}
118174
119175resource "elasticstack_elasticsearch_alias" "test_alias" {
@@ -124,12 +180,18 @@ resource "elasticstack_elasticsearch_alias" "test_alias" {
124180 status = "published"
125181 }
126182 })
183+
184+ depends_on = [elasticstack_elasticsearch_index.test_index]
127185}
128186 ` , indexName , aliasName )
129187}
130188
131189func testAccResourceAliasDataStreamCreate (aliasName , dsName string ) string {
132190 return fmt .Sprintf (`
191+ provider "elasticstack" {
192+ elasticsearch {}
193+ }
194+
133195resource "elasticstack_elasticsearch_index_template" "test_ds_template" {
134196 name = "%s"
135197 index_patterns = ["%s"]
@@ -160,22 +222,28 @@ func checkResourceAliasDestroy(s *terraform.State) error {
160222 if rs .Type != "elasticstack_elasticsearch_alias" {
161223 continue
162224 }
163- compId , _ := clients .CompositeIdFromStr (rs .Primary .ID )
225+
226+ // Handle the case where ID might not be in the expected format
227+ aliasName := rs .Primary .ID
228+ if compId , err := clients .CompositeIdFromStr (rs .Primary .ID ); err == nil {
229+ aliasName = compId .ResourceId
230+ }
164231
165232 esClient , err := client .GetESClient ()
166233 if err != nil {
167234 return err
168235 }
169236
170237 res , err := esClient .Indices .GetAlias (
171- esClient .Indices .GetAlias .WithName (compId . ResourceId ),
238+ esClient .Indices .GetAlias .WithName (aliasName ),
172239 )
173240 if err != nil {
174241 return err
175242 }
243+ defer res .Body .Close ()
176244
177245 if res .StatusCode != 404 {
178- return fmt .Errorf ("Alias (%s) still exists" , compId . ResourceId )
246+ return fmt .Errorf ("Alias (%s) still exists" , aliasName )
179247 }
180248 }
181249 return nil
0 commit comments