Skip to content

Commit 69aabf2

Browse files
authored
Merge pull request #536 from SumoLogic/INVS-1408-match-lists
INVS-1408: resource_sumologic_cse_match_list bug fixes
2 parents b4f1ccb + 05e5a2d commit 69aabf2

File tree

6 files changed

+216
-92
lines changed

6 files changed

+216
-92
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ FEATURES:
55

66
BUG FIXES:
77
* Enforce validation of `group_by_fields` in cse_*_rule resources, on non empty string elements. (GH-535)
8+
* Fixes `resource_sumologic_cse_match_list` to allow for match lists with more than 1000 items to be created. (INVS-1408)
89

910
## 2.23.0 (May 24, 2023)
1011
FEATURES:

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,20 @@ Then run `terraform init` to initialize it.
5353

5454
In order to run the full suite of Acceptance tests, run `make testacc`.
5555

56+
To run a specific acceptance test, run `go test -v ./sumologic -run YourSpecificTestName`
57+
5658
*Note:*
57-
- Acceptance tests *create real resources*, and often cost money to run. The environment variables `SUMOLOGIC_ACCESSID`, `SUMOLOGIC_ACCESSKEY`, and `SUMOLOGIC_ENVIRONMENT` must also be set for acceptance tests to work properly.
59+
- Acceptance tests *create real resources*, and often cost money to run. The environment variables `SUMOLOGIC_ACCESSID`, `SUMOLOGIC_ACCESSKEY`, `SUMOLOGIC_ENVIRONMENT` / `SUMOLOGIC_BASE_URL`, and `TF_ACC` must also be set for acceptance tests to work properly.
60+
- For example, you can generate a personal access key in stag-alpha [here](https://cse-stag-alpha.stag.sumologic.net/ui/#/preferences). Once your test runs, you are then capable of viewing the real resources created by Terraform in the [stag-alpha UI](https://cse-stag-alpha.stag.sumologic.net/sec/hud?hours=24).
61+
```sh
62+
$ export SUMOLOGIC_ACCESSID="yourAccessID"
63+
$ export SUMOLOGIC_ACCESSKEY="yourAccessKey"
64+
$ export SUMOLOGIC_ENVIRONMENT="stag"
65+
$ export SUMOLOGIC_BASE_URL="https://stag-api.sumologic.net/api/"
66+
$ export TF_ACC=1
67+
```
68+
- More information on configuration can be found at the [Terraform Provider codelabs documentation](https://github.com/Sanyaku/codelabs/blob/master/backend/pages/SumoLogicTerraformProvider.md).
69+
5870
- Environment variable `SUMOLOGIC_TEST_GOOGLE_APPLICATION_CREDENTIALS` must be set for gcp metrics acceptance tests to work properly (ex. below).
5971
- export SUMOLOGIC_TEST_GOOGLE_APPLICATION_CREDENTIALS=`cat /path/to/service_acccount.json`
6072
- Set Environment variable `SUMOLOGIC_ENABLE_GCP_METRICS_ACC_TESTS` to false, to disable acceptance test for Gcp Metrics.

sumologic/resource_sumologic_cse_match_list.go

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package sumologic
22

33
import (
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

1112
func 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

158156
func 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

233230
func 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

Comments
 (0)