@@ -2,45 +2,65 @@ package es
22
33import (
44 "encoding/json"
5+ "fmt"
56 "reflect"
67 "strconv"
78 "strings"
89
910 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+ "github.com/olivere/elastic/v7"
1012 log "github.com/sirupsen/logrus"
1113)
1214
13- // diffSuppressIndexTemplate permit to compare template in current state vs from API
14- func diffSuppressIndexTemplate (k , old , new string , d * schema.ResourceData ) bool {
15- var oo , no map [string ]interface {}
15+ // diffSuppressIndexTemplateLegacy permit to compare template in current state vs from API
16+ func diffSuppressIndexTemplateLegacy (k , old , new string , d * schema.ResourceData ) bool {
17+
18+ oo := & elastic.IndicesGetTemplateResponse {}
19+ no := & elastic.IndicesGetTemplateResponse {}
20+
1621 if err := json .Unmarshal ([]byte (old ), & oo ); err != nil {
22+ fmt .Printf ("[ERR] Error when converting to IndicesGetComponentTemplate: %s" , err .Error ())
23+ log .Errorf ("Error when converting to IndicesGetComponentTemplate: %s" , err .Error ())
1724 return false
1825 }
1926 if err := json .Unmarshal ([]byte (new ), & no ); err != nil {
27+ fmt .Printf ("[ERR] Error when converting to IndicesGetComponentTemplate: %s" , err .Error ())
28+ log .Errorf ("Error when converting to IndicesGetComponentTemplate: %s" , err .Error ())
2029 return false
2130 }
2231
23- // Add default parameters on new index template if needed
24- if _ , ok := no ["order" ]; ! ok {
25- no ["order" ] = 0
32+ // inits default values
33+
34+ if oo .Aliases == nil {
35+ oo .Aliases = make (map [string ]interface {})
2636 }
27- if _ , ok := no ["settings" ]; ! ok {
28- no ["settings" ] = make (map [string ]interface {})
37+ if oo .Mappings == nil {
38+ oo .Mappings = make (map [string ]interface {})
39+ }
40+ if oo .Settings == nil {
41+ oo .Settings = make (map [string ]interface {})
42+ }
43+
44+ if no .Aliases == nil {
45+ no .Aliases = make (map [string ]interface {})
2946 }
30- if _ , ok := no [ "mappings" ]; ! ok {
31- no [ "mappings" ] = make (map [string ]interface {})
47+ if no . Mappings == nil {
48+ no . Mappings = make (map [string ]interface {})
3249 }
33- if _ , ok := no [ "aliases" ]; ! ok {
34- no [ "aliases" ] = make (map [string ]interface {})
50+ if no . Settings == nil {
51+ no . Settings = make (map [string ]interface {})
3552 }
3653
37- ob , _ := json .Marshal (oo )
38- nb , _ := json .Marshal (parseAllDotProperties (no ))
54+ // force undot properties to compare the same think
55+ oo .Aliases = parseAllDotProperties (oo .Aliases )
56+ oo .Mappings = parseAllDotProperties (oo .Mappings )
57+ oo .Settings = parseAllDotProperties (oo .Settings )
3958
40- log .Debugf ("Old: %s" , string (ob ))
41- log .Debugf ("New: %s" , string (nb ))
59+ no .Aliases = parseAllDotProperties (no .Aliases )
60+ no .Mappings = parseAllDotProperties (no .Mappings )
61+ no .Settings = parseAllDotProperties (no .Settings )
4262
43- return reflect .DeepEqual (oo , parseAllDotProperties ( no ) )
63+ return reflect .DeepEqual (oo , no )
4464}
4565
4666// suppressEquivalentJSON permit to compare state store as JSON string
@@ -121,3 +141,113 @@ func parseDotPropertie(key string, value interface{}, result map[string]interfac
121141 }
122142
123143}
144+
145+ // diffSuppressIndexComponentTemplate permit to compare index component template in current state vs from API
146+ func diffSuppressIndexComponentTemplate (k , old , new string , d * schema.ResourceData ) bool {
147+ oo := & elastic.IndicesGetComponentTemplate {}
148+ no := & elastic.IndicesGetComponentTemplate {}
149+
150+ if err := json .Unmarshal ([]byte (old ), & oo ); err != nil {
151+ fmt .Printf ("[ERR] Error when converting to IndicesGetComponentTemplate: %s" , err .Error ())
152+ log .Errorf ("Error when converting to IndicesGetComponentTemplate: %s" , err .Error ())
153+ return false
154+ }
155+ if err := json .Unmarshal ([]byte (new ), & no ); err != nil {
156+ fmt .Printf ("[ERR] Error when converting to IndicesGetComponentTemplate: %s" , err .Error ())
157+ log .Errorf ("Error when converting to IndicesGetComponentTemplate: %s" , err .Error ())
158+ return false
159+ }
160+
161+ // inits default values
162+ if oo .Template != nil {
163+ if oo .Template .Aliases == nil {
164+ oo .Template .Aliases = make (map [string ]interface {})
165+ }
166+ if oo .Template .Mappings == nil {
167+ oo .Template .Mappings = make (map [string ]interface {})
168+ }
169+ if oo .Template .Settings == nil {
170+ oo .Template .Settings = make (map [string ]interface {})
171+ }
172+ }
173+ if no .Template != nil {
174+ if no .Template .Aliases == nil {
175+ no .Template .Aliases = make (map [string ]interface {})
176+ }
177+ if no .Template .Mappings == nil {
178+ no .Template .Mappings = make (map [string ]interface {})
179+ }
180+ if no .Template .Settings == nil {
181+ no .Template .Settings = make (map [string ]interface {})
182+ }
183+ }
184+
185+ // force undot properties to compare the same think
186+ if oo .Template != nil {
187+ oo .Template .Aliases = parseAllDotProperties (oo .Template .Aliases )
188+ oo .Template .Mappings = parseAllDotProperties (oo .Template .Mappings )
189+ oo .Template .Settings = parseAllDotProperties (oo .Template .Settings )
190+ }
191+ if no .Template != nil {
192+ no .Template .Aliases = parseAllDotProperties (no .Template .Aliases )
193+ no .Template .Mappings = parseAllDotProperties (no .Template .Mappings )
194+ no .Template .Settings = parseAllDotProperties (no .Template .Settings )
195+ }
196+
197+ return reflect .DeepEqual (oo , no )
198+ }
199+
200+ // diffSuppressIndexTemplate permit to compare index template in current state vs from API
201+ func diffSuppressIndexTemplate (k , old , new string , d * schema.ResourceData ) bool {
202+ oo := & elastic.IndicesGetIndexTemplate {}
203+ no := & elastic.IndicesGetIndexTemplate {}
204+
205+ if err := json .Unmarshal ([]byte (old ), & oo ); err != nil {
206+ fmt .Printf ("[ERR] Error when converting to IndicesGetIndexTemplate on old object: %s" , err .Error ())
207+ log .Errorf ("Error when converting to IndicesGetIndexTemplate on old object: %s\n %s" , err .Error (), old )
208+ return false
209+ }
210+ if err := json .Unmarshal ([]byte (new ), & no ); err != nil {
211+ fmt .Printf ("[ERR] Error when converting to IndicesGetIndexTemplate on new object: %s" , err .Error ())
212+ log .Errorf ("Error when converting to IndicesGetIndexTemplate on new object: %s\n %s" , err .Error (), new )
213+ return false
214+ }
215+
216+ // inits default values
217+ if oo .Template != nil {
218+ if oo .Template .Aliases == nil {
219+ oo .Template .Aliases = make (map [string ]interface {})
220+ }
221+ if oo .Template .Mappings == nil {
222+ oo .Template .Mappings = make (map [string ]interface {})
223+ }
224+ if oo .Template .Settings == nil {
225+ oo .Template .Settings = make (map [string ]interface {})
226+ }
227+ }
228+ if no .Template != nil {
229+ if no .Template .Aliases == nil {
230+ no .Template .Aliases = make (map [string ]interface {})
231+ }
232+ if no .Template .Mappings == nil {
233+ no .Template .Mappings = make (map [string ]interface {})
234+ }
235+ if no .Template .Settings == nil {
236+ no .Template .Settings = make (map [string ]interface {})
237+ }
238+ }
239+
240+ // force undot properties to compare the same think
241+ if oo .Template != nil {
242+ oo .Template .Aliases = parseAllDotProperties (oo .Template .Aliases )
243+ oo .Template .Mappings = parseAllDotProperties (oo .Template .Mappings )
244+ oo .Template .Settings = parseAllDotProperties (oo .Template .Settings )
245+ }
246+ if no .Template != nil {
247+ no .Template .Aliases = parseAllDotProperties (no .Template .Aliases )
248+ no .Template .Mappings = parseAllDotProperties (no .Template .Mappings )
249+ no .Template .Settings = parseAllDotProperties (no .Template .Settings )
250+ }
251+
252+ return reflect .DeepEqual (no , oo )
253+ }
0 commit comments