Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Allow version changes without a destroy/create cycle with `elasticstack_fleet_integration` ([#1255](https://github.com/elastic/terraform-provider-elasticstack/pull/1255)). This fixes an issue where it was impossible to upgrade integrations which are used by an integration policy.
- Add `namespace` attribute to `elasticstack_kibana_synthetics_monitor` resource to support setting data stream namespace independently from `space_id` ([#1247](https://github.com/elastic/terraform-provider-elasticstack/pull/1247))
- Migrate `elasticstack_elasticsearch_security_role_mapping` resource and data source to Terraform Plugin Framework ([#1279](https://github.com/elastic/terraform-provider-elasticstack/pull/1279))
- Add support for `inactivity_timeout` in `elasticstack_fleet_agent_policy` ([#641](https://github.com/elastic/terraform-provider-elasticstack/issues/641))

## [0.11.17] - 2025-07-21

Expand Down
1 change: 1 addition & 0 deletions docs/resources/fleet_agent_policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ resource "elasticstack_fleet_agent_policy" "test_policy" {
- `download_source_id` (String) The identifier for the Elastic Agent binary download server.
- `fleet_server_host_id` (String) The identifier for the Fleet server host.
- `global_data_tags` (Attributes Map) User-defined data tags to apply to all inputs. Values can be strings (string_value) or numbers (number_value) but not both. Example -- key1 = {string_value = value1}, key2 = {number_value = 42} (see [below for nested schema](#nestedatt--global_data_tags))
- `inactivity_timeout` (Number) The inactivity timeout (in seconds) for the agent policy. If an agent does not report within this time period, it will be considered inactive.
- `monitor_logs` (Boolean) Enable collection of agent logs.
- `monitor_metrics` (Boolean) Enable collection of agent metrics.
- `monitoring_output_id` (String) The identifier for monitoring output.
Expand Down
34 changes: 19 additions & 15 deletions internal/fleet/agent_policy/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ type globalDataTagsItemModel struct {
}

type agentPolicyModel struct {
ID types.String `tfsdk:"id"`
PolicyID types.String `tfsdk:"policy_id"`
Name types.String `tfsdk:"name"`
Namespace types.String `tfsdk:"namespace"`
Description types.String `tfsdk:"description"`
DataOutputId types.String `tfsdk:"data_output_id"`
MonitoringOutputId types.String `tfsdk:"monitoring_output_id"`
FleetServerHostId types.String `tfsdk:"fleet_server_host_id"`
DownloadSourceId types.String `tfsdk:"download_source_id"`
MonitorLogs types.Bool `tfsdk:"monitor_logs"`
MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
SkipDestroy types.Bool `tfsdk:"skip_destroy"`
SupportsAgentless types.Bool `tfsdk:"supports_agentless"`
GlobalDataTags types.Map `tfsdk:"global_data_tags"` //> globalDataTagsModel
ID types.String `tfsdk:"id"`
PolicyID types.String `tfsdk:"policy_id"`
Name types.String `tfsdk:"name"`
Namespace types.String `tfsdk:"namespace"`
Description types.String `tfsdk:"description"`
DataOutputId types.String `tfsdk:"data_output_id"`
MonitoringOutputId types.String `tfsdk:"monitoring_output_id"`
FleetServerHostId types.String `tfsdk:"fleet_server_host_id"`
DownloadSourceId types.String `tfsdk:"download_source_id"`
MonitorLogs types.Bool `tfsdk:"monitor_logs"`
MonitorMetrics types.Bool `tfsdk:"monitor_metrics"`
SysMonitoring types.Bool `tfsdk:"sys_monitoring"`
SkipDestroy types.Bool `tfsdk:"skip_destroy"`
SupportsAgentless types.Bool `tfsdk:"supports_agentless"`
InactivityTimeout types.Float32 `tfsdk:"inactivity_timeout"`
GlobalDataTags types.Map `tfsdk:"global_data_tags"` //> globalDataTagsModel
}

func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.AgentPolicy) diag.Diagnostics {
Expand Down Expand Up @@ -73,6 +74,7 @@ func (model *agentPolicyModel) populateFromAPI(ctx context.Context, data *kbapi.
model.Name = types.StringValue(data.Name)
model.Namespace = types.StringValue(data.Namespace)
model.SupportsAgentless = types.BoolPointerValue(data.SupportsAgentless)
model.InactivityTimeout = types.Float32PointerValue(data.InactivityTimeout)
if utils.Deref(data.GlobalDataTags) != nil {
diags := diag.Diagnostics{}
var map0 = make(map[string]globalDataTagsItemModel)
Expand Down Expand Up @@ -183,6 +185,7 @@ func (model *agentPolicyModel) toAPICreateModel(ctx context.Context, feat featur
Name: model.Name.ValueString(),
Namespace: model.Namespace.ValueString(),
SupportsAgentless: model.SupportsAgentless.ValueBoolPointer(),
InactivityTimeout: model.InactivityTimeout.ValueFloat32Pointer(),
}

tags, diags := model.convertGlobalDataTags(ctx, feat)
Expand Down Expand Up @@ -223,6 +226,7 @@ func (model *agentPolicyModel) toAPIUpdateModel(ctx context.Context, feat featur
Name: model.Name.ValueString(),
Namespace: model.Namespace.ValueString(),
SupportsAgentless: model.SupportsAgentless.ValueBoolPointer(),
InactivityTimeout: model.InactivityTimeout.ValueFloat32Pointer(),
}

tags, diags := model.convertGlobalDataTags(ctx, feat)
Expand Down
37 changes: 37 additions & 0 deletions internal/fleet/agent_policy/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ func TestAccResourceAgentPolicy(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"skip_destroy"},
},
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minVersionAgentPolicy),
Config: testAccResourceAgentPolicyCreateWithInactivityTimeout(policyName, false, 120),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "name", fmt.Sprintf("Policy %s", policyName)),
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "namespace", "default"),
resource.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "description", "Test Agent Policy with Inactivity Timeout"),
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.TestCheckResourceAttr("elasticstack_fleet_agent_policy.test_policy", "inactivity_timeout", "120"),
),
},
{
SkipFunc: versionutils.CheckIfVersionIsUnsupported(agent_policy.MinVersionGlobalDataTags),
Config: testAccResourceAgentPolicyCreateWithGlobalDataTags(policyNameGlobalDataTags, false),
Expand Down Expand Up @@ -295,6 +308,30 @@ data "elasticstack_fleet_enrollment_tokens" "test_policy" {
`, fmt.Sprintf("Policy %s", id), skipDestroy)
}

func testAccResourceAgentPolicyCreateWithInactivityTimeout(id string, skipDestroy bool, inactivityTimeout float32) string {
return fmt.Sprintf(`
provider "elasticstack" {
elasticsearch {}
kibana {}
}

resource "elasticstack_fleet_agent_policy" "test_policy" {
name = "%s"
namespace = "default"
description = "Test Agent Policy with Inactivity Timeout"
monitor_logs = true
monitor_metrics = false
skip_destroy = %t
inactivity_timeout = %g
}

data "elasticstack_fleet_enrollment_tokens" "test_policy" {
policy_id = elasticstack_fleet_agent_policy.test_policy.policy_id
}

`, fmt.Sprintf("Policy %s", id), skipDestroy, inactivityTimeout)
}

func testAccResourceAgentPolicyCreateWithBadGlobalDataTags(id string, skipDestroy bool) string {
return fmt.Sprintf(`
provider "elasticstack" {
Expand Down
5 changes: 5 additions & 0 deletions internal/fleet/agent_policy/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func getSchema() schema.Schema {
boolplanmodifier.RequiresReplace(),
},
},
"inactivity_timeout": schema.Float32Attribute{
Description: "The inactivity timeout (in seconds) for the agent policy. If an agent does not report within this time period, it will be considered inactive.",
Computed: true,
Optional: true,
},
"global_data_tags": schema.MapNestedAttribute{
Description: "User-defined data tags to apply to all inputs. Values can be strings (string_value) or numbers (number_value) but not both. Example -- key1 = {string_value = value1}, key2 = {number_value = 42}",
NestedObject: schema.NestedAttributeObject{
Expand Down
Loading