@@ -8,13 +8,17 @@ import (
8
8
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
9
9
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
10
10
11
- "github.com/hashicorp/go-version"
12
11
"github.com/hashicorp/terraform-plugin-framework/attr"
13
12
"github.com/hashicorp/terraform-plugin-framework/diag"
14
13
"github.com/hashicorp/terraform-plugin-framework/path"
15
14
"github.com/hashicorp/terraform-plugin-framework/types"
16
15
)
17
16
17
+ type features struct {
18
+ SupportsGlobalDataTags bool
19
+ SupportsSupportsAgentless bool
20
+ }
21
+
18
22
type globalDataTagsItemModel struct {
19
23
StringValue types.String `tfsdk:"string_value"`
20
24
NumberValue types.Float32 `tfsdk:"number_value"`
@@ -101,18 +105,18 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
101
105
102
106
// convertGlobalDataTags converts the global data tags from terraform model to API model
103
107
// and performs version validation
104
- func (model * agentPolicyModel ) convertGlobalDataTags (ctx context.Context , serverVersion * version. Version ) (* []kbapi.AgentPolicyGlobalDataTagsItem , diag.Diagnostics ) {
108
+ func (model * agentPolicyModel ) convertGlobalDataTags (ctx context.Context , feat features ) (* []kbapi.AgentPolicyGlobalDataTagsItem , diag.Diagnostics ) {
105
109
var diags diag.Diagnostics
106
110
107
111
if len (model .GlobalDataTags .Elements ()) == 0 {
108
- if serverVersion . GreaterThanOrEqual ( MinVersionGlobalDataTags ) {
112
+ if feat . SupportsGlobalDataTags {
109
113
emptyList := make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 )
110
114
return & emptyList , diags
111
115
}
112
116
return nil , diags
113
117
}
114
118
115
- if serverVersion . LessThan ( MinVersionGlobalDataTags ) {
119
+ if ! feat . SupportsGlobalDataTags {
116
120
diags .AddError ("global_data_tags ES version error" , fmt .Sprintf ("Global data tags are only supported in Elastic Stack %s and above" , MinVersionGlobalDataTags ))
117
121
return nil , diags
118
122
}
@@ -148,7 +152,7 @@ func (model *agentPolicyModel) convertGlobalDataTags(ctx context.Context, server
148
152
return & itemsList , diags
149
153
}
150
154
151
- func (model * agentPolicyModel ) toAPICreateModel (ctx context.Context , serverVersion * version. Version ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
155
+ func (model * agentPolicyModel ) toAPICreateModel (ctx context.Context , feat features ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
152
156
monitoring := make ([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled , 0 , 2 )
153
157
154
158
if model .MonitorLogs .ValueBool () {
@@ -158,6 +162,16 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
158
162
monitoring = append (monitoring , kbapi .PostFleetAgentPoliciesJSONBodyMonitoringEnabledMetrics )
159
163
}
160
164
165
+ if utils .IsKnown (model .SupportsAgentless ) && ! feat .SupportsSupportsAgentless {
166
+ return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diag.Diagnostics {
167
+ diag .NewAttributeErrorDiagnostic (
168
+ path .Root ("supports_agentless" ),
169
+ "Unsupported Elasticsearch version" ,
170
+ fmt .Sprintf ("Supports agentless is only supported in Elastic Stack %s and above" , MinSupportsAgentlessVersion ),
171
+ ),
172
+ }
173
+ }
174
+
161
175
body := kbapi.PostFleetAgentPoliciesJSONRequestBody {
162
176
DataOutputId : model .DataOutputId .ValueStringPointer (),
163
177
Description : model .Description .ValueStringPointer (),
@@ -171,7 +185,7 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
171
185
SupportsAgentless : model .SupportsAgentless .ValueBoolPointer (),
172
186
}
173
187
174
- tags , diags := model .convertGlobalDataTags (ctx , serverVersion )
188
+ tags , diags := model .convertGlobalDataTags (ctx , feat )
175
189
if diags .HasError () {
176
190
return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
177
191
}
@@ -180,7 +194,7 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
180
194
return body , nil
181
195
}
182
196
183
- func (model * agentPolicyModel ) toAPIUpdateModel (ctx context.Context , serverVersion * version. Version ) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody , diag.Diagnostics ) {
197
+ func (model * agentPolicyModel ) toAPIUpdateModel (ctx context.Context , feat features ) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody , diag.Diagnostics ) {
184
198
monitoring := make ([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled , 0 , 2 )
185
199
if model .MonitorLogs .ValueBool () {
186
200
monitoring = append (monitoring , kbapi .Logs )
@@ -189,6 +203,16 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
189
203
monitoring = append (monitoring , kbapi .Metrics )
190
204
}
191
205
206
+ if utils .IsKnown (model .SupportsAgentless ) && ! feat .SupportsSupportsAgentless {
207
+ return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diag.Diagnostics {
208
+ diag .NewAttributeErrorDiagnostic (
209
+ path .Root ("supports_agentless" ),
210
+ "Unsupported Elasticsearch version" ,
211
+ fmt .Sprintf ("Supports agentless is only supported in Elastic Stack %s and above" , MinSupportsAgentlessVersion ),
212
+ ),
213
+ }
214
+ }
215
+
192
216
body := kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {
193
217
DataOutputId : model .DataOutputId .ValueStringPointer (),
194
218
Description : model .Description .ValueStringPointer (),
@@ -201,7 +225,7 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
201
225
SupportsAgentless : model .SupportsAgentless .ValueBoolPointer (),
202
226
}
203
227
204
- tags , diags := model .convertGlobalDataTags (ctx , serverVersion )
228
+ tags , diags := model .convertGlobalDataTags (ctx , feat )
205
229
if diags .HasError () {
206
230
return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
207
231
}
0 commit comments