Skip to content

Commit 761cc7c

Browse files
authored
UseStateForUnknown for agent policy ids (#885)
* UseStateForUnknown for agent policy ids * Changelog
1 parent 7845a51 commit 761cc7c

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Support multiple group by fields in SLOs ([#870](https://github.com/elastic/terraform-provider-elasticstack/pull/878))
44
- Use the auto-generated OAS schema from elastic/kibana for the Fleet API. ([#834](https://github.com/elastic/terraform-provider-elasticstack/issues/834))
55
- Support description in `elasticstack_elasticsearch_security_role` data sources. ([#884](https://github.com/elastic/terraform-provider-elasticstack/pull/884))
6+
- Prevent spurious recreation of `elasticstack_fleet_agent_policy` resources due to 'changing' policy ids ([#885](https://github.com/elastic/terraform-provider-elasticstack/pull/885))
67

78
## [0.11.11] - 2024-10-25
89

internal/fleet/agent_policy/resource_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package agent_policy_test
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"testing"
78

@@ -62,6 +63,7 @@ func TestAccResourceAgentPolicyFromSDK(t *testing.T) {
6263

6364
func TestAccResourceAgentPolicy(t *testing.T) {
6465
policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum)
66+
var originalPolicyId string
6567

6668
resource.Test(t, resource.TestCase{
6769
PreCheck: func() { acctest.PreCheck(t) },
@@ -78,6 +80,15 @@ func TestAccResourceAgentPolicy(t *testing.T) {
7880
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "true"),
7981
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "false"),
8082
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"),
83+
resource.TestCheckResourceAttrWith("elasticstack_fleet_agent_policy.test_policy", "policy_id", func(value string) error {
84+
originalPolicyId = value
85+
86+
if len(value) == 0 {
87+
return errors.New("expected policy_id to be non empty")
88+
}
89+
90+
return nil
91+
}),
8192
),
8293
},
8394
{
@@ -90,6 +101,13 @@ func TestAccResourceAgentPolicy(t *testing.T) {
90101
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "false"),
91102
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "true"),
92103
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"),
104+
resource.TestCheckResourceAttrWith("elasticstack_fleet_agent_policy.test_policy", "policy_id", func(value string) error {
105+
if value != originalPolicyId {
106+
return fmt.Errorf("expected policy_id to not change between test steps. Was [%s], now [%s]", originalPolicyId, value)
107+
}
108+
109+
return nil
110+
}),
93111
),
94112
},
95113
{

internal/fleet/agent_policy/schema.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ func (r *agentPolicyResource) Schema(ctx context.Context, req resource.SchemaReq
1717
"id": schema.StringAttribute{
1818
Description: "The ID of this resource.",
1919
Computed: true,
20+
PlanModifiers: []planmodifier.String{
21+
stringplanmodifier.UseStateForUnknown(),
22+
},
2023
},
2124
"policy_id": schema.StringAttribute{
2225
Description: "Unique identifier of the agent policy.",
2326
Computed: true,
2427
Optional: true,
2528
PlanModifiers: []planmodifier.String{
29+
stringplanmodifier.UseStateForUnknown(),
2630
stringplanmodifier.RequiresReplace(),
2731
},
2832
},

0 commit comments

Comments
 (0)