@@ -13,6 +13,11 @@ import (
1313 "github.com/hashicorp/terraform-plugin-framework/types"
1414)
1515
16+ type globalDataTagsItemModel struct {
17+ StringValue types.String `tfsdk:"string_value"`
18+ NumberValue types.Float32 `tfsdk:"number_value"`
19+ }
20+
1621type agentPolicyModel struct {
1722 ID types.String `tfsdk:"id"`
1823 PolicyID types.String `tfsdk:"policy_id"`
@@ -27,7 +32,7 @@ type agentPolicyModel struct {
2732 MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
2833 SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
2934 SkipDestroy types.Bool `tfsdk:"skip_destroy"`
30- GlobalDataTags types.Map `tfsdk:"global_data_tags"`
35+ GlobalDataTags types.Map `tfsdk:"global_data_tags"` //> globalDataTagsModel
3136}
3237
3338func (model * agentPolicyModel ) populateFromAPI (ctx context.Context , data * kbapi.AgentPolicy , serverVersion * version.Version ) diag.Diagnostics {
@@ -62,20 +67,20 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
6267 model .Namespace = types .StringValue (data .Namespace )
6368 if utils .Deref (data .GlobalDataTags ) != nil {
6469 diags := diag.Diagnostics {}
65- var map0 = make (map [string ]any )
70+ var map0 = make (map [string ]globalDataTagsItemModel )
6671 for _ , v := range utils .Deref (data .GlobalDataTags ) {
6772 maybeFloat , error := v .Value .AsAgentPolicyGlobalDataTagsItemValue1 ()
6873 if error != nil {
6974 maybeString , error := v .Value .AsAgentPolicyGlobalDataTagsItemValue0 ()
7075 if error != nil {
7176 diags .AddError ("Failed to unmarshal global data tags" , error .Error ())
7277 }
73- map0 [v .Name ] = map [ string ] string {
74- "string_value" : string (maybeString ),
78+ map0 [v .Name ] = globalDataTagsItemModel {
79+ StringValue : types . StringValue (maybeString ),
7580 }
7681 } else {
77- map0 [v .Name ] = map [ string ] float32 {
78- "number_value" : float32 (maybeFloat ),
82+ map0 [v .Name ] = globalDataTagsItemModel {
83+ NumberValue : types . Float32Value ( float32 (maybeFloat ) ),
7984 }
8085 }
8186 }
@@ -118,37 +123,40 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
118123 return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
119124 }
120125
121- var items []kbapi.AgentPolicyGlobalDataTagsItem
122- itemsMap := utils .MapTypeAs [struct {
123- string_value * string
124- number_value * float32
125- }](ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags )
126+ items := utils .MapTypeToMap (ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags ,
127+ func (item globalDataTagsItemModel , meta utils.MapMeta ) kbapi.AgentPolicyGlobalDataTagsItem {
128+ // do some checks
129+ if item .StringValue .ValueStringPointer () == nil && item .NumberValue .ValueFloat32Pointer () == nil || item .StringValue .ValueStringPointer () != nil && item .NumberValue .ValueFloat32Pointer () != nil {
130+ diags .AddError ("global_data_tags validation_error" , "Global data tags must have exactly one of string_value or number_value" )
131+ return kbapi.AgentPolicyGlobalDataTagsItem {}
132+ }
133+
134+ var value kbapi.AgentPolicyGlobalDataTagsItem_Value
135+ var err error
136+ if item .StringValue .ValueStringPointer () != nil {
137+ err = value .FromAgentPolicyGlobalDataTagsItemValue0 (* item .StringValue .ValueStringPointer ())
138+ } else {
139+ err = value .FromAgentPolicyGlobalDataTagsItemValue1 (* item .NumberValue .ValueFloat32Pointer ())
140+ }
141+ if err != nil {
142+ diags .AddError ("global_data_tags validation_error_converting_values" , err .Error ())
143+ return kbapi.AgentPolicyGlobalDataTagsItem {}
144+ }
145+ return kbapi.AgentPolicyGlobalDataTagsItem {
146+ Name : meta .Key ,
147+ Value : value ,
148+ }
149+ })
150+
126151 if diags .HasError () {
127152 return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
128153 }
129- for k , v := range itemsMap {
130- if (v .string_value != nil && v .number_value != nil ) || (v .string_value == nil && v .number_value == nil ) {
131- diags .AddError ("global_data_tags ES version error" , "Global data tags must have exactly one of string_value or number_value" )
132- return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
133- }
134- var value kbapi.AgentPolicyGlobalDataTagsItem_Value
135- var err error
136- if v .string_value != nil {
137- err = value .FromAgentPolicyGlobalDataTagsItemValue0 (* v .string_value )
138- } else {
139- err = value .FromAgentPolicyGlobalDataTagsItemValue1 (* v .number_value )
140- }
141- if err != nil {
142- diags .AddError ("global_data_tags ES version error" , "could not convert global data tags value" )
143- return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
144- }
145- items = append (items , kbapi.AgentPolicyGlobalDataTagsItem {
146- Name : k ,
147- Value : value ,
148- })
149- }
150154
151- body .GlobalDataTags = & items
155+ itemsList := make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 , len (items ))
156+ for _ , v := range items {
157+ itemsList = append (itemsList , v )
158+ }
159+ body .GlobalDataTags = & itemsList
152160 }
153161
154162 return body , nil
@@ -181,38 +189,39 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
181189 return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
182190 }
183191
184- var items []kbapi.AgentPolicyGlobalDataTagsItem
185- itemsMap := utils .MapTypeAs [struct {
186- string_value * string
187- number_value * float32
188- }](ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags )
192+ items := utils .MapTypeToMap (ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags ,
193+ func (item globalDataTagsItemModel , meta utils.MapMeta ) kbapi.AgentPolicyGlobalDataTagsItem {
194+ // do some checks
195+ if item .StringValue .ValueStringPointer () == nil && item .NumberValue .ValueFloat32Pointer () == nil || item .StringValue .ValueStringPointer () != nil && item .NumberValue .ValueFloat32Pointer () != nil {
196+ diags .AddError ("global_data_tags validation_error" , "Global data tags must have exactly one of string_value or number_value" )
197+ return kbapi.AgentPolicyGlobalDataTagsItem {}
198+ }
199+
200+ var value kbapi.AgentPolicyGlobalDataTagsItem_Value
201+ var err error
202+ if item .StringValue .ValueStringPointer () != nil {
203+ err = value .FromAgentPolicyGlobalDataTagsItemValue0 (* item .StringValue .ValueStringPointer ())
204+ } else {
205+ err = value .FromAgentPolicyGlobalDataTagsItemValue1 (* item .NumberValue .ValueFloat32Pointer ())
206+ }
207+ if err != nil {
208+ diags .AddError ("global_data_tags validation_error_converting_values" , err .Error ())
209+ return kbapi.AgentPolicyGlobalDataTagsItem {}
210+ }
211+ return kbapi.AgentPolicyGlobalDataTagsItem {
212+ Name : meta .Key ,
213+ Value : value ,
214+ }
215+ })
189216 if diags .HasError () {
190217 return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
191218 }
192- for k , v := range itemsMap {
193- if (v .string_value != nil && v .number_value != nil ) || (v .string_value == nil && v .number_value == nil ) {
194- diags .AddError ("global_data_tags ES version error" , "Global data tags must have exactly one of string_value or number_value" )
195- return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
196- }
197- var value kbapi.AgentPolicyGlobalDataTagsItem_Value
198- var err error
199- if v .string_value != nil {
200- // s := *v.string_value
201- err = value .FromAgentPolicyGlobalDataTagsItemValue0 (* v .string_value )
202- } else {
203- err = value .FromAgentPolicyGlobalDataTagsItemValue1 (* v .number_value )
204- }
205- if err != nil {
206- diags .AddError ("global_data_tags ES version error" , "could not convert global data tags value" )
207- return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
208- }
209- items = append (items , kbapi.AgentPolicyGlobalDataTagsItem {
210- Name : k ,
211- Value : value ,
212- })
213- }
214219
215- body .GlobalDataTags = & items
220+ itemsList := make ([]kbapi.AgentPolicyGlobalDataTagsItem , 0 , len (items ))
221+ for _ , v := range items {
222+ itemsList = append (itemsList , v )
223+ }
224+ body .GlobalDataTags = & itemsList
216225 }
217226
218227 return body , nil
0 commit comments