Skip to content

Commit a6073f0

Browse files
author
Josh Williams
authored
Merge pull request #289 from SumoLogic/cse-custom-insight-resource
Add support for CSE Custom Insights
2 parents f07e54c + 6dbccbe commit a6073f0

File tree

6 files changed

+441
-0
lines changed

6 files changed

+441
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ FEATURES:
1616
* **New Resource:** sumologic_cse_chain_rule (GH-290)
1717
* **New Resource:** sumologic_cse_match_rule (GH-287)
1818
* **New Resource:** sumologic_cse_threshold_rule (GH-287)
19+
* **New Resource:** sumologic_cse_custom_insight (GH-289)
1920

2021
BUG FIXES:
2122

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_rule_tuning_expression": resourceSumologicCSERuleTuningExpression(),
4848
"sumologic_cse_network_block": resourceSumologicCSENetworkBlock(),
4949
"sumologic_cse_custom_entity_type": resourceSumologicCSECustomEntityType(),
50+
"sumologic_cse_custom_insight": resourceSumologicCSECustomInsight(),
5051
"sumologic_cse_entity_criticality_config": resourceSumologicCSEEntityCriticalityConfig(),
5152
"sumologic_cse_insights_configuration": resourceSumologicCSEInsightsConfiguration(),
5253
"sumologic_cse_insights_resolution": resourceSumologicCSEInsightsResolution(),
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+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
6+
"log"
7+
)
8+
9+
func resourceSumologicCSECustomInsight() *schema.Resource {
10+
return &schema.Resource{
11+
Create: resourceSumologicCSECustomInsightCreate,
12+
Read: resourceSumologicCSECustomInsightRead,
13+
Delete: resourceSumologicCSECustomInsightDelete,
14+
Update: resourceSumologicCSECustomInsightUpdate,
15+
Importer: &schema.ResourceImporter{
16+
State: schema.ImportStatePassthrough,
17+
},
18+
19+
Schema: map[string]*schema.Schema{
20+
"description": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
"enabled": {
25+
Type: schema.TypeBool,
26+
Required: true,
27+
},
28+
"name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
},
32+
"ordered": {
33+
Type: schema.TypeBool,
34+
Required: true,
35+
},
36+
"rule_ids": {
37+
Type: schema.TypeList,
38+
Optional: true,
39+
Elem: &schema.Schema{
40+
Type: schema.TypeString,
41+
},
42+
},
43+
"severity": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
ValidateFunc: validation.StringInSlice([]string{"HIGH", "MEDIUM", "LOW"}, false),
47+
},
48+
"signal_names": {
49+
Type: schema.TypeList,
50+
Optional: true,
51+
Elem: &schema.Schema{
52+
Type: schema.TypeString,
53+
},
54+
},
55+
"tags": {
56+
Type: schema.TypeList,
57+
Required: true,
58+
Elem: &schema.Schema{
59+
Type: schema.TypeString,
60+
},
61+
},
62+
},
63+
}
64+
}
65+
66+
func resourceSumologicCSECustomInsightRead(d *schema.ResourceData, meta interface{}) error {
67+
c := meta.(*Client)
68+
69+
var CSECustomInsightGet *CSECustomInsight
70+
id := d.Id()
71+
72+
CSECustomInsightGet, err := c.GetCSECustomInsight(id)
73+
if err != nil {
74+
log.Printf("[WARN] CSE Custom Insight not found when looking by id: %s, err: %v", id, err)
75+
}
76+
77+
if CSECustomInsightGet == nil {
78+
log.Printf("[WARN] CSE Custom Insight not found, removing from state: %v - %v", id, err)
79+
d.SetId("")
80+
return nil
81+
}
82+
83+
d.Set("description", CSECustomInsightGet.Description)
84+
d.Set("enabled", CSECustomInsightGet.Enabled)
85+
d.Set("name", CSECustomInsightGet.Name)
86+
d.Set("ordered", CSECustomInsightGet.Ordered)
87+
d.Set("rule_ids", CSECustomInsightGet.RuleIds)
88+
d.Set("severity", CSECustomInsightGet.Severity)
89+
d.Set("signal_names", CSECustomInsightGet.SignalNames)
90+
d.Set("tags", CSECustomInsightGet.Tags)
91+
92+
return nil
93+
}
94+
95+
func resourceSumologicCSECustomInsightDelete(d *schema.ResourceData, meta interface{}) error {
96+
c := meta.(*Client)
97+
98+
return c.DeleteCSECustomInsight(d.Id())
99+
}
100+
101+
func resourceSumologicCSECustomInsightCreate(d *schema.ResourceData, meta interface{}) error {
102+
c := meta.(*Client)
103+
104+
if d.Id() == "" {
105+
id, err := c.CreateCSECustomInsight(CSECustomInsight{
106+
Description: d.Get("description").(string),
107+
Enabled: d.Get("enabled").(bool),
108+
RuleIds: resourceToStringArray(d.Get("rule_ids").([]interface{})),
109+
Name: d.Get("name").(string),
110+
Ordered: d.Get("ordered").(bool),
111+
Severity: d.Get("severity").(string),
112+
SignalNames: resourceToStringArray(d.Get("signal_names").([]interface{})),
113+
Tags: resourceToStringArray(d.Get("tags").([]interface{})),
114+
})
115+
116+
if err != nil {
117+
return err
118+
}
119+
d.SetId(id)
120+
}
121+
122+
return resourceSumologicCSECustomInsightRead(d, meta)
123+
}
124+
125+
func resourceSumologicCSECustomInsightUpdate(d *schema.ResourceData, meta interface{}) error {
126+
CSECustomInsight, err := resourceToCSECustomInsight(d)
127+
if err != nil {
128+
return err
129+
}
130+
131+
c := meta.(*Client)
132+
if err = c.UpdateCSECustomInsight(CSECustomInsight); err != nil {
133+
return err
134+
}
135+
136+
return resourceSumologicCSECustomInsightRead(d, meta)
137+
}
138+
139+
func resourceToCSECustomInsight(d *schema.ResourceData) (CSECustomInsight, error) {
140+
id := d.Id()
141+
if id == "" {
142+
return CSECustomInsight{}, nil
143+
}
144+
145+
return CSECustomInsight{
146+
ID: id,
147+
Description: d.Get("description").(string),
148+
Enabled: d.Get("enabled").(bool),
149+
RuleIds: resourceToStringArray(d.Get("rule_ids").([]interface{})),
150+
Name: d.Get("name").(string),
151+
Ordered: d.Get("ordered").(bool),
152+
Severity: d.Get("severity").(string),
153+
SignalNames: resourceToStringArray(d.Get("signal_names").([]interface{})),
154+
Tags: resourceToStringArray(d.Get("tags").([]interface{})),
155+
}, nil
156+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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 TestAccSumologicCSECustomInsight_createAndUpdate(t *testing.T) {
12+
var CustomInsight CSECustomInsight
13+
description := "Test description"
14+
enabled := true
15+
ordered := true
16+
name := "Test Custom Insight"
17+
severity := "HIGH"
18+
signalName1 := "Some Signal Name *"
19+
signalName2 := "Some Other Signal Name *"
20+
tag := "foo"
21+
22+
nameUpdated := "Updated Custom Insight"
23+
severityUpdated := "LOW"
24+
25+
resourceName := "sumologic_cse_custom_insight.custom_insight"
26+
resource.Test(t, resource.TestCase{
27+
PreCheck: func() { testAccPreCheck(t) },
28+
Providers: testAccProviders,
29+
CheckDestroy: testAccCSECustomInsightDestroy,
30+
Steps: []resource.TestStep{
31+
{
32+
Config: testCreateCSECustomInsightConfig(description, enabled,
33+
ordered, name, severity, signalName1, signalName2, tag),
34+
Check: resource.ComposeTestCheckFunc(
35+
testCheckCSECustomInsightExists(resourceName, &CustomInsight),
36+
testCheckCustomInsightValues(&CustomInsight, description, enabled,
37+
ordered, name, severity, signalName1, signalName2, tag),
38+
resource.TestCheckResourceAttrSet(resourceName, "id"),
39+
),
40+
},
41+
{
42+
Config: testCreateCSECustomInsightConfig(description, enabled,
43+
ordered, nameUpdated, severityUpdated, signalName1,
44+
signalName2, tag),
45+
Check: resource.ComposeTestCheckFunc(
46+
testCheckCSECustomInsightExists(resourceName, &CustomInsight),
47+
testCheckCustomInsightValues(&CustomInsight, description, enabled,
48+
ordered, nameUpdated, severityUpdated, signalName1,
49+
signalName2, tag),
50+
resource.TestCheckResourceAttrSet(resourceName, "id"),
51+
),
52+
},
53+
},
54+
})
55+
}
56+
57+
func testAccCSECustomInsightDestroy(s *terraform.State) error {
58+
client := testAccProvider.Meta().(*Client)
59+
60+
for _, rs := range s.RootModule().Resources {
61+
if rs.Type != "sumologic_cse_custom_insight" {
62+
continue
63+
}
64+
65+
if rs.Primary.ID == "" {
66+
return fmt.Errorf("CSE Custom Insight destruction check: CSE Custom Insight ID is not set")
67+
}
68+
69+
s, err := client.GetCSECustomInsight(rs.Primary.ID)
70+
if err != nil {
71+
return fmt.Errorf("Encountered an error: " + err.Error())
72+
}
73+
if s != nil {
74+
return fmt.Errorf("Custom Insight still exists")
75+
}
76+
}
77+
return nil
78+
}
79+
80+
func testCreateCSECustomInsightConfig(
81+
description string, enabled bool, ordered bool, name string,
82+
severity string, signalName1 string, signalName2 string, tag string) string {
83+
return fmt.Sprintf(`
84+
resource "sumologic_cse_custom_insight" "custom_insight" {
85+
description = "%s"
86+
enabled = %t
87+
ordered = %t
88+
name = "%s"
89+
severity = "%s"
90+
signal_names = ["%s", "%s"]
91+
tags = ["%s"]
92+
}
93+
`, description, enabled, ordered, name, severity, signalName1,
94+
signalName2, tag)
95+
}
96+
97+
func testCheckCSECustomInsightExists(n string, CustomInsight *CSECustomInsight) resource.TestCheckFunc {
98+
return func(s *terraform.State) error {
99+
rs, ok := s.RootModule().Resources[n]
100+
if !ok {
101+
return fmt.Errorf("not found: %s", n)
102+
}
103+
104+
if rs.Primary.ID == "" {
105+
return fmt.Errorf("chain rule ID is not set")
106+
}
107+
108+
c := testAccProvider.Meta().(*Client)
109+
CustomInsightResp, err := c.GetCSECustomInsight(rs.Primary.ID)
110+
if err != nil {
111+
return err
112+
}
113+
114+
*CustomInsight = *CustomInsightResp
115+
116+
return nil
117+
}
118+
}
119+
120+
func testCheckCustomInsightValues(CustomInsight *CSECustomInsight, description string,
121+
enabled bool, ordered bool, name string, severity string, signalName1 string,
122+
signalName2 string, tag string) resource.TestCheckFunc {
123+
return func(s *terraform.State) error {
124+
if CustomInsight.Description != description {
125+
return fmt.Errorf("bad description, expected \"%s\", got %#v", description, CustomInsight.Description)
126+
}
127+
if CustomInsight.Enabled != enabled {
128+
return fmt.Errorf("bad enabled, expected \"%t\", got %#v", enabled, CustomInsight.Enabled)
129+
}
130+
if CustomInsight.Ordered != ordered {
131+
return fmt.Errorf("bad ordered, expected \"%t\", got %#v", ordered, CustomInsight.Ordered)
132+
}
133+
if CustomInsight.Name != name {
134+
return fmt.Errorf("bad name, expected \"%s\", got %#v", name, CustomInsight.Name)
135+
}
136+
if CustomInsight.Severity != severity {
137+
return fmt.Errorf("bad severity, expected \"%s\", got %#v", severity, CustomInsight.Severity)
138+
}
139+
if CustomInsight.SignalNames[0] != signalName1 {
140+
return fmt.Errorf("bad signalName1, expected \"%s\", got %#v", signalName1, CustomInsight.SignalNames[0])
141+
}
142+
if CustomInsight.SignalNames[1] != signalName2 {
143+
return fmt.Errorf("bad signalName2, expected \"%s\", got %#v", signalName2, CustomInsight.SignalNames[1])
144+
}
145+
if CustomInsight.Tags[0] != tag {
146+
return fmt.Errorf("bad tag, expected \"%s\", got %#v", tag, CustomInsight.Tags[0])
147+
}
148+
149+
return nil
150+
}
151+
}

0 commit comments

Comments
 (0)