@@ -19,7 +19,7 @@ func resourceSumologicCSEMatchList() *schema.Resource {
1919 Schema : map [string ]* schema.Schema {
2020 "default_ttl" : {
2121 Type : schema .TypeInt ,
22- Required : true ,
22+ Optional : true ,
2323 ForceNew : false ,
2424 },
2525 "description" : {
@@ -130,41 +130,24 @@ func resourceSumologicCSEMatchListRead(d *schema.ResourceData, meta interface{})
130130
131131func setItems (d * schema.ResourceData , items []CSEMatchListItemGet ) {
132132
133- var i []map [string ]interface {}
133+ var its []map [string ]interface {}
134134
135135 for _ , t := range items {
136- mapping := map [string ]interface {}{
136+ item := map [string ]interface {}{
137137 "id" : t .ID ,
138138 "description" : t .Meta .Description ,
139139 "expiration" : t .Expiration ,
140140 "value" : t .Value ,
141141 }
142- i = append (i , mapping )
142+ its = append (its , item )
143143 }
144144
145- d .Set ("items" , i )
145+ d .Set ("items" , its )
146146
147147}
148148
149149func resourceSumologicCSEMatchListDelete (d * schema.ResourceData , meta interface {}) error {
150150 c := meta .(* Client )
151-
152- //Match list items
153- itemsData := d .Get ("items" ).([]interface {})
154- var items []CSEMatchListItemPost
155- for _ , data := range itemsData {
156- items = append (items , resourceToCSEMatchListItem ([]interface {}{data }))
157- }
158-
159- if len (items ) > 0 {
160- for _ , item := range items {
161- err2 := c .DeleteCSEMatchListItem (item .ID )
162- if err2 != nil {
163- log .Printf ("[WARN] An error occurred while updating match list item wiht id: %s, err: %v" , item .ID , err2 )
164- }
165- }
166- }
167-
168151 err := c .DeleteCSEMatchList (d .Id ())
169152 return err
170153
@@ -191,7 +174,9 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
191174 itemsData := d .Get ("items" ).([]interface {})
192175 var items []CSEMatchListItemPost
193176 for _ , data := range itemsData {
194- items = append (items , resourceToCSEMatchListItem ([]interface {}{data }))
177+ item , _ := resourceToCSEMatchListItem ([]interface {}{data })
178+ items = append (items , item )
179+
195180 }
196181
197182 if len (items ) > 0 {
@@ -211,7 +196,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
211196 return resourceSumologicCSEMatchListRead (d , meta )
212197}
213198
214- func resourceToCSEMatchListItem (data interface {}) CSEMatchListItemPost {
199+ func resourceToCSEMatchListItem (data interface {}) ( CSEMatchListItemPost , string ) {
215200 itemsSlice := data .([]interface {})
216201 item := CSEMatchListItemPost {}
217202 if len (itemsSlice ) > 0 {
@@ -222,7 +207,7 @@ func resourceToCSEMatchListItem(data interface{}) CSEMatchListItemPost {
222207 item .Expiration = itemObj ["expiration" ].(string )
223208 item .Value = itemObj ["value" ].(string )
224209 }
225- return item
210+ return item , item . ID
226211}
227212
228213func resourceSumologicCSEMatchListUpdate (d * schema.ResourceData , meta interface {}) error {
@@ -238,19 +223,30 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
238223
239224 //Match list items
240225 itemsData := d .Get ("items" ).([]interface {})
226+ var itemIds []string
241227 var items []CSEMatchListItemPost
242228 for _ , data := range itemsData {
243- items = append (items , resourceToCSEMatchListItem ([]interface {}{data }))
229+ item , id := resourceToCSEMatchListItem ([]interface {}{data })
230+ items = append (items , item )
231+ itemIds = append (itemIds , id )
232+
244233 }
245234
246235 if len (items ) > 0 {
247236 for _ , item := range items {
248237 CSEMatchListItem , er := c .GetCSEMatchListItem (item .ID )
249- log .Printf ("[WARN] An error occurred while getting match list item wiht id: %s, err: %v" , item .ID , er )
238+ log .Printf ("[WARN] An error occurred while getting match list item with id: %s, err: %v" , item .ID , er )
250239 if CSEMatchListItem != nil {
251- err3 := c .UpdateCSEMatchListItem (item )
252- if err3 != nil {
253- log .Printf ("[WARN] An error occurred while updating match list item wiht id: %s, err: %v" , item .ID , err3 )
240+ if contains (itemIds , CSEMatchListItem .ID ) {
241+ err3 := c .UpdateCSEMatchListItem (item )
242+ if err3 != nil {
243+ log .Printf ("[WARN] An error occurred while updating match list item with id: %s, err: %v" , item .ID , err3 )
244+ }
245+ } else {
246+ err3 := c .DeleteCSEMatchListItem (CSEMatchListItem .ID )
247+ if err3 != nil {
248+ log .Printf ("[WARN] An error occurred deleting match list item with id: %s, err: %v" , CSEMatchListItem .ID , err3 )
249+ }
254250 }
255251 } else {
256252 err4 := c .CreateCSEMatchListItems (items , d .Id ())
@@ -259,14 +255,41 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
259255 }
260256 }
261257 }
262- // Calling Sleep method, adding items might take a while before items retrieved in next section
263- // Need to find a better way since feels super hacky
264- time .Sleep (15 * time .Second )
258+ } else {
259+ var CSEMatchListItems * CSEMatchListItemsInMatchListGet
260+
261+ CSEMatchListItems , err2 := c .GetCSEMatchListItemsInMatchList (d .Id ())
262+ if err2 != nil {
263+ log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , d .Id (), err2 )
264+ }
265+ if CSEMatchListItems != nil {
266+
267+ for _ , t := range CSEMatchListItems .CSEMatchListItemsGetObjects {
268+ err3 := c .DeleteCSEMatchListItem (t .ID )
269+ if err3 != nil {
270+ log .Printf ("[WARN] An error occurred deleting match list item with id: %s, err: %v" , t .ID , err3 )
271+ }
272+ }
273+ }
265274 }
266275
276+ // Calling Sleep method, adding/deleting items might take a while before items retrieved in next section
277+ // Need to find a better way since feels super hacky
278+ time .Sleep (15 * time .Second )
279+
267280 return resourceSumologicCSEMatchListRead (d , meta )
268281}
269282
283+ func contains (slice []string , item string ) bool {
284+ set := make (map [string ]struct {}, len (slice ))
285+ for _ , s := range slice {
286+ set [s ] = struct {}{}
287+ }
288+
289+ _ , ok := set [item ]
290+ return ok
291+ }
292+
270293func resourceToCSEMatchList (d * schema.ResourceData ) (CSEMatchListPost , error ) {
271294 id := d .Id ()
272295 if id == "" {
0 commit comments