command/taint: support tainting resources in modules

This commit is contained in:
Mitchell Hashimoto 2015-02-26 10:44:25 -08:00
parent fa9b655fd1
commit 01aa4236c0
2 changed files with 82 additions and 5 deletions

View File

@ -15,7 +15,9 @@ type TaintCommand struct {
func (c *TaintCommand) Run(args []string) int { func (c *TaintCommand) Run(args []string) int {
args = c.Meta.process(args, false) args = c.Meta.process(args, false)
var module string
cmdFlags := c.Meta.flagSet("taint") cmdFlags := c.Meta.flagSet("taint")
cmdFlags.StringVar(&module, "module", "", "module")
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path") cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path") cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
@ -51,13 +53,26 @@ func (c *TaintCommand) Run(args []string) int {
return 1 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 there are no resources in this module, it is an error
if len(mod.Resources) == 0 { if len(mod.Resources) == 0 {
c.Ui.Error(fmt.Sprintf( c.Ui.Error(fmt.Sprintf(
"The module %s has no resources. There is nothing to taint.", "The module %s has no resources. There is nothing to taint.",
strings.Join(mod.Path, "."))) module))
return 1 return 1
} }
@ -67,7 +82,7 @@ func (c *TaintCommand) Run(args []string) int {
c.Ui.Error(fmt.Sprintf( c.Ui.Error(fmt.Sprintf(
"The resource %s couldn't be found in the module %s.", "The resource %s couldn't be found in the module %s.",
name, name,
strings.Join(mod.Path, "."))) module))
return 1 return 1
} }
@ -80,9 +95,9 @@ func (c *TaintCommand) Run(args []string) int {
return 1 return 1
} }
c.Ui.Output( c.Ui.Output(fmt.Sprintf(
"The resource %s in the module %s has been marked as tainted!", "The resource %s in the module %s has been marked as tainted!",
name, strings.Join(mod.Path, ".")) name, module))
return 0 return 0
} }
@ -105,6 +120,10 @@ Options:
modifying. Defaults to the "-state-out" path with modifying. Defaults to the "-state-out" path with
".backup" extension. Set to "-" to disable backup. ".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. -no-color If specified, output won't contain any color.
-state=path Path to read and save state (unless state-out -state=path Path to read and save state (unless state-out

View File

@ -229,6 +229,54 @@ func TestTaint_stateOut(t *testing.T) {
testStateOutput(t, "foo", testTaintStr) 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 = ` const testTaintStr = `
test_instance.foo: (1 tainted) test_instance.foo: (1 tainted)
ID = <not created> ID = <not created>
@ -239,3 +287,13 @@ const testTaintDefaultStr = `
test_instance.foo: test_instance.foo:
ID = bar ID = bar
` `
const testTaintModuleStr = `
test_instance.foo:
ID = bar
module.child:
test_instance.blah: (1 tainted)
ID = <not created>
Tainted ID 1 = blah
`