mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
terraform: tainted transformer
This commit is contained in:
parent
6d46b02fa5
commit
8dc4c56b2e
1
terraform/test-fixtures/transform-tainted-basic/main.tf
Normal file
1
terraform/test-fixtures/transform-tainted-basic/main.tf
Normal file
@ -0,0 +1 @@
|
||||
resource "aws_instance" "web" {}
|
57
terraform/transform_tainted.go
Normal file
57
terraform/transform_tainted.go
Normal 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
|
||||
}
|
49
terraform/transform_tainted_test.go
Normal file
49
terraform/transform_tainted_test.go
Normal 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)
|
||||
`
|
Loading…
Reference in New Issue
Block a user