Skip to content

Commit e0b81ad

Browse files
Merge pull request #640 from SumoLogic/INVS-2114-fix-ml-items
INVS-2114: Fix bad request causing timeouts when updating match list items
2 parents a532545 + 0de9364 commit e0b81ad

File tree

3 files changed

+77
-40
lines changed

3 files changed

+77
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ ENHANCEMENTS:
55

66
BUG FIXES:
77
* Fix error while importing monitor having timeZone as `null` in the Email notification object. (GH-637)
8-
* Fix perpetual diff in Extraction Rules by normalizing the parse expression.
8+
* Fix perpetual diff in Extraction Rules by normalizing the parse expression.
9+
* Fix `resource_sumologic_cse_match_list` timing out when updating match list items (GH-640)
910

1011
## 2.28.3 (March 5, 2024)
1112

sumologic/resource_sumologic_cse_match_list.go

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
171171
})
172172

173173
if err != nil {
174-
return err
174+
return fmt.Errorf("[ERROR] An error occurred converting resource to match list with id %s, err: %v", d.Id(), err)
175175
}
176176
d.SetId(id)
177177

@@ -185,9 +185,8 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
185185
if len(items) > 0 {
186186
err = c.CreateCSEMatchListItems(items, id)
187187
if err != nil {
188-
log.Printf("[WARN] An error occurred while adding match list items to match list id: %s, err: %v", id, err)
188+
return fmt.Errorf("[ERROR] An error occurred while adding match list items to match list with id %s, err: %v", id, err)
189189
}
190-
191190
}
192191

