Skip to content

Commit 6d4bc25

Browse files
committed
INVS-1408: more testing changes
1 parent 74a4a5d commit 6d4bc25

File tree

3 files changed

+51
-79
lines changed

3 files changed

+51
-79
lines changed

sumologic/resource_sumologic_cse_match_list.go

Lines changed: 14 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,76 +3,12 @@ package sumologic
33
import (
44
"fmt"
55
"log"
6-
"reflect"
7-
"strings"
86
"time"
97

108
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
119
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1210
)
1311

14-
func MatchListItemsDiffSuppressFunc(key, oldValue, newValue string, d *schema.ResourceData) bool {
15-
// Suppresses the diff shown if the items are the same but in different order
16-
17-
// The key is either the list itself (e.g "items") or a path to the list element (e.g. "items.0")
18-
lastDotIndex := strings.LastIndex(key, ".")
19-
if lastDotIndex != -1 {
20-
key = string(key[:lastDotIndex])
21-
}
22-
23-
oldData, newData := d.GetChange(key)
24-
25-
// Case where two individual items are being compared
26-
if key != "items" {
27-
oldMap := oldData.(map[string]interface{})
28-
newMap := newData.(map[string]interface{})
29-
return reflect.DeepEqual(oldMap, newMap)
30-
}
31-
32-
// Check if lists are null or different lengths
33-
if oldData == nil || newData == nil {
34-
return false
35-
}
36-
37-
println("key: "+key, fmt.Sprintf("%T", oldData), fmt.Sprintf("%T", newData))
38-
39-
oldArray := oldData.([]interface{})
40-
newArray := newData.([]interface{})
41-
42-
println("len oldArray: ", len(oldArray), " len newArray:", len(newArray))
43-
44-
if len(oldArray) != len(newArray) {
45-
return false
46-
}
47-
48-
// Check if the new array contains each map in the old array
49-
for i := 0; i < len(oldArray); i++ {
50-
newArrayContainsOldMap := false
51-
52-
println(fmt.Sprintf("%T", oldArray[i]))
53-
54-
oldMap := oldArray[i].(map[string]interface{})
55-
56-
println(fmt.Sprintf("oldmap %#v", oldMap))
57-
58-
for j := 0; j < len(newArray); j++ {
59-
60-
newMap := newArray[j].(map[string]interface{})
61-
if reflect.DeepEqual(oldMap, newMap) {
62-
println(fmt.Sprintf("newmap match %#v", newMap))
63-
64-
newArrayContainsOldMap = true
65-
break
66-
}
67-
}
68-
69-
if !newArrayContainsOldMap {
70-
return false
71-
}
72-
}
73-
return true
74-
}
75-
7612
func resourceSumologicCSEMatchList() *schema.Resource {
7713
return &schema.Resource{
7814
Create: resourceSumologicCSEMatchListCreate,
@@ -123,7 +59,6 @@ func resourceSumologicCSEMatchList() *schema.Resource {
12359
"items": {
12460
Type: schema.TypeSet,
12561
Optional: true,
126-
//DiffSuppressFunc: MatchListItemsDiffSuppressFunc,
12762
Elem: &schema.Resource{
12863
Schema: map[string]*schema.Schema{
12964
"id": {
@@ -215,13 +150,20 @@ func setItems(d *schema.ResourceData, items []CSEMatchListItemGet) {
215150
}
216151

217152
func resourceSumologicCSEMatchListDelete(d *schema.ResourceData, meta interface{}) error {
153+
println("delete: started resourceSumologicCSEMatchListDelete")
154+
218155
c := meta.(*Client)
219156
err := c.DeleteCSEMatchList(d.Id())
157+
158+
println("delete: finished resourceSumologicCSEMatchListDelete")
159+
220160
return err
221161

222162
}
223163

224164
func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{}) error {
165+
println("create: starting resourceSumologicCSEMatchListCreate")
166+
225167
c := meta.(*Client)
226168

227169
if d.Id() == "" {
@@ -279,7 +221,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
279221

280222
}
281223

282-
fmt.Println("finished creating match list")
224+
println("create: finished resourceSumologicCSEMatchListCreate")
283225

284226
return resourceSumologicCSEMatchListRead(d, meta)
285227
}
@@ -299,7 +241,7 @@ func resourceToCSEMatchListItem(data interface{}) (CSEMatchListItemPost, string)
299241
}
300242

301243
func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{}) error {
302-
println("in update")
244+
println("update: starting resourceSumologicCSEMatchListUpdate")
303245
CSEMatchListPost, err := resourceToCSEMatchList(d)
304246
if err != nil {
305247
return err
@@ -322,7 +264,7 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
322264
itemIds = append(itemIds, id)
323265
}
324266

325-
println(itemIds)
267+
println("update: creating new updated items")
326268

327269
if len(items) > 0 {
328270
err2 := c.CreateCSEMatchListItems(items, d.Id())
@@ -340,7 +282,7 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
340282
}
341283
if CSEMatchListItems != nil {
342284

343-
println("343")
285+
println(fmt.Sprintf("update: checking %d items for deletion", len(CSEMatchListItems.CSEMatchListItemsGetObjects)))
344286

345287
for _, t := range CSEMatchListItems.CSEMatchListItemsGetObjects {
346288
if !contains(itemIds, t.ID) {
@@ -352,7 +294,7 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
352294
}
353295
}
354296

355-
println("355")
297+
println("update: finished deleting, now getting items")
356298

357299
createStateConf := &resource.StateChangeConf{
358300
Target: []string{
@@ -373,12 +315,12 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
373315

374316
_, err = createStateConf.WaitForState()
375317

376-
println("376")
377-
378318
if err != nil {
379319
return fmt.Errorf("error waiting for match list (%s) to be updated: %s", d.Id(), err)
380320
}
381321

322+
println("update: finished resourceSumologicCSEMatchListUpdate")
323+
382324
return resourceSumologicCSEMatchListRead(d, meta)
383325
}
384326

sumologic/resource_sumologic_cse_match_list_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ func TestAccSumologicSCEMatchList_createAndUpdate(t *testing.T) {
3232
CheckDestroy: testAccCSEMatchListDestroy,
3333
Steps: []resource.TestStep{
3434
{
35-
Config: testCreateCSEMatchListConfig(nDefaultTtl, nDescription, nName, nTargetColumn, liDescription, liExpiration, liValue, 51),
35+
Config: testCreateCSEMatchListConfig(nDefaultTtl, nDescription, nName, nTargetColumn, liDescription, liExpiration, liValue, 1000),
3636
Check: resource.ComposeTestCheckFunc(
3737
testCheckCSEMatchListExists(resourceName, &matchList),
3838
testCheckMatchListValues(&matchList, nDefaultTtl, nDescription, nName, nTargetColumn),
3939
resource.TestCheckResourceAttrSet(resourceName, "id"),
4040
),
4141
},
4242
{
43-
Config: testCreateCSEMatchListConfig(uDefaultTtl, uDescription, nName, nTargetColumn, uliDescription, liExpiration, liValue, 51),
43+
Config: testCreateCSEMatchListConfig(uDefaultTtl, uDescription, nName, nTargetColumn, uliDescription, liExpiration, liValue, 1000),
4444
Check: resource.ComposeTestCheckFunc(
4545
testCheckCSEMatchListExists(resourceName, &matchList),
4646
testCheckMatchListValues(&matchList, uDefaultTtl, uDescription, nName, nTargetColumn),
@@ -87,10 +87,10 @@ func testCreateCSEMatchListConfig(nDefaultTtl int, nDescription string, nName st
8787

8888
itemsStr += fmt.Sprintf(`
8989
items {
90-
description = "%s %d"
90+
description = "%s %d %s"
9191
expiration = "%s"
9292
value = "%s %d %s"
93-
}`, liDescription, i, liExpiration, liValue, i, id)
93+
}`, liDescription, i, id, liExpiration, liValue, i, id)
9494
}
9595

9696
var str = fmt.Sprintf(`

sumologic/sumologic_cse_match_list_item.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
)
77

8+
var limit = 1000
9+
810
func (s *Client) GetCSEMatchListItem(id string) (*CSEMatchListItemGet, error) {
911
data, _, err := s.Get(fmt.Sprintf("sec/v1/match-list-items/%s", id))
1012
if err != nil {
@@ -24,8 +26,8 @@ func (s *Client) GetCSEMatchListItem(id string) (*CSEMatchListItemGet, error) {
2426
return &response.CSEMatchListItemGet, nil
2527
}
2628

27-
func (s *Client) GetCSEMatchListItemsInMatchList(MatchListId string) (*CSEMatchListItemsInMatchListGet, error) {
28-
data, _, err := s.Get(fmt.Sprintf("sec/v1/match-list-items?listIds=%s", MatchListId))
29+
func (s *Client) SendGetCSEMatchListItemsRequest(MatchListId string, offset int) (*CSEMatchListItemsInMatchListGet, error) {
30+
data, _, err := s.Get(fmt.Sprintf("sec/v1/match-list-items?listIds=%s&limit=%d&offset=%d", MatchListId, limit, offset))
2931
if err != nil {
3032
return nil, err
3133
}
@@ -43,6 +45,34 @@ func (s *Client) GetCSEMatchListItemsInMatchList(MatchListId string) (*CSEMatchL
4345
return &response.CSEMatchListItemsGetData, nil
4446
}
4547

48+
func (s *Client) GetCSEMatchListItemsInMatchList(MatchListId string) (*CSEMatchListItemsInMatchListGet, error) {
49+
offset := 0
50+
response, err := s.SendGetCSEMatchListItemsRequest(MatchListId, offset)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
println(fmt.Sprintf("get: GetCSEMatchListItemsInMatchList response total: %d", response.Total))
56+
57+
// When the match list has over 1000 items, fetch items from the remaining pages
58+
for offset = limit; offset < response.Total; offset += limit {
59+
println(fmt.Sprintf("Checking next page from %d to %d", offset, offset+1000))
60+
61+
nextPageResponse, err := s.SendGetCSEMatchListItemsRequest(MatchListId, offset)
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
println(fmt.Sprintf("Next page has %d items", len(nextPageResponse.CSEMatchListItemsGetObjects)))
67+
68+
for i := 0; i < len(nextPageResponse.CSEMatchListItemsGetObjects); i++ {
69+
response.CSEMatchListItemsGetObjects = append(response.CSEMatchListItemsGetObjects, nextPageResponse.CSEMatchListItemsGetObjects[i])
70+
}
71+
}
72+
73+
return response, nil
74+
}
75+
4676
func (s *Client) DeleteCSEMatchListItem(id string) error {
4777
_, err := s.Delete(fmt.Sprintf("sec/v1/match-list-items/%s", id))
4878

@@ -61,7 +91,7 @@ func (s *Client) SendCreateCSEMatchListItemsRequest(CSEMatchListItemPost []CSEMa
6191
return err
6292
}
6393

64-
fmt.Printf("create request for %d items was sent\n", len(CSEMatchListItemPost))
94+
fmt.Printf("SendCreateCSEMatchListItemsRequest: sent request for %d items\n", len(CSEMatchListItemPost))
6595

6696
err = json.Unmarshal(responseBody, &response)
6797

0 commit comments

Comments
 (0)