Skip to content

Commit 573c2fa

Browse files
authored
Don't panic when the template is not defined (#598)
* Don't panic when the template is not defined * Add acceptance tests * Changelog
1 parent a0b5596 commit 573c2fa

File tree

4 files changed

+52
-80
lines changed

4 files changed

+52
-80
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## [Unreleased]
22

3+
### Fixed
4+
5+
- Prevent a provider panic when an `elasticstack_elasticsearch_template` or `elasticstack_elasticsearch_component_template` includes an empty `template` (`template {}`) block. ([#598](https://github.com./elastic/terraform-provider-elasticstack/pull/598))
6+
37
## [0.11.2] - 2024-03-13
48

59
### Fixed

internal/elasticsearch/index/component_template.go

+5-53
Original file line numberDiff line numberDiff line change
@@ -156,62 +156,14 @@ func resourceComponentTemplatePut(ctx context.Context, d *schema.ResourceData, m
156156
}
157157

158158
if v, ok := d.GetOk("template"); ok {
159-
// only one template block allowed to be declared
160-
definedTempl := v.([]interface{})[0].(map[string]interface{})
161-
definedAliases := definedTempl["alias"].(*schema.Set)
162-
templ := models.Template{}
163-
164-
aliases := make(map[string]models.IndexAlias, definedAliases.Len())
165-
for _, a := range definedAliases.List() {
166-
alias := a.(map[string]interface{})
167-
templAlias := models.IndexAlias{}
168-
169-
if f, ok := alias["filter"]; ok {
170-
if f.(string) != "" {
171-
filterMap := make(map[string]interface{})
172-
if err := json.Unmarshal([]byte(f.(string)), &filterMap); err != nil {
173-
return diag.FromErr(err)
174-
}
175-
templAlias.Filter = filterMap
176-
}
177-
}
178-
if ir, ok := alias["index_routing"]; ok {
179-
templAlias.IndexRouting = ir.(string)
180-
}
181-
templAlias.IsHidden = alias["is_hidden"].(bool)
182-
templAlias.IsWriteIndex = alias["is_write_index"].(bool)
183-
if r, ok := alias["routing"]; ok {
184-
templAlias.Routing = r.(string)
185-
}
186-
if sr, ok := alias["search_routing"]; ok {
187-
templAlias.SearchRouting = sr.(string)
188-
}
189-
190-
aliases[alias["name"].(string)] = templAlias
191-
}
192-
templ.Aliases = aliases
193-
194-
if mappings, ok := definedTempl["mappings"]; ok {
195-
if mappings.(string) != "" {
196-
maps := make(map[string]interface{})
197-
if err := json.Unmarshal([]byte(mappings.(string)), &maps); err != nil {
198-
return diag.FromErr(err)
199-
}
200-
templ.Mappings = maps
201-
}
159+
templ, ok, diags := expandTemplate(v)
160+
if diags != nil {
161+
return diags
202162
}
203163

204-
if settings, ok := definedTempl["settings"]; ok {
205-
if settings.(string) != "" {
206-
sets := make(map[string]interface{})
207-
if err := json.Unmarshal([]byte(settings.(string)), &sets); err != nil {
208-
return diag.FromErr(err)
209-
}
210-
templ.Settings = sets
211-
}
164+
if ok {
165+
componentTemplate.Template = &templ
212166
}
213-
214-
componentTemplate.Template = &templ
215167
}
216168

217169
if v, ok := d.GetOk("version"); ok {

internal/elasticsearch/index/template.go

+41-27
Original file line numberDiff line numberDiff line change
@@ -257,37 +257,14 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta
257257
}
258258

259259
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 {
266262
return diags
267263
}
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-
}
279264

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
288267
}
289-
290-
indexTemplate.Template = &templ
291268
}
292269

293270
if v, ok := d.GetOk("version"); ok {
@@ -303,6 +280,43 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta
303280
return resourceIndexTemplateRead(ctx, d, meta)
304281
}
305282

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+
306320
func resourceIndexTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
307321
client, diags := clients.NewApiClientFromSDKResource(d, meta)
308322
if diags.HasError() {

internal/elasticsearch/index/template_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ resource "elasticstack_elasticsearch_index_template" "test2" {
111111
data_stream {
112112
hidden = false
113113
}
114+
115+
template {}
114116
}
115117
`, name, name, name)
116118
}

0 commit comments

Comments
 (0)