diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dcaf803..96e11af8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Support multiple group by fields in SLOs ([#870](https://github.com/elastic/terraform-provider-elasticstack/pull/878)) - Use the auto-generated OAS schema from elastic/kibana for the Fleet API. ([#834](https://github.com/elastic/terraform-provider-elasticstack/issues/834)) - Support description in `elasticstack_elasticsearch_security_role` data sources. ([#884](https://github.com/elastic/terraform-provider-elasticstack/pull/884)) +- Prevent spurious recreation of `elasticstack_fleet_agent_policy` resources due to 'changing' policy ids ([#885](https://github.com/elastic/terraform-provider-elasticstack/pull/885)) ## [0.11.11] - 2024-10-25 diff --git a/internal/fleet/agent_policy/resource_test.go b/internal/fleet/agent_policy/resource_test.go index a885e617..cd0d41d1 100644 --- a/internal/fleet/agent_policy/resource_test.go +++ b/internal/fleet/agent_policy/resource_test.go @@ -2,6 +2,7 @@ package agent_policy_test import ( "context" + "errors" "fmt" "testing" @@ -62,6 +63,7 @@ func TestAccResourceAgentPolicyFromSDK(t *testing.T) { func TestAccResourceAgentPolicy(t *testing.T) { policyName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum) + var originalPolicyId string resource.Test(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(t) }, @@ -78,6 +80,15 @@ func TestAccResourceAgentPolicy(t *testing.T) { resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "true"), resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "false"), resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"), + resource.TestCheckResourceAttrWith("elasticstack_fleet_agent_policy.test_policy", "policy_id", func(value string) error { + originalPolicyId = value + + if len(value) == 0 { + return errors.New("expected policy_id to be non empty") + } + + return nil + }), ), }, { @@ -90,6 +101,13 @@ func TestAccResourceAgentPolicy(t *testing.T) { resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_logs", "false"), resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "monitor_metrics", "true"), resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "skip_destroy", "false"), + resource.TestCheckResourceAttrWith("elasticstack_fleet_agent_policy.test_policy", "policy_id", func(value string) error { + if value != originalPolicyId { + return fmt.Errorf("expected policy_id to not change between test steps. Was [%s], now [%s]", originalPolicyId, value) + } + + return nil + }), ), }, { diff --git a/internal/fleet/agent_policy/schema.go b/internal/fleet/agent_policy/schema.go index c64d7319..7b0e4507 100644 --- a/internal/fleet/agent_policy/schema.go +++ b/internal/fleet/agent_policy/schema.go @@ -17,12 +17,16 @@ func (r *agentPolicyResource) Schema(ctx context.Context, req resource.SchemaReq "id": schema.StringAttribute{ Description: "The ID of this resource.", Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, }, "policy_id": schema.StringAttribute{ Description: "Unique identifier of the agent policy.", Computed: true, Optional: true, PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), stringplanmodifier.RequiresReplace(), }, },