Skip to content

Commit 15d2b7c

Browse files
authored
Merge pull request #274 from SumoLogic/APP-1056_insights_related_resources
Add support for CSE Insights related resources
2 parents 7a7d3a6 + d6c3b40 commit 15d2b7c

16 files changed

+1047
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
FEATURES:
33

44
* **New Resource:** sumologic_cse_rule_tuning_expression (GH-281)
5-
5+
* **New Resource:** sumologic_cse_insights_resolution (GH-274)
6+
* **New Resource:** sumologic_cse_insights_status (GH-274)
7+
* **New Resource:** sumologic_cse_insights_configuration (GH-274)
68

79
## 2.10.0 (September 22, 2021)
810

@@ -15,8 +17,8 @@ FEATURES:
1517

1618
POTENTIALLY BREAKING CHANGES:
1719

18-
* resource/sumologic_policies: Changed all policies to be required. Configurations might need to be updated in
19-
case some policies were not specified previously. (GH-279)
20+
* resource/sumologic_policies: Changed all policies to be required. Configurations might need to be updated in
21+
case some policies were not specified previously. (GH-279)
2022

2123
DEPRECATIONS:
2224

sumologic/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func Provider() terraform.ResourceProvider {
4545
ResourcesMap: map[string]*schema.Resource{
4646
"sumologic_cse_rule_tuning_expression": resourceSumologicCSERuleTuningExpression(),
4747
"sumologic_cse_network_block": resourceSumologicCSENetworkBlock(),
48+
"sumologic_cse_insights_configuration": resourceSumologicCSEInsightsConfiguration(),
49+
"sumologic_cse_insights_resolution": resourceSumologicCSEInsightsResolution(),
50+
"sumologic_cse_insights_status": resourceSumologicCSEInsightsStatus(),
4851
"sumologic_collector": resourceSumologicCollector(),
4952
"sumologic_http_source": resourceSumologicHTTPSource(),
5053
"sumologic_gcp_source": resourceSumologicGCPSource(),
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package sumologic
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
"log"
6+
)
7+
8+
func resourceSumologicCSEInsightsConfiguration() *schema.Resource {
9+
return &schema.Resource{
10+
Create: resourceSumologicCSEInsightsConfigurationCreate,
11+
Read: resourceSumologicCSEInsightsConfigurationRead,
12+
Delete: resourceSumologicCSEInsightsConfigurationDelete,
13+
Update: resourceSumologicCSEInsightsConfigurationUpdate,
14+
Importer: &schema.ResourceImporter{
15+
State: schema.ImportStatePassthrough,
16+
},
17+
18+
Schema: map[string]*schema.Schema{
19+
"lookback_days": {
20+
Type: schema.TypeFloat,
21+
Optional: true,
22+
ForceNew: false,
23+
},
24+
"threshold": {
25+
Type: schema.TypeFloat,
26+
Optional: true,
27+
ForceNew: false,
28+
},
29+
},
30+
}
31+
}
32+
33+
func resourceSumologicCSEInsightsConfigurationRead(d *schema.ResourceData, meta interface{}) error {
34+
c := meta.(*Client)
35+
36+
var CSEInsightsConfiguration *CSEInsightsConfiguration
37+
38+
CSEInsightsConfiguration, err := c.GetCSEInsightsConfiguration()
39+
if err != nil {
40+
log.Printf("[WARN] CSE Insights Configuration not found, err: %v", err)
41+
42+
}
43+
44+
if CSEInsightsConfiguration == nil {
45+
log.Printf("[WARN] CSE Insights Configuration not found, removing from state: %v", err)
46+
d.SetId("")
47+
return nil
48+
}
49+
50+
d.Set("lookback_days", CSEInsightsConfiguration.LookbackDays)
51+
d.Set("threshold", CSEInsightsConfiguration.Threshold)
52+
53+
return nil
54+
}
55+
56+
func resourceSumologicCSEInsightsConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
57+
CSEInsightsConfiguration := CSEInsightsConfiguration{}
58+
59+
c := meta.(*Client)
60+
err := c.UpdateCSEInsightsConfiguration(CSEInsightsConfiguration)
61+
if err != nil {
62+
return err
63+
}
64+
65+
return resourceSumologicCSEInsightsConfigurationRead(d, meta)
66+
}
67+
68+
func resourceSumologicCSEInsightsConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
69+
//we are not really creating new object in backend, using constant id for terraform resource
70+
d.SetId("cse-insights-configuration")
71+
return resourceSumologicCSEInsightsConfigurationUpdate(d, meta)
72+
}
73+
74+
func resourceSumologicCSEInsightsConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
75+
CSEInsightsConfiguration, err := resourceToCSEInsightsConfiguration(d)
76+
if err != nil {
77+
return err
78+
}
79+
80+
c := meta.(*Client)
81+
if err = c.UpdateCSEInsightsConfiguration(CSEInsightsConfiguration); err != nil {
82+
return err
83+
}
84+
85+
return resourceSumologicCSEInsightsConfigurationRead(d, meta)
86+
}
87+
88+
func resourceToCSEInsightsConfiguration(d *schema.ResourceData) (CSEInsightsConfiguration, error) {
89+
id := d.Id()
90+
if id == "" {
91+
return CSEInsightsConfiguration{}, nil
92+
}
93+
94+
lookbackDays := d.Get("lookback_days").(float64)
95+
threshold := d.Get("threshold").(float64)
96+
97+
return CSEInsightsConfiguration{
98+
LookbackDays: &lookbackDays,
99+
Threshold: &threshold,
100+
}, nil
101+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 TestAccSumologicSCEInsightsConfiguration_create(t *testing.T) {
12+
var insightConfiguration CSEInsightsConfiguration
13+
nLookbackDays := 10.0
14+
nThreshold := 13.0
15+
resourceName := "sumologic_cse_insights_configuration.insights_configuration"
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCSEInsightsConfigurationDestroy,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testCreateCSEInsightsConfigurationConfig(nLookbackDays, nThreshold),
23+
Check: resource.ComposeTestCheckFunc(
24+
testCheckCSEInsightsConfigurationExists(resourceName, &insightConfiguration),
25+
testCheckInsightsConfigurationValues(&insightConfiguration, nLookbackDays, nThreshold),
26+
resource.TestCheckResourceAttrSet(resourceName, "id"),
27+
),
28+
},
29+
},
30+
})
31+
}
32+
33+
func testAccCSEInsightsConfigurationDestroy(s *terraform.State) error {
34+
client := testAccProvider.Meta().(*Client)
35+
36+
for _, rs := range s.RootModule().Resources {
37+
if rs.Type != "sumologic_cse_insights_configuration" {
38+
continue
39+
}
40+
41+
if rs.Primary.ID == "" {
42+
return fmt.Errorf("CSE Insights Status destruction check: CSE Insights Status ID is not set")
43+
}
44+
45+
s, err := client.GetCSEInsightsConfiguration()
46+
if err != nil {
47+
return fmt.Errorf("Encountered an error: " + err.Error())
48+
}
49+
if s != nil {
50+
if s.Threshold != nil && s.LookbackDays != nil {
51+
return fmt.Errorf("insight Configuration still exists")
52+
}
53+
}
54+
}
55+
return nil
56+
}
57+
58+
func testCreateCSEInsightsConfigurationConfig(nLookbackDays float64, nThreshold float64) string {
59+
return fmt.Sprintf(`
60+
resource "sumologic_cse_insights_configuration" "insights_configuration" {
61+
lookback_days = "%f"
62+
threshold = "%f"
63+
}
64+
`, nLookbackDays, nThreshold)
65+
}
66+
67+
func testCheckCSEInsightsConfigurationExists(n string, insightConfiguration *CSEInsightsConfiguration) resource.TestCheckFunc {
68+
return func(s *terraform.State) error {
69+
rs, ok := s.RootModule().Resources[n]
70+
if !ok {
71+
return fmt.Errorf("not found: %s", n)
72+
}
73+
74+
if rs.Primary.ID == "" {
75+
return fmt.Errorf("insight Configuration ID is not set")
76+
}
77+
78+
c := testAccProvider.Meta().(*Client)
79+
insightConfigurationResp, err := c.GetCSEInsightsConfiguration()
80+
if err != nil {
81+
return err
82+
}
83+
84+
*insightConfiguration = *insightConfigurationResp
85+
86+
return nil
87+
}
88+
}
89+
90+
func testCheckInsightsConfigurationValues(insightConfiguration *CSEInsightsConfiguration, nLookbackDays float64, nThreshold float64) resource.TestCheckFunc {
91+
return func(s *terraform.State) error {
92+
if *insightConfiguration.LookbackDays != nLookbackDays {
93+
return fmt.Errorf("bad lookback days, expected \"%f\", got: %#v", nLookbackDays, insightConfiguration.LookbackDays)
94+
}
95+
if *insightConfiguration.Threshold != nThreshold {
96+
return fmt.Errorf("bad threshold, expected \"%f\", got: %#v", nThreshold, insightConfiguration.Threshold)
97+
}
98+
99+
return nil
100+
}
101+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
"strconv"
8+
)
9+
10+
const Resolved string = "Resolved"
11+
const FalsePositive string = "FalsePositive"
12+
const NoAction string = "NoAction"
13+
const Duplicate string = "Duplicate"
14+
15+
func resourceSumologicCSEInsightsResolution() *schema.Resource {
16+
return &schema.Resource{
17+
Create: resourceSumologicCSEInsightsResolutionCreate,
18+
Read: resourceSumologicCSEInsightsResolutionRead,
19+
Delete: resourceSumologicCSEInsightsResolutionDelete,
20+
Update: resourceSumologicCSEInsightsResolutionUpdate,
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
24+
25+
Schema: map[string]*schema.Schema{
26+
"description": {
27+
Type: schema.TypeString,
28+
Optional: true,
29+
ForceNew: false,
30+
},
31+
"name": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
ForceNew: false,
35+
},
36+
"parent": {
37+
Type: schema.TypeString,
38+
Optional: true,
39+
ForceNew: false,
40+
ValidateFunc: validation.StringInSlice([]string{Resolved, FalsePositive, NoAction, Duplicate}, false),
41+
},
42+
},
43+
}
44+
}
45+
46+
func resourceSumologicCSEInsightsResolutionRead(d *schema.ResourceData, meta interface{}) error {
47+
c := meta.(*Client)
48+
49+
var CSEInsightsResolutionGet *CSEInsightsResolutionGet
50+
id := d.Id()
51+
52+
CSEInsightsResolutionGet, err := c.GetCSEInsightsResolution(id)
53+
if err != nil {
54+
log.Printf("[WARN] CSE Insights Resolution not found when looking by id: %s, err: %v", id, err)
55+
56+
}
57+
58+
if CSEInsightsResolutionGet == nil {
59+
log.Printf("[WARN] CSE Insights Resolution not found, removing from state: %v - %v", id, err)
60+
d.SetId("")
61+
return nil
62+
}
63+
64+
d.Set("name", CSEInsightsResolutionGet.Name)
65+
d.Set("description", CSEInsightsResolutionGet.Description)
66+
d.Set("parent", parentIdToParentName(CSEInsightsResolutionGet.Parent.ID))
67+
68+
return nil
69+
}
70+
71+
func parentIdToParentName(parentId int) string {
72+
73+
parentName := ""
74+
75+
if parentId > 0 {
76+
if parentId == 1 {
77+
parentName = Resolved
78+
} else if parentId == 2 {
79+
parentName = FalsePositive
80+
} else if parentId == 3 {
81+
parentName = NoAction
82+
} else if parentId == 4 {
83+
parentName = Duplicate
84+
}
85+
}
86+
return parentName
87+
}
88+
89+
func parentNameToParentId(parentName string) int {
90+
91+
parentId := -1
92+
93+
if parentName == Resolved {
94+
parentId = 1
95+
} else if parentName == FalsePositive {
96+
parentId = 2
97+
} else if parentName == NoAction {
98+
parentId = 3
99+
} else if parentName == Duplicate {
100+
parentId = 4
101+
}
102+
103+
return parentId
104+
}
105+
106+
func resourceSumologicCSEInsightsResolutionDelete(d *schema.ResourceData, meta interface{}) error {
107+
c := meta.(*Client)
108+
109+
id := d.Id()
110+
return c.DeleteCSEInsightsResolution(id)
111+
112+
}
113+
114+
func resourceSumologicCSEInsightsResolutionCreate(d *schema.ResourceData, meta interface{}) error {
115+
c := meta.(*Client)
116+
117+
if d.Id() == "" {
118+
id, err := c.CreateCSEInsightsResolution(CSEInsightsResolutionPost{
119+
Name: d.Get("name").(string),
120+
Description: d.Get("description").(string),
121+
ParentId: parentNameToParentId(d.Get("parent").(string)),
122+
})
123+
124+
if err != nil {
125+
return err
126+
}
127+
d.SetId(strconv.Itoa(id))
128+
}
129+
130+
return resourceSumologicCSEInsightsResolutionUpdate(d, meta)
131+
}
132+
133+
func resourceSumologicCSEInsightsResolutionUpdate(d *schema.ResourceData, meta interface{}) error {
134+
CSEInsightsResolutionPost, err := resourceToCSEInsightsResolution(d)
135+
if err != nil {
136+
return err
137+
}
138+
139+
c := meta.(*Client)
140+
if err = c.UpdateCSEInsightsResolution(CSEInsightsResolutionPost); err != nil {
141+
return err
142+
}
143+
144+
return resourceSumologicCSEInsightsResolutionRead(d, meta)
145+
}
146+
147+
func resourceToCSEInsightsResolution(d *schema.ResourceData) (CSEInsightsResolutionPost, error) {
148+
id, err := strconv.Atoi(d.Id())
149+
if err != nil {
150+
return CSEInsightsResolutionPost{}, err
151+
}
152+
153+
return CSEInsightsResolutionPost{
154+
ID: id,
155+
Description: d.Get("description").(string),
156+
}, nil
157+
}

0 commit comments

Comments
 (0)