Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INVS-2397-cse-log-mapping-import-fix #675

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.31.2 (Unreleased)

BUG FIXES:
* Fix cse_log_mappings resource conversion affecting import (GH-675)

## 2.31.1 (July 2, 2024)
BUG FIXES:
Expand Down
37 changes: 26 additions & 11 deletions sumologic/resource_sumologic_cse_log_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"strconv"
)

func resourceSumologicCSELogMapping() *schema.Resource {
Expand Down Expand Up @@ -351,11 +352,17 @@ func resourceToCSELogMappingField(data interface{}) CSELogMappingField {
field.SkippedValues = resourceStringArrayToStringArray(fieldObj["skipped_values"].([]interface{}))
field.DefaultValue = fieldObj["default_value"].(string)
field.Format = fieldObj["format"].(string)
field.CaseInsensitive = fieldObj["case_insensitive"].(bool)
caseInsensitive := fieldObj["case_insensitive"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also safeguarding here for bool

if caseInsensitive != nil {
field.CaseInsensitive = fieldObj["case_insensitive"].(bool)
}
field.AlternateValues = resourceStringArrayToStringArray(fieldObj["alternate_values"].([]interface{}))
field.TimeZone = fieldObj["time_zone"].(string)
field.SplitDelimiter = fieldObj["split_delimiter"].(string)
field.SplitIndex = fmt.Sprint(fieldObj["split_index"].(int))
splitIndex := fieldObj["split_index"]
if splitIndex != nil {
field.SplitIndex = fmt.Sprint(splitIndex.(int))
}
field.FieldJoin = resourceStringArrayToStringArray(fieldObj["field_join"].([]interface{}))
field.JoinDelimiter = fieldObj["join_delimiter"].(string)
field.FormatParameters = resourceStringArrayToStringArray(fieldObj["format_parameters"].([]interface{}))
Expand All @@ -366,7 +373,7 @@ func resourceToCSELogMappingField(data interface{}) CSELogMappingField {
lookup = append(lookup, resourceToCSELogMappingLookUp([]interface{}{data}))
}

field.LookUp = lookup
field.LookUp = &lookup

}
return field
Expand Down Expand Up @@ -424,6 +431,7 @@ func setFields(d *schema.ResourceData, fields []CSELogMappingField) {
var f []map[string]interface{}

for _, t := range fields {

mapping := map[string]interface{}{
"name": t.Name,
"value": t.Value,
Expand All @@ -435,28 +443,35 @@ func setFields(d *schema.ResourceData, fields []CSELogMappingField) {
"alternate_values": t.AlternateValues,
"time_zone": t.TimeZone,
"split_delimiter": t.SplitDelimiter,
"split_index": t.SplitIndex,
"field_join": t.FieldJoin,
"join_delimiter": t.JoinDelimiter,
"format_parameters": t.FormatParameters,
"lookup": getLookUpResource(t.LookUp),
}

splitIndex, err := strconv.Atoi(t.SplitIndex)
if err == nil {
mapping["split_index"] = splitIndex
}

f = append(f, mapping)

Comment on lines +451 to +458
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what fixed the import functionality

}

d.Set("fields", f)

}

func getLookUpResource(lookUp []CSELogMappingLookUp) []map[string]interface{} {
func getLookUpResource(lookUp *[]CSELogMappingLookUp) []map[string]interface{} {
var s []map[string]interface{}

for _, l := range lookUp {
mapping := map[string]interface{}{
"key": l.Key,
"value": l.Value,
if lookUp != nil {
for _, l := range *lookUp {
mapping := map[string]interface{}{
"key": l.Key,
"value": l.Value,
}
s = append(s, mapping)
}
s = append(s, mapping)
}

return s
Expand Down
7 changes: 5 additions & 2 deletions sumologic/resource_sumologic_cse_log_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAccSumologicSCELogMapping_create(t *testing.T) {
AlternateValues: []string{"altValue"},
TimeZone: "UTC",
SplitDelimiter: ",",
SplitIndex: "0",
SplitIndex: "1",
FieldJoin: []string{"and"},
JoinDelimiter: "",
FormatParameters: []string{"param"},
Expand Down Expand Up @@ -245,6 +245,9 @@ func testCheckLogMappingValues(logMapping *CSELogMapping, lmName string, lmRecor
if logMapping.SkippedValues[0] != lmSkippedValues {
return fmt.Errorf("bad skippedValues, expected \"%s\", got: %#v", lmSkippedValues, logMapping.SkippedValues[0])
}

lookup := logMapping.Fields[0].LookUp

if logMapping.Fields[0].Name != lmField.Name ||
logMapping.Fields[0].Value != lmField.Value ||
logMapping.Fields[0].ValueType != lmField.ValueType ||
Expand All @@ -258,7 +261,7 @@ func testCheckLogMappingValues(logMapping *CSELogMapping, lmName string, lmRecor
logMapping.Fields[0].SplitIndex != lmField.SplitIndex ||
logMapping.Fields[0].FieldJoin[0] != lmField.FieldJoin[0] ||
logMapping.Fields[0].JoinDelimiter != lmField.JoinDelimiter ||
logMapping.Fields[0].LookUp[0].Key != lmLookUp.Key || logMapping.Fields[0].LookUp[0].Value != lmLookUp.Value {
(*lookup)[0].Key != lmLookUp.Key || (*lookup)[0].Value != lmLookUp.Value {

return fmt.Errorf("bad field, expected \"%#v\", got: %#v", lmField, logMapping.Fields[0])
}
Expand Down
30 changes: 15 additions & 15 deletions sumologic/sumologic_cse_log_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,21 @@ type CSELogMapping struct {
}

type CSELogMappingField struct {
Name string `json:"name"`
Value string `json:"value"`
ValueType string `json:"valueType"`
SkippedValues []string `json:"skippedValues"`
DefaultValue string `json:"defaultValue"`
Format string `json:"format"`
CaseInsensitive bool `json:"caseInsensitive"`
AlternateValues []string `json:"alternateValues"`
TimeZone string `json:"timeZone"`
SplitDelimiter string `json:"splitDelimiter"`
SplitIndex string `json:"splitIndex"`
FieldJoin []string `json:"fieldJoin"`
JoinDelimiter string `json:"joinDelimiter"`
FormatParameters []string `json:"formatParameters"`
LookUp []CSELogMappingLookUp `json:"lookup"`
Name string `json:"name"`
Value string `json:"value,omitempty"`
ValueType string `json:"valueType,omitempty"`
SkippedValues []string `json:"skippedValues,omitempty"`
DefaultValue string `json:"defaultValue,omitempty"`
Format string `json:"format,omitempty"`
CaseInsensitive bool `json:"caseInsensitive,omitempty"`
AlternateValues []string `json:"alternateValues,omitempty"`
TimeZone string `json:"timeZone,omitempty"`
SplitDelimiter string `json:"splitDelimiter,omitempty"`
SplitIndex string `json:"splitIndex,omitempty"`
FieldJoin []string `json:"fieldJoin,omitempty"`
JoinDelimiter string `json:"joinDelimiter,omitempty"`
FormatParameters []string `json:"formatParameters,omitempty"`
LookUp *[]CSELogMappingLookUp `json:"lookup,omitempty"`
}

type CSELogMappingLookUp struct {
Expand Down
Loading