Skip to content

Commit 7055b89

Browse files
committed
Add helper for setting common fields from rules
1 parent e847f2e commit 7055b89

File tree

9 files changed

+305
-599
lines changed

9 files changed

+305
-599
lines changed

internal/kibana/security_detection_rule/models.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,3 +2938,186 @@ func (d *SecurityDetectionRuleData) updateSeverityMappingFromApi(ctx context.Con
29382938

29392939
return diags
29402940
}
2941+
2942+
// Helper function to update index patterns from API response
2943+
func (d *SecurityDetectionRuleData) updateIndexFromApi(ctx context.Context, index *[]string) diag.Diagnostics {
2944+
var diags diag.Diagnostics
2945+
2946+
if index != nil && len(*index) > 0 {
2947+
d.Index = utils.ListValueFrom(ctx, *index, types.StringType, path.Root("index"), &diags)
2948+
} else {
2949+
d.Index = types.ListValueMust(types.StringType, []attr.Value{})
2950+
}
2951+
2952+
return diags
2953+
}
2954+
2955+
// Helper function to update author from API response
2956+
func (d *SecurityDetectionRuleData) updateAuthorFromApi(ctx context.Context, author []string) diag.Diagnostics {
2957+
var diags diag.Diagnostics
2958+
2959+
if len(author) > 0 {
2960+
d.Author = utils.ListValueFrom(ctx, author, types.StringType, path.Root("author"), &diags)
2961+
} else {
2962+
d.Author = types.ListValueMust(types.StringType, []attr.Value{})
2963+
}
2964+
2965+
return diags
2966+
}
2967+
2968+
// Helper function to update tags from API response
2969+
func (d *SecurityDetectionRuleData) updateTagsFromApi(ctx context.Context, tags []string) diag.Diagnostics {
2970+
var diags diag.Diagnostics
2971+
2972+
if len(tags) > 0 {
2973+
d.Tags = utils.ListValueFrom(ctx, tags, types.StringType, path.Root("tags"), &diags)
2974+
} else {
2975+
d.Tags = types.ListValueMust(types.StringType, []attr.Value{})
2976+
}
2977+
2978+
return diags
2979+
}
2980+
2981+
// Helper function to update false positives from API response
2982+
func (d *SecurityDetectionRuleData) updateFalsePositivesFromApi(ctx context.Context, falsePositives []string) diag.Diagnostics {
2983+
var diags diag.Diagnostics
2984+
2985+
if len(falsePositives) > 0 {
2986+
d.FalsePositives = utils.ListValueFrom(ctx, falsePositives, types.StringType, path.Root("false_positives"), &diags)
2987+
} else {
2988+
d.FalsePositives = types.ListValueMust(types.StringType, []attr.Value{})
2989+
}
2990+
2991+
return diags
2992+
}
2993+
2994+
// Helper function to update references from API response
2995+
func (d *SecurityDetectionRuleData) updateReferencesFromApi(ctx context.Context, references []string) diag.Diagnostics {
2996+
var diags diag.Diagnostics
2997+
2998+
if len(references) > 0 {
2999+
d.References = utils.ListValueFrom(ctx, references, types.StringType, path.Root("references"), &diags)
3000+
} else {
3001+
d.References = types.ListValueMust(types.StringType, []attr.Value{})
3002+
}
3003+
3004+
return diags
3005+
}
3006+
3007+
// Helper function to update data view ID from API response
3008+
func (d *SecurityDetectionRuleData) updateDataViewIdFromApi(ctx context.Context, dataViewId *kbapi.SecurityDetectionsAPIDataViewId) diag.Diagnostics {
3009+
var diags diag.Diagnostics
3010+
3011+
if dataViewId != nil {
3012+
d.DataViewId = types.StringValue(string(*dataViewId))
3013+
} else {
3014+
d.DataViewId = types.StringNull()
3015+
}
3016+
3017+
return diags
3018+
}
3019+
3020+
// Helper function to update namespace from API response
3021+
func (d *SecurityDetectionRuleData) updateNamespaceFromApi(ctx context.Context, namespace *kbapi.SecurityDetectionsAPIAlertsIndexNamespace) diag.Diagnostics {
3022+
var diags diag.Diagnostics
3023+
3024+
if namespace != nil {
3025+
d.Namespace = types.StringValue(string(*namespace))
3026+
} else {
3027+
d.Namespace = types.StringNull()
3028+
}
3029+
3030+
return diags
3031+
}
3032+
3033+
// Helper function to update rule name override from API response
3034+
func (d *SecurityDetectionRuleData) updateRuleNameOverrideFromApi(ctx context.Context, ruleNameOverride *kbapi.SecurityDetectionsAPIRuleNameOverride) diag.Diagnostics {
3035+
var diags diag.Diagnostics
3036+
3037+
if ruleNameOverride != nil {
3038+
d.RuleNameOverride = types.StringValue(string(*ruleNameOverride))
3039+
} else {
3040+
d.RuleNameOverride = types.StringNull()
3041+
}
3042+
3043+
return diags
3044+
}
3045+
3046+
// Helper function to update timestamp override from API response
3047+
func (d *SecurityDetectionRuleData) updateTimestampOverrideFromApi(ctx context.Context, timestampOverride *kbapi.SecurityDetectionsAPITimestampOverride) diag.Diagnostics {
3048+
var diags diag.Diagnostics
3049+
3050+
if timestampOverride != nil {
3051+
d.TimestampOverride = types.StringValue(string(*timestampOverride))
3052+
} else {
3053+
d.TimestampOverride = types.StringNull()
3054+
}
3055+
3056+
return diags
3057+
}
3058+
3059+
// Helper function to update timestamp override fallback disabled from API response
3060+
func (d *SecurityDetectionRuleData) updateTimestampOverrideFallbackDisabledFromApi(ctx context.Context, timestampOverrideFallbackDisabled *kbapi.SecurityDetectionsAPITimestampOverrideFallbackDisabled) diag.Diagnostics {
3061+
var diags diag.Diagnostics
3062+
3063+
if timestampOverrideFallbackDisabled != nil {
3064+
d.TimestampOverrideFallbackDisabled = types.BoolValue(bool(*timestampOverrideFallbackDisabled))
3065+
} else {
3066+
d.TimestampOverrideFallbackDisabled = types.BoolNull()
3067+
}
3068+
3069+
return diags
3070+
}
3071+
3072+
// Helper function to update building block type from API response
3073+
func (d *SecurityDetectionRuleData) updateBuildingBlockTypeFromApi(ctx context.Context, buildingBlockType *kbapi.SecurityDetectionsAPIBuildingBlockType) diag.Diagnostics {
3074+
var diags diag.Diagnostics
3075+
3076+
if buildingBlockType != nil {
3077+
d.BuildingBlockType = types.StringValue(string(*buildingBlockType))
3078+
} else {
3079+
d.BuildingBlockType = types.StringNull()
3080+
}
3081+
3082+
return diags
3083+
}
3084+
3085+
// Helper function to update license from API response
3086+
func (d *SecurityDetectionRuleData) updateLicenseFromApi(ctx context.Context, license *kbapi.SecurityDetectionsAPIRuleLicense) diag.Diagnostics {
3087+
var diags diag.Diagnostics
3088+
3089+
if license != nil {
3090+
d.License = types.StringValue(string(*license))
3091+
} else {
3092+
d.License = types.StringNull()
3093+
}
3094+
3095+
return diags
3096+
}
3097+
3098+
// Helper function to update note from API response
3099+
func (d *SecurityDetectionRuleData) updateNoteFromApi(ctx context.Context, note *kbapi.SecurityDetectionsAPIInvestigationGuide) diag.Diagnostics {
3100+
var diags diag.Diagnostics
3101+
3102+
if note != nil {
3103+
d.Note = types.StringValue(string(*note))
3104+
} else {
3105+
d.Note = types.StringNull()
3106+
}
3107+
3108+
return diags
3109+
}
3110+
3111+
// Helper function to update setup from API response
3112+
func (d *SecurityDetectionRuleData) updateSetupFromApi(ctx context.Context, setup kbapi.SecurityDetectionsAPISetupGuide) diag.Diagnostics {
3113+
var diags diag.Diagnostics
3114+
3115+
// Handle setup field - if empty, set to null to maintain consistency with optional schema
3116+
if string(setup) != "" {
3117+
d.Setup = types.StringValue(string(setup))
3118+
} else {
3119+
d.Setup = types.StringNull()
3120+
}
3121+
3122+
return diags
3123+
}