193192
createStateConf := &resource.StateChangeConf{
@@ -197,7 +196,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
197196
Refresh: func() (interface{}, string, error) {
198197
resp, err := c.GetCSEMatchListItemsInMatchList(d.Id())
199198
if err != nil {
200-
log.Printf("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v", d.Id(), err)
199+
log.Printf("[ERROR] CSE Match List items not found when looking by match list id %s, err: %v", d.Id(), err)
201200
return 0, "", err
202201
}
203202
return resp, fmt.Sprint(resp.Total), nil
@@ -210,7 +209,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
210209

211210
_, err = createStateConf.WaitForState()
212211
if err != nil {
213-
return fmt.Errorf("error waiting for match list (%s) to be created: %s", d.Id(), err)
212+
return fmt.Errorf("[ERROR] error waiting for match list with id %s to be created: %s", d.Id(), err)
214213
}
215214

216215
}
@@ -235,63 +234,88 @@ func resourceToCSEMatchListItem(data interface{}) CSEMatchListItemPost {
235234
func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{}) error {
236235
CSEMatchListPost, err := resourceToCSEMatchList(d)
237236
if err != nil {
238-
return err
237+
return fmt.Errorf("[ERROR] An error occurred converting resource to match list with id %s, err: %v", d.Id(), err)
239238
}
240239

241240
c := meta.(*Client)
242241
if err = c.UpdateCSEMatchList(CSEMatchListPost); err != nil {
243-
return err
242+
return fmt.Errorf("[ERROR] An error occurred updating match list with id %s, err: %v", d.Id(), err)
244243
}
245244

246245
itemsData := d.Get("items").(*schema.Set).List()
247246
var newItems []CSEMatchListItemPost
247+
var newItemIds []string
248248
for _, data := range itemsData {
249249
item := resourceToCSEMatchListItem([]interface{}{data})
250250
newItems = append(newItems, item)
251+
newItemIds = append(newItemIds, item.ID)
251252
}
252253

253254
CSEMatchListItems, err := c.GetCSEMatchListItemsInMatchList(d.Id())
254255
if err != nil {
255-
log.Printf("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v", d.Id(), err)
256+
return fmt.Errorf("[ERROR] CSE Match List items not found when looking by match list id %s, err: %v", d.Id(), err)
256257
}
257-
var oldItemIds []string
258+
259+
var deleteItemIds []string
260+
var updateItemIds []string
261+
262+
// Compare currently existing match list items with the new items to determine if they should be deleted or updated
258263
for _, item := range CSEMatchListItems.CSEMatchListItemsGetObjects {
259-
oldItemIds = append(oldItemIds, item.ID)
264+
var oldItemId = item.ID
265+
if contains(newItemIds, oldItemId) {
266+
updateItemIds = append(updateItemIds, oldItemId)
267+
} else {
268+
deleteItemIds = append(deleteItemIds, oldItemId)
269+
}
270+
}
271+
272+
var updateItems []CSEMatchListItemPost
273+
var addItems []CSEMatchListItemPost
274+
275+
// Any new items that are not updates to existing items should be added instead
276+
for _, newItem := range newItems {
277+
if contains(updateItemIds, newItem.ID) {
278+
updateItems = append(updateItems, newItem)
279+
} else {
280+
addItems = append(addItems, newItem)
281+
}
260282
}
261283

262284
// Delete old items
263-
for _, t := range CSEMatchListItems.CSEMatchListItemsGetObjects {
264-
if contains(oldItemIds, t.ID) {
265-
err = c.DeleteCSEMatchListItem(t.ID)
285+
for _, oldItem := range CSEMatchListItems.CSEMatchListItemsGetObjects {
286+
if contains(deleteItemIds, oldItem.ID) {
287+
err = c.DeleteCSEMatchListItem(oldItem.ID)
266288
if err != nil {
267-
log.Printf("[WARN] An error occurred deleting match list item with id: %s, err: %v", t.ID, err)
289+
return fmt.Errorf("[ERROR] An error occurred while deleting match list item with id %s, err: %v", oldItem.ID, err)
268290
}
269291
}
270292
}
271293

272-
//Add new items
273-
if len(newItems) > 0 {
274-
err = c.CreateCSEMatchListItems(newItems, d.Id())
294+
// Update old items with new items
295+
for _, updateItem := range updateItems {
296+
err = c.UpdateCSEMatchListItem(updateItem)
275297
if err != nil {
276-
log.Printf("[WARN] An error occurred while adding match list items to match list id: %s, err: %v", d.Id(), err)
298+
return fmt.Errorf("[ERROR] An error occurred while updating match list item with id %s, err: %v", updateItem.ID, err)
277299
}
278-
279300
}
280301

281-
CSEMatchListItems, err = c.GetCSEMatchListItemsInMatchList(d.Id())
282-
if err != nil {
283-
log.Printf("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v", d.Id(), err)
302+
//Add new items
303+
if len(addItems) > 0 {
304+
err = c.CreateCSEMatchListItems(addItems, d.Id())
305+
if err != nil {
306+
return fmt.Errorf("[ERROR] An error occurred while adding match list items to match list with id %s, err: %v", d.Id(), err)
307+
}
284308
}
285309

286310
// Wait for update to finish
287-
createStateConf := &resource.StateChangeConf{
311+
updateStateConf := &resource.StateChangeConf{
288312
Target: []string{
289313
fmt.Sprint(len(newItems)),
290314
},
291315
Refresh: func() (interface{}, string, error) {
292316
resp, err := c.GetCSEMatchListItemsInMatchList(d.Id())
293317
if err != nil {
294-
log.Printf("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v", d.Id(), err)
318+
log.Printf("[ERROR] CSE Match List items not found when looking by match list id %s, err: %v", d.Id(), err)
295319
return 0, "", err
296320
}
297321
return resp, fmt.Sprint(resp.Total), nil
@@ -302,9 +326,9 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
302326
ContinuousTargetOccurence: 1,
303327
}
304328

305-
_, err = createStateConf.WaitForState()
329+
_, err = updateStateConf.WaitForState()
306330
if err != nil {
307-
return fmt.Errorf("error waiting for match list (%s) to be updated: %s", d.Id(), err)
331+
return fmt.Errorf("[ERROR] Error waiting for match list with id %s to be updated: %s", d.Id(), err)
308332
}
309333

310334
return resourceSumologicCSEMatchListRead(d, meta)

sumologic/resource_sumologic_cse_match_list_test.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestAccSumologicSCEMatchList_createAndUpdate(t *testing.T) {
2424
liDescription := "Match List Item Description"
2525
liExpiration := "2122-02-27T04:00:00"
2626
liValue := "value"
27-
liCount := 1
27+
liCount := 2
2828

2929
// Update values
3030
uDefaultTtl := 3600
@@ -39,6 +39,7 @@ func TestAccSumologicSCEMatchList_createAndUpdate(t *testing.T) {
3939
Providers: testAccProviders,
4040
CheckDestroy: testAccCSEMatchListDestroy,
4141
Steps: []resource.TestStep{
42+
// Creates a match list with 2 match list items
4243
{
4344
Config: testCreateCSEMatchListConfig(nDefaultTtl, nDescription, nName, nTargetColumn, liDescription, liExpiration, liValue, liCount),
4445
Check: resource.ComposeTestCheckFunc(
@@ -48,6 +49,17 @@ func TestAccSumologicSCEMatchList_createAndUpdate(t *testing.T) {
4849
resource.TestCheckResourceAttrSet(resourceName, "id"),
4950
),
5051
},
52+
// Updates the match list and its 2 match list items
53+
{
54+
Config: testCreateCSEMatchListConfig(uDefaultTtl, uDescription, nName, nTargetColumn, liDescription, liExpiration, liValue, liCount),
55+
Check: resource.ComposeTestCheckFunc(
56+
testCheckCSEMatchListExists(resourceName, &matchList),
57+
testCheckMatchListValues(&matchList, uDefaultTtl, uDescription, nName, nTargetColumn),
58+
testCheckMatchListItemsValuesAndCount(resourceName, liDescription, liExpiration, liValue, liCount),
59+
resource.TestCheckResourceAttrSet(resourceName, "id"),
60+
),
61+
},
62+
// Deletes the 2 old match list items and adds 3 new ones
5163
{
5264
Config: testCreateCSEMatchListConfig(uDefaultTtl, uDescription, nName, nTargetColumn, uliDescription, uliExpiration, uliValue, uliCount),
5365
Check: resource.ComposeTestCheckFunc(
@@ -56,6 +68,7 @@ func TestAccSumologicSCEMatchList_createAndUpdate(t *testing.T) {
5668
testCheckMatchListItemsValuesAndCount(resourceName, uliDescription, uliExpiration, uliValue, uliCount),
5769
),
5870
},
71+
// Deletes all the match list items
5972
{
6073
Config: testDeleteCSEMatchListItemConfig(uDefaultTtl, uDescription, nName, nTargetColumn),
6174
Check: resource.ComposeTestCheckFunc(
@@ -93,14 +106,12 @@ func testCreateCSEMatchListConfig(nDefaultTtl int, nDescription string, nName st
93106
var itemsStr = ""
94107

95108
for i := 0; i < numItems; i++ {
96-
id := uuid.New()
97-
98109
itemsStr += fmt.Sprintf(`
99110
items {
100-
description = "%s %d %s"
111+
description = "%s %d"
101112
expiration = "%s"
102-
value = "%s %d %s"
103-
}`, liDescription, i, id, liExpiration, liValue, i, id)
113+
value = "%s %d"
114+
}`, liDescription, i, liExpiration, liValue, i)
104115
}
105116

106117
var str = fmt.Sprintf(`
@@ -151,16 +162,16 @@ func testCheckCSEMatchListExists(n string, matchList *CSEMatchListGet) resource.
151162
func testCheckMatchListValues(matchList *CSEMatchListGet, nDefaultTtl int, nDescription string, nName string, nTargetColumn string) resource.TestCheckFunc {
152163
return func(s *terraform.State) error {
153164
if matchList.DefaultTtl != nDefaultTtl {
154-
return fmt.Errorf("bad default ttl, expected \"%s\", got: %#v", nName, matchList.Name)
165+
return fmt.Errorf("bad default ttl, expected \"%d\", got: \"%d\"", nDefaultTtl, matchList.DefaultTtl)
155166
}
156167
if matchList.Description != nDescription {
157-
return fmt.Errorf("bad description, expected \"%s\", got: %#v", nDescription, matchList.Description)
168+
return fmt.Errorf("bad description, expected \"%s\", got: \"%s\"", nDescription, matchList.Description)
158169
}
159170
if matchList.Name != nName {
160-
return fmt.Errorf("bad name, expected \"%s\", got: %#v", nName, matchList.Name)
171+
return fmt.Errorf("bad name, expected \"%s\", got: \"%s\"", nName, matchList.Name)
161172
}
162173
if matchList.TargetColumn != nTargetColumn {
163-
return fmt.Errorf("bad target column, expected \"%s\", got: %#v", nName, matchList.Name)
174+
return fmt.Errorf("bad target column, expected \"%s\", got: \"%s\"", nName, matchList.Name)
164175
}
165176

166177
return nil
@@ -181,7 +192,7 @@ func testCheckMatchListItemsValuesAndCount(resourceName string, expectedDescript
181192
c := testAccProvider.Meta().(*Client)
182193
matchListResp, err := c.GetCSEMatchListItemsInMatchList(rs.Primary.ID)
183194
if err != nil {
184-
return err
195+
return fmt.Errorf("could not get match list items by match list id %s", rs.Primary.ID)
185196
}
186197

187198
actualCount := len(matchListResp.CSEMatchListItemsGetObjects)
@@ -200,13 +211,14 @@ func testCheckMatchListItemsValuesAndCount(resourceName string, expectedDescript
200211
if !strings.Contains(item.Meta.Description, expectedDescription) {
201212
return fmt.Errorf("expected match list item description to contain \"%s\", but found \"%s\" instead", expectedDescription, item.Meta.Description)
202213
}
203-
if item.Expiration != expectedExpiration {
204-
return fmt.Errorf("expected expiration to be \"%s\", but found \"%s\" instead", expectedExpiration, item.Expiration)
214+
if !strings.Contains(item.Expiration, expectedExpiration) {
215+
return fmt.Errorf("expected expiration to contain \"%s\", but found \"%s\" instead", expectedExpiration, item.Expiration)
205216
}
206217
if !strings.Contains(item.Value, expectedValue) {
207218
return fmt.Errorf("expected match list item value to contain \"%s\", but found \"%s\" instead", expectedValue, item.Value)
208219
}
209220
}
221+
210222
return nil
211223
}
212224
}

0 commit comments

Comments
 (0)