Skip to content

Commit

Permalink
add command for force-unlock (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-rabold authored Nov 10, 2021
1 parent feac8d0 commit 07c60a0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tfexec/force_unlock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tfexec

import (
"context"
"os/exec"
)

type forceUnlockConfig struct {
dir string
}

var defaultForceUnlockOptions = forceUnlockConfig{}

type ForceUnlockOption interface {
configureForceUnlock(*forceUnlockConfig)
}

func (opt *DirOption) configureForceUnlock(conf *forceUnlockConfig) {
conf.dir = opt.path
}

// ForceUnlock represents the `terraform force-unlock` command
func (tf *Terraform) ForceUnlock(ctx context.Context, lockID string, opts ...ForceUnlockOption) error {
unlockCmd := tf.forceUnlockCmd(ctx, lockID, opts...)

if err := tf.runTerraformCmd(ctx, unlockCmd); err != nil {
return err
}

return nil
}

func (tf *Terraform) forceUnlockCmd(ctx context.Context, lockID string, opts ...ForceUnlockOption) *exec.Cmd {
c := defaultForceUnlockOptions

for _, o := range opts {
o.configureForceUnlock(&c)
}
args := []string{"force-unlock", "-force"}

// positional arguments
args = append(args, lockID)

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

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

import (
"context"
"testing"

"github.com/hashicorp/go-version"

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

// LockID set in the test fixture
const inmemLockID = "2b6a6738-5dd5-50d6-c0ae-f6352977666b"

var forceUnlockDirArgMaxVersion = version.Must(version.NewVersion("0.15.0"))

func TestForceUnlock(t *testing.T) {
runTest(t, "inmem-backend-locked", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init: %v", err)
}

err = tf.ForceUnlock(context.Background(), inmemLockID)
if err != nil {
t.Fatalf("error running ForceUnlock: %v", err)
}
})
runTest(t, "inmem-backend-locked", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
if tfv.GreaterThanOrEqual(forceUnlockDirArgMaxVersion) {
t.Skip("legacy positional path argument deprecated in favor of global -chdir flag")
}
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init: %v", err)
}

err = tf.ForceUnlock(context.Background(), inmemLockID, tfexec.Dir(tf.WorkingDir()))
if err != nil {
t.Fatalf("error running ForceUnlock: %v", err)
}
})
}

0 comments on commit 07c60a0

Please sign in to comment.