@@ -257,37 +257,14 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta
257
257
}
258
258
259
259
if v , ok := d .GetOk ("template" ); ok {
260
- templ := models.Template {}
261
- // only one template block allowed to be declared
262
- definedTempl := v .([]interface {})[0 ].(map [string ]interface {})
263
-
264
- aliases , diags := ExpandIndexAliases (definedTempl ["alias" ].(* schema.Set ))
265
- if diags .HasError () {
260
+ templ , ok , diags := expandTemplate (v )
261
+ if diags != nil {
266
262
return diags
267
263
}
268
- templ .Aliases = aliases
269
-
270
- if mappings , ok := definedTempl ["mappings" ]; ok {
271
- if mappings .(string ) != "" {
272
- maps := make (map [string ]interface {})
273
- if err := json .Unmarshal ([]byte (mappings .(string )), & maps ); err != nil {
274
- return diag .FromErr (err )
275
- }
276
- templ .Mappings = maps
277
- }
278
- }
279
264
280
- if settings , ok := definedTempl ["settings" ]; ok {
281
- if settings .(string ) != "" {
282
- sets := make (map [string ]interface {})
283
- if err := json .Unmarshal ([]byte (settings .(string )), & sets ); err != nil {
284
- return diag .FromErr (err )
285
- }
286
- templ .Settings = sets
287
- }
265
+ if ok {
266
+ indexTemplate .Template = & templ
288
267
}
289
-
290
- indexTemplate .Template = & templ
291
268
}
292
269
293
270
if v , ok := d .GetOk ("version" ); ok {
@@ -303,6 +280,43 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta
303
280
return resourceIndexTemplateRead (ctx , d , meta )
304
281
}
305
282
283
+ func expandTemplate (config interface {}) (models.Template , bool , diag.Diagnostics ) {
284
+ templ := models.Template {}
285
+ // only one template block allowed to be declared
286
+ definedTempl , ok := config .([]interface {})[0 ].(map [string ]interface {})
287
+ if ! ok {
288
+ return templ , false , nil
289
+ }
290
+
291
+ aliases , diags := ExpandIndexAliases (definedTempl ["alias" ].(* schema.Set ))
292
+ if diags .HasError () {
293
+ return templ , false , diags
294
+ }
295
+ templ .Aliases = aliases
296
+
297
+ if mappings , ok := definedTempl ["mappings" ]; ok {
298
+ if mappings .(string ) != "" {
299
+ maps := make (map [string ]interface {})
300
+ if err := json .Unmarshal ([]byte (mappings .(string )), & maps ); err != nil {
301
+ return templ , false , diag .FromErr (err )
302
+ }
303
+ templ .Mappings = maps
304
+ }
305
+ }
306
+
307
+ if settings , ok := definedTempl ["settings" ]; ok {
308
+ if settings .(string ) != "" {
309
+ sets := make (map [string ]interface {})
310
+ if err := json .Unmarshal ([]byte (settings .(string )), & sets ); err != nil {
311
+ return templ , false , diag .FromErr (err )
312
+ }
313
+ templ .Settings = sets
314
+ }
315
+ }
316
+
317
+ return templ , true , nil
318
+ }
319
+
306
320
func resourceIndexTemplateRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
307
321
client , diags := clients .NewApiClientFromSDKResource (d , meta )
308
322
if diags .HasError () {
0 commit comments