diff --git a/command/taint.go b/command/taint.go index 35e298888..4876b2eb3 100644 --- a/command/taint.go +++ b/command/taint.go @@ -15,7 +15,9 @@ type TaintCommand struct { func (c *TaintCommand) Run(args []string) int { args = c.Meta.process(args, false) + var module string cmdFlags := c.Meta.flagSet("taint") + cmdFlags.StringVar(&module, "module", "", "module") cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path") cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path") @@ -51,13 +53,26 @@ func (c *TaintCommand) Run(args []string) int { return 1 } - mod := s.RootModule() + // Get the proper module we want to taint + if module == "" { + module = "root" + } else { + module = "root." + module + } + modPath := strings.Split(module, ".") + mod := s.ModuleByPath(modPath) + if mod == nil { + c.Ui.Error(fmt.Sprintf( + "The module %s could not be found. There is nothing to taint.", + module)) + return 1 + } // If there are no resources in this module, it is an error if len(mod.Resources) == 0 { c.Ui.Error(fmt.Sprintf( "The module %s has no resources. There is nothing to taint.", - strings.Join(mod.Path, "."))) + module)) return 1 } @@ -67,7 +82,7 @@ func (c *TaintCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf( "The resource %s couldn't be found in the module %s.", name, - strings.Join(mod.Path, "."))) + module)) return 1 } @@ -80,9 +95,9 @@ func (c *TaintCommand) Run(args []string) int { return 1 } - c.Ui.Output( + c.Ui.Output(fmt.Sprintf( "The resource %s in the module %s has been marked as tainted!", - name, strings.Join(mod.Path, ".")) + name, module)) return 0 } @@ -105,6 +120,10 @@ Options: modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup. + -module=path The module path where the resource lives. By + default this will be root. Child modules can be specified + by names. Ex. "consul" or "consul.vpc" (nested modules). + -no-color If specified, output won't contain any color. -state=path Path to read and save state (unless state-out diff --git a/command/taint_test.go b/command/taint_test.go index a20378d9f..f6e79a50c 100644 --- a/command/taint_test.go +++ b/command/taint_test.go @@ -229,6 +229,54 @@ func TestTaint_stateOut(t *testing.T) { testStateOutput(t, "foo", testTaintStr) } +func TestTaint_module(t *testing.T) { + state := &terraform.State{ + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "bar", + }, + }, + }, + }, + &terraform.ModuleState{ + Path: []string{"root", "child"}, + Resources: map[string]*terraform.ResourceState{ + "test_instance.blah": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "blah", + }, + }, + }, + }, + }, + } + statePath := testStateFile(t, state) + + ui := new(cli.MockUi) + c := &TaintCommand{ + Meta: Meta{ + Ui: ui, + }, + } + + args := []string{ + "-module=child", + "-state", statePath, + "test_instance.blah", + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + testStateOutput(t, statePath, testTaintModuleStr) +} + const testTaintStr = ` test_instance.foo: (1 tainted) ID = @@ -239,3 +287,13 @@ const testTaintDefaultStr = ` test_instance.foo: ID = bar ` + +const testTaintModuleStr = ` +test_instance.foo: + ID = bar + +module.child: + test_instance.blah: (1 tainted) + ID = + Tainted ID 1 = blah +`