Skip to content

Commit 5c65db0

Browse files
committed
INVS-1408 fix all bugs
1 parent 6d4bc25 commit 5c65db0

File tree

3 files changed

+137
-61
lines changed

3 files changed

+137
-61
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,19 @@ 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 single acceptance test, run `TF_ACC=1 go test -v ./sumologic -run TheNameOfYourTest`
56+
To run a specific acceptance test, run `go test -v ./sumologic -run TheSpecificTestName`
5757

5858
*Note:*
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.
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+
- More information on configuration can be found at the [Terraform Provider codelabs documentation](https://github.com/Sanyaku/codelabs/blob/master/backend/pages/SumoLogicTerraformProvider.md).
61+
- Stag example:
62+
```sh
63+
$ export SUMOLOGIC_ACCESSID="yourAccessID"
64+
$ export SUMOLOGIC_ACCESSKEY="yourAccessKey"
65+
$ export SUMOLOGIC_ENVIRONMENT="stag"
66+
$ export SUMOLOGIC_BASE_URL="https://stag-api.sumologic.net/api/"
67+
$ export TF_ACC=1
68+
```
6069
- Environment variable `SUMOLOGIC_TEST_GOOGLE_APPLICATION_CREDENTIALS` must be set for gcp metrics acceptance tests to work properly (ex. below).
6170
- export SUMOLOGIC_TEST_GOOGLE_APPLICATION_CREDENTIALS=`cat /path/to/service_acccount.json`
6271
- 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: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ func resourceSumologicCSEMatchList() *schema.Resource {
8787
}
8888
}
8989

