Skip to content

Commit a98c34e

Browse files
committed
applied suggestions
1 parent c1f255c commit a98c34e

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

docs/resources/fleet_integration_policy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ resource "elasticstack_fleet_integration_policy" "sample" {
9797

9898
### Optional
9999

100-
- `agent_policy_id` (String, Deprecated) ID of the agent policy.
100+
- `agent_policy_id` (String) ID of the agent policy.
101101
- `agent_policy_ids` (List of String) List of agent policy IDs.
102102
- `description` (String) The description of the integration policy.
103103
- `enabled` (Boolean) Enable the integration policy.

internal/fleet/integration_policy/models.go

Lines changed: 24 additions & 8 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,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
120123
func (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(),

internal/fleet/integration_policy/schema.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import (
55
_ "embed"
66

77
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
8+
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
9+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
810
"github.com/hashicorp/terraform-plugin-framework/attr"
11+
"github.com/hashicorp/terraform-plugin-framework/path"
912
"github.com/hashicorp/terraform-plugin-framework/resource"
1013
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1114
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
1215
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1316
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
17+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1418
"github.com/hashicorp/terraform-plugin-framework/types"
1519
)
1620

@@ -51,14 +55,20 @@ func getSchemaV1() schema.Schema {
5155
Required: true,
5256
},
5357
"agent_policy_id": schema.StringAttribute{
54-
Description: "ID of the agent policy.",
55-
DeprecationMessage: "Use agent_policy_ids instead. This field will be removed in a future version.",
56-
Optional: true,
58+
Description: "ID of the agent policy.",
59+
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)