Skip to content

Commit 0dd46ff

Browse files
achugh-sumodineshmeka701557
authored andcommitted
SUMO-275219: Fix breaking test
1 parent a19a9a2 commit 0dd46ff

File tree

3 files changed

+101
-57
lines changed

3 files changed

+101
-57
lines changed

sumologic/resource_sumologic_event_extraction_rule.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,35 @@ func resourceSumologicEventExtractionRule() *schema.Resource {
6767
},
6868

6969
"configuration": {
70-
Type: schema.TypeList,
70+
Type: schema.TypeSet,
7171
Required: true,
72-
Description: "Structured field mappings for the extraction rule.",
72+
Description: "Field mappings for the extraction rule. Each block represents a field with its value_source and optional mapping_type.",
7373
Elem: &schema.Resource{
7474
Schema: map[string]*schema.Schema{
7575
"field_name": {
7676
Type: schema.TypeString,
7777
Required: true,
78-
Description: "The name of the field being mapped.",
78+
Description: "The name of the field (e.g., eventType, eventName).",
7979
},
8080
"value_source": {
81-
Type: schema.TypeString,
82-
Required: true,
81+
Type: schema.TypeString,
82+
Required: true,
83+
Description: "The source value for the field.",
8384
},
8485
"mapping_type": {
8586
Type: schema.TypeString,
8687
Optional: true,
8788
Default: "HardCoded",
89+
Description: "The mapping type for the field. Defaults to 'HardCoded'.",
8890
ValidateFunc: validation.StringInSlice([]string{"HardCoded"}, false),
8991
},
9092
},
9193
},
94+
Set: func(v interface{}) int {
95+
// Use field_name as the unique identifier for the set
96+
m := v.(map[string]interface{})
97+
return schema.HashString(m["field_name"].(string))
98+
},
9299
},
93100
},
94101
}
@@ -174,13 +181,19 @@ func resourceToEventExtractionRule(d *schema.ResourceData) EventExtractionRule {
174181

175182
config := make(map[string]FieldMapping)
176183
if v, ok := d.GetOk("configuration"); ok {
177-
configs := v.([]interface{})
178-
for _, raw := range configs {
179-
val := raw.(map[string]interface{})
180-
fieldName := val["field_name"].(string)
184+
configSet := v.(*schema.Set)
185+
for _, raw := range configSet.List() {
186+
item := raw.(map[string]interface{})
187+
fieldName := item["field_name"].(string)
188+
189+
mappingType := "HardCoded"
190+
if mt, ok := item["mapping_type"].(string); ok && mt != "" {
191+
mappingType = mt
192+
}
193+
181194
config[fieldName] = FieldMapping{
182-
ValueSource: val["value_source"].(string),
183-
MappingType: val["mapping_type"].(string),
195+
ValueSource: item["value_source"].(string),
196+
MappingType: mappingType,
184197
}
185198
}
186199
}
@@ -199,14 +212,21 @@ func flattenCorrelationExpression(ce *CorrelationExpression) []interface{} {
199212
}
200213
}
201214

202-
func flattenConfiguration(config map[string]FieldMapping) []interface{} {
203-
var result []interface{}
204-
for key, val := range config {
205-
result = append(result, map[string]interface{}{
206-
"field_name": key,
207-
"value_source": val.ValueSource,
208-
"mapping_type": val.MappingType,
215+
func flattenConfiguration(config map[string]FieldMapping) *schema.Set {
216+
// Create a set using the same hash function as the schema
217+
setFunc := func(v interface{}) int {
218+
m := v.(map[string]interface{})
219+
return schema.HashString(m["field_name"].(string))
220+
}
221+
222+
result := &schema.Set{F: setFunc}
223+
for fieldName, fieldMapping := range config {
224+
result.Add(map[string]interface{}{
225+
"field_name": fieldName,
226+
"value_source": fieldMapping.ValueSource,
227+
"mapping_type": fieldMapping.MappingType,
209228
})
210229
}
230+
211231
return result
212232
}

sumologic/resource_sumologic_event_extraction_rule_test.go

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

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

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -22,16 +23,23 @@ func TestAccSumologicEventExtractionRule_crud(t *testing.T) {
2223
{
2324
Config: testAccSumologicEventExtractionRule(name),
2425
Check: resource.ComposeTestCheckFunc(
26+
// Validate basic attributes
27+
resource.TestCheckResourceAttrSet(resourceName, "id"),
2528
resource.TestCheckResourceAttr(resourceName, "name", name),
2629
resource.TestCheckResourceAttr(resourceName, "query", "_sourceCategory=deployments"),
2730
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
2831

32+
// Validate configuration count
33+
resource.TestCheckResourceAttr(resourceName, "configuration.#", "4"),
34+
35+
// Validate individual configuration fields (order may vary, so we check using TypeSetElemNestedAttrs)
2936
resource.TestCheckTypeSetElemNestedAttrs(
3037
resourceName,
3138
"configuration.*",
3239
map[string]string{
3340
"field_name": "eventType",
3441
"value_source": "Deployment",
42+
"mapping_type": "HardCoded",
3543
},
3644
),
3745
resource.TestCheckTypeSetElemNestedAttrs(
@@ -40,6 +48,7 @@ func TestAccSumologicEventExtractionRule_crud(t *testing.T) {
4048
map[string]string{
4149
"field_name": "eventPriority",
4250
"value_source": "High",
51+
"mapping_type": "HardCoded",
4352
},
4453
),
4554
resource.TestCheckTypeSetElemNestedAttrs(
@@ -48,6 +57,7 @@ func TestAccSumologicEventExtractionRule_crud(t *testing.T) {
4857
map[string]string{
4958
"field_name": "eventSource",
5059
"value_source": "Jenkins",
60+
"mapping_type": "HardCoded",
5161
},
5262
),
5363
resource.TestCheckTypeSetElemNestedAttrs(
@@ -56,22 +66,31 @@ func TestAccSumologicEventExtractionRule_crud(t *testing.T) {
5666
map[string]string{
5767
"field_name": "eventName",
5868
"value_source": "monitor-manager deployed",
69+
"mapping_type": "HardCoded",
5970
},
6071
),
6172
),
6273
},
6374
{
6475
Config: testAccSumologicEventExtractionRuleUpdate(name),
6576
Check: resource.ComposeTestCheckFunc(
77+
// Validate basic attributes
78+
resource.TestCheckResourceAttrSet(resourceName, "id"),
79+
resource.TestCheckResourceAttr(resourceName, "name", name),
6680
resource.TestCheckResourceAttr(resourceName, "description", "updated description"),
67-
resource.TestCheckResourceAttr(resourceName, "enabled", "false"),
81+
resource.TestCheckResourceAttr(resourceName, "query", "_sourceCategory=deployments"),
82+
83+
// Validate configuration count (now 5 fields)
84+
resource.TestCheckResourceAttr(resourceName, "configuration.#", "5"),
6885

86+
// Validate the new eventDescription field was added
6987
resource.TestCheckTypeSetElemNestedAttrs(
7088
resourceName,
7189
"configuration.*",
7290
map[string]string{
7391
"field_name": "eventDescription",
7492
"value_source": "2 containers upgraded",
93+
"mapping_type": "HardCoded",
7594
},
7695
),
7796
),
@@ -117,7 +136,6 @@ resource "sumologic_event_extraction_rule" "test" {
117136
name = "%s"
118137
description = "updated description"
119138
query = "_sourceCategory=deployments"
120-
enabled = false
121139
122140
configuration {
123141
field_name = "eventType"
@@ -153,6 +171,10 @@ func testAccCheckEventExtractionRuleDestroy(s *terraform.State) error {
153171

154172
found, err := client.GetEventExtractionRule(r.Primary.ID)
155173
if err != nil {
174+
// If the error is "not found" or "record_not_found", the resource was successfully destroyed
175+
if strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "record_not_found") {
176+
continue
177+
}
156178
return err
157179
}
158180
if found != nil {

website/docs/r/event_extraction_rule.html.markdown

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ resource "sumologic_event_extraction_rule" "deployment_event" {
2121
name = "deployment-event"
2222
query = "_sourceCategory=deployments"
2323
24-
configuration = {
25-
eventType = {
26-
value_source = "Deployment"
27-
}
28-
eventPriority = {
29-
value_source = "High"
30-
}
31-
eventSource = {
32-
value_source = "Jenkins"
33-
}
34-
eventName = {
35-
value_source = "monitor-manager deployed"
36-
}
24+
configuration {
25+
field_name = "eventType"
26+
value_source = "Deployment"
27+
}
28+
configuration {
29+
field_name = "eventPriority"
30+
value_source = "High"
31+
}
32+
configuration {
33+
field_name = "eventSource"
34+
value_source = "Jenkins"
35+
}
36+
configuration {
37+
field_name = "eventName"
38+
value_source = "monitor-manager deployed"
3739
}
3840
}
3941
```
@@ -53,22 +55,25 @@ resource "sumologic_event_extraction_rule" "deployment_event" {
5355
string_matching_algorithm = "ExactMatch"
5456
}
5557
56-
configuration = {
57-
eventType = {
58-
value_source = "Deployment"
59-
}
60-
eventPriority = {
61-
value_source = "High"
62-
}
63-
eventSource = {
64-
value_source = "Jenkins"
65-
}
66-
eventName = {
67-
value_source = "monitor-manager deployed"
68-
}
69-
eventDescription = {
70-
value_source = "2 containers were upgraded"
71-
}
58+
configuration {
59+
field_name = "eventType"
60+
value_source = "Deployment"
61+
}
62+
configuration {
63+
field_name = "eventPriority"
64+
value_source = "High"
65+
}
66+
configuration {
67+
field_name = "eventSource"
68+
value_source = "Jenkins"
69+
}
70+
configuration {
71+
field_name = "eventName"
72+
value_source = "monitor-manager deployed"
73+
}
74+
configuration {
75+
field_name = "eventDescription"
76+
value_source = "2 containers upgraded"
7277
}
7378
}
7479
```
@@ -82,7 +87,7 @@ The following arguments are supported:
8287
- `query` - (Required) Log query used to extract events.
8388
- `enabled` - (Optional) Whether the macro will be enabled. Default true.
8489
- `correlation_expression` - (Block List, Optional) Specifies how to determine related events for a log search query. See [correlationExpression schema](#schema-for-correlationExpression) for details.
85-
- `configuration` - (Block List, Optional) Defines how event fields are mapped to their corresponding values. See [configuration schema](#schema-for-configuration)
90+
- `configuration` - (Block List, Required) Defines how event fields are mapped to their corresponding values. See [configuration schema](#schema-for-configuration)
8691
for details.
8792

8893
## Attributes reference
@@ -97,17 +102,15 @@ In addition to all arguments above, the following attributes are exported:
97102

98103
### Schema for `configuration`
99104

100-
- `configuration` - (Required, Map) This object defines how event fields are mapped to their corresponding values. Each field specifies a valueSource, which provides the actual value, and an optional mappingType, indicating the value is hardcoded.
101-
102-
#### Configuration field mapping
105+
The `configuration` block can be repeated multiple times to define event field mappings. Each block supports:
103106

107+
- `field_name` - (Required) The name of the event field being configured.
104108
- `value_source` - (Required) The value or extracted field used for the event field.
105-
- `mapping_type` - (Optional) Specifies how the value is mapped.
106-
Supported value: `HardCoded` (default)
109+
- `mapping_type` - (Optional) Specifies how the value is mapped. Defaults to `HardCoded`.
107110

108111
#### Required event fields
109112

110-
The following keys **must** be defined inside the `configuration` map:
113+
The following `field_name` values **must** be defined:
111114

112115
- `eventType` - (Required) Type of the event.
113116
Accepted values: `Deployment`, `Feature Flag Change`, `Configuration Change`, `Infrastructure Change`
@@ -119,4 +122,3 @@ The following keys **must** be defined inside the `configuration` map:
119122
#### Optional event fields
120123

121124
- `eventDescription` - (Optional) Additional context or details about the event.
122-
- Custom fields - (Optional) Additional keys can be added to capture domain-specific event metadata.

0 commit comments

Comments
 (0)