-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
251 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
tfjson "github.com/hashicorp/terraform-json" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec" | ||
) | ||
|
||
func TestStateMv(t *testing.T) { | ||
runTest(t, "basic_with_state", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
if tfv.LessThan(showMinVersion) { | ||
t.Skip("terraform show was added in Terraform 0.12, so test is not valid") | ||
} | ||
|
||
providerName := "registry.terraform.io/-/null" | ||
if tfv.LessThan(providerAddressMinVersion) { | ||
providerName = "null" | ||
} | ||
|
||
err := tf.Init(context.Background()) | ||
if err != nil { | ||
t.Fatalf("error running Init in test directory: %s", err) | ||
} | ||
|
||
err = tf.StateMv(context.Background(), "null_resource.foo", "null_resource.bar") | ||
if err != nil { | ||
t.Fatalf("error running StateMv: %s", err) | ||
} | ||
|
||
// test that the new state is as expected | ||
expected := &tfjson.State{ | ||
FormatVersion: "0.1", | ||
// TerraformVersion is ignored to facilitate latest version testing | ||
Values: &tfjson.StateValues{ | ||
RootModule: &tfjson.StateModule{ | ||
Resources: []*tfjson.StateResource{{ | ||
Address: "null_resource.bar", | ||
AttributeValues: map[string]interface{}{ | ||
"id": "5510719323588825107", | ||
"triggers": nil, | ||
}, | ||
Mode: tfjson.ManagedResourceMode, | ||
Type: "null_resource", | ||
Name: "bar", | ||
ProviderName: providerName, | ||
}}, | ||
}, | ||
}, | ||
} | ||
|
||
actual, err := tf.Show(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if diff := diffState(expected, actual); diff != "" { | ||
t.Fatalf("mismatch (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
"strconv" | ||
) | ||
|
||
type stateMvConfig struct { | ||
backup string | ||
backupOut string | ||
dryRun bool | ||
lock bool | ||
lockTimeout string | ||
state string | ||
stateOut string | ||
} | ||
|
||
var defaultStateMvOptions = stateMvConfig{ | ||
lock: true, | ||
lockTimeout: "0s", | ||
} | ||
|
||
// StateMvCmdOption represents options used in the Refresh method. | ||
type StateMvCmdOption interface { | ||
configureStateMv(*stateMvConfig) | ||
} | ||
|
||
func (opt *BackupOption) configureStateMv(conf *stateMvConfig) { | ||
conf.backup = opt.path | ||
} | ||
|
||
func (opt *BackupOutOption) configureStateMv(conf *stateMvConfig) { | ||
conf.backupOut = opt.path | ||
} | ||
|
||
func (opt *DryRunOption) configureStateMv(conf *stateMvConfig) { | ||
conf.dryRun = opt.dryRun | ||
} | ||
|
||
func (opt *LockOption) configureStateMv(conf *stateMvConfig) { | ||
conf.lock = opt.lock | ||
} | ||
|
||
func (opt *LockTimeoutOption) configureStateMv(conf *stateMvConfig) { | ||
conf.lockTimeout = opt.timeout | ||
} | ||
|
||
func (opt *StateOption) configureStateMv(conf *stateMvConfig) { | ||
conf.state = opt.path | ||
} | ||
|
||
func (opt *StateOutOption) configureStateMv(conf *stateMvConfig) { | ||
conf.stateOut = opt.path | ||
} | ||
|
||
// StateMv represents the terraform state mv subcommand. | ||
func (tf *Terraform) StateMv(ctx context.Context, source string, destination string, opts ...StateMvCmdOption) error { | ||
cmd, err := tf.stateMvCmd(ctx, source, destination, opts...) | ||
if err != nil { | ||
return err | ||
} | ||
return tf.runTerraformCmd(cmd) | ||
} | ||
|
||
func (tf *Terraform) stateMvCmd(ctx context.Context, source string, destination string, opts ...StateMvCmdOption) (*exec.Cmd, error) { | ||
c := defaultStateMvOptions | ||
|
||
for _, o := range opts { | ||
o.configureStateMv(&c) | ||
} | ||
|
||
args := []string{"state", "mv", "-no-color"} | ||
|
||
// string opts: only pass if set | ||
if c.backup != "" { | ||
args = append(args, "-backup="+c.backup) | ||
} | ||
if c.backupOut != "" { | ||
args = append(args, "-backup-out="+c.backupOut) | ||
} | ||
if c.lockTimeout != "" { | ||
args = append(args, "-lock-timeout="+c.lockTimeout) | ||
} | ||
if c.state != "" { | ||
args = append(args, "-state="+c.state) | ||
} | ||
if c.stateOut != "" { | ||
args = append(args, "-state-out="+c.stateOut) | ||
} | ||
|
||
// boolean and numerical opts: always pass | ||
args = append(args, "-lock="+strconv.FormatBool(c.lock)) | ||
|
||
// unary flags: pass if true | ||
if c.dryRun { | ||
args = append(args, "-dry-run") | ||
} | ||
|
||
// positional arguments | ||
args = append(args, source) | ||
args = append(args, destination) | ||
|
||
return tf.buildTerraformCmd(ctx, nil, args...), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec/internal/testutil" | ||
) | ||
|
||
func TestStateMvCmd(t *testing.T) { | ||
td := testTempDir(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{}) | ||
|
||
t.Run("defaults", func(t *testing.T) { | ||
stateMvCmd, err := tf.stateMvCmd(context.Background(), "testsource", "testdestination") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertCmd(t, []string{ | ||
"state", | ||
"mv", | ||
"-no-color", | ||
"-lock-timeout=0s", | ||
"-lock=true", | ||
"testsource", | ||
"testdestination", | ||
}, nil, stateMvCmd) | ||
}) | ||
|
||
t.Run("override all defaults", func(t *testing.T) { | ||
stateMvCmd, err := tf.stateMvCmd(context.Background(), "testsrc", "testdest", Backup("testbackup"), BackupOut("testbackupout"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), Lock(false)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assertCmd(t, []string{ | ||
"state", | ||
"mv", | ||
"-no-color", | ||
"-backup=testbackup", | ||
"-backup-out=testbackupout", | ||
"-lock-timeout=200s", | ||
"-state=teststate", | ||
"-state-out=teststateout", | ||
"-lock=false", | ||
"testsrc", | ||
"testdest", | ||
}, nil, stateMvCmd) | ||
}) | ||
} |