@@ -2,10 +2,11 @@ package sumologic
22
33import (
44 "fmt"
5- "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
6- "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
75 "log"
86 "time"
7+
8+ "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+ "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
910)
1011
1112func resourceSumologicCSEMatchList () * schema.Resource {
@@ -56,7 +57,7 @@ func resourceSumologicCSEMatchList() *schema.Resource {
5657 Computed : true ,
5758 },
5859 "items" : {
59- Type : schema .TypeList ,
60+ Type : schema .TypeSet ,
6061 Optional : true ,
6162 Elem : & schema.Resource {
6263 Schema : map [string ]* schema.Schema {
@@ -114,13 +115,11 @@ func resourceSumologicCSEMatchListRead(d *schema.ResourceData, meta interface{})
114115 d .Set ("last_updated" , CSEMatchList .LastUpdated )
115116 d .Set ("last_updated_by" , CSEMatchList .LastUpdatedBy )
116117
117- //items
118- var CSEMatchListItems * CSEMatchListItemsInMatchListGet
119-
120- CSEMatchListItems , err2 := c .GetCSEMatchListItemsInMatchList (id )
121- if err2 != nil {
122- log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , id , err2 )
118+ CSEMatchListItems , err := c .GetCSEMatchListItemsInMatchList (id )
119+ if err != nil {
120+ log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , id , err )
123121 }
122+
124123 if CSEMatchListItems == nil {
125124 d .Set ("items" , nil )
126125 } else {
@@ -152,7 +151,6 @@ func resourceSumologicCSEMatchListDelete(d *schema.ResourceData, meta interface{
152151 c := meta .(* Client )
153152 err := c .DeleteCSEMatchList (d .Id ())
154153 return err
155-
156154}
157155
158156func resourceSumologicCSEMatchListCreate (d * schema.ResourceData , meta interface {}) error {
@@ -172,19 +170,17 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
172170 }
173171 d .SetId (id )
174172
175- //Match list items
176- itemsData := d .Get ("items" ).([]interface {})
173+ itemsData := d .Get ("items" ).(* schema.Set ).List ()
177174 var items []CSEMatchListItemPost
178175 for _ , data := range itemsData {
179- item , _ := resourceToCSEMatchListItem ([]interface {}{data })
176+ item := resourceToCSEMatchListItem ([]interface {}{data })
180177 items = append (items , item )
181-
182178 }
183179
184180 if len (items ) > 0 {
185- err2 : = c .CreateCSEMatchListItems (items , id )
186- if err2 != nil {
187- log .Printf ("[WARN] An error occurred while adding match list items to match list id: %s, err: %v" , id , err2 )
181+ err = c .CreateCSEMatchListItems (items , id )
182+ if err != nil {
183+ log .Printf ("[WARN] An error occurred while adding match list items to match list id: %s, err: %v" , id , err )
188184 }
189185
190186 }
@@ -196,6 +192,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
196192 Refresh : func () (interface {}, string , error ) {
197193 resp , err := c .GetCSEMatchListItemsInMatchList (d .Id ())
198194 if err != nil {
195+ log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , d .Id (), err )
199196 return 0 , "" , err
200197 }
201198 return resp , fmt .Sprint (resp .Total ), nil
@@ -216,7 +213,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
216213 return resourceSumologicCSEMatchListRead (d , meta )
217214}
218215
219- func resourceToCSEMatchListItem (data interface {}) ( CSEMatchListItemPost , string ) {
216+ func resourceToCSEMatchListItem (data interface {}) CSEMatchListItemPost {
220217 itemsSlice := data .([]interface {})
221218 item := CSEMatchListItemPost {}
222219 if len (itemsSlice ) > 0 {
@@ -227,7 +224,7 @@ func resourceToCSEMatchListItem(data interface{}) (CSEMatchListItemPost, string)
227224 item .Expiration = itemObj ["expiration" ].(string )
228225 item .Value = itemObj ["value" ].(string )
229226 }
230- return item , item . ID
227+ return item
231228}
232229
233230func resourceSumologicCSEMatchListUpdate (d * schema.ResourceData , meta interface {}) error {
@@ -241,51 +238,83 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
241238 return err
242239 }
243240
244- //Match list items
245- itemsData := d .Get ("items" ).([]interface {})
246- var itemIds []string
247- var items []CSEMatchListItemPost
241+ itemsData := d .Get ("items" ).(* schema.Set ).List ()
242+ var newItems []CSEMatchListItemPost
248243 for _ , data := range itemsData {
249- item , id := resourceToCSEMatchListItem ([]interface {}{data })
250- item .ID = ""
251- items = append (items , item )
252- itemIds = append (itemIds , id )
244+ item := resourceToCSEMatchListItem ([]interface {}{data })
245+ newItems = append (newItems , item )
246+ }
253247
248+ CSEMatchListItems , err := c .GetCSEMatchListItemsInMatchList (d .Id ())
249+ if err != nil {
250+ log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , d .Id (), err )
254251 }
252+ var oldItemIds []string
253+ for _ , item := range CSEMatchListItems .CSEMatchListItemsGetObjects {
254+ oldItemIds = append (oldItemIds , item .ID )
255+ }
256+
257+ oldItemCount := CSEMatchListItems .Total
258+ newItemCount := len (itemsData ) + oldItemCount
255259
256- if len (items ) > 0 {
257- err2 := c .CreateCSEMatchListItems (items , d .Id ())
258- if err2 != nil {
259- log .Printf ("[WARN] An error occurred while adding match list items to match list id: %s, err: %v" , d .Id (), err2 )
260+ //Add new items
261+ if len (newItems ) > 0 {
262+ err = c .CreateCSEMatchListItems (newItems , d .Id ())
263+ if err != nil {
264+ log .Printf ("[WARN] An error occurred while adding match list items to match list id: %s, err: %v" , d .Id (), err )
260265 }
261266
262267 }
263268
264- var CSEMatchListItems * CSEMatchListItemsInMatchListGet
269+ CSEMatchListItems , err = c .GetCSEMatchListItemsInMatchList (d .Id ())
270+ if err != nil {
271+ log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , d .Id (), err )
272+ }
265273
266- CSEMatchListItems , err2 := c .GetCSEMatchListItemsInMatchList (d .Id ())
267- if err2 != nil {
268- log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , d .Id (), err2 )
274+ // Wait for addition to finish
275+ createStateConf := & resource.StateChangeConf {
276+ Target : []string {
277+ fmt .Sprint (newItemCount ),
278+ },
279+ Refresh : func () (interface {}, string , error ) {
280+ CSEMatchListItems , err = c .GetCSEMatchListItemsInMatchList (d .Id ())
281+ if err != nil {
282+ log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , d .Id (), err )
283+ return 0 , "" , err
284+ }
285+
286+ return CSEMatchListItems , fmt .Sprint (CSEMatchListItems .Total ), nil
287+ },
288+ Timeout : d .Timeout (schema .TimeoutUpdate ),
289+ Delay : 10 * time .Second ,
290+ MinTimeout : 5 * time .Second ,
291+ ContinuousTargetOccurence : 1 ,
269292 }
270- if CSEMatchListItems != nil {
271293
272- for _ , t := range CSEMatchListItems .CSEMatchListItemsGetObjects {
273- if ! contains (itemIds , t .ID ) {
274- err3 := c .DeleteCSEMatchListItem (t .ID )
275- if err3 != nil {
276- log .Printf ("[WARN] An error occurred deleting match list item with id: %s, err: %v" , t .ID , err3 )
277- }
294+ _ , err = createStateConf .WaitForState ()
295+ if err != nil {
296+ return fmt .Errorf ("error waiting for match list (%s) to be updated: %s" , d .Id (), err )
297+ }
298+
299+ // Delete old items
300+ for _ , t := range CSEMatchListItems .CSEMatchListItemsGetObjects {
301+ if contains (oldItemIds , t .ID ) {
302+ err = c .DeleteCSEMatchListItem (t .ID )
303+ if err != nil {
304+ log .Printf ("[WARN] An error occurred deleting match list item with id: %s, err: %v" , t .ID , err )
278305 }
279306 }
280307 }
281308
282- createStateConf := & resource.StateChangeConf {
309+ // Wait for deletion to finish
310+ createStateConf = & resource.StateChangeConf {
283311 Target : []string {
284- fmt .Sprint (len (items )),
312+ fmt .Sprint (len (newItems )),
285313 },
286314 Refresh : func () (interface {}, string , error ) {
287315 resp , err := c .GetCSEMatchListItemsInMatchList (d .Id ())
288316 if err != nil {
317+ log .Printf ("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v" , d .Id (), err )
289318 return 0 , "" , err
290319 }
291320 return resp , fmt .Sprint (resp .Total ), nil
0 commit comments