Skip to content

Commit 2c84cb0

Browse files
authored
Fix fleet_enrollment_tokens returning empty token set in some case (#683)
* 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. * Update changelog * Rename fleet api binding function
1 parent d928605 commit 2c84cb0

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## [Unreleased]
22

33
- Fix setting `id` for Fleet outputs and servers ([#666](https://github.com/elastic/terraform-provider-elasticstack/pull/666))
4+
- Fix `elasticstack_fleet_enrollment_tokens` returning empty tokens in some case ([#683](https://github.com/elastic/terraform-provider-elasticstack/pull/683))
45

56
## [0.11.4] - 2024-06-13
67

@@ -160,7 +161,7 @@
160161
- Add `elasticstack_elasticsearch_watch` for managing Elasticsearch Watches ([#155](https://github.com/elastic/terraform-provider-elasticstack/pull/155))
161162
- Add `elasticstack_kibana_alerting_rule` for managing Kibana alerting rules ([#292](https://github.com/elastic/terraform-provider-elasticstack/pull/292))
162163
- Add client for communicating with the Fleet APIs ([#311](https://github.com/elastic/terraform-provider-elasticstack/pull/311)])
163-
- 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)])
164+
- 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))
164165
- 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)])
165166
- Add `elasticstack_kibana_action_connector` for managing Kibana action connectors ([#306](https://github.com/elastic/terraform-provider-elasticstack/pull/306))
166167

internal/clients/fleet/fleet.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ func AllEnrollmentTokens(ctx context.Context, client *Client) ([]fleetapi.Enroll
2828
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
2929
}
3030

31+
// GetEnrollmentTokensByPolicy Get enrollment tokens by given policy ID
32+
func GetEnrollmentTokensByPolicy(ctx context.Context, client *Client, policyID string) ([]fleetapi.EnrollmentApiKey, diag.Diagnostics) {
33+
resp, err := client.API.GetEnrollmentApiKeysWithResponse(ctx, func(ctx context.Context, req *http.Request) error {
34+
q := req.URL.Query()
35+
q.Set("kuery", "policy_id:"+policyID)
36+
req.URL.RawQuery = q.Encode()
37+
38+
return nil
39+
})
40+
if err != nil {
41+
return nil, diag.FromErr(err)
42+
}
43+
44+
if resp.StatusCode() == http.StatusOK {
45+
return resp.JSON200.Items, nil
46+
}
47+
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
48+
}
49+
3150
// ReadAgentPolicy reads a specific agent policy from the API.
3251
func ReadAgentPolicy(ctx context.Context, client *Client, id string) (*fleetapi.AgentPolicy, diag.Diagnostics) {
3352
resp, err := client.API.AgentPolicyInfoWithResponse(ctx, id)

internal/fleet/enrollment_tokens_data_source.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fleet
33
import (
44
"context"
55

6+
fleetapi "github.com/elastic/terraform-provider-elasticstack/generated/fleet"
67
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
78
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -83,7 +84,14 @@ func dataSourceEnrollmentTokensRead(ctx context.Context, d *schema.ResourceData,
8384
}
8485
policyID := d.Id()
8586

86-
allTokens, diags := fleet.AllEnrollmentTokens(ctx, fleetClient)
87+
var allTokens []fleetapi.EnrollmentApiKey
88+
89+
if policyID == "" {
90+
allTokens, diags = fleet.AllEnrollmentTokens(ctx, fleetClient)
91+
} else {
92+
allTokens, diags = fleet.GetEnrollmentTokensByPolicy(ctx, fleetClient, policyID)
93+
}
94+
8795
if diags.HasError() {
8896
return diags
8997
}

0 commit comments

Comments
 (0)