Skip to content

Commit fc5f9c9

Browse files
committed
refactor: apply request editor pattern to all Fleet resources
Following Tobio's enrollment tokens example, refactor all Fleet client functions to use spaceAwarePathRequestEditor instead of duplicate *InSpace functions with manual HTTP handling. Changes: - Deleted 17 duplicate *InSpace functions (~581 lines of manual HTTP code) - Updated base functions to accept spaceID parameter and use request editor - Simplified all CRUD operations to single function calls - Updated test files for new signatures Benefits: - Single code path using generated client (vs manual HTTP) - Consistent pattern across all Fleet resources - Eliminates 50% code duplication for Fleet operations - Easier to maintain and test All Fleet resources now use: GetResource(ctx, client, id, spaceID) CreateResource(ctx, client, spaceID, body) UpdateResource(ctx, client, id, spaceID, body) DeleteResource(ctx, client, id, spaceID) The spaceAwarePathRequestEditor handles path modification automatically.
1 parent 1e423ad commit fc5f9c9

File tree

25 files changed

+64
-739
lines changed

25 files changed

+64
-739
lines changed

internal/clients/fleet/fleet.go

Lines changed: 34 additions & 615 deletions
Large diffs are not rendered by default.

internal/fleet/agent_policy/acc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ func checkResourceAgentPolicyDestroy(s *terraform.State) error {
622622
if err != nil {
623623
return err
624624
}
625-
policy, diags := fleet.GetAgentPolicy(context.Background(), fleetClient, rs.Primary.ID)
625+
policy, diags := fleet.GetAgentPolicy(context.Background(), fleetClient, rs.Primary.ID, "")
626626
if diags.HasError() {
627627
return diagutil.FwDiagsAsError(diags)
628628
}
@@ -648,15 +648,15 @@ func checkResourceAgentPolicySkipDestroy(s *terraform.State) error {
648648
if err != nil {
649649
return err
650650
}
651-
policy, diags := fleet.GetAgentPolicy(context.Background(), fleetClient, rs.Primary.ID)
651+
policy, diags := fleet.GetAgentPolicy(context.Background(), fleetClient, rs.Primary.ID, "")
652652
if diags.HasError() {
653653
return diagutil.FwDiagsAsError(diags)
654654
}
655655
if policy == nil {
656656
return fmt.Errorf("agent policy id=%v does not exist, but should still exist when skip_destroy is true", rs.Primary.ID)
657657
}
658658

659-
if diags = fleet.DeleteAgentPolicy(context.Background(), fleetClient, rs.Primary.ID); diags.HasError() {
659+
if diags = fleet.DeleteAgentPolicy(context.Background(), fleetClient, rs.Primary.ID, ""); diags.HasError() {
660660
return diagutil.FwDiagsAsError(diags)
661661
}
662662
}

internal/fleet/agent_policy/create.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,19 @@ func (r *agentPolicyResource) Create(ctx context.Context, req resource.CreateReq
5454
// exists within that space context, not in the default space.
5555
var readPolicy *kbapi.AgentPolicy
5656
var getDiags diag.Diagnostics
57+
var spaceID string
5758

5859
if !planModel.SpaceIds.IsNull() && !planModel.SpaceIds.IsUnknown() {
5960
var tempDiags diag.Diagnostics
6061
spaceIDs := utils.SetTypeAs[types.String](ctx, planModel.SpaceIds, path.Root("space_ids"), &tempDiags)
6162
if !tempDiags.HasError() && len(spaceIDs) > 0 {
6263
// Use the first space for the GET request
63-
spaceID := spaceIDs[0].ValueString()
64-
readPolicy, getDiags = fleet.GetAgentPolicyInSpace(ctx, client, policy.Id, spaceID)
65-
} else {
66-
// Fall back to standard GET if we couldn't extract space IDs
67-
readPolicy, getDiags = fleet.GetAgentPolicy(ctx, client, policy.Id)
64+
spaceID = spaceIDs[0].ValueString()
6865
}
69-
} else {
70-
// No space_ids, use standard GET
71-
readPolicy, getDiags = fleet.GetAgentPolicy(ctx, client, policy.Id)
7266
}
7367

68+
readPolicy, getDiags = fleet.GetAgentPolicy(ctx, client, policy.Id, spaceID)
69+
7470
resp.Diagnostics.Append(getDiags...)
7571
if resp.Diagnostics.HasError() {
7672
return

internal/fleet/agent_policy/delete.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ func (r *agentPolicyResource) Delete(ctx context.Context, req resource.DeleteReq
4141
}
4242

4343
// Delete using the operational space from STATE
44-
if spaceID != "" {
45-
diags = fleet.DeleteAgentPolicyInSpace(ctx, client, policyID, spaceID)
46-
} else {
47-
diags = fleet.DeleteAgentPolicy(ctx, client, policyID)
48-
}
44+
diags = fleet.DeleteAgentPolicy(ctx, client, policyID, spaceID)
4945

5046
resp.Diagnostics.Append(diags...)
5147
}

internal/fleet/agent_policy/read.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package agent_policy
33
import (
44
"context"
55

6-
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
76
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
87
fleetutils "github.com/elastic/terraform-provider-elasticstack/internal/fleet"
98
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -34,12 +33,7 @@ func (r *agentPolicyResource) Read(ctx context.Context, req resource.ReadRequest
3433
}
3534

3635
// Query using the operational space from STATE
37-
var policy *kbapi.AgentPolicy
38-
if spaceID != "" {
39-
policy, diags = fleet.GetAgentPolicyInSpace(ctx, client, policyID, spaceID)
40-
} else {
41-
policy, diags = fleet.GetAgentPolicy(ctx, client, policyID)
42-
}
36+
policy, diags := fleet.GetAgentPolicy(ctx, client, policyID, spaceID)
4337

4438
resp.Diagnostics.Append(diags...)
4539
if resp.Diagnostics.HasError() {

internal/fleet/agent_policy/update.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package agent_policy
33
import (
44
"context"
55

6-
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
76
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
87
fleetutils "github.com/elastic/terraform-provider-elasticstack/internal/fleet"
98
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -52,12 +51,7 @@ func (r *agentPolicyResource) Update(ctx context.Context, req resource.UpdateReq
5251

5352
// Update using the operational space from STATE
5453
// The API will handle adding/removing the policy from spaces based on space_ids in body
55-
var policy *kbapi.AgentPolicy
56-
if spaceID != "" {
57-
policy, diags = fleet.UpdateAgentPolicyInSpace(ctx, client, policyID, spaceID, body)
58-
} else {
59-
policy, diags = fleet.UpdateAgentPolicy(ctx, client, policyID, body)
60-
}
54+
policy, diags := fleet.UpdateAgentPolicy(ctx, client, policyID, spaceID, body)
6155

6256
resp.Diagnostics.Append(diags...)
6357
if resp.Diagnostics.HasError() {

internal/fleet/enrollment_tokens/data_source_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func checkResourceAgentPolicyDestroy(s *terraform.State) error {
6868
if err != nil {
6969
return err
7070
}
71-
policy, diags := fleet.GetAgentPolicy(context.Background(), fleetClient, rs.Primary.ID)
71+
policy, diags := fleet.GetAgentPolicy(context.Background(), fleetClient, rs.Primary.ID, "")
7272
if diags.HasError() {
7373
return diagutil.FwDiagsAsError(diags)
7474
}

internal/fleet/integration/acc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestAccResourceIntegrationDeleted(t *testing.T) {
136136
require.NoError(t, err)
137137

138138
ctx := context.Background()
139-
diags := fleet.Uninstall(ctx, fleetClient, "sysmon_linux", "1.7.0", true)
139+
diags := fleet.Uninstall(ctx, fleetClient, "sysmon_linux", "1.7.0", "", true)
140140
require.Empty(t, diags)
141141
},
142142
// Expect the plan to want to reinstall

internal/fleet/integration/create.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ func (r integrationResource) create(ctx context.Context, plan tfsdk.Plan, state
4545
}
4646
}
4747

48-
if spaceID != "" && spaceID != "default" {
49-
diags = fleet.InstallPackageInSpace(ctx, client, name, version, spaceID, force)
50-
} else {
51-
diags = fleet.InstallPackage(ctx, client, name, version, force)
52-
}
48+
diags = fleet.InstallPackage(ctx, client, name, version, spaceID, force)
5349
respDiags.Append(diags...)
5450
if respDiags.HasError() {
5551
return

internal/fleet/integration/delete.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ func (r *integrationResource) Delete(ctx context.Context, req resource.DeleteReq
4646
}
4747
}
4848

49-
if spaceID != "" && spaceID != "default" {
50-
diags = fleet.UninstallInSpace(ctx, client, name, version, spaceID, force)
51-
} else {
52-
diags = fleet.Uninstall(ctx, client, name, version, force)
53-
}
49+
diags = fleet.Uninstall(ctx, client, name, version, spaceID, force)
5450
resp.Diagnostics.Append(diags...)
5551
}

0 commit comments

Comments
 (0)