@@ -15,6 +15,9 @@ func resourceelkAliasesIndex() *schema.Resource {
1515 Read : resourceelkAliasesIndexRead ,
1616 Update : resourceelkAliasesIndexUpdate ,
1717 Delete : resourceelkAliasesIndexDelete ,
18+ Importer : & schema.ResourceImporter {
19+ State : resourceelkAliasesIndexImport ,
20+ },
1821
1922 Schema : map [string ]* schema.Schema {
2023 "name" : {
@@ -41,7 +44,8 @@ func resourceelkAliasesIndex() *schema.Resource {
4144 Required : true ,
4245 },
4346 "alias" : {
44- Type : schema .TypeList ,
47+ Type : schema .TypeList ,
48+ Optional : true ,
4549 Elem : & schema.Resource {
4650 Schema : map [string ]* schema.Schema {
4751 "name" : {
@@ -54,7 +58,6 @@ func resourceelkAliasesIndex() *schema.Resource {
5458 },
5559 },
5660 },
57- Optional : true ,
5861 },
5962 },
6063 },
@@ -200,11 +203,11 @@ func resourceelkAliasesIndexRead(d *schema.ResourceData, m interface{}) error {
200203 }
201204
202205 // Handle aliases
203- if aliases , ok := templateContent ["aliases" ].(map [string ]interface {}); ok {
204- templateAliases := d .Get ("template.0.alias" ).([]any )
205-
206+ if aliases , ok := templateContent ["aliases" ].(map [string ]any ); ok {
207+ stateAliases := d .Get ("template.0.alias" ).([]any )
206208 var aliasList []any
207- for _ , alias := range templateAliases {
209+
210+ for _ , alias := range stateAliases {
208211 alias := alias .(map [string ]any )
209212 if config , exist := aliases [alias ["name" ].(string )]; exist {
210213 filterJson , err := json .Marshal (config .(map [string ]any )["filter" ])
@@ -217,6 +220,20 @@ func resourceelkAliasesIndexRead(d *schema.ResourceData, m interface{}) error {
217220 })
218221 }
219222 }
223+
224+ for name , config := range aliases {
225+ if ! isInMap (stateAliases , "name" , name ) {
226+ filterJson , err := json .Marshal (config .(map [string ]any )["filter" ])
227+ if err != nil {
228+ return fmt .Errorf ("error marshaling filter: %s" , err )
229+ }
230+ aliasList = append (aliasList , map [string ]any {
231+ "name" : name ,
232+ "filter" : string (filterJson ),
233+ })
234+ }
235+ }
236+
220237 templateVar ["alias" ] = aliasList
221238 }
222239 }
@@ -229,10 +246,46 @@ func resourceelkAliasesIndexRead(d *schema.ResourceData, m interface{}) error {
229246 return nil
230247}
231248
249+ func isInMap (list []any , key string , value any ) bool {
250+ for _ , element := range list {
251+ element := element .(map [string ]any )
252+ if element [key ] == value {
253+ return true
254+ }
255+ }
256+ return false
257+ }
258+
232259func resourceelkAliasesIndexUpdate (d * schema.ResourceData , m interface {}) error {
233260 return resourceelkAliasesIndexCreate (d , m )
234261}
235262
236263func resourceelkAliasesIndexDelete (d * schema.ResourceData , m interface {}) error {
264+ es := m .(* elasticsearch.Client )
265+
266+ name := d .Id ()
267+
268+ // Perform the delete operation
269+ res , err := es .Indices .DeleteIndexTemplate (name )
270+ if err != nil {
271+ return fmt .Errorf ("error deleting Elasticsearch index template: %s" , err )
272+ }
273+ defer res .Body .Close ()
274+
275+ if res .IsError () {
276+ return fmt .Errorf ("error response from Elasticsearch when deleting index template: %s" , res .String ())
277+ }
278+
279+ // If delete is successful, remove it from the state
280+ d .SetId ("" )
281+
237282 return nil
238283}
284+
285+ func resourceelkAliasesIndexImport (d * schema.ResourceData , m any ) ([]* schema.ResourceData , error ) {
286+ err := resourceelkAliasesIndexRead (d , m )
287+ if err != nil {
288+ return nil , err
289+ }
290+ return []* schema.ResourceData {d }, nil
291+ }
0 commit comments