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

Implemented state remove command #122

Merged
merged 3 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 45 additions & 0 deletions tfexec/internal/e2etest/state_rm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package e2etest

import (
"context"
"testing"

"github.com/hashicorp/go-version"
tfjson "github.com/hashicorp/terraform-json"

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

func TestStateRm(t *testing.T) {
runTest(t, "basic_with_state", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
if tfv.LessThan(providerAddressMinVersion) {
t.Skip("state file provider FQNs not compatible with this Terraform version")
}

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

err = tf.StateRm(context.Background(), "null_resource.foo")
if err != nil {
t.Fatalf("error running StateRm: %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: nil,
}

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)
}
})
}
104 changes: 104 additions & 0 deletions tfexec/state_rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package tfexec

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

type stateRmConfig struct {
backup string
backupOut string
dryRun bool
lock bool
lockTimeout string
state string
stateOut string
}

var defaultStateRmOptions = stateRmConfig{
lock: true,
lockTimeout: "0s",
}

// StateRmCmdOption represents options used in the Refresh method.
type StateRmCmdOption interface {
configureStateRm(*stateRmConfig)
}

func (opt *BackupOption) configureStateRm(conf *stateRmConfig) {
conf.backup = opt.path
}

func (opt *BackupOutOption) configureStateRm(conf *stateRmConfig) {
conf.backupOut = opt.path
}

func (opt *DryRunOption) configureStateRm(conf *stateRmConfig) {
conf.dryRun = opt.dryRun
}

func (opt *LockOption) configureStateRm(conf *stateRmConfig) {
conf.lock = opt.lock
}

func (opt *LockTimeoutOption) configureStateRm(conf *stateRmConfig) {
conf.lockTimeout = opt.timeout
}

func (opt *StateOption) configureStateRm(conf *stateRmConfig) {
conf.state = opt.path
}

func (opt *StateOutOption) configureStateRm(conf *stateRmConfig) {
conf.stateOut = opt.path
}

// StateRm represents the terraform state rm subcommand.
func (tf *Terraform) StateRm(ctx context.Context, source string, opts ...StateRmCmdOption) error {
aleksanderaleksic marked this conversation as resolved.
Show resolved Hide resolved
cmd, err := tf.stateRmCmd(ctx, source, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(ctx, cmd)
}

func (tf *Terraform) stateRmCmd(ctx context.Context, source string, opts ...StateRmCmdOption) (*exec.Cmd, error) {
c := defaultStateRmOptions

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

args := []string{"state", "rm", "-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)

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

import (
"context"
"testing"

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

func TestStateRmCmd(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) {
stateRmCmd, err := tf.stateRmCmd(context.Background(), "testsource")
if err != nil {
t.Fatal(err)
}

assertCmd(t, []string{
"state",
"rm",
"-no-color",
"-lock-timeout=0s",
"-lock=true",
"testsource",
}, nil, stateRmCmd)
})

t.Run("override all defaults", func(t *testing.T) {
stateRmCmd, err := tf.stateRmCmd(context.Background(), "testsrc", Backup("testbackup"), BackupOut("testbackupout"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), Lock(false))
if err != nil {
t.Fatal(err)
}

assertCmd(t, []string{
"state",
"rm",
"-no-color",
"-backup=testbackup",
"-backup-out=testbackupout",
"-lock-timeout=200s",
"-state=teststate",
"-state-out=teststateout",
"-lock=false",
"testsrc",
}, nil, stateRmCmd)
})
}