Skip to content

Commit cae29a9

Browse files
authored
Merge pull request #573 from SumoLogic/INVS-1625_Support_CSE_context_actions
INVS-1625: Support CSE context actions
2 parents cc25f28 + 18d64ed commit cae29a9

File tree

6 files changed

+457
-0
lines changed

6 files changed

+457
-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: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
ValidateFunc: validation.All(
28+
validation.StringIsNotEmpty,
29+
validation.StringInSlice([]string{"URL", "QUERY"}, false)),
30+
},
31+
"template": {
32+
Type: schema.TypeString,
33+
Optional: true,
34+
},
35+
"ioc_types": {
36+
Type: schema.TypeList,
37+
Required: true,
38+
Elem: &schema.Schema{
39+
Type: schema.TypeString,
40+
ValidateFunc: validation.All(validation.StringIsNotEmpty, validation.StringInSlice([]string{"ASN", "DOMAIN", "HASH", "IP_ADDRESS", "MAC_ADDRESS", "PORT", "RECORD_PROPERTY", "URL"}, false)),
41+
},
42+
},
43+
"entity_types": {
44+
Type: schema.TypeList,
45+
Optional: true,
46+
Elem: &schema.Schema{
47+
Type: schema.TypeString,
48+
ValidateFunc: validation.StringIsNotEmpty,
49+
},
50+
},
51+
"record_fields": {
52+
Type: schema.TypeList,
53+
Optional: true,
54+
Elem: &schema.Schema{
55+
Type: schema.TypeString,
56+
ValidateFunc: validation.StringIsNotEmpty,
57+
},
58+
},
59+
"all_record_fields": {
60+
Type: schema.TypeBool,
61+
Optional: true,
62+
Default: true,
63+
},
64+
"enabled": {
65+
Type: schema.TypeBool,
66+
Optional: true,
67+
Default: true,
68+
},
69+
},
70+
}
71+
}
72+
73+
func resourceSumologicCSEContextActionRead(d *schema.ResourceData, meta interface{}) error {
74+
c := meta.(*Client)
75+
76+
var CSEContextAction *CSEContextAction
77+
id := d.Id()
78+
79+
CSEContextAction, err := c.GetCSEContextAction(id)
80+
if err != nil {
81+
log.Printf("[WARN] CSE Context Action not found when looking by id: %s, err: %v", id, err)
82+
83+
}
84+
85+
if CSEContextAction == nil {
86+
log.Printf("[WARN] CSE Context Action not found, removing from state: %v - %v", id, err)
87+
d.SetId("")
88+
return nil
89+
}
90+
91+
d.Set("name", CSEContextAction.Name)
92+
d.Set("type", CSEContextAction.Type)
93+
d.Set("template", CSEContextAction.Template)
94+
d.Set("ioc_types", CSEContextAction.IocTypes)
95+
d.Set("entity_types", CSEContextAction.EntityTypes)
96+
d.Set("record_fields", CSEContextAction.RecordFields)
97+
d.Set("all_record_fields", CSEContextAction.AllRecordFields)
98+
d.Set("enabled", CSEContextAction.Enabled)
99+
100+
return nil
101+
}
102+
103+
func resourceSumologicCSEContextActionDelete(d *schema.ResourceData, meta interface{}) error {
104+
c := meta.(*Client)
105+
106+
return c.DeleteCSEContextAction(d.Id())
107+
108+
}
109+
110+
func resourceSumologicCSEContextActionCreate(d *schema.ResourceData, meta interface{}) error {
111+
c := meta.(*Client)
112+
113+
if d.Id() == "" {
114+
id, err := c.CreateCSEContextAction(CSEContextAction{
115+
Name: d.Get("name").(string),
116+
Type: d.Get("type").(string),
117+
Template: d.Get("template").(string),
118+
IocTypes: resourceFieldsToStringArray(d.Get("ioc_types").([]interface{})),
119+
EntityTypes: resourceFieldsToStringArray(d.Get("entity_types").([]interface{})),
120+
RecordFields: resourceFieldsToStringArray(d.Get("record_fields").([]interface{})),
121+
AllRecordFields: d.Get("all_record_fields").(bool),
122+
Enabled: d.Get("enabled").(bool),
123+
})
124+
125+
if err != nil {
126+
return err
127+
}
128+
129+
d.SetId(id)
130+
}
131+
132+
return resourceSumologicCSEContextActionRead(d, meta)
133+
}
134+
135+
func resourceSumologicCSEContextActionUpdate(d *schema.ResourceData, meta interface{}) error {
136+
CSEContextAction, err := resourceToCSEContextAction(d)
137+
if err != nil {
138+
return err
139+
}
140+
c := meta.(*Client)
141+
if err = c.UpdateCSEContextAction(CSEContextAction); err != nil {
142+
return err
143+
}
144+
145+
return resourceSumologicCSEContextActionRead(d, meta)
146+
}
147+
148+
func resourceToCSEContextAction(d *schema.ResourceData) (CSEContextAction, error) {
149+
id := d.Id()
150+
if id == "" {
151+
return CSEContextAction{}, nil
152+
}
153+
154+
return CSEContextAction{
155+
ID: id,
156+
Name: d.Get("name").(string),
157+
Type: d.Get("type").(string),
158+
Template: d.Get("template").(string),
159+
IocTypes: resourceFieldsToStringArray(d.Get("ioc_types").([]interface{})),
160+
EntityTypes: resourceFieldsToStringArray(d.Get("entity_types").([]interface{})),
161+
RecordFields: resourceFieldsToStringArray(d.Get("record_fields").([]interface{})),
162+
AllRecordFields: d.Get("all_record_fields").(bool),
163+
Enabled: d.Get("enabled").(bool),
164+
}, nil
165+
}
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)