90+
func getCSEMatchListItemsInMatchList(id string, meta interface{}) *CSEMatchListItemsInMatchListGet {
91+
c := meta.(*Client)
92+
93+
CSEMatchListItems, err := c.GetCSEMatchListItemsInMatchList(id)
94+
if err != nil {
95+
log.Printf("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v", id, err)
96+
}
97+
return CSEMatchListItems
98+
}
99+
90100
func resourceSumologicCSEMatchListRead(d *schema.ResourceData, meta interface{}) error {
91101
c := meta.(*Client)
92102

@@ -115,13 +125,8 @@ func resourceSumologicCSEMatchListRead(d *schema.ResourceData, meta interface{})
115125
d.Set("last_updated", CSEMatchList.LastUpdated)
116126
d.Set("last_updated_by", CSEMatchList.LastUpdatedBy)
117127

118-
//items
119-
var CSEMatchListItems *CSEMatchListItemsInMatchListGet
128+
CSEMatchListItems := getCSEMatchListItemsInMatchList(id, meta)
120129

121-
CSEMatchListItems, err2 := c.GetCSEMatchListItemsInMatchList(id)
122-
if err2 != nil {
123-
log.Printf("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v", id, err2)
124-
}
125130
if CSEMatchListItems == nil {
126131
d.Set("items", nil)
127132
} else {
@@ -185,7 +190,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
185190
itemsData := d.Get("items").(*schema.Set).List()
186191
var items []CSEMatchListItemPost
187192
for _, data := range itemsData {
188-
item, _ := resourceToCSEMatchListItem([]interface{}{data})
193+
item := resourceToCSEMatchListItem([]interface{}{data})
189194
items = append(items, item)
190195
}
191196

@@ -226,18 +231,29 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
226231
return resourceSumologicCSEMatchListRead(d, meta)
227232
}
228233

229-
func resourceToCSEMatchListItem(data interface{}) (CSEMatchListItemPost, string) {
234+
func resourceToCSEMatchListItem(data interface{}) CSEMatchListItemPost {
230235
itemsSlice := data.([]interface{})
231236
item := CSEMatchListItemPost{}
232237
if len(itemsSlice) > 0 {
233238
itemObj := itemsSlice[0].(map[string]interface{})
239+
// for _, map := range itemObj {
240+
// println("itemObj id: ", )
241+
// }
242+
// b, err := json.Marshal(itemObj)
243+
// if err != nil {
244+
// log.Fatal(err)
245+
// }
246+
// fmt.Println("b: ", string(b))
234247
item.ID = itemObj["id"].(string)
235248
item.Description = itemObj["description"].(string)
236249
item.Active = true
237250
item.Expiration = itemObj["expiration"].(string)
238251
item.Value = itemObj["value"].(string)
252+
// println("resourceToCSEMatchListItem itemid1: " + itemObj["id"].(string))
253+
// println("resourceToCSEMatchListItem itemid2: " + item.ID)
239254
}
240-
return item, item.ID
255+
//println("resourceToCSEMatchListItem returning id", item.ID)
256+
return item //, item.ID
241257
}
242258

243259
func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{}) error {
@@ -255,41 +271,62 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
255271
//Match list items
256272
//itemsData := d.Get("items").([]interface{})
257273
itemsData := d.Get("items").(*schema.Set).List()
258-
var itemIds []string
259274
var items []CSEMatchListItemPost
260275
for _, data := range itemsData {
261-
item, id := resourceToCSEMatchListItem([]interface{}{data})
262-
item.ID = ""
276+
//println("in itemsData, id is")
277+
item := resourceToCSEMatchListItem([]interface{}{data}) //TODO remove id return
278+
// item.ID = ""
263279
items = append(items, item)
264-
itemIds = append(itemIds, id)
280+
// println("appending id to itemsID: " + id)
281+
// itemIds = append(itemIds, id)
265282
}
266283

267-
println("update: creating new updated items")
284+
// itemIdsString := ""
285+
// for i := 0; i < len(itemIds); i++ {
286+
// itemIdsString += itemIds[i] + " "
287+
// }
288+
// println("update: itemIds are " + itemIdsString)
289+
290+
CSEMatchListItems := getCSEMatchListItemsInMatchList(d.Id(), meta)
291+
var itemIds []string
292+
for _, item := range CSEMatchListItems.CSEMatchListItemsGetObjects {
293+
//println("current id: ", item.ID)
294+
itemIds = append(itemIds, item.ID)
295+
}
296+
297+
currentItemCount := CSEMatchListItems.Total
298+
newItemCount := len(itemsData) + currentItemCount
299+
300+
println(fmt.Sprintf("current count %d, new count %d", currentItemCount, newItemCount))
268301

269302
if len(items) > 0 {
270-
err2 := c.CreateCSEMatchListItems(items, d.Id())
271-
if err2 != nil {
272-
log.Printf("[WARN] An error occurred while adding match list items to match list id: %s, err: %v", d.Id(), err2)
303+
err = c.CreateCSEMatchListItems(items, d.Id())
304+
if err != nil {
305+
log.Printf("[WARN] An error occurred while adding match list items to match list id: %s, err: %v", d.Id(), err)
273306
}
274307

275308
}
276309

277-
var CSEMatchListItems *CSEMatchListItemsInMatchListGet
310+
CSEMatchListItems = getCSEMatchListItemsInMatchList(d.Id(), meta)
278311

279-
CSEMatchListItems, err2 := c.GetCSEMatchListItemsInMatchList(d.Id())
280-
if err2 != nil {
281-
log.Printf("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v", d.Id(), err2)
312+
// Waits until all new items have finished being indexed in ES
313+
println(fmt.Sprintf("total %d, len %d, goal %d", CSEMatchListItems.Total, len(CSEMatchListItems.CSEMatchListItemsGetObjects), newItemCount))
314+
for CSEMatchListItems.Total < newItemCount {
315+
println(fmt.Sprintf("update: only got %d/%d items, sleeping for 3 seconds", CSEMatchListItems.Total, newItemCount))
316+
time.Sleep(3 * time.Second)
317+
CSEMatchListItems = getCSEMatchListItemsInMatchList(d.Id(), meta)
282318
}
283-
if CSEMatchListItems != nil {
284319

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

287-
for _, t := range CSEMatchListItems.CSEMatchListItemsGetObjects {
288-
if !contains(itemIds, t.ID) {
289-
err3 := c.DeleteCSEMatchListItem(t.ID)
290-
if err3 != nil {
291-
log.Printf("[WARN] An error occurred deleting match list item with id: %s, err: %v", t.ID, err3)
292-
}
322+
// Delete all the old items
323+
for _, t := range CSEMatchListItems.CSEMatchListItemsGetObjects {
324+
//println("update: checking id for deletion: " + t.ID)
325+
if contains(itemIds, t.ID) {
326+
//println("update: itemIds contains " + t.ID)
327+
err = c.DeleteCSEMatchListItem(t.ID)
328+
if err != nil {
329+
log.Printf("[WARN] An error occurred deleting match list item with id: %s, err: %v", t.ID, err)
293330
}
294331
}
295332
}

sumologic/resource_sumologic_cse_match_list_test.go

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sumologic
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/google/uuid"
@@ -14,42 +15,52 @@ func TestAccSumologicSCEMatchList_createAndUpdate(t *testing.T) {
1415
SkipCseTest(t)
1516

1617
var matchList CSEMatchListGet
18+
resourceName := "sumologic_cse_match_list.match_list"
19+
20+
// Create values
21+
nName := fmt.Sprintf("Terraform Test Match List %s", uuid.New())
1722
nDefaultTtl := 10800
18-
nDescription := "New Match List Description"
19-
nName := fmt.Sprintf("Match List Name %s", uuid.New())
23+
nDescription := "Match List Description"
2024
nTargetColumn := "SrcIp"
2125
liDescription := "Match List Item Description"
22-
liValue := "value"
2326
liExpiration := "2122-02-27T04:00:00"
27+
liValue := "value"
28+
liCount := 1001
29+
30+
// Update values
2431
uDefaultTtl := 3600
2532
uDescription := "Updated Match List Description"
2633
uliDescription := "Updated Match List item Description"
27-
resourceName := "sumologic_cse_match_list.match_list"
34+
uliExpiration := "2122-02-27T05:00:00"
35+
uliValue := "updated value"
36+
uliCount := 1001
2837

2938
resource.Test(t, resource.TestCase{
3039
PreCheck: func() { testAccPreCheck(t) },
3140
Providers: testAccProviders,
3241
CheckDestroy: testAccCSEMatchListDestroy,
3342
Steps: []resource.TestStep{
3443
{
35-
Config: testCreateCSEMatchListConfig(nDefaultTtl, nDescription, nName, nTargetColumn, liDescription, liExpiration, liValue, 1000),
44+
Config: testCreateCSEMatchListConfig(nDefaultTtl, nDescription, nName, nTargetColumn, liDescription, liExpiration, liValue, liCount),
3645
Check: resource.ComposeTestCheckFunc(
3746
testCheckCSEMatchListExists(resourceName, &matchList),
3847
testCheckMatchListValues(&matchList, nDefaultTtl, nDescription, nName, nTargetColumn),
48+
testCheckMatchListItemsValuesAndCount(resourceName, liDescription, liExpiration, liValue, liCount),
3949
resource.TestCheckResourceAttrSet(resourceName, "id"),
4050
),
4151
},
4252
{
43-
Config: testCreateCSEMatchListConfig(uDefaultTtl, uDescription, nName, nTargetColumn, uliDescription, liExpiration, liValue, 1000),
53+
Config: testCreateCSEMatchListConfig(uDefaultTtl, uDescription, nName, nTargetColumn, uliDescription, uliExpiration, uliValue, uliCount),
4454
Check: resource.ComposeTestCheckFunc(
4555
testCheckCSEMatchListExists(resourceName, &matchList),
4656
testCheckMatchListValues(&matchList, uDefaultTtl, uDescription, nName, nTargetColumn),
57+
testCheckMatchListItemsValuesAndCount(resourceName, uliDescription, uliExpiration, uliValue, uliCount),
4758
),
4859
},
4960
{
5061
Config: testDeleteCSEMatchListItemConfig(uDefaultTtl, uDescription, nName, nTargetColumn),
5162
Check: resource.ComposeTestCheckFunc(
52-
testCheckMatchListItemsEmpty(resourceName),
63+
testCheckMatchListItemsValuesAndCount(resourceName, "", "", "", 0),
5364
),
5465
},
5566
},
@@ -139,15 +150,34 @@ func testCheckCSEMatchListExists(n string, matchList *CSEMatchListGet) resource.
139150
}
140151
}
141152

142-
func testCheckMatchListItemsEmpty(n string) resource.TestCheckFunc {
153+
func testCheckMatchListValues(matchList *CSEMatchListGet, nDefaultTtl int, nDescription string, nName string, nTargetColumn string) resource.TestCheckFunc {
143154
return func(s *terraform.State) error {
144-
rs, ok := s.RootModule().Resources[n]
155+
if matchList.DefaultTtl != nDefaultTtl {
156+
return fmt.Errorf("bad default ttl, expected \"%s\", got: %#v", nName, matchList.Name)
157+
}
158+
if matchList.Description != nDescription {
159+
return fmt.Errorf("bad description, expected \"%s\", got: %#v", nDescription, matchList.Description)
160+
}
161+
if matchList.Name != nName {
162+
return fmt.Errorf("bad name, expected \"%s\", got: %#v", nName, matchList.Name)
163+
}
164+
if matchList.TargetColumn != nTargetColumn {
165+
return fmt.Errorf("bad target column, expected \"%s\", got: %#v", nName, matchList.Name)
166+
}
167+
168+
return nil
169+
}
170+
}
171+
172+
func testCheckMatchListItemsValuesAndCount(resourceName string, expectedDescription string, expectedExpiration string, expectedValue string, expectedCount int) resource.TestCheckFunc {
173+
return func(s *terraform.State) error {
174+
rs, ok := s.RootModule().Resources[resourceName]
145175
if !ok {
146-
return fmt.Errorf("not found: %s", n)
176+
return fmt.Errorf("not found: %s", resourceName)
147177
}
148178

149179
if rs.Primary.ID == "" {
150-
return fmt.Errorf("match List ID is not set")
180+
return fmt.Errorf("expected match list ID to be non-empty, but found empty string instead")
151181
}
152182

153183
c := testAccProvider.Meta().(*Client)
@@ -156,29 +186,29 @@ func testCheckMatchListItemsEmpty(n string) resource.TestCheckFunc {
156186
return err
157187
}
158188

159-
if len(matchListResp.CSEMatchListItemsGetObjects) != 0 {
160-
return fmt.Errorf("match list items not empty")
189+
actualCount := len(matchListResp.CSEMatchListItemsGetObjects)
190+
if actualCount != expectedCount {
191+
return fmt.Errorf("expected %d match list items, but found %d instead", expectedCount, actualCount)
161192
}
162193

163-
return nil
164-
}
165-
}
166-
167-
func testCheckMatchListValues(matchList *CSEMatchListGet, nDefaultTtl int, nDescription string, nName string, nTargetColumn string) resource.TestCheckFunc {
168-
return func(s *terraform.State) error {
169-
if matchList.DefaultTtl != nDefaultTtl {
170-
return fmt.Errorf("bad default ttl, expected \"%s\", got: %#v", nName, matchList.Name)
171-
}
172-
if matchList.Description != nDescription {
173-
return fmt.Errorf("bad description, expected \"%s\", got: %#v", nDescription, matchList.Description)
174-
}
175-
if matchList.Name != nName {
176-
return fmt.Errorf("bad name, expected \"%s\", got: %#v", nName, matchList.Name)
177-
}
178-
if matchList.TargetColumn != nTargetColumn {
179-
return fmt.Errorf("bad target column, expected \"%s\", got: %#v", nName, matchList.Name)
194+
if expectedCount == 0 {
195+
return nil
180196
}
181197

198+
for _, item := range matchListResp.CSEMatchListItemsGetObjects {
199+
if item.ID == "" {
200+
return fmt.Errorf("expected match list item ID to be non-empty, but found empty string instead")
201+
}
202+
if !strings.Contains(item.Meta.Description, expectedDescription) {
203+
return fmt.Errorf("expected match list item description to contain \"%s\", but found \"%s\" instead", expectedDescription, item.Meta.Description)
204+
}
205+
if item.Expiration != expectedExpiration {
206+
return fmt.Errorf("expected expiration to be \"%s\", but found \"%s\" instead", expectedExpiration, item.Expiration)
207+
}
208+
if !strings.Contains(item.Value, expectedValue) {
209+
return fmt.Errorf("expected match list item value to contain \"%s\", but found \"%s\" instead", expectedValue, item.Value)
210+
}
211+
}
182212
return nil
183213
}
184214
}

0 commit comments

Comments
 (0)