internal/kibana/security_detection_rule/models_eql.go

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
88
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
99
"github.com/google/uuid"
10-
"github.com/hashicorp/terraform-plugin-framework/attr"
1110
"github.com/hashicorp/terraform-plugin-framework/diag"
12-
"github.com/hashicorp/terraform-plugin-framework/path"
1311
"github.com/hashicorp/terraform-plugin-framework/types"
1412
)
1513

@@ -178,35 +176,11 @@ func (d *SecurityDetectionRuleData) updateFromEqlRule(ctx context.Context, rule
178176
d.Type = types.StringValue(string(rule.Type))
179177

180178
// Update common fields
181-
if rule.DataViewId != nil {
182-
d.DataViewId = types.StringValue(string(*rule.DataViewId))
183-
} else {
184-
d.DataViewId = types.StringNull()
185-
}
186-
187-
if rule.Namespace != nil {
188-
d.Namespace = types.StringValue(string(*rule.Namespace))
189-
} else {
190-
d.Namespace = types.StringNull()
191-
}
192-
193-
if rule.RuleNameOverride != nil {
194-
d.RuleNameOverride = types.StringValue(string(*rule.RuleNameOverride))
195-
} else {
196-
d.RuleNameOverride = types.StringNull()
197-
}
198-
199-
if rule.TimestampOverride != nil {
200-
d.TimestampOverride = types.StringValue(string(*rule.TimestampOverride))
201-
} else {
202-
d.TimestampOverride = types.StringNull()
203-
}
204-
205-
if rule.TimestampOverrideFallbackDisabled != nil {
206-
d.TimestampOverrideFallbackDisabled = types.BoolValue(bool(*rule.TimestampOverrideFallbackDisabled))
207-
} else {
208-
d.TimestampOverrideFallbackDisabled = types.BoolNull()
209-
}
179+
diags.Append(d.updateDataViewIdFromApi(ctx, rule.DataViewId)...)
180+
diags.Append(d.updateNamespaceFromApi(ctx, rule.Namespace)...)
181+
diags.Append(d.updateRuleNameOverrideFromApi(ctx, rule.RuleNameOverride)...)
182+
diags.Append(d.updateTimestampOverrideFromApi(ctx, rule.TimestampOverride)...)
183+
diags.Append(d.updateTimestampOverrideFallbackDisabledFromApi(ctx, rule.TimestampOverrideFallbackDisabled)...)
210184

211185
d.Query = types.StringValue(rule.Query)
212186
d.Language = types.StringValue(string(rule.Language))
@@ -221,11 +195,7 @@ func (d *SecurityDetectionRuleData) updateFromEqlRule(ctx context.Context, rule
221195
d.Version = types.Int64Value(int64(rule.Version))
222196

223197
// Update building block type
224-
if rule.BuildingBlockType != nil {
225-
d.BuildingBlockType = types.StringValue(string(*rule.BuildingBlockType))
226-
} else {
227-
d.BuildingBlockType = types.StringNull()
228-
}
198+
diags.Append(d.updateBuildingBlockTypeFromApi(ctx, rule.BuildingBlockType)...)
229199

230200
// Update read-only fields
231201
d.CreatedAt = utils.TimeToStringValue(rule.CreatedAt)
@@ -235,59 +205,24 @@ func (d *SecurityDetectionRuleData) updateFromEqlRule(ctx context.Context, rule
235205
d.Revision = types.Int64Value(int64(rule.Revision))
236206

237207
// Update index patterns
238-
if rule.Index != nil && len(*rule.Index) > 0 {
239-
d.Index = utils.ListValueFrom(ctx, *rule.Index, types.StringType, path.Root("index"), &diags)
240-
} else {
241-
d.Index = types.ListValueMust(types.StringType, []attr.Value{})
242-
}
208+
diags.Append(d.updateIndexFromApi(ctx, rule.Index)...)
243209

244210
// Update author
245-
if len(rule.Author) > 0 {
246-
d.Author = utils.ListValueFrom(ctx, rule.Author, types.StringType, path.Root("author"), &diags)
247-
} else {
248-
d.Author = types.ListValueMust(types.StringType, []attr.Value{})
249-
}
211+
diags.Append(d.updateAuthorFromApi(ctx, rule.Author)...)
250212

251213
// Update tags
252-
if len(rule.Tags) > 0 {
253-
d.Tags = utils.ListValueFrom(ctx, rule.Tags, types.StringType, path.Root("tags"), &diags)
254-
} else {
255-
d.Tags = types.ListValueMust(types.StringType, []attr.Value{})
256-
}
214+
diags.Append(d.updateTagsFromApi(ctx, rule.Tags)...)
257215

258216
// Update false positives
259-
if len(rule.FalsePositives) > 0 {
260-
d.FalsePositives = utils.ListValueFrom(ctx, rule.FalsePositives, types.StringType, path.Root("false_positives"), &diags)
261-
} else {
262-
d.FalsePositives = types.ListValueMust(types.StringType, []attr.Value{})
263-
}
217+
diags.Append(d.updateFalsePositivesFromApi(ctx, rule.FalsePositives)...)
264218

265219
// Update references
266-
if len(rule.References) > 0 {
267-
d.References = utils.ListValueFrom(ctx, rule.References, types.StringType, path.Root("references"), &diags)
268-
} else {
269-
d.References = types.ListValueMust(types.StringType, []attr.Value{})
270-
}
220+
diags.Append(d.updateReferencesFromApi(ctx, rule.References)...)
271221

272222
// Update optional string fields
273-
if rule.License != nil {
274-
d.License = types.StringValue(string(*rule.License))
275-
} else {
276-
d.License = types.StringNull()
277-
}
278-
279-
if rule.Note != nil {
280-
d.Note = types.StringValue(string(*rule.Note))
281-
} else {
282-
d.Note = types.StringNull()
283-
}
284-
285-
// Handle setup field - if empty, set to null to maintain consistency with optional schema
286-
if string(rule.Setup) != "" {
287-
d.Setup = types.StringValue(string(rule.Setup))
288-
} else {
289-
d.Setup = types.StringNull()
290-
}
223+
diags.Append(d.updateLicenseFromApi(ctx, rule.License)...)
224+
diags.Append(d.updateNoteFromApi(ctx, rule.Note)...)
225+
diags.Append(d.updateSetupFromApi(ctx, rule.Setup)...)
291226

292227
// EQL-specific fields
293228
if rule.TiebreakerField != nil {

0 commit comments

Comments
 (0)