@@ -2,6 +2,7 @@ package agent_policy
22
33import (
44 "context"
5+ "encoding/json"
56 "slices"
67
78 "github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
@@ -12,24 +13,24 @@ import (
1213 "github.com/hashicorp/terraform-plugin-framework/types"
1314)
1415
15- type globalDataTagModel struct {
16- Name types.String `tfsdk:"name"`
17- Value types.String `tfsdk:"value"`
18- }
19-
20- func newGlobalDataTagModel (data struct {
21- Name string "json:\" name\" "
22- Value kbapi.AgentPolicy_GlobalDataTags_Value "json:\" value\" "
23- }) globalDataTagModel {
24- val , err := data .Value .AsAgentPolicyGlobalDataTagsValue0 ()
25- if err != nil {
26- panic (err )
27- }
28- return globalDataTagModel {
29- Name : types .StringValue (data .Name ),
30- Value : types .StringValue (val ),
31- }
32- }
16+ // type globalDataTagModel struct {
17+ // Name types.String `tfsdk:"name"`
18+ // Value types.String `tfsdk:"value"`
19+ // }
20+
21+ // func newGlobalDataTagModel(data struct {
22+ // Name string "json:\"name\""
23+ // Value kbapi.AgentPolicy_GlobalDataTags_Value "json:\"value\""
24+ // }) globalDataTagModel {
25+ // val, err := data.Value.AsAgentPolicyGlobalDataTagsValue0()
26+ // if err != nil {
27+ // panic(err)
28+ // }
29+ // return globalDataTagModel{
30+ // Name: types.StringValue(data.Name),
31+ // Value: types.StringValue(val),
32+ // }
33+ // }
3334
3435type agentPolicyModel struct {
3536 ID types.String `tfsdk:"id"`
@@ -45,12 +46,12 @@ type agentPolicyModel struct {
4546 MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
4647 SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
4748 SkipDestroy types.Bool `tfsdk:"skip_destroy"`
48- GlobalDataTags types.List `tfsdk:"global_data_tags"`
49+ GlobalDataTags types.String `tfsdk:"global_data_tags"`
4950}
5051
51- func (model * agentPolicyModel ) populateFromAPI (ctx context.Context , data * kbapi.AgentPolicy , serverVersion * version.Version ) {
52+ func (model * agentPolicyModel ) populateFromAPI (ctx context.Context , data * kbapi.AgentPolicy , serverVersion * version.Version ) diag. Diagnostics {
5253 if data == nil {
53- return
54+ return nil
5455 }
5556
5657 model .ID = types .StringValue (data .Id )
@@ -79,17 +80,16 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
7980 model .Name = types .StringValue (data .Name )
8081 model .Namespace = types .StringValue (data .Namespace )
8182 if serverVersion .GreaterThanOrEqual (MinVersionGlobalDataTags ) && utils .Deref (data .GlobalDataTags ) != nil {
82-
83- gdt := []globalDataTagModel {}
84- for _ , val := range utils .Deref (data .GlobalDataTags ) {
85- gdt = append (gdt , newGlobalDataTagModel (val ))
83+ diags := diag.Diagnostics {}
84+ d , err := json .Marshal (data .GlobalDataTags )
85+ if err != nil {
86+ diags .AddError ("Failed to marshal global data tags" , err .Error ())
87+ return diags
8688 }
87- gdtList , diags := types .ListValueFrom (ctx , getGlobalDataTagsType (), gdt )
88- if diags .HasError () {
89- return
90- }
91- model .GlobalDataTags = gdtList
89+ model .GlobalDataTags = types .StringValue (string (d ))
9290 }
91+
92+ return nil
9393}
9494
9595func (model * agentPolicyModel ) toAPICreateModel (ctx context.Context , serverVersion * version.Version ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
@@ -114,19 +114,20 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
114114 Namespace : model .Namespace .ValueString (),
115115 }
116116
117- if len (model .GlobalDataTags .Elements ()) > 0 {
117+ if len (model .GlobalDataTags .ValueString ()) > 0 {
118+ var diags diag.Diagnostics
118119 if serverVersion .LessThan (MinVersionGlobalDataTags ) {
119- return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diag.Diagnostics {
120- diag .NewErrorDiagnostic ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" ),
121- }
122-
120+ diags .AddError ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" )
121+ return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
123122 }
124- diags := model .GlobalDataTags .ElementsAs (ctx , body .GlobalDataTags , false )
125- if diags .HasError () {
123+
124+ str := model .GlobalDataTags .ValueString ()
125+ err := json .Unmarshal ([]byte (str ), body .GlobalDataTags )
126+ if err != nil {
127+ diags .AddError (err .Error (), "" )
126128 return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
127129 }
128130 }
129-
130131 return body , nil
131132}
132133
@@ -150,17 +151,20 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
150151 Namespace : model .Namespace .ValueString (),
151152 }
152153
153- if len (model .GlobalDataTags .Elements ()) > 0 {
154+ if len (model .GlobalDataTags .ValueString ()) > 0 {
155+ var diags diag.Diagnostics
154156 if serverVersion .LessThan (MinVersionGlobalDataTags ) {
155- return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diag.Diagnostics {
156- diag .NewErrorDiagnostic ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" ),
157- }
158-
157+ diags .AddError ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" )
158+ return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
159159 }
160- diags := model .GlobalDataTags .ElementsAs (ctx , body .GlobalDataTags , false )
161- if diags .HasError () {
160+
161+ str := model .GlobalDataTags .ValueString ()
162+ err := json .Unmarshal ([]byte (str ), body .GlobalDataTags )
163+ if err != nil {
164+ diags .AddError (err .Error (), "" )
162165 return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
163166 }
167+
164168 }
165169
166170 return body , nil
0 commit comments