Skip to content

Commit 031d7bc

Browse files
committed
update integration_policy to include agent_policy_ids
1 parent 1cc6bd6 commit 031d7bc

File tree

5 files changed

+313
-10
lines changed

5 files changed

+313
-10
lines changed

docs/resources/fleet_integration_policy.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ resource "elasticstack_fleet_integration_policy" "sample" {
8282

8383
### Required
8484

85-
- `agent_policy_id` (String) ID of the agent policy.
8685
- `integration_name` (String) The name of the integration package.
8786
- `integration_version` (String) The version of the integration package.
8887
- `name` (String) The name of the integration policy.
8988
- `namespace` (String) The namespace of the integration policy.
9089

9190
### Optional
9291

92+
- `agent_policy_id` (String, Deprecated) ID of the agent policy.
93+
- `agent_policy_ids` (List of String) List of agent policy IDs.
9394
- `description` (String) The description of the integration policy.
9495
- `enabled` (Boolean) Enable the integration policy.
9596
- `force` (Boolean) Force operations, such as creation and deletion, to occur.

generated/kbapi/kibana.gen.go

Lines changed: 80 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/fleet/integration_policy/models.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type integrationPolicyModel struct {
1818
Name types.String `tfsdk:"name"`
1919
Namespace types.String `tfsdk:"namespace"`
2020
AgentPolicyID types.String `tfsdk:"agent_policy_id"`
21+
AgentPolicyIDs types.List `tfsdk:"agent_policy_ids"`
2122
Description types.String `tfsdk:"description"`
2223
Enabled types.Bool `tfsdk:"enabled"`
2324
Force types.Bool `tfsdk:"force"`
@@ -45,7 +46,38 @@ func (model *integrationPolicyModel) populateFromAPI(ctx context.Context, data *
4546
model.PolicyID = types.StringValue(data.Id)
4647
model.Name = types.StringValue(data.Name)
4748
model.Namespace = types.StringPointerValue(data.Namespace)
48-
model.AgentPolicyID = types.StringPointerValue(data.PolicyId)
49+
50+
// Only populate the agent policy field that was originally configured
51+
// to avoid Terraform detecting inconsistent state
52+
originallyUsedAgentPolicyID := !model.AgentPolicyID.IsNull() && !model.AgentPolicyID.IsUnknown()
53+
originallyUsedAgentPolicyIDs := !model.AgentPolicyIDs.IsNull() && !model.AgentPolicyIDs.IsUnknown()
54+
55+
if originallyUsedAgentPolicyID && !originallyUsedAgentPolicyIDs {
56+
// Only set agent_policy_id if it was originally used
57+
model.AgentPolicyID = types.StringPointerValue(data.PolicyId)
58+
} else if originallyUsedAgentPolicyIDs && !originallyUsedAgentPolicyID {
59+
// Only set agent_policy_ids if it was originally used
60+
if data.PolicyIds != nil {
61+
agentPolicyIDs, d := types.ListValueFrom(ctx, types.StringType, *data.PolicyIds)
62+
diags.Append(d...)
63+
model.AgentPolicyIDs = agentPolicyIDs
64+
} else {
65+
model.AgentPolicyIDs = types.ListNull(types.StringType)
66+
}
67+
} else {
68+
// Handle edge cases: both fields configured or neither configured
69+
// Default to the behavior based on API response structure
70+
if data.PolicyIds != nil && len(*data.PolicyIds) > 1 {
71+
// Multiple policy IDs, use agent_policy_ids
72+
agentPolicyIDs, d := types.ListValueFrom(ctx, types.StringType, *data.PolicyIds)
73+
diags.Append(d...)
74+
model.AgentPolicyIDs = agentPolicyIDs
75+
} else {
76+
// Single policy ID, use agent_policy_id
77+
model.AgentPolicyID = types.StringPointerValue(data.PolicyId)
78+
}
79+
}
80+
4981
model.Description = types.StringPointerValue(data.Description)
5082
model.Enabled = types.BoolValue(data.Enabled)
5183
model.IntegrationName = types.StringValue(data.Package.Name)
@@ -94,7 +126,18 @@ func (model integrationPolicyModel) toAPIModel(ctx context.Context, isUpdate boo
94126
Version: model.IntegrationVersion.ValueString(),
95127
},
96128
PolicyId: model.AgentPolicyID.ValueStringPointer(),
97-
Vars: utils.MapRef(utils.NormalizedTypeToMap[any](model.VarsJson, path.Root("vars_json"), &diags)),
129+
PolicyIds: func() *[]string {
130+
if !model.AgentPolicyIDs.IsNull() && !model.AgentPolicyIDs.IsUnknown() {
131+
var policyIDs []string
132+
d := model.AgentPolicyIDs.ElementsAs(ctx, &policyIDs, false)
133+
diags.Append(d...)
134+
return &policyIDs
135+
}
136+
// Return empty array instead of nil when agent_policy_ids is not defined
137+
emptyArray := []string{}
138+
return &emptyArray
139+
}(),
140+
Vars: utils.MapRef(utils.NormalizedTypeToMap[any](model.VarsJson, path.Root("vars_json"), &diags)),
98141
}
99142

100143
if isUpdate {

0 commit comments

Comments
 (0)