Skip to content

Commit dda976e

Browse files
author
Pedro Montiel
committed
INVS-11: PR comments
1 parent 950fa00 commit dda976e

File tree

5 files changed

+82
-82
lines changed

5 files changed

+82
-82
lines changed

sumologic/resource_sumologic_cse_match_list.go

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func resourceSumologicCSEMatchList() *schema.Resource {
1919
Schema: map[string]*schema.Schema{
2020
"default_ttl": {
2121
Type: schema.TypeInt,
22-
Required: true,
22+
Optional: true,
2323
ForceNew: false,
2424
},
2525
"description": {
@@ -130,41 +130,24 @@ func resourceSumologicCSEMatchListRead(d *schema.ResourceData, meta interface{})
130130

131131
func setItems(d *schema.ResourceData, items []CSEMatchListItemGet) {
132132

133-
var i []map[string]interface{}
133+
var its []map[string]interface{}
134134

135135
for _, t := range items {
136-
mapping := map[string]interface{}{
136+
item := map[string]interface{}{
137137
"id": t.ID,
138138
"description": t.Meta.Description,
139139
"expiration": t.Expiration,
140140
"value": t.Value,
141141
}
142-
i = append(i, mapping)
142+
its = append(its, item)
143143
}
144144

145-
d.Set("items", i)
145+
d.Set("items", its)
146146

147147
}
148148

149149
func resourceSumologicCSEMatchListDelete(d *schema.ResourceData, meta interface{}) error {
150150
c := meta.(*Client)
151-
152-
//Match list items
153-
itemsData := d.Get("items").([]interface{})
154-
var items []CSEMatchListItemPost
155-
for _, data := range itemsData {
156-
items = append(items, resourceToCSEMatchListItem([]interface{}{data}))
157-
}
158-
159-
if len(items) > 0 {
160-
for _, item := range items {
161-
err2 := c.DeleteCSEMatchListItem(item.ID)
162-
if err2 != nil {
163-
log.Printf("[WARN] An error occurred while updating match list item wiht id: %s, err: %v", item.ID, err2)
164-
}
165-
}
166-
}
167-
168151
err := c.DeleteCSEMatchList(d.Id())
169152
return err
170153

@@ -191,7 +174,9 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
191174
itemsData := d.Get("items").([]interface{})
192175
var items []CSEMatchListItemPost
193176
for _, data := range itemsData {
194-
items = append(items, resourceToCSEMatchListItem([]interface{}{data}))
177+
item, _ := resourceToCSEMatchListItem([]interface{}{data})
178+
items = append(items, item)
179+
195180
}
196181

197182
if len(items) > 0 {
@@ -211,7 +196,7 @@ func resourceSumologicCSEMatchListCreate(d *schema.ResourceData, meta interface{
211196
return resourceSumologicCSEMatchListRead(d, meta)
212197
}
213198

214-
func resourceToCSEMatchListItem(data interface{}) CSEMatchListItemPost {
199+
func resourceToCSEMatchListItem(data interface{}) (CSEMatchListItemPost, string) {
215200
itemsSlice := data.([]interface{})
216201
item := CSEMatchListItemPost{}
217202
if len(itemsSlice) > 0 {
@@ -222,7 +207,7 @@ func resourceToCSEMatchListItem(data interface{}) CSEMatchListItemPost {
222207
item.Expiration = itemObj["expiration"].(string)
223208
item.Value = itemObj["value"].(string)
224209
}
225-
return item
210+
return item, item.ID
226211
}
227212

228213
func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{}) error {
@@ -238,19 +223,30 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
238223

239224
//Match list items
240225
itemsData := d.Get("items").([]interface{})
226+
var itemIds []string
241227
var items []CSEMatchListItemPost
242228
for _, data := range itemsData {
243-
items = append(items, resourceToCSEMatchListItem([]interface{}{data}))
229+
item, id := resourceToCSEMatchListItem([]interface{}{data})
230+
items = append(items, item)
231+
itemIds = append(itemIds, id)
232+
244233
}
245234

246235
if len(items) > 0 {
247236
for _, item := range items {
248237
CSEMatchListItem, er := c.GetCSEMatchListItem(item.ID)
249-
log.Printf("[WARN] An error occurred while getting match list item wiht id: %s, err: %v", item.ID, er)
238+
log.Printf("[WARN] An error occurred while getting match list item with id: %s, err: %v", item.ID, er)
250239
if CSEMatchListItem != nil {
251-
err3 := c.UpdateCSEMatchListItem(item)
252-
if err3 != nil {
253-
log.Printf("[WARN] An error occurred while updating match list item wiht id: %s, err: %v", item.ID, err3)
240+
if contains(itemIds, CSEMatchListItem.ID) {
241+
err3 := c.UpdateCSEMatchListItem(item)
242+
if err3 != nil {
243+
log.Printf("[WARN] An error occurred while updating match list item with id: %s, err: %v", item.ID, err3)
244+
}
245+
} else {
246+
err3 := c.DeleteCSEMatchListItem(CSEMatchListItem.ID)
247+
if err3 != nil {
248+
log.Printf("[WARN] An error occurred deleting match list item with id: %s, err: %v", CSEMatchListItem.ID, err3)
249+
}
254250
}
255251
} else {
256252
err4 := c.CreateCSEMatchListItems(items, d.Id())
@@ -259,14 +255,41 @@ func resourceSumologicCSEMatchListUpdate(d *schema.ResourceData, meta interface{
259255
}
260256
}
261257
}
262-
// Calling Sleep method, adding 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)
258+
} else {
259+
var CSEMatchListItems *CSEMatchListItemsInMatchListGet
260+
261+
CSEMatchListItems, err2 := c.GetCSEMatchListItemsInMatchList(d.Id())
262+
if err2 != nil {
263+
log.Printf("[WARN] CSE Match List items not found when looking by match list id: %s, err: %v", d.Id(), err2)
264+
}
265+
if CSEMatchListItems != nil {
266+
267+
for _, t := range CSEMatchListItems.CSEMatchListItemsGetObjects {
268+
err3 := c.DeleteCSEMatchListItem(t.ID)
269+
if err3 != nil {
270+
log.Printf("[WARN] An error occurred deleting match list item with id: %s, err: %v", t.ID, err3)
271+
}
272+
}
273+
}
265274
}
266275

276+
// Calling Sleep method, adding/deleting items might take a while before items retrieved in next section
277+
// Need to find a better way since feels super hacky
278+
time.Sleep(15 * time.Second)
279+
267280
return resourceSumologicCSEMatchListRead(d, meta)
268281
}
269282

283+
func contains(slice []string, item string) bool {
284+
set := make(map[string]struct{}, len(slice))
285+
for _, s := range slice {
286+
set[s] = struct{}{}
287+
}
288+
289+
_, ok := set[item]
290+
return ok
291+
}
292+
270293
func resourceToCSEMatchList(d *schema.ResourceData) (CSEMatchListPost, error) {
271294
id := d.Id()
272295
if id == "" {

sumologic/resource_sumologic_cse_match_list_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ func TestAccSumologicSCEMatchList_createAndUpdate(t *testing.T) {
4444
testCheckMatchListValues(&matchList, uDefaultTtl, uDescription, nName, nTargetColumn),
4545
),
4646
},
47+
{
48+
Config: testDeleteCSEMatchListItemConfig(uDefaultTtl, uDescription, nName, nTargetColumn),
49+
Check: resource.ComposeTestCheckFunc(
50+
testCheckMatchListItemsEmpty(resourceName),
51+
),
52+
},
4753
},
4854
})
4955
}
@@ -87,6 +93,17 @@ resource "sumologic_cse_match_list" "match_list" {
8793
`, nDefaultTtl, nDescription, nName, nTargetColumn, liDescription, liExpiration, liValue)
8894
}
8995

96+
func testDeleteCSEMatchListItemConfig(nDefaultTtl int, nDescription string, nName string, nTargetColumn string) string {
97+
return fmt.Sprintf(`
98+
resource "sumologic_cse_match_list" "match_list" {
99+
default_ttl = "%d"
100+
description = "%s"
101+
name = "%s"
102+
target_column = "%s"
103+
}
104+
`, nDefaultTtl, nDescription, nName, nTargetColumn)
105+
}
106+
90107
func testCheckCSEMatchListExists(n string, matchList *CSEMatchListGet) resource.TestCheckFunc {
91108
return func(s *terraform.State) error {
92109
rs, ok := s.RootModule().Resources[n]
@@ -109,7 +126,7 @@ func testCheckCSEMatchListExists(n string, matchList *CSEMatchListGet) resource.
109126
}
110127
}
111128

112-
func testCheckCSEMatchListItemExists(n string, matchListItem *CSEMatchListItemGet) resource.TestCheckFunc {
129+
func testCheckMatchListItemsEmpty(n string) resource.TestCheckFunc {
113130
return func(s *terraform.State) error {
114131
rs, ok := s.RootModule().Resources[n]
115132
if !ok {
@@ -121,12 +138,14 @@ func testCheckCSEMatchListItemExists(n string, matchListItem *CSEMatchListItemGe
121138
}
122139

123140
c := testAccProvider.Meta().(*Client)
124-
matchListResp, err := c.GetCSEMatchListItem(rs.Primary.ID)
141+
matchListResp, err := c.GetCSEMatchListItemsInMatchList(rs.Primary.ID)
125142
if err != nil {
126143
return err
127144
}
128145

129-
*matchListItem = *matchListResp
146+
if len(matchListResp.CSEMatchListItemsGetObjects) != 0 {
147+
return fmt.Errorf("match list items not empty")
148+
}
130149

131150
return nil
132151
}

sumologic/sumologic_client.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -268,40 +268,6 @@ func (s *Client) Get(urlPath string) ([]byte, string, error) {
268268
return d, resp.Header.Get("ETag"), nil
269269
}
270270

271-
func (s *Client) GetWithPayload(urlPath string, payload interface{}) ([]byte, string, error) {
272-
relativeURL, _ := url.Parse(urlPath)
273-
sumoURL := s.BaseURL.ResolveReference(relativeURL)
274-
275-
body, _ := json.Marshal(payload)
276-
req, err := createNewRequest(http.MethodGet, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey, s.AuthJwt)
277-
if err != nil {
278-
return nil, "", err
279-
}
280-
281-
if s.IsInAdminMode {
282-
req.Header.Add("isAdminMode", "true")
283-
}
284-
285-
<-rateLimiter.C
286-
resp, err := s.httpClient.Do(req)
287-
if err != nil {
288-
return nil, "", err
289-
}
290-
defer resp.Body.Close()
291-
292-
d, err := ioutil.ReadAll(resp.Body)
293-
if err != nil {
294-
return nil, "", err
295-
}
296-
if resp.StatusCode == 404 {
297-
return nil, "", nil
298-
} else if resp.StatusCode >= 400 {
299-
return nil, "", errors.New(string(d))
300-
}
301-
302-
return d, resp.Header.Get("ETag"), nil
303-
}
304-
305271
func (s *Client) Delete(urlPath string) ([]byte, error) {
306272
relativeURL, _ := url.Parse(urlPath)
307273
sumoURL := s.BaseURL.ResolveReference(relativeURL)

sumologic/sumologic_cse_match_list_item.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ func (s *Client) GetCSEMatchListItem(id string) (*CSEMatchListItemGet, error) {
2424
return &response.CSEMatchListItemGet, nil
2525
}
2626

27-
func (s *Client) GetCSEMatchListItemsInMatchList(MatcListId string) (*CSEMatchListItemsInMatchListGet, error) {
27+
func (s *Client) GetCSEMatchListItemsInMatchList(MatchListId string) (*CSEMatchListItemsInMatchListGet, error) {
2828

29-
request := CSEMatchListItemsRequest{
30-
ListIds: []string{MatcListId},
31-
}
32-
33-
data, _, err := s.GetWithPayload("sec/v1/match-list-items", request)
29+
data, _, err := s.Get(fmt.Sprintf("sec/v1/match-list-items?listIds[%s]", MatchListId))
3430
if err != nil {
3531
return nil, err
3632
}
@@ -104,10 +100,6 @@ type CSEMatchListItemResponse struct {
104100
CSEMatchListItemGet CSEMatchListItemGet `json:"data"`
105101
}
106102

107-
type CSEMatchListItemsRequest struct {
108-
ListIds []string `json:"listIds"`
109-
}
110-
111103
type CSEMatchListItemsInMatchListResponse struct {
112104
CSEMatchListItemsGetData CSEMatchListItemsInMatchListGet `json:"data"`
113105
}

website/docs/r/cse_log_match_list.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ resource "sumologic_cse_match_list" "match_list" {
2727

2828
The following arguments are supported:
2929

30-
- `default_ttl` - (Required) The match list time to live. Specified in seconds.
30+
- `default_ttl` - (Optional) The default time to live for match list items added through the UI. Specified in seconds.
3131
- `description` - (Required) Match list description.
3232
- `name` - (Required) Match list name.
3333
- `target_column` - (Required) Target column. (possible values: Hostname, FileHash, Url, SrcIp, DstIp, Domain, Username, Ip, Asn, Isp, Org, SrcAsn, SrcIsp, SrcOrg, DstAsn, DstIsp, DstOrg or any custom column.)
@@ -44,7 +44,7 @@ The following attributes are exported:
4444

4545
## Import
4646

47-
Mach List can be imported using the field id, e.g.:
47+
Match List can be imported using the field id, e.g.:
4848
```hcl
4949
terraform import sumologic_cse_match_list.match_list id
5050
```

0 commit comments

Comments
 (0)