11package agent_policy
22
33import (
4- "encoding/json "
4+ "context "
55 "slices"
66
77 "github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
88 "github.com/elastic/terraform-provider-elasticstack/internal/utils"
99
1010 "github.com/hashicorp/go-version"
1111 "github.com/hashicorp/terraform-plugin-framework/diag"
12+ "github.com/hashicorp/terraform-plugin-framework/path"
1213 "github.com/hashicorp/terraform-plugin-framework/types"
1314)
1415
@@ -26,10 +27,10 @@ type agentPolicyModel struct {
2627 MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
2728 SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
2829 SkipDestroy types.Bool `tfsdk:"skip_destroy"`
29- GlobalDataTags types.String `tfsdk:"global_data_tags"`
30+ GlobalDataTags types.Map `tfsdk:"global_data_tags"`
3031}
3132
32- func (model * agentPolicyModel ) populateFromAPI (data * kbapi.AgentPolicy , serverVersion * version.Version ) diag.Diagnostics {
33+ func (model * agentPolicyModel ) populateFromAPI (ctx context. Context , data * kbapi.AgentPolicy , serverVersion * version.Version ) diag.Diagnostics {
3334 if data == nil {
3435 return nil
3536 }
@@ -59,21 +60,36 @@ func (model *agentPolicyModel) populateFromAPI(data *kbapi.AgentPolicy, serverVe
5960 model .MonitoringOutputId = types .StringPointerValue (data .MonitoringOutputId )
6061 model .Name = types .StringValue (data .Name )
6162 model .Namespace = types .StringValue (data .Namespace )
62- if serverVersion . GreaterThanOrEqual ( MinVersionGlobalDataTags ) && utils .Deref (data .GlobalDataTags ) != nil {
63+ if utils .Deref (data .GlobalDataTags ) != nil {
6364 diags := diag.Diagnostics {}
64- d , err := json .Marshal (utils .Deref (data .GlobalDataTags ))
65- if err != nil {
66- diags .AddError ("Failed to marshal global data tags" , err .Error ())
65+ var map0 = make (map [string ]any )
66+ for _ , v := range utils .Deref (data .GlobalDataTags ) {
67+ maybeFloat , error := v .Value .AsAgentPolicyGlobalDataTagsItemValue1 ()
68+ if error != nil {
69+ maybeString , error := v .Value .AsAgentPolicyGlobalDataTagsItemValue0 ()
70+ if error != nil {
71+ diags .AddError ("Failed to unmarshal global data tags" , error .Error ())
72+ }
73+ map0 [v .Name ] = map [string ]string {
74+ "string_value" : string (maybeString ),
75+ }
76+ } else {
77+ map0 [v .Name ] = map [string ]float32 {
78+ "number_value" : float32 (maybeFloat ),
79+ }
80+ }
81+ }
82+ gdt := utils .MapValueFrom (ctx , map0 , getGlobalDataTagsAttrType (), path .Root ("global_data_tags" ), & diags )
83+ if diags .HasError () {
6784 return diags
6885 }
69- strD := string (d )
70- model .GlobalDataTags = types .StringPointerValue (& strD )
86+ model .GlobalDataTags = gdt
7187 }
7288
7389 return nil
7490}
7591
76- func (model * agentPolicyModel ) toAPICreateModel (serverVersion * version.Version ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
92+ func (model * agentPolicyModel ) toAPICreateModel (ctx context. Context , serverVersion * version.Version ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
7793 monitoring := make ([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled , 0 , 2 )
7894
7995 if model .MonitorLogs .ValueBool () {
@@ -95,29 +111,50 @@ func (model *agentPolicyModel) toAPICreateModel(serverVersion *version.Version)
95111 Namespace : model .Namespace .ValueString (),
96112 }
97113
98- if len (model .GlobalDataTags .ValueString ()) > 0 {
114+ if len (model .GlobalDataTags .Elements ()) > 0 {
99115 var diags diag.Diagnostics
100116 if serverVersion .LessThan (MinVersionGlobalDataTags ) {
101117 diags .AddError ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" )
102118 return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
103119 }
104120
105- str := model .GlobalDataTags .ValueStringPointer ()
106121 var items []kbapi.AgentPolicyGlobalDataTagsItem
107-
108- err := json .Unmarshal ([]byte (* str ), & items )
109- if err != nil {
110- diags .AddError (err .Error (), "" )
122+ itemsMap := utils .MapTypeAs [struct {
123+ string_value * string
124+ number_value * float32
125+ }](ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags )
126+ if diags .HasError () {
111127 return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
112128 }
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+ }
113150
114151 body .GlobalDataTags = & items
115152 }
116153
117154 return body , nil
118155}
119156
120- func (model * agentPolicyModel ) toAPIUpdateModel (serverVersion * version.Version ) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody , diag.Diagnostics ) {
157+ func (model * agentPolicyModel ) toAPIUpdateModel (ctx context. Context , serverVersion * version.Version ) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody , diag.Diagnostics ) {
121158 monitoring := make ([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled , 0 , 2 )
122159 if model .MonitorLogs .ValueBool () {
123160 monitoring = append (monitoring , kbapi .Logs )
@@ -137,21 +174,43 @@ func (model *agentPolicyModel) toAPIUpdateModel(serverVersion *version.Version)
137174 Namespace : model .Namespace .ValueString (),
138175 }
139176
140- if len (model .GlobalDataTags .ValueString ()) > 0 {
177+ if len (model .GlobalDataTags .Elements ()) > 0 {
141178 var diags diag.Diagnostics
142179 if serverVersion .LessThan (MinVersionGlobalDataTags ) {
143180 diags .AddError ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" )
144181 return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
145182 }
146183
147- str := model .GlobalDataTags .ValueStringPointer ()
148184 var items []kbapi.AgentPolicyGlobalDataTagsItem
149-
150- err := json .Unmarshal ([]byte (* str ), & items )
151- if err != nil {
152- diags .AddError (err .Error (), "" )
185+ itemsMap := utils .MapTypeAs [struct {
186+ string_value * string
187+ number_value * float32
188+ }](ctx , model .GlobalDataTags , path .Root ("global_data_tags" ), & diags )
189+ if diags .HasError () {
153190 return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
154191 }
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+ }
155214
156215 body .GlobalDataTags = & items
157216 }
0 commit comments