From 7c6df7ff17c40dca5b61821dd77b1a75faa80429 Mon Sep 17 00:00:00 2001 From: Hollis Wu Date: Wed, 17 Jul 2024 23:08:46 -0400 Subject: [PATCH 1/3] Fix fleet_enrollment_tokens returning empty token set in some case The data source is not handling pagination at all. This fix the case when kibana server got more than 20 fleet enrollment tokens and the provider is not paginating the result when searching for a match. Fetching all tokens case (empty policyID in input) is still not handled. Just trying to push a fix with minimum effort for now. Partially fixes #528. --- internal/clients/fleet/fleet.go | 19 +++++++++++++++++++ .../fleet/enrollment_tokens_data_source.go | 10 +++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/internal/clients/fleet/fleet.go b/internal/clients/fleet/fleet.go index 5655dd58d..bd03cf3d6 100644 --- a/internal/clients/fleet/fleet.go +++ b/internal/clients/fleet/fleet.go @@ -28,6 +28,25 @@ func AllEnrollmentTokens(ctx context.Context, client *Client) ([]fleetapi.Enroll return nil, reportUnknownError(resp.StatusCode(), resp.Body) } +// SearchEnrollmentTokens Get enrollment tokens by given policy ID +func SearchEnrollmentTokens(ctx context.Context, client *Client, policyID string) ([]fleetapi.EnrollmentApiKey, diag.Diagnostics) { + resp, err := client.API.GetEnrollmentApiKeysWithResponse(ctx, func(ctx context.Context, req *http.Request) error { + q := req.URL.Query() + q.Set("kuery", "policy_id:"+policyID) + req.URL.RawQuery = q.Encode() + + return nil + }) + if err != nil { + return nil, diag.FromErr(err) + } + + if resp.StatusCode() == http.StatusOK { + return resp.JSON200.Items, nil + } + return nil, reportUnknownError(resp.StatusCode(), resp.Body) +} + // ReadAgentPolicy reads a specific agent policy from the API. func ReadAgentPolicy(ctx context.Context, client *Client, id string) (*fleetapi.AgentPolicy, diag.Diagnostics) { resp, err := client.API.AgentPolicyInfoWithResponse(ctx, id) diff --git a/internal/fleet/enrollment_tokens_data_source.go b/internal/fleet/enrollment_tokens_data_source.go index 5f0fe851a..899756ae8 100644 --- a/internal/fleet/enrollment_tokens_data_source.go +++ b/internal/fleet/enrollment_tokens_data_source.go @@ -3,6 +3,7 @@ package fleet import ( "context" + fleetapi "github.com/elastic/terraform-provider-elasticstack/generated/fleet" "github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet" "github.com/elastic/terraform-provider-elasticstack/internal/utils" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -83,7 +84,14 @@ func dataSourceEnrollmentTokensRead(ctx context.Context, d *schema.ResourceData, } policyID := d.Id() - allTokens, diags := fleet.AllEnrollmentTokens(ctx, fleetClient) + var allTokens []fleetapi.EnrollmentApiKey + + if policyID == "" { + allTokens, diags = fleet.AllEnrollmentTokens(ctx, fleetClient) + } else { + allTokens, diags = fleet.SearchEnrollmentTokens(ctx, fleetClient, policyID) + } + if diags.HasError() { return diags } From dccd9fd9fe565a045e47e2abe771393f15cbb1aa Mon Sep 17 00:00:00 2001 From: Hollis Wu Date: Wed, 24 Jul 2024 10:58:39 -0400 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afa61a261..f097c23c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [Unreleased] - Fix setting `id` for Fleet outputs and servers ([#666](https://github.com/elastic/terraform-provider-elasticstack/pull/666)) +- Fix `elasticstack_fleet_enrollment_tokens` returning empty tokens in some case ([#683](https://github.com/elastic/terraform-provider-elasticstack/pull/683)) ## [0.11.4] - 2024-06-13 @@ -160,7 +161,7 @@ - Add `elasticstack_elasticsearch_watch` for managing Elasticsearch Watches ([#155](https://github.com/elastic/terraform-provider-elasticstack/pull/155)) - Add `elasticstack_kibana_alerting_rule` for managing Kibana alerting rules ([#292](https://github.com/elastic/terraform-provider-elasticstack/pull/292)) - Add client for communicating with the Fleet APIs ([#311](https://github.com/elastic/terraform-provider-elasticstack/pull/311)]) -- Add `elasticstack_fleet_enrollment_tokens` and `elasticstack_fleet_agent_policy` for managing Fleet enrollment tokens and agent policies ([#322](https://github.com/elastic/terraform-provider-elasticstack/pull/322)]) +- Add `elasticstack_fleet_enrollment_tokens` and `elasticstack_fleet_agent_policy` for managing Fleet enrollment tokens and agent policies ([#322](https://github.com/elastic/terraform-provider-elasticstack/pull/322)) - Add `elasticstack_fleet_output` and `elasticstack_fleet_server_host` for managing Fleet outputs and server hosts ([#327](https://github.com/elastic/terraform-provider-elasticstack/pull/327)]) - Add `elasticstack_kibana_action_connector` for managing Kibana action connectors ([#306](https://github.com/elastic/terraform-provider-elasticstack/pull/306)) From a8d0e2395188537016a98c0594b000be662eaad6 Mon Sep 17 00:00:00 2001 From: Hollis Wu Date: Wed, 24 Jul 2024 10:59:35 -0400 Subject: [PATCH 3/3] Rename fleet api binding function --- internal/clients/fleet/fleet.go | 4 ++-- internal/fleet/enrollment_tokens_data_source.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/clients/fleet/fleet.go b/internal/clients/fleet/fleet.go index bd03cf3d6..0bf11ee4e 100644 --- a/internal/clients/fleet/fleet.go +++ b/internal/clients/fleet/fleet.go @@ -28,8 +28,8 @@ func AllEnrollmentTokens(ctx context.Context, client *Client) ([]fleetapi.Enroll return nil, reportUnknownError(resp.StatusCode(), resp.Body) } -// SearchEnrollmentTokens Get enrollment tokens by given policy ID -func SearchEnrollmentTokens(ctx context.Context, client *Client, policyID string) ([]fleetapi.EnrollmentApiKey, diag.Diagnostics) { +// GetEnrollmentTokensByPolicy Get enrollment tokens by given policy ID +func GetEnrollmentTokensByPolicy(ctx context.Context, client *Client, policyID string) ([]fleetapi.EnrollmentApiKey, diag.Diagnostics) { resp, err := client.API.GetEnrollmentApiKeysWithResponse(ctx, func(ctx context.Context, req *http.Request) error { q := req.URL.Query() q.Set("kuery", "policy_id:"+policyID) diff --git a/internal/fleet/enrollment_tokens_data_source.go b/internal/fleet/enrollment_tokens_data_source.go index 899756ae8..a0790637c 100644 --- a/internal/fleet/enrollment_tokens_data_source.go +++ b/internal/fleet/enrollment_tokens_data_source.go @@ -89,7 +89,7 @@ func dataSourceEnrollmentTokensRead(ctx context.Context, d *schema.ResourceData, if policyID == "" { allTokens, diags = fleet.AllEnrollmentTokens(ctx, fleetClient) } else { - allTokens, diags = fleet.SearchEnrollmentTokens(ctx, fleetClient, policyID) + allTokens, diags = fleet.GetEnrollmentTokensByPolicy(ctx, fleetClient, policyID) } if diags.HasError() {