Skip to content

Commit 83d9479

Browse files
committed
applied suggestions
1 parent c1f255c commit 83d9479

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

internal/fleet/integration_policy/models.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package integration_policy
22

33
import (
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
120110
func (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(),

internal/fleet/integration_policy/schema.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
88
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
"github.com/hashicorp/terraform-plugin-framework/path"
910
"github.com/hashicorp/terraform-plugin-framework/resource"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1112
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
1213
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1314
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
15+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
16+
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
17+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1418
"github.com/hashicorp/terraform-plugin-framework/types"
1519
)
1620

@@ -52,13 +56,19 @@ func getSchemaV1() schema.Schema {
5256
},
5357
"agent_policy_id": schema.StringAttribute{
5458
Description: "ID of the agent policy.",
55-
DeprecationMessage: "Use agent_policy_ids instead. This field will be removed in a future version.",
5659
Optional: true,
60+
Validators: []validator.String{
61+
stringvalidator.ConflictsWith(path.Root("agent_policy_ids").Expression()),
62+
},
5763
},
5864
"agent_policy_ids": schema.ListAttribute{
5965
Description: "List of agent policy IDs.",
6066
ElementType: types.StringType,
6167
Optional: true,
68+
Validators: []validator.List{
69+
listvalidator.ConflictsWith(path.Root("agent_policy_ids").Expression()),
70+
listvalidator.SizeAtLeast(1),
71+
},
6272
},
6373
"description": schema.StringAttribute{
6474
Description: "The description of the integration policy.",

0 commit comments

Comments
 (0)