Skip to content

Commit 3ca7bb2

Browse files
Copilottobio
andcommitted
Use jsontypes.NormalizedType for query field and extract duplicate read logic
Co-authored-by: tobio <[email protected]>
1 parent c9c91d1 commit 3ca7bb2

File tree

4 files changed

+45
-52
lines changed

4 files changed

+45
-52
lines changed

internal/elasticsearch/enrich/data_source.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
88
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
99
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
10+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1011
"github.com/hashicorp/terraform-plugin-framework/datasource"
1112
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
12-
"github.com/hashicorp/terraform-plugin-framework/path"
1313
"github.com/hashicorp/terraform-plugin-framework/types"
1414

1515
providerschema "github.com/elastic/terraform-provider-elasticstack/internal/schema"
@@ -72,6 +72,7 @@ func GetDataSourceSchema() schema.Schema {
7272
},
7373
"query": schema.StringAttribute{
7474
MarkdownDescription: "Query used to filter documents in the enrich index for matching.",
75+
CustomType: jsontypes.NormalizedType{},
7576
Computed: true,
7677
},
7778
},
@@ -111,24 +112,8 @@ func (d *enrichPolicyDataSource) Read(ctx context.Context, req datasource.ReadRe
111112
return
112113
}
113114

114-
// Convert model to framework types
115-
data.Name = types.StringValue(policy.Name)
116-
data.PolicyType = types.StringValue(policy.Type)
117-
data.MatchField = types.StringValue(policy.MatchField)
118-
119-
if policy.Query != "" && policy.Query != "null" {
120-
data.Query = types.StringValue(policy.Query)
121-
} else {
122-
data.Query = types.StringNull()
123-
}
124-
125-
// Convert string slices to List
126-
data.Indices = utils.SliceToListType_String(ctx, policy.Indices, path.Empty(), &resp.Diagnostics)
127-
if resp.Diagnostics.HasError() {
128-
return
129-
}
130-
131-
data.EnrichFields = utils.SliceToListType_String(ctx, policy.EnrichFields, path.Empty(), &resp.Diagnostics)
115+
// Convert model to framework types using shared function
116+
data.populateFromPolicy(ctx, policy, &resp.Diagnostics)
132117
if resp.Diagnostics.HasError() {
133118
return
134119
}
Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
package enrich
22

33
import (
4+
"context"
5+
6+
"github.com/elastic/terraform-provider-elasticstack/internal/models"
7+
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
8+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/path"
411
"github.com/hashicorp/terraform-plugin-framework/types"
512
)
613

714
type EnrichPolicyData struct {
8-
Id types.String `tfsdk:"id"`
9-
ElasticsearchConnection types.List `tfsdk:"elasticsearch_connection"`
10-
Name types.String `tfsdk:"name"`
11-
PolicyType types.String `tfsdk:"policy_type"`
12-
Indices types.List `tfsdk:"indices"`
13-
MatchField types.String `tfsdk:"match_field"`
14-
EnrichFields types.List `tfsdk:"enrich_fields"`
15-
Query types.String `tfsdk:"query"`
16-
Execute types.Bool `tfsdk:"execute"`
15+
Id types.String `tfsdk:"id"`
16+
ElasticsearchConnection types.List `tfsdk:"elasticsearch_connection"`
17+
Name types.String `tfsdk:"name"`
18+
PolicyType types.String `tfsdk:"policy_type"`
19+
Indices types.List `tfsdk:"indices"`
20+
MatchField types.String `tfsdk:"match_field"`
21+
EnrichFields types.List `tfsdk:"enrich_fields"`
22+
Query jsontypes.Normalized `tfsdk:"query"`
23+
Execute types.Bool `tfsdk:"execute"`
24+
}
25+
26+
// populateFromPolicy converts models.EnrichPolicy to EnrichPolicyData fields
27+
func (data *EnrichPolicyData) populateFromPolicy(ctx context.Context, policy *models.EnrichPolicy, diagnostics *diag.Diagnostics) {
28+
data.Name = types.StringValue(policy.Name)
29+
data.PolicyType = types.StringValue(policy.Type)
30+
data.MatchField = types.StringValue(policy.MatchField)
31+
32+
if policy.Query != "" && policy.Query != "null" {
33+
data.Query = jsontypes.NewNormalizedValue(policy.Query)
34+
} else {
35+
data.Query = jsontypes.NewNormalizedNull()
36+
}
37+
38+
// Convert string slices to List
39+
data.Indices = utils.SliceToListType_String(ctx, policy.Indices, path.Empty(), diagnostics)
40+
if diagnostics.HasError() {
41+
return
42+
}
43+
44+
data.EnrichFields = utils.SliceToListType_String(ctx, policy.EnrichFields, path.Empty(), diagnostics)
1745
}

internal/elasticsearch/enrich/read.go

Lines changed: 2 additions & 20 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/clients/elasticsearch"
99
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
10-
"github.com/hashicorp/terraform-plugin-framework/path"
1110
"github.com/hashicorp/terraform-plugin-framework/resource"
12-
"github.com/hashicorp/terraform-plugin-framework/types"
1311
"github.com/hashicorp/terraform-plugin-log/tflog"
1412
)
1513

@@ -45,24 +43,8 @@ func (r *enrichPolicyResource) Read(ctx context.Context, req resource.ReadReques
4543
return
4644
}
4745

48-
// Convert model to framework types
49-
data.Name = types.StringValue(policy.Name)
50-
data.PolicyType = types.StringValue(policy.Type)
51-
data.MatchField = types.StringValue(policy.MatchField)
52-
53-
if policy.Query != "" && policy.Query != "null" {
54-
data.Query = types.StringValue(policy.Query)
55-
} else {
56-
data.Query = types.StringNull()
57-
}
58-
59-
// Convert string slices to List
60-
data.Indices = utils.SliceToListType_String(ctx, policy.Indices, path.Empty(), &resp.Diagnostics)
61-
if resp.Diagnostics.HasError() {
62-
return
63-
}
64-
65-
data.EnrichFields = utils.SliceToListType_String(ctx, policy.EnrichFields, path.Empty(), &resp.Diagnostics)
46+
// Convert model to framework types using shared function
47+
data.populateFromPolicy(ctx, policy, &resp.Diagnostics)
6648
if resp.Diagnostics.HasError() {
6749
return
6850
}

internal/elasticsearch/enrich/resource.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package enrich
22

33
import (
44
"context"
5-
"regexp"
65

6+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
77
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
88
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
99
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -108,12 +108,10 @@ func GetResourceSchema() schema.Schema {
108108
"query": schema.StringAttribute{
109109
MarkdownDescription: "Query used to filter documents in the enrich index. The policy only uses documents matching this query to enrich incoming documents. Defaults to a match_all query.",
110110
Optional: true,
111+
CustomType: jsontypes.NormalizedType{},
111112
PlanModifiers: []planmodifier.String{
112113
stringplanmodifier.RequiresReplace(),
113114
},
114-
Validators: []validator.String{
115-
stringvalidator.RegexMatches(regexp.MustCompile(`^\{.*\}$`), "must be valid JSON"),
116-
},
117115
},
118116
"execute": schema.BoolAttribute{
119117
MarkdownDescription: "Whether to call the execute API function in order to create the enrich index.",

0 commit comments

Comments
 (0)