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 command for force-unlock #223

Merged
Merged
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
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)
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
terraform {
backend "inmem" {
lock_id = "2b6a6738-5dd5-50d6-c0ae-f6352977666b"
}
}