Skip to content

Commit ca37831

Browse files
committed
Add response processor abstraction / Rearrange utilities
1 parent aa72fee commit ca37831

13 files changed

+2331
-1957
lines changed

internal/kibana/security_detection_rule/models.go

Lines changed: 0 additions & 1938 deletions
Large diffs are not rendered by default.

internal/kibana/security_detection_rule/models_eql.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,53 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/types"
1212
)
1313

14-
func (d SecurityDetectionRuleData) toEqlRuleCreateProps(ctx context.Context, client clients.MinVersionEnforceable) (kbapi.SecurityDetectionsAPIRuleCreateProps, diag.Diagnostics) {
14+
type EqlRuleProcessor struct{}
15+
16+
func (e EqlRuleProcessor) HandlesRuleType(t string) bool {
17+
return t == "eql"
18+
}
19+
20+
func (e EqlRuleProcessor) ToCreateProps(ctx context.Context, client clients.MinVersionEnforceable, d SecurityDetectionRuleData) (kbapi.SecurityDetectionsAPIRuleCreateProps, diag.Diagnostics) {
21+
return toEqlRuleCreateProps(ctx, client, d)
22+
}
23+
24+
func (e EqlRuleProcessor) ToUpdateProps(ctx context.Context, client clients.MinVersionEnforceable, d SecurityDetectionRuleData) (kbapi.SecurityDetectionsAPIRuleUpdateProps, diag.Diagnostics) {
25+
return toEqlRuleUpdateProps(ctx, client, d)
26+
}
27+
28+
func (e EqlRuleProcessor) HandlesAPIRuleResponse(rule any) bool {
29+
_, ok := rule.(kbapi.SecurityDetectionsAPIEqlRule)
30+
return ok
31+
}
32+
33+
func (e EqlRuleProcessor) UpdateFromResponse(ctx context.Context, rule any, d *SecurityDetectionRuleData) diag.Diagnostics {
34+
var diags diag.Diagnostics
35+
value, ok := rule.(kbapi.SecurityDetectionsAPIEqlRule)
36+
if !ok {
37+
diags.AddError(
38+
"Error extracting rule ID",
39+
"Could not extract rule ID from response",
40+
)
41+
return diags
42+
}
43+
44+
return updateFromEqlRule(ctx, &value, d)
45+
}
46+
47+
func (e EqlRuleProcessor) ExtractId(response any) (string, diag.Diagnostics) {
48+
var diags diag.Diagnostics
49+
value, ok := response.(kbapi.SecurityDetectionsAPIEqlRule)
50+
if !ok {
51+
diags.AddError(
52+
"Error extracting rule ID",
53+
"Could not extract rule ID from response",
54+
)
55+
return "", diags
56+
}
57+
return value.Id.String(), diags
58+
}
59+
60+
func toEqlRuleCreateProps(ctx context.Context, client clients.MinVersionEnforceable, d SecurityDetectionRuleData) (kbapi.SecurityDetectionsAPIRuleCreateProps, diag.Diagnostics) {
1561
var diags diag.Diagnostics
1662
var createProps kbapi.SecurityDetectionsAPIRuleCreateProps
1763

@@ -77,7 +123,7 @@ func (d SecurityDetectionRuleData) toEqlRuleCreateProps(ctx context.Context, cli
77123

78124
return createProps, diags
79125
}
80-
func (d SecurityDetectionRuleData) toEqlRuleUpdateProps(ctx context.Context, client clients.MinVersionEnforceable) (kbapi.SecurityDetectionsAPIRuleUpdateProps, diag.Diagnostics) {
126+
func toEqlRuleUpdateProps(ctx context.Context, client clients.MinVersionEnforceable, d SecurityDetectionRuleData) (kbapi.SecurityDetectionsAPIRuleUpdateProps, diag.Diagnostics) {
81127
var diags diag.Diagnostics
82128
var updateProps kbapi.SecurityDetectionsAPIRuleUpdateProps
83129

@@ -162,7 +208,7 @@ func (d SecurityDetectionRuleData) toEqlRuleUpdateProps(ctx context.Context, cli
162208

163209
return updateProps, diags
164210
}
165-
func (d *SecurityDetectionRuleData) updateFromEqlRule(ctx context.Context, rule *kbapi.SecurityDetectionsAPIEqlRule) diag.Diagnostics {
211+
func updateFromEqlRule(ctx context.Context, rule *kbapi.SecurityDetectionsAPIEqlRule, d *SecurityDetectionRuleData) diag.Diagnostics {
166212
var diags diag.Diagnostics
167213

168214
compId := clients.CompositeId{

internal/kibana/security_detection_rule/models_esql.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,52 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/types"
1313
)
1414

15+
type EsqlRuleProcessor struct{}
16+
17+
func (e EsqlRuleProcessor) HandlesRuleType(t string) bool {
18+
return t == "esql"
19+
}
20+
21+
func (e EsqlRuleProcessor) ToCreateProps(ctx context.Context, client clients.MinVersionEnforceable, d SecurityDetectionRuleData) (kbapi.SecurityDetectionsAPIRuleCreateProps, diag.Diagnostics) {
22+
return d.toEsqlRuleCreateProps(ctx, client)
23+
}
24+
25+
func (e EsqlRuleProcessor) ToUpdateProps(ctx context.Context, client clients.MinVersionEnforceable, d SecurityDetectionRuleData) (kbapi.SecurityDetectionsAPIRuleUpdateProps, diag.Diagnostics) {
26+
return d.toEsqlRuleUpdateProps(ctx, client)
27+
}
28+
29+
func (e EsqlRuleProcessor) HandlesAPIRuleResponse(rule any) bool {
30+
_, ok := rule.(kbapi.SecurityDetectionsAPIEsqlRule)
31+
return ok
32+
}
33+
34+
func (e EsqlRuleProcessor) UpdateFromResponse(ctx context.Context, rule any, d *SecurityDetectionRuleData) diag.Diagnostics {
35+
var diags diag.Diagnostics
36+
value, ok := rule.(kbapi.SecurityDetectionsAPIEsqlRule)
37+
if !ok {
38+
diags.AddError(
39+
"Error extracting rule ID",
40+
"Could not extract rule ID from response",
41+
)
42+
return diags
43+
}
44+
45+
return d.updateFromEsqlRule(ctx, &value)
46+
}
47+
48+
func (e EsqlRuleProcessor) ExtractId(response any) (string, diag.Diagnostics) {
49+
var diags diag.Diagnostics
50+
value, ok := response.(kbapi.SecurityDetectionsAPIEsqlRule)
51+
if !ok {
52+
diags.AddError(
53+
"Error extracting rule ID",
54+
"Could not extract rule ID from response",
55+
)
56+
return "", diags
57+
}
58+
return value.Id.String(), diags
59+
}
60+
1561
func (d SecurityDetectionRuleData) toEsqlRuleCreateProps(ctx context.Context, client clients.MinVersionEnforceable) (kbapi.SecurityDetectionsAPIRuleCreateProps, diag.Diagnostics) {
1662
var diags diag.Diagnostics
1763
var createProps kbapi.SecurityDetectionsAPIRuleCreateProps

0 commit comments

Comments
 (0)