Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for terraform plan -generate-config-out #446

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions tfexec/internal/e2etest/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

var (
generateConfigOutMinVersion = version.Must(version.NewVersion("1.5.0"))
)

func TestPlan(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
Expand Down Expand Up @@ -92,3 +96,24 @@ func TestPlanJSON_TF015AndLater(t *testing.T) {
}
})
}

func TestPlanGenerateConfigOut(t *testing.T) {
runTest(t, "generate_config_out", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
if tfv.LessThan(generateConfigOutMinVersion) {
t.Skip("terraform plan -generate-config-out was added in Terraform 1.5.0, so test is not valid")
}

err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init in test directory: %s", err)
}

hasChanges, err := tf.Plan(context.Background(), tfexec.GenerateConfigOut("generated.tf"))
if err != nil {
t.Fatalf("error running Plan: %s", err)
}
if !hasChanges {
t.Fatalf("expected: true, got: %t", hasChanges)
}
})
}
4 changes: 4 additions & 0 deletions tfexec/internal/e2etest/testdata/generate_config_out/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {
id = "bar"
to = terraform_data.foo
}
4 changes: 2 additions & 2 deletions tfexec/internal/testutil/tfcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const (
Latest015 = "0.15.5"
Latest_v1 = "1.0.11"
Latest_v1_1 = "1.1.9"
Latest_v1_5 = "1.5.3"
Latest_v1_6 = "1.6.0-alpha20230719"
Latest_v1_5 = "1.5.7"
Latest_v1_6 = "1.6.6"
)

const appendUserAgent = "tfexec-testutil"
Expand Down
9 changes: 9 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ func FromModule(source string) *FromModuleOption {
return &FromModuleOption{source}
}

type GenerateConfigOutOption struct {
generateConfigOut string
}

// GenerateConfigOut represents the -generate-config-out flag.
func GenerateConfigOut(generateConfigOut string) *GenerateConfigOutOption {
return &GenerateConfigOutOption{generateConfigOut}
}

type GetOption struct {
get bool
}
Expand Down
40 changes: 26 additions & 14 deletions tfexec/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ import (
)

type planConfig struct {
destroy bool
dir string
lock bool
lockTimeout string
out string
parallelism int
reattachInfo ReattachInfo
refresh bool
refreshOnly bool
replaceAddrs []string
state string
targets []string
vars []string
varFiles []string
destroy bool
dir string
generateConfigOut string
lock bool
lockTimeout string
out string
parallelism int
reattachInfo ReattachInfo
refresh bool
refreshOnly bool
replaceAddrs []string
state string
targets []string
vars []string
varFiles []string
}

var defaultPlanOptions = planConfig{
Expand Down Expand Up @@ -97,6 +98,10 @@ func (opt *DestroyFlagOption) configurePlan(conf *planConfig) {
conf.destroy = opt.destroy
}

func (opt *GenerateConfigOutOption) configurePlan(conf *planConfig) {
conf.generateConfigOut = opt.generateConfigOut
}

// Plan executes `terraform plan` with the specified options and waits for it
// to complete.
//
Expand Down Expand Up @@ -189,6 +194,13 @@ func (tf *Terraform) buildPlanArgs(ctx context.Context, c planConfig) ([]string,
args := []string{"plan", "-no-color", "-input=false", "-detailed-exitcode"}

// string opts: only pass if set
if c.generateConfigOut != "" {
err := tf.compatible(ctx, tf1_5_0, nil)
if err != nil {
return nil, fmt.Errorf("generate-config-out option was introduced in Terraform 1.5.0: %w", err)
}
args = append(args, "-generate-config-out="+c.generateConfigOut)
}
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
Expand Down
43 changes: 41 additions & 2 deletions tfexec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestPlanCmd(t *testing.T) {
td := t.TempDir()

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest_v1))
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest_v1_5))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -101,12 +101,31 @@ func TestPlanCmd(t *testing.T) {
"-refresh-only",
}, nil, planCmd)
})

t.Run("run a generate-config-out plan", func(t *testing.T) {
planCmd, err := tf.planCmd(context.Background(), GenerateConfigOut("generated.tf"))
if err != nil {
t.Fatal(err)
}

assertCmd(t, []string{
"plan",
"-no-color",
"-input=false",
"-detailed-exitcode",
"-generate-config-out=generated.tf",
"-lock-timeout=0s",
"-lock=true",
"-parallelism=10",
"-refresh=true",
}, nil, planCmd)
})
}

func TestPlanJSONCmd(t *testing.T) {
td := t.TempDir()

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest_v1))
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest_v1_5))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -177,4 +196,24 @@ func TestPlanJSONCmd(t *testing.T) {
"earth",
}, nil, planCmd)
})

t.Run("generate-config-out", func(t *testing.T) {
planCmd, err := tf.planJSONCmd(context.Background(), GenerateConfigOut("generated.tf"))
if err != nil {
t.Fatal(err)
}

assertCmd(t, []string{
"plan",
"-no-color",
"-input=false",
"-detailed-exitcode",
"-generate-config-out=generated.tf",
"-lock-timeout=0s",
"-lock=true",
"-parallelism=10",
"-refresh=true",
"-json",
}, nil, planCmd)
})
}
1 change: 1 addition & 0 deletions tfexec/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
tf0_15_4 = version.Must(version.NewVersion("0.15.4"))
tf1_1_0 = version.Must(version.NewVersion("1.1.0"))
tf1_4_0 = version.Must(version.NewVersion("1.4.0"))
tf1_5_0 = version.Must(version.NewVersion("1.5.0"))
tf1_6_0 = version.Must(version.NewVersion("1.6.0"))
)

Expand Down