terraform: tainted transformer

This commit is contained in:
Mitchell Hashimoto 2015-01-27 14:28:33 -08:00
parent 6d46b02fa5
commit 8dc4c56b2e
3 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1 @@
resource "aws_instance" "web" {}

View File

@ -0,0 +1,57 @@
package terraform
import (
"fmt"
)
// TraintedTransformer is a GraphTransformer that adds tainted resources
// to the graph.
type TaintedTransformer struct {
// State is the global state. We'll automatically find the correct
// ModuleState based on the Graph.Path that is being transformed.
State *State
}
func (t *TaintedTransformer) Transform(g *Graph) error {
state := t.State.ModuleByPath(g.Path)
if state == nil {
// If there is no state for our module there can't be any tainted
// resources, since they live in the state.
return nil
}
// Go through all the resources in our state to look for tainted resources
for k, rs := range state.Resources {
// If we have no tainted resources, then move on
if len(rs.Tainted) == 0 {
continue
}
for i, _ := range rs.Tainted {
g.Add(&graphNodeTaintedResource{
Index: i,
ResourceName: k,
})
}
}
return nil
}
// graphNodeTaintedResource is the graph vertex representing a tainted resource.
type graphNodeTaintedResource struct {
Index int
ResourceName string
}
func (n *graphNodeTaintedResource) DependableName() []string {
return []string{n.dependableName()}
}
func (n *graphNodeTaintedResource) Name() string {
return fmt.Sprintf("%s (tainted #%d)", n.ResourceName, n.Index+1)
}
func (n *graphNodeTaintedResource) dependableName() string {
return n.ResourceName
}

View File

@ -0,0 +1,49 @@
package terraform
import (
"strings"
"testing"
)
func TestTaintedTransformer(t *testing.T) {
mod := testModule(t, "transform-tainted-basic")
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
Type: "aws_instance",
Tainted: []*InstanceState{
&InstanceState{ID: "foo"},
},
},
},
},
},
}
g := Graph{Path: RootModulePath}
{
tf := &ConfigTransformer{Module: mod}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
transform := &TaintedTransformer{State: state}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformTaintedBasicStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
const testTransformTaintedBasicStr = `
aws_instance.web
aws_instance.web (tainted #1)
`