@@ -87,12 +87,8 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
8787 return
8888}
8989
90- func (model agentPolicyModel ) toAPICreateModel (ctx context.Context ) kbapi.PostFleetAgentPoliciesJSONRequestBody {
90+ func (model agentPolicyModel ) toAPICreateModel (ctx context.Context , serverVersion * version. Version ) ( kbapi.PostFleetAgentPoliciesJSONRequestBody , diag. Diagnostics ) {
9191 monitoring := make ([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled , 0 , 2 )
92- tags := make ([]struct {
93- Name string `json:"name"`
94- Value kbapi.PostFleetAgentPoliciesJSONBody_GlobalDataTags_Value `json:"value"`
95- }, 0 , len (model .GlobalDataTags .ElementsAs (ctx , getGlobalDataTagsType , false )))
9692
9793 if model .MonitorLogs .ValueBool () {
9894 monitoring = append (monitoring , kbapi .PostFleetAgentPoliciesJSONBodyMonitoringEnabledLogs )
@@ -113,14 +109,23 @@ func (model agentPolicyModel) toAPICreateModel(ctx context.Context) kbapi.PostFl
113109 Namespace : model .Namespace .ValueString (),
114110 }
115111
116- if len (tags ) > 0 {
117- body .GlobalDataTags = & tags
112+ if len (model .GlobalDataTags .Elements ()) > 0 {
113+ if serverVersion .LessThan (MinVersionGlobalDataTags ) {
114+ return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diag.Diagnostics {
115+ diag .NewErrorDiagnostic ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" ),
116+ }
117+
118+ }
119+ diags := model .GlobalDataTags .ElementsAs (ctx , body .GlobalDataTags , false )
120+ if diags .HasError () {
121+ return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
122+ }
118123 }
119124
120- return body
125+ return body , nil
121126}
122127
123- func (model agentPolicyModel ) toAPIUpdateModel (ctx context.Context ) kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {
128+ func (model agentPolicyModel ) toAPIUpdateModel (ctx context.Context , serverVersion * version. Version ) ( kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody , diag. Diagnostics ) {
124129 monitoring := make ([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled , 0 , 2 )
125130 if model .MonitorLogs .ValueBool () {
126131 monitoring = append (monitoring , kbapi .Logs )
@@ -129,11 +134,6 @@ func (model agentPolicyModel) toAPIUpdateModel(ctx context.Context) kbapi.PutFle
129134 monitoring = append (monitoring , kbapi .Metrics )
130135 }
131136
132- tags := make ([]struct {
133- Name string `json:"name"`
134- Value kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBody_GlobalDataTags_Value `json:"value"`
135- }, 0 , len (model .GlobalDataTags .ElementsAs (ctx , getGlobalDataTagsType , false )))
136-
137137 body := kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {
138138 DataOutputId : model .DataOutputId .ValueStringPointer (),
139139 Description : model .Description .ValueStringPointer (),
@@ -145,9 +145,18 @@ func (model agentPolicyModel) toAPIUpdateModel(ctx context.Context) kbapi.PutFle
145145 Namespace : model .Namespace .ValueString (),
146146 }
147147
148- if len (tags ) > 0 {
149- body .GlobalDataTags = & tags
148+ if len (model .GlobalDataTags .Elements ()) > 0 {
149+ if serverVersion .LessThan (MinVersionGlobalDataTags ) {
150+ return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diag.Diagnostics {
151+ diag .NewErrorDiagnostic ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" ),
152+ }
153+
154+ }
155+ diags := model .GlobalDataTags .ElementsAs (ctx , body .GlobalDataTags , false )
156+ if diags .HasError () {
157+ return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
158+ }
150159 }
151160
152- return body
161+ return body , nil
153162}
0 commit comments