@@ -30,24 +30,25 @@ type globalDataTagsItemModel struct {
3030}
3131
3232type agentPolicyModel struct {
33- ID types.String `tfsdk:"id"`
34- PolicyID types.String `tfsdk:"policy_id"`
35- Name types.String `tfsdk:"name"`
36- Namespace types.String `tfsdk:"namespace"`
37- Description types.String `tfsdk:"description"`
38- DataOutputId types.String `tfsdk:"data_output_id"`
39- MonitoringOutputId types.String `tfsdk:"monitoring_output_id"`
40- FleetServerHostId types.String `tfsdk:"fleet_server_host_id"`
41- DownloadSourceId types.String `tfsdk:"download_source_id"`
42- MonitorLogs types.Bool `tfsdk:"monitor_logs"`
43- MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
44- SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
45- SkipDestroy types.Bool `tfsdk:"skip_destroy"`
46- SupportsAgentless types.Bool `tfsdk:"supports_agentless"`
47- InactivityTimeout customtypes.Duration `tfsdk:"inactivity_timeout"`
48- UnenrollmentTimeout customtypes.Duration `tfsdk:"unenrollment_timeout"`
49- GlobalDataTags types.Map `tfsdk:"global_data_tags"` //> globalDataTagsModel
50- SpaceIds types.Set `tfsdk:"space_ids"`
33+ ID types.String `tfsdk:"id"`
34+ PolicyID types.String `tfsdk:"policy_id"`
35+ Name types.String `tfsdk:"name"`
36+ Namespace types.String `tfsdk:"namespace"`
37+ Description types.String `tfsdk:"description"`
38+ DataOutputId types.String `tfsdk:"data_output_id"`
39+ MonitoringOutputId types.String `tfsdk:"monitoring_output_id"`
40+ FleetServerHostId types.String `tfsdk:"fleet_server_host_id"`
41+ DownloadSourceId types.String `tfsdk:"download_source_id"`
42+ MonitorLogs types.Bool `tfsdk:"monitor_logs"`
43+ MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
44+ SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
45+ SkipDestroy types.Bool `tfsdk:"skip_destroy"`
46+ SupportsAgentless types.Bool `tfsdk:"supports_agentless"`
47+ InactivityTimeout customtypes.Duration `tfsdk:"inactivity_timeout"`
48+ UnenrollmentTimeout customtypes.Duration `tfsdk:"unenrollment_timeout"`
49+ GlobalDataTags types.Map `tfsdk:"global_data_tags"` //> globalDataTagsModel
50+ SpaceIds types.Set `tfsdk:"space_ids"`
51+ RequiredVersions RequiredVersionsValue `tfsdk:"required_versions"`
5152}
5253
5354func (model * agentPolicyModel ) populateFromAPI (ctx context.Context , data * kbapi.AgentPolicy ) diag.Diagnostics {
@@ -134,6 +135,37 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
134135 model .SpaceIds = types .SetNull (types .StringType )
135136 }
136137
138+ // Handle required_versions
139+ if data .RequiredVersions != nil && len (* data .RequiredVersions ) > 0 {
140+ elemType := getRequiredVersionsElementType ()
141+ elements := make ([]attr.Value , 0 , len (* data .RequiredVersions ))
142+
143+ for _ , rv := range * data .RequiredVersions {
144+ objValue , d := types .ObjectValue (
145+ map [string ]attr.Type {
146+ "version" : types .StringType ,
147+ "percentage" : types .Int32Type ,
148+ },
149+ map [string ]attr.Value {
150+ "version" : types .StringValue (rv .Version ),
151+ "percentage" : types .Int32Value (int32 (rv .Percentage )),
152+ },
153+ )
154+ if d .HasError () {
155+ return d
156+ }
157+ elements = append (elements , objValue )
158+ }
159+
160+ reqVersions , d := NewRequiredVersionsValue (elemType , elements )
161+ if d .HasError () {
162+ return d
163+ }
164+ model .RequiredVersions = reqVersions
165+ } else {
166+ model .RequiredVersions = NewRequiredVersionsValueNull (getRequiredVersionsElementType ())
167+ }
168+
137169 return nil
138170}
139171
@@ -186,6 +218,63 @@ func (model *agentPolicyModel) convertGlobalDataTags(ctx context.Context, feat f
186218 return & itemsList , diags
187219}
188220
221+ // convertRequiredVersions converts the required versions from terraform model to API model
222+ func (model * agentPolicyModel ) convertRequiredVersions (ctx context.Context ) (* []struct {
223+ Percentage float32 `json:"percentage"`
224+ Version string `json:"version"`
225+ }, diag.Diagnostics ) {
226+ var diags diag.Diagnostics
227+
228+ if model .RequiredVersions .IsNull () || model .RequiredVersions .IsUnknown () {
229+ return nil , diags
230+ }
231+
232+ elements := model .RequiredVersions .Elements ()
233+ if len (elements ) == 0 {
234+ return nil , diags
235+ }
236+
237+ result := make ([]struct {
238+ Percentage float32 `json:"percentage"`
239+ Version string `json:"version"`
240+ }, 0 , len (elements ))
241+
242+ for _ , elem := range elements {
243+ obj , ok := elem .(types.Object )
244+ if ! ok {
245+ diags .AddError ("required_versions conversion error" , fmt .Sprintf ("Expected ObjectValue, got %T" , elem ))
246+ continue
247+ }
248+
249+ attrs := obj .Attributes ()
250+ versionAttr := attrs ["version" ].(types.String )
251+ percentageAttr := attrs ["percentage" ].(types.Int32 )
252+
253+ if versionAttr .IsNull () || versionAttr .IsUnknown () {
254+ diags .AddError ("required_versions validation error" , "version cannot be null or unknown" )
255+ continue
256+ }
257+ if percentageAttr .IsNull () || percentageAttr .IsUnknown () {
258+ diags .AddError ("required_versions validation error" , "percentage cannot be null or unknown" )
259+ continue
260+ }
261+
262+ result = append (result , struct {
263+ Percentage float32 `json:"percentage"`
264+ Version string `json:"version"`
265+ }{
266+ Percentage : float32 (percentageAttr .ValueInt32 ()),
267+ Version : versionAttr .ValueString (),
268+ })
269+ }
270+
271+ if diags .HasError () {
272+ return nil , diags
273+ }
274+
275+ return & result , diags
276+ }
277+
189278func (model * agentPolicyModel ) toAPICreateModel (ctx context.Context , feat features ) (kbapi.PostFleetAgentPoliciesJSONRequestBody , diag.Diagnostics ) {
190279 monitoring := make ([]kbapi.PostFleetAgentPoliciesJSONBodyMonitoringEnabled , 0 , 2 )
191280
@@ -282,6 +371,13 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, feat featur
282371 body .SpaceIds = & spaceIds
283372 }
284373
374+ // Handle required_versions
375+ requiredVersions , d := model .convertRequiredVersions (ctx )
376+ if d .HasError () {
377+ return kbapi.PostFleetAgentPoliciesJSONRequestBody {}, d
378+ }
379+ body .RequiredVersions = requiredVersions
380+
285381 return body , nil
286382}
287383
@@ -379,5 +475,12 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, feat featur
379475 body .SpaceIds = & spaceIds
380476 }
381477
478+ // Handle required_versions
479+ requiredVersions , d := model .convertRequiredVersions (ctx )
480+ if d .HasError () {
481+ return kbapi.PutFleetAgentPoliciesAgentpolicyidJSONRequestBody {}, d
482+ }
483+ body .RequiredVersions = requiredVersions
484+
382485 return body , nil
383486}
0 commit comments