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

Add migrate state flag to init command. #460

Open
wants to merge 7 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion tfexec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,38 @@ func (tf *Terraform) runTerraformCmdJSON(ctx context.Context, cmd *exec.Cmd, v i
return err
}

dec := json.NewDecoder(&outbuf)
// Skip backend log lines
jsonData := SkipLogLines(outbuf)

dec := json.NewDecoder(&jsonData)
dec.UseNumber()
return dec.Decode(v)
}

func SkipLogLines(buf bytes.Buffer) bytes.Buffer {
// Initialize a scanner to read line by line
scanner := bufio.NewScanner(&buf)
var jsonData bytes.Buffer
inJsonBlock := false

// Scan each line and accumulate the JSON content
for scanner.Scan() {
line := scanner.Text()
trimmed := strings.TrimSpace(line)

// Start accumulating once we encounter the start of a JSON object
if strings.HasPrefix(trimmed, "{") {
inJsonBlock = true
}

// Accumulate lines if inside a JSON block
if inJsonBlock {
jsonData.WriteString(line + "\n")
}
}
return jsonData
}

// mergeUserAgent does some minor deduplication to ensure we aren't
// just using the same append string over and over.
func mergeUserAgent(uas ...string) string {
Expand Down
17 changes: 17 additions & 0 deletions tfexec/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type initConfig struct {
pluginDir []string
reattachInfo ReattachInfo
reconfigure bool
migrate bool
upgrade bool
verifyPlugins bool
}
Expand All @@ -34,6 +35,7 @@ var defaultInitOptions = initConfig{
lock: true,
lockTimeout: "0s",
reconfigure: false,
migrate: false,
upgrade: false,
verifyPlugins: true,
}
Expand Down Expand Up @@ -87,6 +89,10 @@ func (opt *ReattachOption) configureInit(conf *initConfig) {
conf.reattachInfo = opt.info
}

func (opt *MigrateStateOption) configureInit(conf *initConfig) {
conf.migrate = opt.migrate
}

func (opt *ReconfigureOption) configureInit(conf *initConfig) {
conf.reconfigure = opt.reconfigure
}
Expand Down Expand Up @@ -155,6 +161,17 @@ func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) (*exec.Cmd
args = append(args, "-force-copy")
}

// -migrate-state added in 0.15.4 before it was automatic and returned errors
err = tf.compatible(ctx, tf0_15_4, nil)
if err == nil {
if c.migrate && c.reconfigure {
return nil, fmt.Errorf("the -migrate-state and -reconfigure options are mutually-exclusive")
}
if c.migrate {
args = append(args, "-migrate-state")
}
}

// unary flags: pass if true
if c.reconfigure {
args = append(args, "-reconfigure")
Expand Down
36 changes: 35 additions & 1 deletion tfexec/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestInitCmd_v012(t *testing.T) {
})

t.Run("override all defaults", func(t *testing.T) {
initCmd, err := tf.initCmd(context.Background(), Backend(false), BackendConfig("confpath1"), BackendConfig("confpath2"), ForceCopy(true), FromModule("testsource"), Get(false), GetPlugins(false), Lock(false), LockTimeout("999s"), PluginDir("testdir1"), PluginDir("testdir2"), Reconfigure(true), Upgrade(true), VerifyPlugins(false), Dir("initdir"))
initCmd, err := tf.initCmd(context.Background(), Backend(false), BackendConfig("confpath1"), BackendConfig("confpath2"), ForceCopy(true), FromModule("testsource"), Get(false), GetPlugins(false), Lock(false), LockTimeout("999s"), PluginDir("testdir1"), PluginDir("testdir2"), Reconfigure(true), MigrateState(true), Upgrade(true), VerifyPlugins(false), Dir("initdir"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -104,6 +104,17 @@ func TestInitCmd_v1(t *testing.T) {
}, nil, initCmd)
})

t.Run("reconfigure and migrate error", func(t *testing.T) {
// defaults
initCmd, err := tf.initCmd(context.Background(), Reconfigure(true), MigrateState(true))
if err == nil {
t.Fatal("expected -migrate-state and -reconfigure options are mutually-exclusive error")
}
if initCmd != nil {
t.Fatal("error returned and command not null")
}
})

t.Run("override all defaults", func(t *testing.T) {
initCmd, err := tf.initCmd(context.Background(), Backend(false), BackendConfig("confpath1"), BackendConfig("confpath2"), FromModule("testsource"), Get(false), PluginDir("testdir1"), PluginDir("testdir2"), Reconfigure(true), Upgrade(true), Dir("initdir"))
if err != nil {
Expand All @@ -126,4 +137,27 @@ func TestInitCmd_v1(t *testing.T) {
"initdir",
}, nil, initCmd)
})

t.Run("override all defaults with migrate", func(t *testing.T) {
initCmd, err := tf.initCmd(context.Background(), Backend(false), BackendConfig("confpath1"), BackendConfig("confpath2"), FromModule("testsource"), Get(false), PluginDir("testdir1"), PluginDir("testdir2"), MigrateState(true), Upgrade(true), Dir("initdir"))
if err != nil {
t.Fatal(err)
}

assertCmd(t, []string{
"init",
"-no-color",
"-input=false",
"-from-module=testsource",
"-backend=false",
"-get=false",
"-upgrade=true",
"-migrate-state",
"-backend-config=confpath1",
"-backend-config=confpath2",
"-plugin-dir=testdir1",
"-plugin-dir=testdir2",
"initdir",
}, nil, initCmd)
})
}
9 changes: 9 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ func LockTimeout(lockTimeout string) *LockTimeoutOption {
return &LockTimeoutOption{lockTimeout}
}

type MigrateStateOption struct {
migrate bool
}

// MigrateState represents the -migrate-state flag.
func MigrateState(migrate bool) *MigrateStateOption {
return &MigrateStateOption{migrate}
}

type NetMirrorOption struct {
netMirror string
}
Expand Down