Skip to content

Commit dd3cadc

Browse files
DET-362: Add rule tuning expression import. (#612)
* DET-362: Add rule tuning expression import test. * DET-362: Change ruleIds resource type to Set to properly handle unsorted collections. * DET-362: Improved assertions for cse rule tuning expression. * DET-362: Added changelog entry.
1 parent 7685d51 commit dd3cadc

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 2.28.2 (Unreleased)
22

3+
BUG FIXES:
4+
* Uses the recently added API support for `rule_ids` for CSE Rule Tuning Expressions to fix the resource import functionality. (GH-612)
35

46
## 2.28.1 (January 19, 2024)
57

sumologic/resource_sumologic_cse_rule_tuning_expression.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func resourceSumologicCSERuleTuningExpression() *schema.Resource {
4141
Required: true,
4242
},
4343
"rule_ids": {
44-
Type: schema.TypeList,
44+
Type: schema.TypeSet,
4545
Required: true,
4646
Elem: &schema.Schema{
4747
Type: schema.TypeString,
@@ -75,7 +75,7 @@ func resourceSumologicCSERuleTuningExpressionRead(d *schema.ResourceData, meta i
7575
d.Set("enabled", CSERuleTuningExpressionGet.Enabled)
7676
d.Set("exclude", CSERuleTuningExpressionGet.Exclude)
7777
d.Set("is_global", CSERuleTuningExpressionGet.IsGlobal)
78-
d.Set("rule_Ids", CSERuleTuningExpressionGet.RuleIds)
78+
d.Set("rule_ids", CSERuleTuningExpressionGet.RuleIds)
7979

8080
return nil
8181
}
@@ -98,7 +98,7 @@ func resourceSumologicCSERuleTuningExpressionCreate(d *schema.ResourceData, meta
9898
Enabled: d.Get("enabled").(bool),
9999
Exclude: d.Get("exclude").(bool),
100100
IsGlobal: d.Get("is_global").(bool),
101-
RuleIds: resourceRuleIdsToStringArray(d.Get("rule_ids").([]interface{})),
101+
RuleIds: resourceRuleIdsToStringArray(d.Get("rule_ids").(*schema.Set)),
102102
})
103103

104104
if err != nil {
@@ -125,11 +125,11 @@ func resourceSumologicCSERuleTuningExpressionUpdate(d *schema.ResourceData, meta
125125
return resourceSumologicCSERuleTuningExpressionRead(d, meta)
126126
}
127127

128-
func resourceRuleIdsToStringArray(resourceRuleIds []interface{}) []string {
129-
ruleIds := make([]string, len(resourceRuleIds))
130-
131-
for i, ruleId := range resourceRuleIds {
132-
ruleIds[i] = ruleId.(string)
128+
func resourceRuleIdsToStringArray(resourceRuleIds *schema.Set) []string {
129+
rawRuleIds := resourceRuleIds.List()
130+
ruleIds := make([]string, len(rawRuleIds))
131+
for i, v := range rawRuleIds {
132+
ruleIds[i] = v.(string)
133133
}
134134

135135
return ruleIds
@@ -149,6 +149,6 @@ func resourceToCSERuleTuningExpression(d *schema.ResourceData) (CSERuleTuningExp
149149
Enabled: d.Get("enabled").(bool),
150150
Exclude: d.Get("exclude").(bool),
151151
IsGlobal: d.Get("is_global").(bool),
152-
RuleIds: resourceRuleIdsToStringArray(d.Get("rule_ids").([]interface{})),
152+
RuleIds: resourceRuleIdsToStringArray(d.Get("rule_ids").(*schema.Set)),
153153
}, nil
154154
}

sumologic/resource_sumologic_cse_rule_tuning_expression_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package sumologic
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
89
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10+
"github.com/stretchr/testify/assert"
911
)
1012

1113
func TestAccSumologicSCERuleTuningExpression_create(t *testing.T) {
@@ -18,7 +20,7 @@ func TestAccSumologicSCERuleTuningExpression_create(t *testing.T) {
1820
nEnabled := true
1921
nExclude := true
2022
nIsGlobal := false
21-
nRuleIds := []string{"LEGACY-S00084"}
23+
nRuleIds := []string{"LEGACY-S00084", "THRESHOLD-S00514", "AGGREGATION-S00002"}
2224
resourceName := "sumologic_cse_rule_tuning_expression.rule_tuning_expression"
2325
resource.Test(t, resource.TestCase{
2426
PreCheck: func() { testAccPreCheck(t) },
@@ -29,10 +31,15 @@ func TestAccSumologicSCERuleTuningExpression_create(t *testing.T) {
2931
Config: testCreateCSERuleTuningExpressionConfig(nName, nDescription, nExpression, nEnabled, nExclude, nIsGlobal, nRuleIds),
3032
Check: resource.ComposeTestCheckFunc(
3133
testCheckCSERuleTuningExpressionExists(resourceName, &ruleTuningExpression),
32-
testCheckRuleTuningExpressionValues(&ruleTuningExpression, nName, nDescription, nExpression, nEnabled, nExclude, nIsGlobal),
34+
testCheckRuleTuningExpressionValues(t, &ruleTuningExpression, nName, nDescription, nExpression, nEnabled, nExclude, nIsGlobal, nRuleIds),
3335
resource.TestCheckResourceAttrSet(resourceName, "id"),
3436
),
3537
},
38+
{
39+
ResourceName: resourceName,
40+
ImportState: true,
41+
ImportStateVerify: true,
42+
},
3643
},
3744
})
3845
}
@@ -61,6 +68,11 @@ func testAccCSERuleTuningExpressionDestroy(s *terraform.State) error {
6168
}
6269

6370
func testCreateCSERuleTuningExpressionConfig(nName string, nDescription string, nExpression string, nEnabled bool, nExclude bool, nIsGlobal bool, nRuleIds []string) string {
71+
quotedRuleIds := make([]string, len(nRuleIds))
72+
for i, id := range nRuleIds {
73+
quotedRuleIds[i] = fmt.Sprintf(`"%s"`, id)
74+
}
75+
6476
return fmt.Sprintf(`
6577
resource "sumologic_cse_rule_tuning_expression" "rule_tuning_expression" {
6678
name = "%s"
@@ -69,9 +81,9 @@ resource "sumologic_cse_rule_tuning_expression" "rule_tuning_expression" {
6981
enabled = "%t"
7082
exclude = "%t"
7183
is_global = "%t"
72-
rule_ids = ["%s"]
84+
rule_ids = [%s]
7385
}
74-
`, nName, nDescription, nExpression, nEnabled, nExclude, nIsGlobal, nRuleIds[0])
86+
`, nName, nDescription, nExpression, nEnabled, nExclude, nIsGlobal, strings.Join(quotedRuleIds, ", "))
7587
}
7688

7789
func testCheckCSERuleTuningExpressionExists(n string, ruleTuningExpression *CSERuleTuningExpression) resource.TestCheckFunc {
@@ -97,27 +109,15 @@ func testCheckCSERuleTuningExpressionExists(n string, ruleTuningExpression *CSER
97109
}
98110
}
99111

100-
func testCheckRuleTuningExpressionValues(ruleTuningExpression *CSERuleTuningExpression, nName string, nDescription string, nExpression string, nEnabled bool, nExclude bool, nIsGlobal bool) resource.TestCheckFunc {
112+
func testCheckRuleTuningExpressionValues(t *testing.T, ruleTuningExpression *CSERuleTuningExpression, nName string, nDescription string, nExpression string, nEnabled bool, nExclude bool, nIsGlobal bool, nRuleIds []string) resource.TestCheckFunc {
101113
return func(s *terraform.State) error {
102-
if ruleTuningExpression.Name != nName {
103-
return fmt.Errorf("bad name, expected \"%s\", got: %#v", nName, ruleTuningExpression.Name)
104-
}
105-
if ruleTuningExpression.Description != nDescription {
106-
return fmt.Errorf("bad description, expected \"%s\", got: %#v", nDescription, ruleTuningExpression.Description)
107-
}
108-
if ruleTuningExpression.Expression != nExpression {
109-
return fmt.Errorf("bad expression, expected \"%s\", got: %#v", nExpression, ruleTuningExpression.Expression)
110-
}
111-
if ruleTuningExpression.Enabled != nEnabled {
112-
return fmt.Errorf("bad enabled flag, expected \"%t\", got: %#v", nEnabled, ruleTuningExpression.Enabled)
113-
}
114-
if ruleTuningExpression.Exclude != nExclude {
115-
return fmt.Errorf("bad exclude flag, expected \"%t\", got: %#v", nExclude, ruleTuningExpression.Exclude)
116-
}
117-
if ruleTuningExpression.IsGlobal != nIsGlobal {
118-
return fmt.Errorf("bad isGlobal flag, expected \"%t\", got: %#v", nIsGlobal, ruleTuningExpression.IsGlobal)
119-
}
120-
114+
assert.Equal(t, nName, ruleTuningExpression.Name)
115+
assert.Equal(t, nDescription, ruleTuningExpression.Description)
116+
assert.Equal(t, nExpression, ruleTuningExpression.Expression)
117+
assert.Equal(t, nEnabled, ruleTuningExpression.Enabled)
118+
assert.Equal(t, nExclude, ruleTuningExpression.Exclude)
119+
assert.Equal(t, nIsGlobal, ruleTuningExpression.IsGlobal)
120+
assert.ElementsMatch(t, nRuleIds, ruleTuningExpression.RuleIds)
121121
return nil
122122
}
123123
}

0 commit comments

Comments
 (0)