@@ -2,6 +2,7 @@ package integration_policy
22
33import (
44 "context"
5+ "fmt"
56 "sort"
67
78 "github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
@@ -53,13 +54,14 @@ func (model *integrationPolicyModel) populateFromAPI(ctx context.Context, data *
5354
5455 // Only populate the agent policy field that was originally configured
5556 // to avoid Terraform detecting inconsistent state
56- originallyUsedAgentPolicyID := ! model . AgentPolicyID . IsNull () && ! model .AgentPolicyID . IsUnknown ( )
57- originallyUsedAgentPolicyIDs := ! model . AgentPolicyIDs . IsNull () && ! model .AgentPolicyIDs . IsUnknown ( )
57+ originallyUsedAgentPolicyID := utils . IsKnown ( model .AgentPolicyID )
58+ originallyUsedAgentPolicyIDs := utils . IsKnown ( model .AgentPolicyIDs )
5859
59- if originallyUsedAgentPolicyID && ! originallyUsedAgentPolicyIDs {
60+ if originallyUsedAgentPolicyID {
6061 // Only set agent_policy_id if it was originally used
6162 model .AgentPolicyID = types .StringPointerValue (data .PolicyId )
62- } else if originallyUsedAgentPolicyIDs && ! originallyUsedAgentPolicyID {
63+ }
64+ if originallyUsedAgentPolicyIDs {
6365 // Only set agent_policy_ids if it was originally used
6466 if data .PolicyIds != nil {
6567 agentPolicyIDs , d := types .ListValueFrom (ctx , types .StringType , * data .PolicyIds )
@@ -68,19 +70,7 @@ func (model *integrationPolicyModel) populateFromAPI(ctx context.Context, data *
6870 } else {
6971 model .AgentPolicyIDs = types .ListNull (types .StringType )
7072 }
71- } else {
72- // Handle edge cases: both fields configured or neither configured
73- // Default to the behavior based on API response structure
74- if data .PolicyIds != nil && len (* data .PolicyIds ) > 1 {
75- // Multiple policy IDs, use agent_policy_ids
76- agentPolicyIDs , d := types .ListValueFrom (ctx , types .StringType , * data .PolicyIds )
77- diags .Append (d ... )
78- model .AgentPolicyIDs = agentPolicyIDs
79- } else {
80- // Single policy ID, use agent_policy_id
81- model .AgentPolicyID = types .StringPointerValue (data .PolicyId )
82- }
83- }
73+ }
8474
8575 model .Description = types .StringPointerValue (data .Description )
8676 model .Enabled = types .BoolValue (data .Enabled )
@@ -120,6 +110,19 @@ func (model *integrationPolicyModel) populateInputFromAPI(ctx context.Context, i
120110func (model integrationPolicyModel ) toAPIModel (ctx context.Context , isUpdate bool , feat features ) (kbapi.PackagePolicyRequest , diag.Diagnostics ) {
121111 var diags diag.Diagnostics
122112
113+ // Check if agent_policy_ids is configured and version supports it
114+ if utils .IsKnown (model .AgentPolicyIDs ) {
115+ if ! feat .SupportsPolicyIds {
116+ return kbapi.PackagePolicyRequest {}, diag.Diagnostics {
117+ diag .NewAttributeErrorDiagnostic (
118+ path .Root ("agent_policy_ids" ),
119+ "Unsupported Elasticsearch version" ,
120+ fmt .Sprintf ("Agent policy IDs are only supported in Elastic Stack %s and above" , MinVersionPolicyIds ),
121+ ),
122+ }
123+ }
124+ }
125+
123126 body := kbapi.PackagePolicyRequest {
124127 Description : model .Description .ValueStringPointer (),
125128 Force : model .Force .ValueBoolPointer (),
0 commit comments