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

Adding Taint/Untaint command support #251

Merged
merged 5 commits into from
Dec 1, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ FEATURES:
- Add support for `workspace delete` command [GH-212]
- Add support for `workspace show` command [GH-245]
- Add support for `force-unlock` command [GH-223]
- Add support for `taint`/`untaint` command [GH-251]
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved

# 0.15.0 (October 05, 2021)

Expand Down
29 changes: 29 additions & 0 deletions tfexec/internal/e2etest/taint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package e2etest

import (
"context"
"testing"

"github.com/hashicorp/go-version"

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

func TestTaint(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init in test directory: %s", err)
}

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

err = tf.Taint(context.Background(), "null_resource.foo")
if err != nil {
t.Fatalf("error running Taint: %s", err)
}
})
}
34 changes: 34 additions & 0 deletions tfexec/internal/e2etest/untaint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package e2etest

import (
"context"
"testing"

"github.com/hashicorp/go-version"

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

func TestUntaint(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init in test directory: %s", err)
}

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

err = tf.Taint(context.Background(), "null_resource.foo")
if err != nil {
t.Fatalf("error running Taint: %s", err)
}

err = tf.Untaint(context.Background(), "null_resource.foo")
if err != nil {
t.Fatalf("error running UnTaint: %s", err)
radeksimko marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
2 changes: 1 addition & 1 deletion tfexec/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type OutputMeta struct {

// Output represents the terraform output subcommand.
func (tf *Terraform) Output(ctx context.Context, opts ...OutputOption) (map[string]OutputMeta, error) {

rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved
outputCmd := tf.outputCmd(ctx, opts...)
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved

outputs := map[string]OutputMeta{}
Expand All @@ -51,7 +52,6 @@ func (tf *Terraform) outputCmd(ctx context.Context, opts ...OutputOption) *exec.
for _, o := range opts {
o.configureOutput(&c)
}

rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved
args := []string{"output", "-no-color", "-json"}

// string opts: only pass if set
Expand Down
51 changes: 51 additions & 0 deletions tfexec/taint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tfexec

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

type taintConfig struct {
state string
}

var defaultTaintOptions = taintConfig{}

// TaintOption represents options used in the Taint method.
type TaintOption interface {
configureTaint(*taintConfig)
}

func (opt *StateOption) configureTaint(conf *taintConfig) {
conf.state = opt.path
}

// Taint represents the terraform taint subcommand.
func (tf *Terraform) Taint(ctx context.Context, address string, opts ...TaintOption) error {
err := tf.compatible(ctx, tf0_4_1, nil)
if err != nil {
return fmt.Errorf("taint was first introduced in Terraform 0.4.1: %w", err)
}
taintCmd := tf.taintCmd(ctx, address, opts...)
return tf.runTerraformCmd(ctx, taintCmd)
}

func (tf *Terraform) taintCmd(ctx context.Context, address string, opts ...TaintOption) *exec.Cmd {
c := defaultTaintOptions

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

args := []string{"taint", "-no-color"}

// string opts: only pass if set
if c.state != "" {
args = append(args, "-state="+c.state)
}

args = append(args, address)

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

import (
"context"
"testing"

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

func TestTaintCmd(t *testing.T) {
td := t.TempDir()

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) {
taintCmd := tf.taintCmd(context.Background(), "aws_instance.foo")

assertCmd(t, []string{
"taint",
"-no-color",
"aws_instance.foo",
}, nil, taintCmd)
})

t.Run("override all defaults", func(t *testing.T) {
taintCmd := tf.taintCmd(context.Background(), "aws_instance.foo", State("teststate"))

assertCmd(t, []string{
"taint",
"-no-color",
"-state=teststate",
"aws_instance.foo",
}, nil, taintCmd)
})
}
51 changes: 51 additions & 0 deletions tfexec/untaint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tfexec

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

type untaintConfig struct {
state string
}

var defaultUnTaintOptions = untaintConfig{}
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved

// OutputOption represents options used in the Output method.
type UnTaintOption interface {
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved
configureUnTaint(*untaintConfig)
}

func (opt *StateOption) configureUnTaint(conf *untaintConfig) {
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved
conf.state = opt.path
}

// Untaint represents the terraform untaint subcommand.
func (tf *Terraform) Untaint(ctx context.Context, address string, opts ...UnTaintOption) error {
err := tf.compatible(ctx, tf0_6_13, nil)
if err != nil {
return fmt.Errorf("untaint was first introduced in Terraform 0.6.13: %w", err)
}
unTaintCmd := tf.unTaintCmd(ctx, address, opts...)
return tf.runTerraformCmd(ctx, unTaintCmd)
}

func (tf *Terraform) unTaintCmd(ctx context.Context, address string, opts ...UnTaintOption) *exec.Cmd {
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved
c := defaultUnTaintOptions

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

args := []string{"untaint", "-no-color"}

// string opts: only pass if set
if c.state != "" {
args = append(args, "-state="+c.state)
}

args = append(args, address)

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

import (
"context"
"testing"

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

func TestUnTaintCmd(t *testing.T) {
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved
td := t.TempDir()

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) {
unTaintCmd := tf.unTaintCmd(context.Background(), "aws_instance.foo")
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved

assertCmd(t, []string{
"untaint",
"-no-color",
"aws_instance.foo",
}, nil, unTaintCmd)
})

t.Run("override all defaults", func(t *testing.T) {
unTaintCmd := tf.unTaintCmd(context.Background(), "aws_instance.foo", State("teststate"))
rambabuiitk marked this conversation as resolved.
Show resolved Hide resolved

assertCmd(t, []string{
"untaint",
"-no-color",
"-state=teststate",
"aws_instance.foo",
}, nil, unTaintCmd)
})
}
2 changes: 2 additions & 0 deletions tfexec/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
)

var (
tf0_4_1 = version.Must(version.NewVersion("0.4.1"))
tf0_6_13 = version.Must(version.NewVersion("0.6.13"))
tf0_7_7 = version.Must(version.NewVersion("0.7.7"))
tf0_10_0 = version.Must(version.NewVersion("0.10.0"))
tf0_12_0 = version.Must(version.NewVersion("0.12.0"))
Expand Down