@@ -22,6 +22,7 @@ type features struct {
2222 SupportsInactivityTimeout bool
2323 SupportsUnenrollmentTimeout bool
2424 SupportsSpaceIds bool
25+ SupportsRequiredVersions bool
2526}
2627
2728type globalDataTagsItemModel struct {
@@ -140,7 +141,9 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
140141 versionMap := make (map [string ]attr.Value )
141142
142143 for _ , rv := range * data .RequiredVersions {
143- versionMap [rv .Version ] = types .Int32Value (int32 (rv .Percentage ))
144+ // Round the float32 percentage to nearest integer since we use Int32 in the schema
145+ percentage := int32 (rv .Percentage + 0.5 )
146+ versionMap [rv .Version ] = types .Int32Value (percentage )
144147 }
145148
146149 reqVersions , d := types .MapValue (types .Int32Type , versionMap )
@@ -205,7 +208,7 @@ func (model *agentPolicyModel) convertGlobalDataTags(ctx context.Context, feat f
205208}
206209
207210// convertRequiredVersions converts the required versions from terraform model to API model
208- func (model * agentPolicyModel ) convertRequiredVersions (ctx context.Context ) (* []struct {
211+ func (model * agentPolicyModel ) convertRequiredVersions (ctx context.Context , feat features ) (* []struct {
209212 Percentage float32 `json:"percentage"`
210213 Version string `json:"version"`
211214}, diag.Diagnostics ) {
@@ -215,9 +218,26 @@ func (model *agentPolicyModel) convertRequiredVersions(ctx context.Context) (*[]
215218 return nil , diags
216219 }
217220
221+ // Check if required_versions is supported
222+ if ! feat .SupportsRequiredVersions {
223+ return nil , diag.Diagnostics {
224+ diag .NewAttributeErrorDiagnostic (
225+ path .Root ("required_versions" ),
226+ "Unsupported Elasticsearch version" ,
227+ fmt .Sprintf ("Required versions (automatic agent upgrades) are only supported in Elastic Stack %s and above" , MinVersionRequiredVersions ),
228+ ),
229+ }
230+ }
231+
218232 elements := model .RequiredVersions .Elements ()
233+
234+ // If the map is empty (required_versions = {}), return an empty array to clear upgrades
219235 if len (elements ) == 0 {
220- return nil , diags
236+ emptyArray := make ([]struct {
237+ Percentage float32 `json:"percentage"`
238+ Version string `json:"version"`
239+ }, 0 )
240+ return & emptyArray , diags
221241 }
222242
223243 result := make ([]struct {
@@ -350,7 +370,7 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, feat featur
350370 }
351371
352372 // Handle required_versions
353- requiredVersions , d := model .convertRequiredVersions (ctx )
373+ requiredVersions , d := model .convertRequiredVersions (ctx , feat )
354374 if d .HasError () {
355375 return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, d
356376 }
@@ -454,7 +474,7 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, feat featur
454474 }
455475
456476 // Handle required_versions
457- requiredVersions , d := model .convertRequiredVersions (ctx )
477+ requiredVersions , d := model .convertRequiredVersions (ctx , feat )
458478 if d .HasError () {
459479 return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, d
460480 }
0 commit comments