|
1 | 1 | package sumologic |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
4 | 6 | "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
5 | 7 | "log" |
6 | 8 | "time" |
@@ -185,10 +187,28 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{ |
185 | 187 | log.Printf("[WARN] An error occurred while adding match list items to match list id: %s, err: %v", id, err2) |
186 | 188 | } |
187 | 189 |
|
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 | + } |
191 | 191 |
|
| 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) |
192 | 212 | } |
193 | 213 |
|
194 | 214 | } |
@@ -259,9 +279,27 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{ |
259 | 279 | } |
260 | 280 | } |
261 | 281 |
|
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 | + } |
265 | 303 |
|
266 | 304 | return resourceSumologicCSEMatchListRead(d, meta) |
267 | 305 | } |
|
0 commit comments