Skip to content

Commit 23fa28a

Browse files
author
Pedro Montiel
committed
INVS-1625: Support CSE context actions
1 parent a2dceb4 commit 23fa28a

File tree

6 files changed

+458
-0
lines changed

6 files changed

+458
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## 2.26.1 (Unreleased)
2+
FEATURES:
3+
* **New Resource:** sumologic_cse_context_action (GH-573)
24

35
## 2.26.0 (September 7, 2023)
46
FEATURES:

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Provider() terraform.ResourceProvider {
4343
},
4444
},
4545
ResourcesMap: map[string]*schema.Resource{
46+
"sumologic_cse_context_action": resourceSumologicCSEContextAction(),
4647
"sumologic_cse_automation": resourceSumologicCSEAutomation(),
4748
"sumologic_cse_entity_normalization_configuration": resourceSumologicCSEEntityNormalizationConfiguration(),
4849
"sumologic_cse_inventory_entity_group_configuration": resourceSumologicCSEInventoryEntityGroupConfiguration(),
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package sumologic
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
6+
"log"
7+
)
8+
9+
func resourceSumologicCSEContextAction() *schema.Resource {
10+
return &schema.Resource{
11+
Create: resourceSumologicCSEContextActionCreate,
12+
Read: resourceSumologicCSEContextActionRead,
13+
Delete: resourceSumologicCSEContextActionDelete,
14+
Update: resourceSumologicCSEContextActionUpdate,
15+
Importer: &schema.ResourceImporter{
16+
State: schema.ImportStatePassthrough,
17+
},
18+
19+
Schema: map[string]*schema.Schema{
20+
"name": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
"type": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Elem: &schema.Schema{
28+
Type: schema.TypeString,
29+
ValidateFunc: validation.All(validation.StringIsNotEmpty, validation.StringInSlice([]string{"URL", "QUERY"}, false)),
30+
},
31+
},
32+
"template": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
},
36+
"ioc_types": {
37+
Type: schema.TypeList,
38+
Required: true,
39+
Elem: &schema.Schema{
40+
Type: schema.TypeString,
41+
ValidateFunc: validation.All(validation.StringIsNotEmpty, validation.StringInSlice([]string{"ASN", "DOMAIN", "HASH", "IP_ADDRESS", "MAC_ADDRESS", "PORT", "RECORD_PROPERTY", "URL"}, false)),
42+
},
43+
},
44+
"entity_types": {
45+
Type: schema.TypeList,
46+
Optional: true,
47+
Elem: &schema.Schema{
48+
Type: schema.TypeString,
49+
ValidateFunc: validation.StringIsNotEmpty,
50+
},
51+
},
52+
"record_fields": {
53+
Type: schema.TypeList,
54+
Optional: true,
55+
Elem: &schema.Schema{
56+
Type: schema.TypeString,
57+
ValidateFunc: validation.StringIsNotEmpty,
58+
},
59+
},
60+
"all_record_fields": {
61+
Type: schema.TypeBool,
62+
Optional: true,
63+
Default: true,
64+
},
65+
"enabled": {
66+
Type: schema.TypeBool,
67+
Optional: true,
68+
Default: true,
69+
},
70+
},
71+
}
72+
}
73+
74+
func resourceSumologicCSEContextActionRead(d *schema.ResourceData, meta interface{}) error {
75+
c := meta.(*Client)
76+
77+
var CSEContextAction *CSEContextAction
78+
id := d.Id()
79+
80+
CSEContextAction, err := c.GetCSEContextAction(id)
81+
if err != nil {
82+
log.Printf("[WARN] CSE Context Action not found when looking by id: %s, err: %v", id, err)
83+
84+
}
85+
86+
if CSEContextAction == nil {
87+
log.Printf("[WARN] CSE Context Action not found, removing from state: %v - %v", id, err)
88+
d.SetId("")
89+
return nil
90+
}
91+
92+
d.Set("name", CSEContextAction.Name)
93+
d.Set("type", CSEContextAction.Type)
94+
d.Set("template", CSEContextAction.Template)
95+
d.Set("ioc_types", CSEContextAction.IocTypes)
96+
d.Set("entity_types", CSEContextAction.EntityTypes)
97+
d.Set("record_fields", CSEContextAction.RecordFields)
98+
d.Set("all_record_fields", CSEContextAction.AllRecordFields)
99+
d.Set("enabled", CSEContextAction.Enabled)
100+
101+
return nil
102+
}
103+
104+
func resourceSumologicCSEContextActionDelete(d *schema.ResourceData, meta interface{}) error {
105+
c := meta.(*Client)
106+
107+
return c.DeleteCSEContextAction(d.Id())
108+
109+
}
110+
111+
func resourceSumologicCSEContextActionCreate(d *schema.ResourceData, meta interface{}) error {
112+
c := meta.(*Client)
113+
114+
if d.Id() == "" {
115+
id, err := c.CreateCSEContextAction(CSEContextAction{
116+
Name: d.Get("name").(string),
117+
Type: d.Get("type").(string),
118+
Template: d.Get("template").(string),
119+
IocTypes: resourceFieldsToStringArray(d.Get("ioc_types").([]interface{})),
120+
EntityTypes: resourceFieldsToStringArray(d.Get("entity_types").([]interface{})),
121+
RecordFields: resourceFieldsToStringArray(d.Get("record_fields").([]interface{})),
122+
AllRecordFields: d.Get("all_record_fields").(bool),
123+
Enabled: d.Get("enabled").(bool),
124+
})
125+
126+
if err != nil {
127+
return err
128+
}
129+
130+
d.SetId(id)
131+
}
132+
133+
return resourceSumologicCSEContextActionRead(d, meta)
134+
}
135+
136+
func resourceSumologicCSEContextActionUpdate(d *schema.ResourceData, meta interface{}) error {
137+
CSEContextAction, err := resourceToCSEContextAction(d)
138+
if err != nil {
139+
return err
140+
}
141+
c := meta.(*Client)
142+
if err = c.UpdateCSEContextAction(CSEContextAction); err != nil {
143+
return err
144+
}
145+
146+
return resourceSumologicCSEContextActionRead(d, meta)
147+
}
148+
149+
func resourceToCSEContextAction(d *schema.ResourceData) (CSEContextAction, error) {
150+
id := d.Id()
151+
if id == "" {
152+
return CSEContextAction{}, nil
153+
}
154+
155+
return CSEContextAction{
156+
ID: id,
157+
Name: d.Get("name").(string),
158+
Type: d.Get("type").(string),
159+
Template: d.Get("template").(string),
160+
IocTypes: resourceFieldsToStringArray(d.Get("ioc_types").([]interface{})),
161+
EntityTypes: resourceFieldsToStringArray(d.Get("entity_types").([]interface{})),
162+
RecordFields: resourceFieldsToStringArray(d.Get("record_fields").([]interface{})),
163+
AllRecordFields: d.Get("all_record_fields").(bool),
164+
Enabled: d.Get("enabled").(bool),
165+
}, nil
166+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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 TestAccSumologicSCEContextAction_create_update(t *testing.T) {
12+
SkipCseTest(t)
13+
14+
var ContextAction CSEContextAction
15+
nName := "Test Context Action"
16+
nType := "URL"
17+
nTemplate := "https://bar.com/?q={{value}}"
18+
nIocTypes := []string{"IP_ADDRESS"}
19+
nEntityTypes := []string{"_hostname"}
20+
nRecordFields := []string{"request_url"}
21+
nAllRecordFields := false
22+
nEnabled := true
23+
uIocTypes := []string{"MAC_ADDRESS"}
24+
uEnabled := false
25+
resourceName := "sumologic_cse_context_action.context_action"
26+
resource.Test(t, resource.TestCase{
27+
PreCheck: func() { testAccPreCheck(t) },
28+
Providers: testAccProviders,
29+
CheckDestroy: testAccCSEContextActionDestroy,
30+
Steps: []resource.TestStep{
31+
{
32+
Config: testCreateCSEContextActionConfig(nName, nType, nTemplate, nIocTypes, nEntityTypes, nRecordFields, nAllRecordFields, nEnabled),
33+
Check: resource.ComposeTestCheckFunc(
34+
testCheckCSEContextActionExists(resourceName, &ContextAction),
35+
testCheckContextActionValues(&ContextAction, nName, nType, nTemplate, nIocTypes, nEntityTypes, nRecordFields, nAllRecordFields, nEnabled),
36+
resource.TestCheckResourceAttrSet(resourceName, "id"),
37+
),
38+
},
39+
{
40+
Config: testCreateCSEContextActionConfig(nName, nType, nTemplate, uIocTypes, nEntityTypes, nRecordFields, nAllRecordFields, uEnabled),
41+
Check: resource.ComposeTestCheckFunc(
42+
testCheckCSEContextActionExists(resourceName, &ContextAction),
43+
testCheckContextActionValues(&ContextAction, nName, nType, nTemplate, uIocTypes, nEntityTypes, nRecordFields, nAllRecordFields, uEnabled),
44+
),
45+
},
46+
},
47+
})
48+
}
49+
50+
func testAccCSEContextActionDestroy(s *terraform.State) error {
51+
client := testAccProvider.Meta().(*Client)
52+
53+
for _, rs := range s.RootModule().Resources {
54+
if rs.Type != "sumologic_cse_context_action" {
55+
continue
56+
}
57+
58+
if rs.Primary.ID == "" {
59+
return fmt.Errorf("CSE Context Action destruction check: CSE Context Action ID is not set")
60+
}
61+
62+
s, err := client.GetCSEContextAction(rs.Primary.ID)
63+
if err != nil {
64+
return fmt.Errorf("Encountered an error: " + err.Error())
65+
}
66+
if s != nil {
67+
return fmt.Errorf("Context Action still exists")
68+
}
69+
}
70+
return nil
71+
}
72+
73+
func testCreateCSEContextActionConfig(nName string, nType string, nTemplate string, nIocTypes []string, nEntityTypes []string, nRecordFields []string, nAllRecordFields bool, nEnabled bool) string {
74+
75+
return fmt.Sprintf(`
76+
resource "sumologic_cse_context_action" "context_action" {
77+
name = "%s"
78+
type = "%s"
79+
template = "%s"
80+
ioc_types = ["%s"]
81+
entity_types = ["%s"]
82+
record_fields = ["%s"]
83+
all_record_fields = "%t"
84+
enabled = "%t"
85+
}
86+
`, nName, nType, nTemplate, nIocTypes[0], nEntityTypes[0], nRecordFields[0], nAllRecordFields, nEnabled)
87+
}
88+
89+
func testCheckCSEContextActionExists(n string, ContextAction *CSEContextAction) resource.TestCheckFunc {
90+
return func(s *terraform.State) error {
91+
rs, ok := s.RootModule().Resources[n]
92+
if !ok {
93+
return fmt.Errorf("not found: %s", n)
94+
}
95+
96+
if rs.Primary.ID == "" {
97+
return fmt.Errorf("Context Action ID is not set")
98+
}
99+
100+
c := testAccProvider.Meta().(*Client)
101+
ContextActionResp, err := c.GetCSEContextAction(rs.Primary.ID)
102+
if err != nil {
103+
return err
104+
}
105+
106+
*ContextAction = *ContextActionResp
107+
108+
return nil
109+
}
110+
}
111+
112+
func testCheckContextActionValues(ContextAction *CSEContextAction, nName string, nType string, nTemplate string, nIocTypes []string, nEntityTypes []string, nRecordFields []string, nAllRecordFields bool, nEnabled bool) resource.TestCheckFunc {
113+
return func(s *terraform.State) error {
114+
if ContextAction.Name != nName {
115+
return fmt.Errorf("bad name, expected \"%s\", got: %#v", nName, ContextAction.Name)
116+
}
117+
if ContextAction.Type != nType {
118+
return fmt.Errorf("bad type, expected \"%s\", got: %#v", nType, ContextAction.Type)
119+
}
120+
if ContextAction.Template != nTemplate {
121+
return fmt.Errorf("bad template, expected \"%s\", got: %#v", nTemplate, ContextAction.Template)
122+
}
123+
if ContextAction.IocTypes != nil {
124+
if len(ContextAction.IocTypes) != len(nIocTypes) {
125+
return fmt.Errorf("bad ioc_types list lenght, expected \"%d\", got: %d", len(nIocTypes), len(ContextAction.IocTypes))
126+
}
127+
if ContextAction.IocTypes[0] != nIocTypes[0] {
128+
return fmt.Errorf("bad ioc_types in list, expected \"%s\", got: %s", nIocTypes[0], ContextAction.IocTypes[0])
129+
}
130+
}
131+
if ContextAction.EntityTypes != nil {
132+
if len(ContextAction.EntityTypes) != len(nEntityTypes) {
133+
return fmt.Errorf("bad entity_types list lenght, expected \"%d\", got: %d", len(nEntityTypes), len(ContextAction.EntityTypes))
134+
}
135+
if ContextAction.EntityTypes[0] != nEntityTypes[0] {
136+
return fmt.Errorf("bad entity_types in list, expected \"%s\", got: %s", nEntityTypes[0], ContextAction.EntityTypes[0])
137+
}
138+
}
139+
if ContextAction.RecordFields != nil {
140+
if len(ContextAction.RecordFields) != len(nRecordFields) {
141+
return fmt.Errorf("bad record_fields list lenght, expected \"%d\", got: %d", len(nRecordFields), len(ContextAction.RecordFields))
142+
}
143+
if ContextAction.RecordFields[0] != nRecordFields[0] {
144+
return fmt.Errorf("bad record_fields in list, expected \"%s\", got: %s", nRecordFields[0], ContextAction.RecordFields[0])
145+
}
146+
}
147+
if ContextAction.AllRecordFields != nAllRecordFields {
148+
return fmt.Errorf("bad all_record_fields field, expected \"%t\", got: %#v", nAllRecordFields, ContextAction.AllRecordFields)
149+
}
150+
if ContextAction.Enabled != nEnabled {
151+
return fmt.Errorf("bad enabled field, expected \"%t\", got: %#v", nEnabled, ContextAction.Enabled)
152+
}
153+
154+
return nil
155+
}
156+
}

0 commit comments

Comments
 (0)