11package agent_policy
22
33import (
4- "context"
54 "encoding/json"
65 "slices"
76
@@ -13,25 +12,6 @@ import (
1312 "github.com/hashicorp/terraform-plugin-framework/types"
1413)
1514
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- // }
34-
3515type agentPolicyModel struct {
3616 ID types.String `tfsdk:"id"`
3717 PolicyID types.String `tfsdk:"policy_id"`
@@ -49,7 +29,7 @@ type agentPolicyModel struct {
4929 GlobalDataTags types.String `tfsdk:"global_data_tags"`
5030}
5131
52- func (model * agentPolicyModel ) populateFromAPI (ctx context. Context , data * kbapi.AgentPolicy , serverVersion * version.Version ) diag.Diagnostics {
32+ func (model * agentPolicyModel ) populateFromAPI (data * kbapi.AgentPolicy , serverVersion * version.Version ) diag.Diagnostics {
5333 if data == nil {
5434 return nil
5535 }
@@ -81,18 +61,19 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
8161 model .Namespace = types .StringValue (data .Namespace )
8262 if serverVersion .GreaterThanOrEqual (MinVersionGlobalDataTags ) && utils .Deref (data .GlobalDataTags ) != nil {
8363 diags := diag.Diagnostics {}
84- d , err := json .Marshal (data .GlobalDataTags )
64+ d , err := json .Marshal (utils . Deref ( data .GlobalDataTags ) )
8565 if err != nil {
8666 diags .AddError ("Failed to marshal global data tags" , err .Error ())
8767 return diags
8868 }
89- model .GlobalDataTags = types .StringValue (string (d ))
69+ strD := string (d )
70+ model .GlobalDataTags = types .StringPointerValue (& strD )
9071 }
9172
9273 return nil
9374}
9475
95- func (model * agentPolicyModel ) toAPICreateModel (ctx context. Context , serverVersion * version.Version ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
76+ func (model * agentPolicyModel ) toAPICreateModel (serverVersion * version.Version ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
9677 monitoring := make ([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled , 0 , 2 )
9778
9879 if model .MonitorLogs .ValueBool () {
@@ -122,22 +103,21 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, serverVersi
122103 }
123104
124105 str := model .GlobalDataTags .ValueStringPointer ()
125- var items []struct {
126- Name string `json:"name"`
127- Value kbapi.PostFleetAgentPoliciesJSONBody_GlobalDataTags_Value `json:"value"`
128- }
106+ var items []kbapi.AgentPolicyGlobalDataTagsItem
129107
130- err := json .Unmarshal ([]byte (utils . Deref ( str ) ), & items )
108+ err := json .Unmarshal ([]byte (* str ), & items )
131109 if err != nil {
132110 diags .AddError (err .Error (), "" )
133111 return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, diags
134112 }
135- * body .GlobalDataTags = items
113+
114+ body .GlobalDataTags = & items
136115 }
116+
137117 return body , nil
138118}
139119
140- func (model * agentPolicyModel ) toAPIUpdateModel (ctx context. Context , serverVersion * version.Version ) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody , diag.Diagnostics ) {
120+ func (model * agentPolicyModel ) toAPIUpdateModel (serverVersion * version.Version ) (kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody , diag.Diagnostics ) {
141121 monitoring := make ([]kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBodyMonitoringEnabled , 0 , 2 )
142122 if model .MonitorLogs .ValueBool () {
143123 monitoring = append (monitoring , kbapi .Logs )
@@ -163,17 +143,17 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, serverVersi
163143 diags .AddError ("global_data_tags ES version error" , "Global data tags are only supported in Elastic Stack 8.15.0 and above" )
164144 return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
165145 }
146+
166147 str := model .GlobalDataTags .ValueStringPointer ()
167- var items []struct {
168- Name string `json:"name"`
169- Value kbapi.PutFleetAgentPoliciesAgentpolicyidJSONBody_GlobalDataTags_Value `json:"value"`
170- }
171- err := json .Unmarshal ([]byte (utils .Deref (str )), & items )
148+ var items []kbapi.AgentPolicyGlobalDataTagsItem
149+
150+ err := json .Unmarshal ([]byte (* str ), & items )
172151 if err != nil {
173152 diags .AddError (err .Error (), "" )
174153 return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, diags
175154 }
176- * body .GlobalDataTags = items
155+
156+ body .GlobalDataTags = & items
177157 }
178158
179159 return body , nil
0 commit comments