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

Adds support for the terraform 0.12upgrade command #105

Merged
merged 6 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ func Destroy(destroy bool) *DestroyFlagOption {
return &DestroyFlagOption{destroy}
}

type ForceOption struct {
force bool
}

func Force(force bool) *ForceOption {
return &ForceOption{force}
}

type ForceCopyOption struct {
forceCopy bool
}
Expand Down Expand Up @@ -294,3 +302,11 @@ type VerifyPluginsOption struct {
func VerifyPlugins(verifyPlugins bool) *VerifyPluginsOption {
return &VerifyPluginsOption{verifyPlugins}
}

type YesOption struct {
yes bool
}

func Yes(yes bool) *YesOption {
return &YesOption{yes}
}
89 changes: 89 additions & 0 deletions tfexec/upgrade012.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package tfexec

import (
"context"
"fmt"
"os/exec"
)

type upgrade012Config struct {
dir string
force bool
yes bool

reattachInfo ReattachInfo
}

var defaultUpgrade012Options = upgrade012Config{
yes: false,
force: false,
}

// Upgrade012Option represents options used in the Destroy method.
type Upgrade012Option interface {
configureUpgrade012(*upgrade012Config)
}

func (opt *DirOption) configureUpgrade012(conf *upgrade012Config) {
conf.dir = opt.path
}

func (opt *ForceOption) configureUpgrade012(conf *upgrade012Config) {
conf.force = opt.force
}

func (opt *YesOption) configureUpgrade012(conf *upgrade012Config) {
conf.yes = opt.yes
}

func (opt *ReattachOption) configureUpgrade012(conf *upgrade012Config) {
conf.reattachInfo = opt.info
}

// Upgrade012 represents the terraform 0.12upgrade subcommand.
func (tf *Terraform) Upgrade012(ctx context.Context, opts ...Upgrade012Option) error {
cmd, err := tf.upgrade012Cmd(ctx, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(cmd)
}

func (tf *Terraform) upgrade012Cmd(ctx context.Context, opts ...Upgrade012Option) (*exec.Cmd, error) {
err := tf.compatible(ctx, tf0_12_0, tf0_13_0)
if err != nil {
return nil, fmt.Errorf("terraform 0.12upgrade is only supported in 0.12 releases: %w", err)
}

c := defaultUpgrade012Options

for _, o := range opts {
o.configureUpgrade012(&c)
}

args := []string{"0.12upgrade", "-no-color"}

// boolean opts: only pass if set
if c.yes {
args = append(args, "-yes")
}
if c.force {
args = append(args, "-force")
}

// optional positional argument
if c.dir != "" {
args = append(args, c.dir)
}

mergeEnv := map[string]string{}
if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
return nil, err
}
mergeEnv[reattachEnvVar] = reattachStr
}

return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil
}
76 changes: 76 additions & 0 deletions tfexec/upgrade012_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package tfexec

import (
"context"
"errors"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

func TestUpgrade012(t *testing.T) {
td := testTempDir(t)

t.Run("defaults", func(t *testing.T) {
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

upgrade012Cmd, err := tf.upgrade012Cmd(context.Background())
if err != nil {
t.Fatal(err)
}

assertCmd(t, []string{
"0.12upgrade",
"-no-color",
}, nil, upgrade012Cmd)
})

t.Run("override all defaults", func(t *testing.T) {
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

upgrade012Cmd, err := tf.upgrade012Cmd(context.Background(), Yes(true), Force(true), Dir("upgrade012dir"))
if err != nil {
t.Fatal(err)
}

assertCmd(t, []string{
"0.12upgrade",
"-no-color",
"-yes",
"-force",
"upgrade012dir",
}, nil, upgrade012Cmd)
})

t.Run("unsupported on 0.13", func(t *testing.T) {
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest013))
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

_, err = tf.upgrade012Cmd(context.Background())
if err == nil {
t.Fatal("expected old version to fail")
}

var expectedErr *ErrVersionMismatch
if !errors.As(err, &expectedErr) {
t.Fatalf("error doesn't match: %#v", err)
}
})
}