mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
terraform: untainted resource depends on tainted resource if it exists
This commit is contained in:
parent
d2f3dc8d1f
commit
e1d3f308a6
@ -55,23 +55,12 @@ func (g *Graph) Add(v dag.Vertex) dag.Vertex {
|
||||
// GraphNodeDependables. It returns the list of dependents it was
|
||||
// unable to connect to.
|
||||
func (g *Graph) ConnectDependent(raw dag.Vertex) []string {
|
||||
g.once.Do(g.init)
|
||||
|
||||
v, ok := raw.(GraphNodeDependent)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
var missing []string
|
||||
for _, t := range v.DependentOn() {
|
||||
if dest := g.dependableMap[t]; dest != nil {
|
||||
g.Connect(dag.BasicEdge(v, dest))
|
||||
} else {
|
||||
missing = append(missing, t)
|
||||
}
|
||||
}
|
||||
|
||||
return missing
|
||||
return g.ConnectTo(v, v.DependentOn())
|
||||
}
|
||||
|
||||
// ConnectDependents goes through the graph, connecting all the
|
||||
@ -88,6 +77,40 @@ func (g *Graph) ConnectDependents() {
|
||||
}
|
||||
}
|
||||
|
||||
// ConnectFrom creates an edge by finding the source from a DependableName
|
||||
// and connecting it to the specific vertex.
|
||||
func (g *Graph) ConnectFrom(source string, target dag.Vertex) {
|
||||
g.once.Do(g.init)
|
||||
|
||||
if source := g.dependableMap[source]; source != nil {
|
||||
g.Connect(dag.BasicEdge(source, target))
|
||||
}
|
||||
}
|
||||
|
||||
// ConnectTo connects a vertex to a raw string of targets that are the
|
||||
// result of DependableName, and returns the list of targets that are missing.
|
||||
func (g *Graph) ConnectTo(v dag.Vertex, targets []string) []string {
|
||||
g.once.Do(g.init)
|
||||
|
||||
var missing []string
|
||||
for _, t := range targets {
|
||||
if dest := g.dependableMap[t]; dest != nil {
|
||||
g.Connect(dag.BasicEdge(v, dest))
|
||||
} else {
|
||||
missing = append(missing, t)
|
||||
}
|
||||
}
|
||||
|
||||
return missing
|
||||
}
|
||||
|
||||
// Dependable finds the vertices in the graph that have the given dependable
|
||||
// names and returns them.
|
||||
func (g *Graph) Dependable(n string) dag.Vertex {
|
||||
// TODO: do we need this?
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Graph) init() {
|
||||
if g.Graph == nil {
|
||||
g.Graph = new(dag.Graph)
|
||||
|
@ -28,14 +28,17 @@ func (t *TaintedTransformer) Transform(g *Graph) error {
|
||||
}
|
||||
|
||||
for i, _ := range rs.Tainted {
|
||||
g.Add(&graphNodeTaintedResource{
|
||||
// Add the graph node and make the connection from any untainted
|
||||
// resources with this name to the tainted resource, so that
|
||||
// the tainted resource gets destroyed first.
|
||||
g.ConnectFrom(k, g.Add(&graphNodeTaintedResource{
|
||||
Index: i,
|
||||
ResourceName: k,
|
||||
})
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Dependencies?
|
||||
// TODO: Any other dependencies?
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -46,14 +49,10 @@ type graphNodeTaintedResource struct {
|
||||
ResourceName string
|
||||
}
|
||||
|
||||
func (n *graphNodeTaintedResource) DependableName() []string {
|
||||
return []string{n.dependableName()}
|
||||
func (n *graphNodeTaintedResource) DependentOn() []string {
|
||||
return []string{n.ResourceName}
|
||||
}
|
||||
|
||||
func (n *graphNodeTaintedResource) Name() string {
|
||||
return fmt.Sprintf("%s (tainted #%d)", n.ResourceName, n.Index+1)
|
||||
}
|
||||
|
||||
func (n *graphNodeTaintedResource) dependableName() string {
|
||||
return n.ResourceName
|
||||
}
|
||||
|
@ -45,5 +45,6 @@ func TestTaintedTransformer(t *testing.T) {
|
||||
|
||||
const testTransformTaintedBasicStr = `
|
||||
aws_instance.web
|
||||
aws_instance.web (tainted #1)
|
||||
aws_instance.web (tainted #1)
|
||||
`
|
||||
|
Loading…
Reference in New Issue
Block a user