@@ -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,30 +54,32 @@ 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 ()
5857
59- if originallyUsedAgentPolicyID && ! originallyUsedAgentPolicyIDs {
60- // Only set agent_policy_id if it was originally used
58+ originallyUsedAgentPolicyID := utils .IsKnown (model .AgentPolicyID )
59+ originallyUsedAgentPolicyIDs := utils .IsKnown (model .AgentPolicyIDs )
60+
61+ if originallyUsedAgentPolicyID {
6162 model .AgentPolicyID = types .StringPointerValue (data .PolicyId )
62- } else if originallyUsedAgentPolicyIDs && ! originallyUsedAgentPolicyID {
63- // Only set agent_policy_ids if it was originally used
63+ }
64+ if originallyUsedAgentPolicyIDs {
6465 if data .PolicyIds != nil {
6566 agentPolicyIDs , d := types .ListValueFrom (ctx , types .StringType , * data .PolicyIds )
6667 diags .Append (d ... )
6768 model .AgentPolicyIDs = agentPolicyIDs
6869 } else {
6970 model .AgentPolicyIDs = types .ListNull (types .StringType )
7071 }
71- } else {
72+ }
73+
74+ if ! originallyUsedAgentPolicyID && ! originallyUsedAgentPolicyIDs {
7275 // Handle edge cases: both fields configured or neither configured
7376 // Default to the behavior based on API response structure
7477 if data .PolicyIds != nil && len (* data .PolicyIds ) > 1 {
7578 // Multiple policy IDs, use agent_policy_ids
7679 agentPolicyIDs , d := types .ListValueFrom (ctx , types .StringType , * data .PolicyIds )
7780 diags .Append (d ... )
7881 model .AgentPolicyIDs = agentPolicyIDs
79- } else {
82+ } else if data . PolicyId != nil {
8083 // Single policy ID, use agent_policy_id
8184 model .AgentPolicyID = types .StringPointerValue (data .PolicyId )
8285 }
@@ -120,6 +123,19 @@ func (model *integrationPolicyModel) populateInputFromAPI(ctx context.Context, i
120123func (model integrationPolicyModel ) toAPIModel (ctx context.Context , isUpdate bool , feat features ) (kbapi.PackagePolicyRequest , diag.Diagnostics ) {
121124 var diags diag.Diagnostics
122125
126+ // Check if agent_policy_ids is configured and version supports it
127+ if utils .IsKnown (model .AgentPolicyIDs ) {
128+ if ! feat .SupportsPolicyIds {
129+ return kbapi.PackagePolicyRequest {}, diag.Diagnostics {
130+ diag .NewAttributeErrorDiagnostic (
131+ path .Root ("agent_policy_ids" ),
132+ "Unsupported Elasticsearch version" ,
133+ fmt .Sprintf ("Agent policy IDs are only supported in Elastic Stack %s and above" , MinVersionPolicyIds ),
134+ ),
135+ }
136+ }
137+ }
138+
123139 body := kbapi.PackagePolicyRequest {
124140 Description : model .Description .ValueStringPointer (),
125141 Force : model .Force .ValueBoolPointer (),
0 commit comments