-
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
1 parent
feac8d0
commit 07c60a0
Showing
2 changed files
with
93 additions
and
0 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
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...) | ||
} |
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,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) | ||
} | ||
}) | ||
} |