From 60c24e3319d7ce6aee5d0b7f306287949c3bbbe4 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sun, 8 May 2016 02:51:46 -0700 Subject: [PATCH] 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. --- command/taint.go | 13 +++++++++++++ config/config.go | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/command/taint.go b/command/taint.go index 54aa00146..399f0e330 100644 --- a/command/taint.go +++ b/command/taint.go @@ -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 { diff --git a/config/config.go b/config/config.go index 6ad568a7e..848552aac 100644 --- a/config/config.go +++ b/config/config.go @@ -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)) + } +}