Skip to content

Commit 2fe64ca

Browse files
author
Pedro Montiel
committed
StateChangeConf instead of time.Sleep
1 parent ae7e712 commit 2fe64ca

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

sumologic/resource_sumologic_cse_match_list.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sumologic
22

33
import (
4+
"fmt"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
46
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
57
"log"
68
"time"
@@ -185,10 +187,28 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
185187
log.Printf("[WARN] An error occurred while adding match list items to match list id: %s, err: %v", id, err2)
186188
}
187189

188-
// Calling Sleep method, adding items might take a while before items retrieved in next section
189-
// Need to find a better way since feels super hacky
190-
time.Sleep(15 * time.Second)
190+
}
191191

192+
createStateConf := &resource.StateChangeConf{
193+
Target: []string{
194+
fmt.Sprint(len(items)),
195+
},
196+
Refresh: func() (interface{}, string, error) {
197+
resp, err := c.GetCSEMatchListItemsInMatchList(d.Id())
198+
if err != nil {
199+
return 0, "", err
200+
}
201+
return resp, fmt.Sprint(len(resp.CSEMatchListItemsGetObjects)), nil
202+
},
203+
Timeout: d.Timeout(schema.TimeoutCreate),
204+
Delay: 10 * time.Second,
205+
MinTimeout: 5 * time.Second,
206+
ContinuousTargetOccurence: 1,
207+
}
208+
209+
_, err = createStateConf.WaitForState()
210+
if err != nil {
211+
return fmt.Errorf("error waiting for match list (%s) to be created: %s", d.Id(), err)
192212
}
193213

194214
}
@@ -259,9 +279,27 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
259279
}
260280
}
261281

262-
// Calling Sleep method, adding/deleting 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)
282+
createStateConf := &resource.StateChangeConf{
283+
Target: []string{
284+
fmt.Sprint(len(items)),
285+
},
286+
Refresh: func() (interface{}, string, error) {
287+
resp, err := c.GetCSEMatchListItemsInMatchList(d.Id())
288+
if err != nil {
289+
return 0, "", err
290+
}
291+
return resp, fmt.Sprint(len(resp.CSEMatchListItemsGetObjects)), nil
292+
},
293+
Timeout: d.Timeout(schema.TimeoutUpdate),
294+
Delay: 10 * time.Second,
295+
MinTimeout: 5 * time.Second,
296+
ContinuousTargetOccurence: 1,
297+
}
298+
299+
_, err = createStateConf.WaitForState()
300+
if err != nil {
301+
return fmt.Errorf("error waiting for match list (%s) to be updated: %s", d.Id(), err)
302+
}
265303

266304
return resourceSumologicCSEMatchListRead(d, meta)
267305
}

0 commit comments

Comments
 (0)