Skip to content

Commit 8b6d17f

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

13 files changed

Lines changed: 26 additions & 44 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ func (b *Local) opRefresh(
4949

5050
// Get our context
5151
lr, _, opState, contextDiags := b.localRun(op)
52-
defer lr.Finish()
53-
5452
diags = diags.Append(contextDiags)
5553
if contextDiags.HasErrors() {
5654
op.ReportResult(runningOp, diags)

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: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ 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+
if client != nil {
22+
client.Stop()
23+
}
24+
}
2025
if !c.AllowExperimentalFeatures {
2126
log.Printf("[DEBUG] Policies are not supported without experiments enabled, skipping policy client setup")
22-
return client, nil
27+
return client, nil, closer
2328
}
2429
if len(policyPaths) == 0 {
2530
log.Printf("[DEBUG] No policy paths configured, skipping policy client setup")
26-
return client, nil
31+
return client, nil, closer
2732
}
2833

2934
// Use a pre-initialized client for tests if one is available
3035
if c.testingOverrides != nil {
3136
if client := c.testingOverrides.PolicyClient; client != nil {
32-
return client, nil
37+
return client, nil, closer
3338
}
3439
}
3540

@@ -41,7 +46,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
4146
fmt.Sprintf("Failed to connect to policy engine: %s.", err),
4247
policy.SetupErrorResult,
4348
))
44-
return nil, diags
49+
return nil, diags, closer
4550
}
4651

4752
var callbackServiceID uint32
@@ -50,8 +55,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
5055
if srv, ok := client.(policy.CallbackService); ok {
5156
callbackServer, cbDiags := srv.RegisterCallbackService(ctx)
5257
if cbDiags != nil {
53-
client.Stop()
54-
return nil, cbDiags
58+
return nil, cbDiags, closer
5559
}
5660
callbackServiceID = callbackServer.ID
5761
}
@@ -66,7 +70,6 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
6670
for _, config := range resp.ServerConfigurations() {
6771
version, err := constraints.ParseRubyStyleMulti(config.RequiredVersion)
6872
if err != nil {
69-
client.Stop()
7073
diags = append(diags, policy.NewErrorDiagnostic(
7174
"Failed to validate required Terraform version",
7275
fmt.Sprintf("The policy file %s had a Terraform version constraint that could not be parsed: %s.", config.File, err),
@@ -80,7 +83,7 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
8083

8184
if len(diags) > 0 {
8285
client.Stop()
83-
return nil, diags
86+
return nil, diags, closer
8487
}
8588

8689
terraformVersion, err := versions.ParseVersion(version.Version)
@@ -97,10 +100,9 @@ func (c *Meta) PolicyClient(ctx context.Context, policyPaths []string) (policy.C
97100
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()),
98101
policy.SetupErrorResult,
99102
))
100-
client.Stop()
101-
return nil, diags
103+
return nil, diags, closer
102104
}
103105

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

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)