Skip to content

Commit ee19934

Browse files
authored
Merge pull request #462 from SumoLogic/INVS-884_custom_match_list_column
INVS-884: Add cse Custom Matchlist Column support
2 parents 5c3ee7a + 7da5ab5 commit ee19934

6 files changed

+346
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## 2.20.0 (Unreleased)
2+
FEATURES:
3+
* **New Resource: sumologic_cse_custom_match_list_column (GH-462)
4+
25
BUG FIXES:
36
* Fix typo on cse_match_list documentation (GH-461)
47

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func Provider() terraform.ResourceProvider {
4747
"sumologic_cse_inventory_entity_group_configuration": resourceSumologicCSEInventoryEntityGroupConfiguration(),
4848
"sumologic_cse_entity_entity_group_configuration": resourceSumologicCSEEntityEntityGroupConfiguration(),
4949
"sumologic_cse_match_list": resourceSumologicCSEMatchList(),
50+
"sumologic_cse_custom_match_list_column": resourceSumologicCSECustomMatchListColumn(),
5051
"sumologic_cse_log_mapping": resourceSumologicCSELogMapping(),
5152
"sumologic_cse_rule_tuning_expression": resourceSumologicCSERuleTuningExpression(),
5253
"sumologic_cse_network_block": resourceSumologicCSENetworkBlock(),
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package sumologic
2+
3+
import (
4+
"errors"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
7+
"log"
8+
)
9+
10+
func resourceSumologicCSECustomMatchListColumn() *schema.Resource {
11+
return &schema.Resource{
12+
Create: resourceSumologicCSECustomMatchListColumnCreate,
13+
Read: resourceSumologicCSECustomMatchListColumnRead,
14+
Delete: resourceSumologicCSECustomMatchListColumnDelete,
15+
Update: resourceSumologicCSECustomMatchListColumnUpdate,
16+
Importer: &schema.ResourceImporter{
17+
State: schema.ImportStatePassthrough,
18+
},
19+
20+
Schema: map[string]*schema.Schema{
21+
"name": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
ForceNew: false,
25+
},
26+
"fields": {
27+
Type: schema.TypeList,
28+
Required: true,
29+
Elem: &schema.Schema{
30+
Type: schema.TypeString,
31+
ValidateFunc: validation.StringIsNotEmpty,
32+
},
33+
},
34+
},
35+
}
36+
}
37+
38+
func resourceSumologicCSECustomMatchListColumnRead(d *schema.ResourceData, meta interface{}) error {
39+
c := meta.(*Client)
40+
41+
var CSECustomMatchListColumn *CSECustomMatchListColumn
42+
id := d.Id()
43+
44+
CSECustomMatchListColumn, err := c.GetCSECustomMatchListColumn(id)
45+
if err != nil {
46+
log.Printf("[WARN] CSE Custom Match List Column not found when looking by id: %s, err: %v", id, err)
47+
48+
}
49+
50+
if CSECustomMatchListColumn == nil {
51+
log.Printf("[WARN] CSE Custom Match List Column not found, removing from state: %v - %v", id, err)
52+
d.SetId("")
53+
return nil
54+
}
55+
56+
d.Set("name", CSECustomMatchListColumn.Name)
57+
d.Set("fields", CSECustomMatchListColumn.Fields)
58+
59+
return nil
60+
}
61+
62+
func resourceSumologicCSECustomMatchListColumnDelete(d *schema.ResourceData, meta interface{}) error {
63+
c := meta.(*Client)
64+
65+
id := d.Id()
66+
return c.DeleteCSECustomMatchListColumn(id)
67+
68+
}
69+
70+
func resourceSumologicCSECustomMatchListColumnCreate(d *schema.ResourceData, meta interface{}) error {
71+
c := meta.(*Client)
72+
73+
if d.Id() == "" {
74+
id, err := c.CreateCSECustomMatchListColumn(CSECustomMatchListColumn{
75+
Name: d.Get("name").(string),
76+
Fields: fieldsToStringArray(d.Get("fields").([]interface{})),
77+
})
78+
79+
if err != nil {
80+
return err
81+
}
82+
d.SetId(id)
83+
}
84+
85+
return resourceSumologicCSECustomMatchListColumnUpdate(d, meta)
86+
}
87+
88+
func fieldsToStringArray(resourceStrings []interface{}) []string {
89+
result := make([]string, len(resourceStrings))
90+
91+
for i, resourceString := range resourceStrings {
92+
result[i] = resourceString.(string)
93+
}
94+
95+
return result
96+
}
97+
98+
func resourceSumologicCSECustomMatchListColumnUpdate(d *schema.ResourceData, meta interface{}) error {
99+
CSECustomMatchListColumn, err := resourceToCSECustomMatchListColumn(d)
100+
if err != nil {
101+
return err
102+
}
103+
104+
c := meta.(*Client)
105+
if err = c.UpdateCSECustomMatchListColumn(CSECustomMatchListColumn); err != nil {
106+
return err
107+
}
108+
109+
return resourceSumologicCSECustomMatchListColumnRead(d, meta)
110+
}
111+
112+
func resourceToCSECustomMatchListColumn(d *schema.ResourceData) (CSECustomMatchListColumn, error) {
113+
id := d.Id()
114+
if id == "" {
115+
return CSECustomMatchListColumn{}, errors.New("Custom Match List Column id not specified")
116+
}
117+
118+
return CSECustomMatchListColumn{
119+
ID: id,
120+
Name: d.Get("name").(string),
121+
Fields: fieldsToStringArray(d.Get("fields").([]interface{})),
122+
}, nil
123+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package sumologic
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
9+
)
10+
11+
func TestAccSumologicSCECustomMatchListColumn_create(t *testing.T) {
12+
SkipCseTest(t)
13+
14+
var CustomMatchListColumn CSECustomMatchListColumn
15+
nName := "Custom Match List Column Test Terraform"
16+
nField := "device_ip"
17+
resourceName := "sumologic_cse_custom_match_list_column.custom_match_list_column"
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testAccCSECustomMatchListColumnDestroy,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testCreateCSECustomMatchListColumnConfig(nName, nField),
25+
Check: resource.ComposeTestCheckFunc(
26+
testCheckCustomMatchListColumnExists(resourceName, &CustomMatchListColumn),
27+
testCheckCustomMatchListColumnValues(&CustomMatchListColumn, nName, nField),
28+
resource.TestCheckResourceAttrSet(resourceName, "id"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
func testAccCSECustomMatchListColumnDestroy(s *terraform.State) error {
36+
client := testAccProvider.Meta().(*Client)
37+
38+
for _, rs := range s.RootModule().Resources {
39+
if rs.Type != "sumologic_cse_custom_match_list_column" {
40+
continue
41+
}
42+
43+
if rs.Primary.ID == "" {
44+
return fmt.Errorf("CSE Custom Match List Column destruction check: CSE Custom Match List Column ID is not set")
45+
}
46+
47+
CustomMatchListColumnID := rs.Primary.Attributes["id"]
48+
49+
s, err := client.GetCSECustomMatchListColumn(CustomMatchListColumnID)
50+
if err != nil {
51+
return fmt.Errorf("Encountered an error: " + err.Error())
52+
}
53+
if s != nil {
54+
return fmt.Errorf("Custom Match List Column still exists")
55+
}
56+
}
57+
return nil
58+
}
59+
60+
func testCreateCSECustomMatchListColumnConfig(nName string, nField string) string {
61+
return fmt.Sprintf(`
62+
resource "sumologic_cse_custom_match_list_column" "custom_match_list_column" {
63+
name = "%s"
64+
fields = ["%s"]
65+
}
66+
`, nName, nField)
67+
}
68+
69+
func testCheckCustomMatchListColumnExists(n string, CustomMatchListColumn *CSECustomMatchListColumn) resource.TestCheckFunc {
70+
return func(s *terraform.State) error {
71+
rs, ok := s.RootModule().Resources[n]
72+
if !ok {
73+
return fmt.Errorf("not found: %s", n)
74+
}
75+
76+
if rs.Primary.ID == "" {
77+
return fmt.Errorf("Custom Match List Column ID is not set")
78+
}
79+
80+
CustomMatchListColumnID := rs.Primary.Attributes["id"]
81+
82+
c := testAccProvider.Meta().(*Client)
83+
CustomMatchListColumnResp, err := c.GetCSECustomMatchListColumn(CustomMatchListColumnID)
84+
if err != nil {
85+
return err
86+
}
87+
88+
*CustomMatchListColumn = *CustomMatchListColumnResp
89+
90+
return nil
91+
}
92+
}
93+
94+
func testCheckCustomMatchListColumnValues(CustomMatchListColumn *CSECustomMatchListColumn, nName string, nField string) resource.TestCheckFunc {
95+
return func(s *terraform.State) error {
96+
if CustomMatchListColumn.Name != nName {
97+
return fmt.Errorf("bad name, expected \"%s\", got: %#v", nName, CustomMatchListColumn.Name)
98+
}
99+
if CustomMatchListColumn.Fields[0] != nField {
100+
return fmt.Errorf("bad field, expected \"%s\", got %#v", nField, CustomMatchListColumn.Fields[0])
101+
}
102+
return nil
103+
}
104+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package sumologic
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
func (s *Client) GetCSECustomMatchListColumn(id string) (*CSECustomMatchListColumn, error) {
9+
data, _, err := s.Get(fmt.Sprintf("sec/v1/custom-match-list-columns/%s", id))
10+
if err != nil {
11+
return nil, err
12+
}
13+
14+
if data == nil {
15+
return nil, nil
16+
}
17+
18+
var response CSECustomMatchListColumnResponse
19+
err = json.Unmarshal(data, &response)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
return &response.CSECustomMatchListColumn, nil
25+
}
26+
27+
func (s *Client) DeleteCSECustomMatchListColumn(id string) error {
28+
_, err := s.Delete(fmt.Sprintf("sec/v1/custom-match-list-columns/%s", id))
29+
30+
return err
31+
}
32+
33+
func (s *Client) CreateCSECustomMatchListColumn(CSECustomMatchListColumn CSECustomMatchListColumn) (string, error) {
34+
35+
request := CSECustomMatchListColumnRequest{
36+
CSECustomMatchListColumn: CSECustomMatchListColumn,
37+
}
38+
39+
var response CSECustomMatchListColumnResponse
40+
41+
responseBody, err := s.Post("sec/v1/custom-match-list-columns", request)
42+
if err != nil {
43+
return "", err
44+
}
45+
46+
err = json.Unmarshal(responseBody, &response)
47+
48+
if err != nil {
49+
return "", err
50+
}
51+
52+
return response.CSECustomMatchListColumn.ID, nil
53+
}
54+
55+
func (s *Client) UpdateCSECustomMatchListColumn(CSECustomMatchListColumn CSECustomMatchListColumn) error {
56+
url := fmt.Sprintf("sec/v1/custom-match-list-columns/%s", CSECustomMatchListColumn.ID)
57+
CSECustomMatchListColumn.ID = ""
58+
request := CSECustomMatchListColumnRequest{
59+
CSECustomMatchListColumn: CSECustomMatchListColumn,
60+
}
61+
62+
_, err := s.Put(url, request)
63+
64+
return err
65+
}
66+
67+
type CSECustomMatchListColumnRequest struct {
68+
CSECustomMatchListColumn CSECustomMatchListColumn `json:"fields"`
69+
}
70+
71+
type CSECustomMatchListColumnResponse struct {
72+
CSECustomMatchListColumn CSECustomMatchListColumn `json:"data"`
73+
}
74+
75+
type CSECustomMatchListColumn struct {
76+
ID string `json:"id,omitempty"`
77+
Name string `json:"name"`
78+
Fields []string `json:"fields"`
79+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
layout: "sumologic"
3+
page_title: "SumoLogic: sumologic_cse_custom_match_list_column"
4+
description: |-
5+
Provides a Sumologic CSE Custom Match List Column
6+
---
7+
8+
# custom_match_list_column
9+
Provides a Sumologic CSE Custom Match List Column.
10+
11+
## Example Usage
12+
```hcl
13+
resource "sumologic_cse_custom_match_list_column" "custom_match_list_column" {
14+
name = "Custom Match List Column name"
15+
fields = ["srcDevice_ip"]
16+
}
17+
```
18+
19+
## Argument reference
20+
21+
The following arguments are supported:
22+
23+
- `name` - (Required) Custom Match List Column name.
24+
- `fields` - (Required) Custom Match List Column fields.
25+
26+
The following attributes are exported:
27+
28+
- `id` - The internal ID of the Custom Match List Column.
29+
30+
## Import
31+
32+
Custom Match List Column can be imported using the field id, e.g.:
33+
```hcl
34+
terraform import sumologic_cse_custom_match_list_column.custom_match_list_column id
35+
```
36+

0 commit comments

Comments
 (0)