@@ -8,13 +8,17 @@ import (
88	"github.com/elastic/terraform-provider-elasticstack/generated/kbapi" 
99	"github.com/elastic/terraform-provider-elasticstack/internal/utils" 
1010
11- 	"github.com/hashicorp/go-version" 
1211	"github.com/hashicorp/terraform-plugin-framework/attr" 
1312	"github.com/hashicorp/terraform-plugin-framework/diag" 
1413	"github.com/hashicorp/terraform-plugin-framework/path" 
1514	"github.com/hashicorp/terraform-plugin-framework/types" 
1615)
1716
17+ type  features  struct  {
18+ 	SupportsGlobalDataTags     bool 
19+ 	SupportsSupportsAgentless  bool 
20+ }
21+ 
1822type  globalDataTagsItemModel  struct  {
1923	StringValue  types.String   `tfsdk:"string_value"` 
2024	NumberValue  types.Float32  `tfsdk:"number_value"` 
@@ -101,18 +105,18 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
101105
102106// convertGlobalDataTags converts the global data tags from terraform model to API model 
103107// 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 ) {
105109	var  diags  diag.Diagnostics 
106110
107111	if  len (model .GlobalDataTags .Elements ()) ==  0  {
108- 		if  serverVersion . GreaterThanOrEqual ( MinVersionGlobalDataTags )  {
112+ 		if  feat . SupportsGlobalDataTags  {
109113			emptyList  :=  make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 )
110114			return  & emptyList , diags 
111115		}
112116		return  nil , diags 
113117	}
114118
115- 	if  serverVersion . LessThan ( MinVersionGlobalDataTags )  {
119+ 	if  ! feat . SupportsGlobalDataTags  {
116120		diags .AddError ("global_data_tags ES version error" , fmt .Sprintf ("Global data tags are only supported in Elastic Stack %s and above" , MinVersionGlobalDataTags ))
117121		return  nil , diags 
118122	}
@@ -148,7 +152,7 @@ func (model *agentPolicyModel) convertGlobalDataTags(ctx context.Context, server
148152	return  & itemsList , diags 
149153}
150154
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 ) {
152156	monitoring  :=  make ([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled , 0 , 2 )
153157
154158	if  model .MonitorLogs .ValueBool () {
@@ -158,6 +162,16 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
158162		monitoring  =  append (monitoring , kbapi .PostFleetAgentPoliciesJSONBodyMonitoringEnabledMetrics )
159163	}
160164
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+ 
161175	body  :=  kbapi.PostFleetAgentPoliciesJSONRequestBody {
162176		DataOutputId :       model .DataOutputId .ValueStringPointer (),
163177		Description :        model .Description .ValueStringPointer (),
@@ -171,7 +185,7 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
171185		SupportsAgentless :  model .SupportsAgentless .ValueBoolPointer (),
172186	}
173187
174- 	tags , diags  :=  model .convertGlobalDataTags (ctx , serverVersion )
188+ 	tags , diags  :=  model .convertGlobalDataTags (ctx , feat )
175189	if  diags .HasError () {
176190		return  kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags 
177191	}
@@ -180,7 +194,7 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
180194	return  body , nil 
181195}
182196
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 ) {
184198	monitoring  :=  make ([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled , 0 , 2 )
185199	if  model .MonitorLogs .ValueBool () {
186200		monitoring  =  append (monitoring , kbapi .Logs )
@@ -189,6 +203,16 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
189203		monitoring  =  append (monitoring , kbapi .Metrics )
190204	}
191205
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+ 
192216	body  :=  kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {
193217		DataOutputId :       model .DataOutputId .ValueStringPointer (),
194218		Description :        model .Description .ValueStringPointer (),
@@ -201,7 +225,7 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
201225		SupportsAgentless :  model .SupportsAgentless .ValueBoolPointer (),
202226	}
203227
204- 	tags , diags  :=  model .convertGlobalDataTags (ctx , serverVersion )
228+ 	tags , diags  :=  model .convertGlobalDataTags (ctx , feat )
205229	if  diags .HasError () {
206230		return  kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags 
207231	}
0 commit comments