command: Prevent data resources from being tainted

Since the data resource lifecycle contains no steps to deal with tainted
instances, we must make sure that they never get created.

Doing this out in the command layer is not the best, but this is currently
the only layer that has enough information to make this decision and so
this simple solution was preferred over a more disruptive refactoring,
under the assumption that this taint functionality eventually gets
reworked in terms of StateFilter anyway.
This commit is contained in:
Martin Atkins 2016-05-08 02:51:46 -07:00
parent 61ab8bf39a
commit 60c24e3319
2 changed files with 24 additions and 0 deletions

View File

@ -4,6 +4,8 @@ import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/terraform"
)
// TaintCommand is a cli.Command implementation that manually taints
@ -43,6 +45,17 @@ func (c *TaintCommand) Run(args []string) int {
module = "root." + module
}
rsk, err := terraform.ParseResourceStateKey(name)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to parse resource name: %s", err))
return 1
}
if !rsk.Mode.Taintable() {
c.Ui.Error(fmt.Sprintf("Resource '%s' cannot be tainted", name))
return 1
}
// Get the state that we'll be modifying
state, err := c.State()
if err != nil {

View File

@ -938,3 +938,14 @@ func (v *Variable) inferTypeFromDefault() VariableType {
return VariableTypeUnknown
}
func (m ResourceMode) Taintable() bool {
switch m {
case ManagedResourceMode:
return true
case DataResourceMode:
return false
default:
panic(fmt.Errorf("unsupported ResourceMode value %s", m))
}
}