Skip to content

Commit 27f16d4

Browse files
committed
remove Finish() and stop client in command
1 parent 9cdafbe commit 27f16d4

12 files changed

Lines changed: 20 additions & 39 deletions

File tree

internal/backend/backendrun/local_run.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,3 @@ type LocalRun struct {
8585
// during the run.
8686
PolicyClient policy.Client
8787
}
88-
89-
// Finish should be called when the local run has completed executing and
90-
// the resources should be cleaned up.
91-
func (lr *LocalRun) Finish() {
92-
if lr == nil {
93-
return
94-
}
95-
if lr.PolicyClient != nil {
96-
lr.PolicyClient.Stop()
97-
}
98-
}

internal/backend/local/backend_apply.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func (b *Local) opApply(
5959

6060
// Get our context
6161
lr, _, opState, contextDiags := b.localRun(op)
62-
defer lr.Finish()
6362

6463
diags = diags.Append(contextDiags)
6564
if contextDiags.HasErrors() {

internal/backend/local/backend_plan.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ func (b *Local) opPlan(
9090

9191
// Set up backend and get our context
9292
lr, configSnap, opState, ctxDiags := b.localRun(op)
93-
defer lr.Finish()
9493

9594
diags = diags.Append(ctxDiags)
9695
if ctxDiags.HasErrors() {

internal/backend/local/backend_refresh.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func (b *Local) opRefresh(
4949

5050
// Get our context
5151
lr, _, opState, contextDiags := b.localRun(op)
52-
defer lr.Finish()
5352

5453
diags = diags.Append(contextDiags)
5554
if contextDiags.HasErrors() {

internal/command/apply.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/hashicorp/terraform/internal/command/arguments"
1313
"github.com/hashicorp/terraform/internal/command/views"
1414
"github.com/hashicorp/terraform/internal/plans/planfile"
15-
"github.com/hashicorp/terraform/internal/policy"
1615
"github.com/hashicorp/terraform/internal/tfdiags"
1716
)
1817

@@ -106,13 +105,14 @@ func (c *ApplyCommand) Run(rawArgs []string) int {
106105
return 1
107106
}
108107

109-
if len(c.policyPaths) > 0 {
110-
var policyDiags policy.Diagnostics
111-
opReq.PolicyClient, policyDiags = c.PolicyClient(context.Background(), c.policyPaths)
108+
if len(c.Meta.policyPaths) > 0 {
109+
client, policyDiags, stopClient := c.PolicyClient(context.Background(), c.policyPaths)
112110
// if there has been any errors when setting up the policy client, we'll log them
113111
if opReq.View != nil && policyDiags != nil {
114112
opReq.View.PolicyResults(nil, policyDiags)
115113
}
114+
opReq.PolicyClient = client
115+
defer stopClient()
116116
}
117117

118118
// Collect variable value and add them to the operation request

internal/command/console.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ func (c *ConsoleCommand) Run(args []string) int {
9595

9696
// Get the context
9797
lr, _, ctxDiags := local.LocalRun(context.Background(), opReq)
98-
defer lr.Finish()
9998

10099
diags = diags.Append(ctxDiags)
101100
if ctxDiags.HasErrors() {

internal/command/graph.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ func (c *GraphCommand) Run(rawArgs []string) int {
9797

9898
// Get the context
9999
lr, _, ctxDiags := local.LocalRun(context.Background(), opReq)
100-
defer lr.Finish()
101100

102101
diags = diags.Append(ctxDiags)
103102
if ctxDiags.HasErrors() {

internal/command/import.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ func (c *ImportCommand) Run(args []string) int {
208208

209209
// Get the context
210210
lr, state, ctxDiags := local.LocalRun(context.Background(), opReq)
211-
defer lr.Finish()
212211

213212
diags = diags.Append(ctxDiags)
214213
if ctxDiags.HasErrors() {

internal/command/meta_policy.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,24 @@ import (
1515
"github.com/hashicorp/terraform/version"
1616
)
1717

18-
func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.Client, policy.Diagnostics) {
18+
func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.Client, policy.Diagnostics, func()) {
1919
var client policy.Client
20+
closer := func() {
21+
client.Stop()
22+
}
2023
if !c.AllowExperimentalFeatures {
2124
log.Printf("[DEBUG] Policies are not supported without experiments enabled, skipping policy client setup")
22-
return client, nil
25+
return client, nil, closer
2326
}
2427
if len(policyPaths) == 0 {
2528
log.Printf("[DEBUG] No policy paths configured, skipping policy client setup")
26-
return client, nil
29+
return client, nil, closer
2730
}
2831

2932
// Use a pre-initialized client for tests if one is available
3033
if c.testingOverrides != nil {
3134
if client := c.testingOverrides.PolicyClient; client != nil {
32-
return client, nil
35+
return client, nil, closer
3336
}
3437
}
3538

@@ -41,7 +44,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
4144
fmt.Sprintf("Failed to connect to policy engine: %s.", err),
4245
policy.SetupErrorResult,
4346
))
44-
return nil, diags
47+
return nil, diags, closer
4548
}
4649

4750
var callbackServiceID uint32
@@ -50,8 +53,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
5053
if srv, ok := client.(policy.CallbackService); ok {
5154
callbackServer, cbDiags := srv.RegisterCallbackService(ctx)
5255
if cbDiags != nil {
53-
client.Stop()
54-
return nil, cbDiags
56+
return nil, cbDiags, closer
5557
}
5658
callbackServiceID = callbackServer.ID
5759
}
@@ -66,7 +68,6 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
6668
for _, config := range resp.ServerConfigurations() {
6769
version, err := constraints.ParseRubyStyleMulti(config.RequiredVersion)
6870
if err != nil {
69-
client.Stop()
7071
diags = append(diags, policy.NewErrorDiagnostic(
7172
"Failed to validate required Terraform version",
7273
fmt.Sprintf("The policy file %s had a Terraform version constraint that could not be parsed: %s.", config.File, err),
@@ -80,7 +81,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
8081

8182
if len(diags) > 0 {
8283
client.Stop()
83-
return nil, diags
84+
return nil, diags, closer
8485
}
8586

8687
terraformVersion, err := versions.ParseVersion(version.Version)
@@ -97,10 +98,9 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
9798
fmt.Sprintf("The current version of Terraform is %s, and it is not compatible with the versions of Terraform required by the selected policies.", version.String()),
9899
policy.SetupErrorResult,
99100
))
100-
client.Stop()
101-
return nil, diags
101+
return nil, diags, closer
102102
}
103103

104104
log.Printf("[INFO] backend/operation/policy: Policy engine initialized")
105-
return client, diags
105+
return client, diags, closer
106106
}

internal/command/plan.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/hashicorp/terraform/internal/backend/backendrun"
1212
"github.com/hashicorp/terraform/internal/command/arguments"
1313
"github.com/hashicorp/terraform/internal/command/views"
14-
"github.com/hashicorp/terraform/internal/policy"
1514
"github.com/hashicorp/terraform/internal/tfdiags"
1615
)
1716

@@ -181,12 +180,13 @@ func (c *PlanCommand) OperationRequest(be backendrun.OperationsBackend, view vie
181180
}
182181

183182
if len(c.Meta.policyPaths) > 0 {
184-
var policyDiags policy.Diagnostics
185-
opReq.PolicyClient, policyDiags = c.PolicyClient(context.Background(), c.policyPaths)
186-
// if there has been any errors when setting up the policy client, we'll want to log them
183+
client, policyDiags, stopClient := c.PolicyClient(context.Background(), c.policyPaths)
184+
// if there has been any errors when setting up the policy client, we'll log them
187185
if opReq.View != nil && policyDiags != nil {
188186
opReq.View.PolicyResults(nil, policyDiags)
189187
}
188+
opReq.PolicyClient = client
189+
defer stopClient()
190190
}
191191

192192
return opReq, diags

0 commit comments

Comments
 (0)