@@ -60,8 +60,8 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
6060 if ! utils .IsKnown (model .MonitorLogs ) {
6161 model .MonitorLogs = types .BoolValue (false )
6262 }
63- if ! utils .IsKnown (model .MonitorLogs ) {
64- model .MonitorLogs = types .BoolValue (false )
63+ if ! utils .IsKnown (model .MonitorMetrics ) {
64+ model .MonitorMetrics = types .BoolValue (false )
6565 }
6666
6767 model .MonitoringOutputId = types .StringPointerValue (data .MonitoringOutputId )
@@ -97,6 +97,55 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
9797 return nil
9898}
9999
100+ // convertGlobalDataTags converts the global data tags from terraform model to API model
101+ // and performs version validation
102+ func (model * agentPolicyModel ) convertGlobalDataTags (ctx context.Context , serverVersion * version.Version ) (* []kbapi.AgentPolicyGlobalDataTagsItem , diag.Diagnostics ) {
103+ var diags diag.Diagnostics
104+
105+ if len (model .GlobalDataTags .Elements ()) == 0 {
106+ if serverVersion .GreaterThanOrEqual (MinVersionGlobalDataTags ) {
107+ emptyList := make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 )
108+ return & emptyList , diags
109+ }
110+ return nil , diags
111+ }
112+
113+ if serverVersion .LessThan (MinVersionGlobalDataTags ) {
114+ diags .AddError ("global_data_tags ES version error" , fmt .Sprintf ("Global data tags are only supported in Elastic Stack %s and above" , MinVersionGlobalDataTags ))
115+ return nil , diags
116+ }
117+
118+ items := utils .MapTypeToMap (ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags ,
119+ func (item globalDataTagsItemModel , meta utils.MapMeta ) kbapi.AgentPolicyGlobalDataTagsItem {
120+ var value kbapi.AgentPolicyGlobalDataTagsItem_Value
121+ var err error
122+ if item .StringValue .ValueStringPointer () != nil {
123+ err = value .FromAgentPolicyGlobalDataTagsItemValue0 (* item .StringValue .ValueStringPointer ())
124+ } else {
125+ err = value .FromAgentPolicyGlobalDataTagsItemValue1 (* item .NumberValue .ValueFloat32Pointer ())
126+ }
127+ if err != nil {
128+ diags .AddError ("global_data_tags validation_error_converting_values" , err .Error ())
129+ return kbapi.AgentPolicyGlobalDataTagsItem {}
130+ }
131+ return kbapi.AgentPolicyGlobalDataTagsItem {
132+ Name : meta .Key ,
133+ Value : value ,
134+ }
135+ })
136+
137+ if diags .HasError () {
138+ return nil , diags
139+ }
140+
141+ itemsList := make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 , len (items ))
142+ for _ , v := range items {
143+ itemsList = append (itemsList , v )
144+ }
145+
146+ return & itemsList , diags
147+ }
148+
100149func (model * agentPolicyModel ) toAPICreateModel (ctx context.Context , serverVersion * version.Version ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
101150 monitoring := make ([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled , 0 , 2 )
102151
@@ -119,42 +168,11 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
119168 Namespace : model .Namespace .ValueString (),
120169 }
121170
122- if len (model .GlobalDataTags .Elements ()) > 0 {
123- var diags diag.Diagnostics
124- if serverVersion .LessThan (MinVersionGlobalDataTags ) {
125- diags .AddError ("global_data_tags ES version error" , fmt .Sprintf ("Global data tags are only supported in Elastic Stack %s and above" , MinVersionGlobalDataTags ))
126- return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
127- }
128-
129- items := utils .MapTypeToMap (ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags ,
130- func (item globalDataTagsItemModel , meta utils.MapMeta ) kbapi.AgentPolicyGlobalDataTagsItem {
131- var value kbapi.AgentPolicyGlobalDataTagsItem_Value
132- var err error
133- if item .StringValue .ValueStringPointer () != nil {
134- err = value .FromAgentPolicyGlobalDataTagsItemValue0 (* item .StringValue .ValueStringPointer ())
135- } else {
136- err = value .FromAgentPolicyGlobalDataTagsItemValue1 (* item .NumberValue .ValueFloat32Pointer ())
137- }
138- if err != nil {
139- diags .AddError ("global_data_tags validation_error_converting_values" , err .Error ())
140- return kbapi.AgentPolicyGlobalDataTagsItem {}
141- }
142- return kbapi.AgentPolicyGlobalDataTagsItem {
143- Name : meta .Key ,
144- Value : value ,
145- }
146- })
147-
148- if diags .HasError () {
149- return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
150- }
151-
152- itemsList := make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 , len (items ))
153- for _ , v := range items {
154- itemsList = append (itemsList , v )
155- }
156- body .GlobalDataTags = & itemsList
171+ tags , diags := model .convertGlobalDataTags (ctx , serverVersion )
172+ if diags .HasError () {
173+ return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
157174 }
175+ body .GlobalDataTags = tags
158176
159177 return body , nil
160178}
@@ -179,47 +197,11 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
179197 Namespace : model .Namespace .ValueString (),
180198 }
181199
182- if len (model .GlobalDataTags .Elements ()) > 0 {
183- var diags diag.Diagnostics
184- if serverVersion .LessThan (MinVersionGlobalDataTags ) {
185- diags .AddError ("global_data_tags ES version error" , fmt .Sprintf ("Global data tags are only supported in Elastic Stack %s and above" , MinVersionGlobalDataTags ))
186- return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
187- }
188-
189- items := utils .MapTypeToMap (ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags ,
190- func (item globalDataTagsItemModel , meta utils.MapMeta ) kbapi.AgentPolicyGlobalDataTagsItem {
191-
192- var value kbapi.AgentPolicyGlobalDataTagsItem_Value
193- var err error
194- if item .StringValue .ValueStringPointer () != nil {
195- err = value .FromAgentPolicyGlobalDataTagsItemValue0 (* item .StringValue .ValueStringPointer ())
196- } else {
197- err = value .FromAgentPolicyGlobalDataTagsItemValue1 (* item .NumberValue .ValueFloat32Pointer ())
198- }
199- if err != nil {
200- diags .AddError ("global_data_tags validation_error_converting_values" , err .Error ())
201- return kbapi.AgentPolicyGlobalDataTagsItem {}
202- }
203- return kbapi.AgentPolicyGlobalDataTagsItem {
204- Name : meta .Key ,
205- Value : value ,
206- }
207- })
208- if diags .HasError () {
209- return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
210- }
211-
212- itemsList := make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 , len (items ))
213- for _ , v := range items {
214- itemsList = append (itemsList , v )
215- }
216- body .GlobalDataTags = & itemsList
217- } else {
218- if serverVersion .GreaterThanOrEqual (MinVersionGlobalDataTags ) {
219- itemsList := make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 )
220- body .GlobalDataTags = & itemsList
221- }
200+ tags , diags := model .convertGlobalDataTags (ctx , serverVersion )
201+ if diags .HasError () {
202+ return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
222203 }
204+ body .GlobalDataTags = tags
223205
224206 return body , nil
225207}
0 commit comments