Skip to content

Commit

Permalink
Fallback to using standard terraform-exec commands (e.g., Apply() rat…
Browse files Browse the repository at this point in the history
…her than ApplyJSON()) if running commands with -json flag raises an ErrVersionMismatch error (#16)
  • Loading branch information
bendbennett committed Jan 16, 2023
1 parent 9a154c6 commit da0f4e9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hashicorp/hc-install v0.4.0
github.com/hashicorp/hcl/v2 v2.15.0
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/terraform-exec v0.17.4-0.20230110163400-c387e4f7bf98
github.com/hashicorp/terraform-exec v0.17.4-0.20230116095935-bc76870e2b9b
github.com/hashicorp/terraform-json v0.14.0
github.com/hashicorp/terraform-plugin-framework v1.0.1
github.com/hashicorp/terraform-plugin-go v0.14.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.17.4-0.20230110163400-c387e4f7bf98 h1:WkSxIVXfV/u9tvDGXdSw4v2eCpRMSt7mYRBseX+OaSU=
github.com/hashicorp/terraform-exec v0.17.4-0.20230110163400-c387e4f7bf98/go.mod h1:5M9hP3RX39/2AWDpAEWf0whTJFibOUN7DfH1E1lCZN8=
github.com/hashicorp/terraform-exec v0.17.4-0.20230116095935-bc76870e2b9b h1:z4a1oo2M/yWj2ljEoXsFQRkmp2dRqvq4Y9HjQl3eBz8=
github.com/hashicorp/terraform-exec v0.17.4-0.20230116095935-bc76870e2b9b/go.mod h1:5M9hP3RX39/2AWDpAEWf0whTJFibOUN7DfH1E1lCZN8=
github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e17dKDpqV7s=
github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
github.com/hashicorp/terraform-plugin-framework v1.0.1 h1:apX2jtaEKa15+do6H2izBJdl1dEH2w5BPVkDJ3Q3mKA=
Expand Down
89 changes: 81 additions & 8 deletions helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"

"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
"github.com/mitchellh/go-testing-interface"

Expand All @@ -32,7 +33,17 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
return wd.RefreshJSON(ctx, w)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running pre-apply refresh: %s", getJSONOutputStr(wd))
target := &tfexec.ErrVersionMismatch{}
if errors.As(err, &target) {
err = runProviderCommand(ctx, t, func() error {
return wd.Refresh(ctx)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running pre-apply refresh: %w", err)
}
} else {
return fmt.Errorf("Error running pre-apply refresh: %s", getJSONOutputStr(wd))
}
}

// If this step is a PlanOnly step, skip over this first Plan and
Expand All @@ -49,7 +60,20 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
return wd.CreatePlanJSON(ctx, w)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running pre-apply plan: %s", getJSONOutputStr(wd))
target := &tfexec.ErrVersionMismatch{}
if errors.As(err, &target) {
err = runProviderCommand(ctx, t, func() error {
if step.Destroy {
return wd.CreateDestroyPlan(ctx)
}
return wd.CreatePlan(ctx)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running pre-apply plan: %w", err)
}
} else {
return fmt.Errorf("Error running pre-apply plan: %s", getJSONOutputStr(wd))
}
}

// We need to keep a copy of the state prior to destroying such
Expand All @@ -72,10 +96,23 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
return wd.ApplyJSON(ctx, w)
}, wd, providers)
if err != nil {
if step.Destroy {
return fmt.Errorf("Error running destroy: %w", err)
target := &tfexec.ErrVersionMismatch{}
if errors.As(err, &target) {
err = runProviderCommand(ctx, t, func() error {
return wd.Apply(ctx)
}, wd, providers)
if err != nil {
if step.Destroy {
return fmt.Errorf("Error running destroy: %w", err)
}
return fmt.Errorf("Error running apply: %w", err)
}
} else {
if step.Destroy {
return fmt.Errorf("Error running destroy: %s", getJSONOutputStr(wd))
}
return fmt.Errorf("Error running apply: %s", getJSONOutputStr(wd))
}
return fmt.Errorf("Error running apply: %s", getJSONOutputStr(wd))
}

// Get the new state
Expand Down Expand Up @@ -119,7 +156,20 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
return wd.CreatePlanJSON(ctx, w)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running post-apply plan: %s", getJSONOutputStr(wd))
target := &tfexec.ErrVersionMismatch{}
if errors.As(err, &target) {
err = runProviderCommand(ctx, t, func() error {
if step.Destroy {
return wd.CreateDestroyPlan(ctx)
}
return wd.CreatePlan(ctx)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running post-apply plan: %w", err)
}
} else {
return fmt.Errorf("Error running post-apply plan: %s", getJSONOutputStr(wd))
}
}

var plan *tfjson.Plan
Expand Down Expand Up @@ -151,7 +201,17 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
return wd.RefreshJSON(ctx, w)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running post-apply refresh: %s", getJSONOutputStr(wd))
target := &tfexec.ErrVersionMismatch{}
if errors.As(err, &target) {
err = runProviderCommand(ctx, t, func() error {
return wd.Refresh(ctx)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running post-apply refresh: %w", err)
}
} else {
return fmt.Errorf("Error running post-apply refresh: %s", getJSONOutputStr(wd))
}
}
}

Expand All @@ -163,7 +223,20 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
return wd.CreatePlanJSON(ctx, w)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running second post-apply plan: %s", getJSONOutputStr(wd))
target := &tfexec.ErrVersionMismatch{}
if errors.As(err, &target) {
err = runProviderCommand(ctx, t, func() error {
if step.Destroy {
return wd.CreateDestroyPlan(ctx)
}
return wd.CreatePlan(ctx)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running second post-apply plan: %w", err)
}
} else {
return fmt.Errorf("Error running second post-apply plan: %s", getJSONOutputStr(wd))
}
}

err = runProviderCommand(ctx, t, func() error {
Expand Down
26 changes: 24 additions & 2 deletions helper/resource/testing_new_refresh_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ package resource

import (
"context"
"errors"
"fmt"
"io"

"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
"github.com/mitchellh/go-testing-interface"

Expand Down Expand Up @@ -37,7 +39,17 @@ func testStepNewRefreshState(ctx context.Context, t testing.T, wd *plugintest.Wo
return wd.RefreshJSON(ctx, w)
}, wd, providers)
if err != nil {
return fmt.Errorf("%s", getJSONOutputStr(wd))
target := &tfexec.ErrVersionMismatch{}
if errors.As(err, &target) {
err = runProviderCommand(ctx, t, func() error {
return wd.Refresh(ctx)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running refresh: %w", err)
}
} else {
return fmt.Errorf("Error running refresh: %s", getJSONOutputStr(wd))
}
}

var refreshState *terraform.State
Expand Down Expand Up @@ -68,7 +80,17 @@ func testStepNewRefreshState(ctx context.Context, t testing.T, wd *plugintest.Wo
return wd.CreatePlanJSON(ctx, w)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running post-apply plan: %s", getJSONOutputStr(wd))
target := &tfexec.ErrVersionMismatch{}
if errors.As(err, &target) {
err = runProviderCommand(ctx, t, func() error {
return wd.CreatePlan(ctx)
}, wd, providers)
if err != nil {
return fmt.Errorf("Error running post-apply plan: %w", err)
}
} else {
return fmt.Errorf("Error running post-apply plan: %s", getJSONOutputStr(wd))
}
}

var plan *tfjson.Plan
Expand Down

0 comments on commit da0f4e9

Please sign in to comment.