Skip to content

Commit

Permalink
command: Prevent data resources from being tainted
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apparentlymart committed May 8, 2016
1 parent c2cd14c commit 730ca3b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions command/taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/terraform"
)

// TaintCommand is a cli.Command implementation that manually taints
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,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))
}
}

0 comments on commit 730ca3b

Please sign in to comment.