1+ package enrich
2+
3+ import (
4+ "context"
5+ "fmt"
6+
7+ "github.com/elastic/terraform-provider-elasticstack/internal/clients"
8+ "github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
9+ "github.com/elastic/terraform-provider-elasticstack/internal/utils"
10+ "github.com/hashicorp/terraform-plugin-framework/datasource"
11+ "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
12+ "github.com/hashicorp/terraform-plugin-framework/path"
13+ "github.com/hashicorp/terraform-plugin-framework/types"
14+
15+ providerschema "github.com/elastic/terraform-provider-elasticstack/internal/schema"
16+ )
17+
18+ func NewEnrichPolicyDataSource () datasource.DataSource {
19+ return & enrichPolicyDataSource {}
20+ }
21+
22+ type enrichPolicyDataSource struct {
23+ client * clients.ApiClient
24+ }
25+
26+ func (d * enrichPolicyDataSource ) Metadata (_ context.Context , req datasource.MetadataRequest , resp * datasource.MetadataResponse ) {
27+ resp .TypeName = req .ProviderTypeName + "_elasticsearch_enrich_policy"
28+ }
29+
30+ func (d * enrichPolicyDataSource ) Configure (_ context.Context , req datasource.ConfigureRequest , resp * datasource.ConfigureResponse ) {
31+ client , diags := clients .ConvertProviderData (req .ProviderData )
32+ resp .Diagnostics .Append (diags ... )
33+ d .client = client
34+ }
35+
36+ func (d * enrichPolicyDataSource ) Schema (_ context.Context , _ datasource.SchemaRequest , resp * datasource.SchemaResponse ) {
37+ resp .Schema = GetDataSourceSchema ()
38+ }
39+
40+ func GetDataSourceSchema () schema.Schema {
41+ return schema.Schema {
42+ MarkdownDescription : "Returns information about an enrich policy. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/get-enrich-policy-api.html" ,
43+ Blocks : map [string ]schema.Block {
44+ "elasticsearch_connection" : providerschema .GetEsFWConnectionBlock ("elasticsearch_connection" , false ),
45+ },
46+ Attributes : map [string ]schema.Attribute {
47+ "id" : schema.StringAttribute {
48+ MarkdownDescription : "Internal identifier of the resource" ,
49+ Computed : true ,
50+ },
51+ "name" : schema.StringAttribute {
52+ MarkdownDescription : "The name of the policy." ,
53+ Required : true ,
54+ },
55+ "policy_type" : schema.StringAttribute {
56+ MarkdownDescription : "The type of enrich policy, can be one of geo_match, match, range." ,
57+ Computed : true ,
58+ },
59+ "indices" : schema.ListAttribute {
60+ MarkdownDescription : "Array of one or more source indices used to create the enrich index." ,
61+ ElementType : types .StringType ,
62+ Computed : true ,
63+ },
64+ "match_field" : schema.StringAttribute {
65+ MarkdownDescription : "Field from the source indices used to match incoming documents." ,
66+ Computed : true ,
67+ },
68+ "enrich_fields" : schema.ListAttribute {
69+ MarkdownDescription : "Fields to add to matching incoming documents. These fields must be present in the source indices." ,
70+ ElementType : types .StringType ,
71+ Computed : true ,
72+ },
73+ "query" : schema.StringAttribute {
74+ MarkdownDescription : "Query used to filter documents in the enrich index for matching." ,
75+ Computed : true ,
76+ },
77+ },
78+ }
79+ }
80+
81+ func (d * enrichPolicyDataSource ) Read (ctx context.Context , req datasource.ReadRequest , resp * datasource.ReadResponse ) {
82+ var data EnrichPolicyData
83+ resp .Diagnostics .Append (req .Config .Get (ctx , & data )... )
84+ if resp .Diagnostics .HasError () {
85+ return
86+ }
87+
88+ policyName := data .Name .ValueString ()
89+ client , diags := clients .MaybeNewApiClientFromFrameworkResource (ctx , data .ElasticsearchConnection , d .client )
90+ resp .Diagnostics .Append (diags ... )
91+ if resp .Diagnostics .HasError () {
92+ return
93+ }
94+
95+ id , sdkDiags := client .ID (ctx , policyName )
96+ resp .Diagnostics .Append (utils .FrameworkDiagsFromSDK (sdkDiags )... )
97+ if resp .Diagnostics .HasError () {
98+ return
99+ }
100+ data .Id = types .StringValue (id .String ())
101+
102+ // Use the same read logic as the resource
103+ policy , sdkDiags := elasticsearch .GetEnrichPolicy (ctx , client , policyName )
104+ resp .Diagnostics .Append (utils .FrameworkDiagsFromSDK (sdkDiags )... )
105+ if resp .Diagnostics .HasError () {
106+ return
107+ }
108+
109+ if policy == nil {
110+ resp .Diagnostics .AddError ("Policy not found" , fmt .Sprintf ("Enrich policy '%s' not found" , policyName ))
111+ return
112+ }
113+
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 )
132+ if resp .Diagnostics .HasError () {
133+ return
134+ }
135+
136+ resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
137+ }
0 commit comments