mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
fix CreateBeforeDestroy with datasources
The graph transformation we implement around create_before_destroy need to re-order all resources that depend on the create_before_destroy resource. Up until now, we've requires that users mark all of these resources as create_before_destroy. Data soruces however don't have a lifecycle block for create_before_destroy, and could not be marked this way. This PR checks each DestroyNode that doesn't implement CreateBeforeDestroy for any ancestors that do implement CreateBeforeDestroy. If there are any, we inherit the behavior and re-order the graph as such.
This commit is contained in:
parent
9cc881558b
commit
cf3a259cd9
@ -173,6 +173,7 @@ func TestBuiltinGraphBuilder_cbdDepNonCbd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// This now returns no errors due to a general fix while building the graph
|
||||
func TestBuiltinGraphBuilder_cbdDepNonCbd_errorsWhenVerbose(t *testing.T) {
|
||||
b := &BuiltinGraphBuilder{
|
||||
Root: testModule(t, "graph-builder-cbd-non-cbd"),
|
||||
@ -181,8 +182,8 @@ func TestBuiltinGraphBuilder_cbdDepNonCbd_errorsWhenVerbose(t *testing.T) {
|
||||
}
|
||||
|
||||
_, err := b.Build(RootModulePath)
|
||||
if err == nil {
|
||||
t.Fatalf("expected err, got none")
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/terraform/dag"
|
||||
)
|
||||
import "github.com/hashicorp/terraform/dag"
|
||||
|
||||
// GraphNodeDestroyable is the interface that nodes that can be destroyed
|
||||
// must implement. This is used to automatically handle the creation of
|
||||
@ -153,7 +151,7 @@ func (t *CreateBeforeDestroyTransformer) Transform(g *Graph) error {
|
||||
}
|
||||
|
||||
// If the node doesn't need to create before destroy, then continue
|
||||
if !dn.CreateBeforeDestroy() {
|
||||
if !dn.CreateBeforeDestroy() && noCreateBeforeDestroyAncestors(g, dn) {
|
||||
continue
|
||||
}
|
||||
|
||||
@ -200,6 +198,30 @@ func (t *CreateBeforeDestroyTransformer) Transform(g *Graph) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// noCreateBeforeDestroyAncestors verifies that a vertex has no ancestors that
|
||||
// are CreateBeforeDestroy.
|
||||
// If this vertex has an ancestor with CreateBeforeDestroy, we will need to
|
||||
// inherit that behavior and re-order the edges even if this node type doesn't
|
||||
// directly implement CreateBeforeDestroy.
|
||||
func noCreateBeforeDestroyAncestors(g *Graph, v dag.Vertex) bool {
|
||||
s, _ := g.Ancestors(v)
|
||||
if s == nil {
|
||||
return true
|
||||
}
|
||||
for _, v := range s.List() {
|
||||
dn, ok := v.(GraphNodeDestroy)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if dn.CreateBeforeDestroy() {
|
||||
// some ancestor is CreateBeforeDestroy, so we need to follow suit
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// PruneDestroyTransformer is a GraphTransformer that removes the destroy
|
||||
// nodes that aren't in the diff.
|
||||
type PruneDestroyTransformer struct {
|
||||
|
Loading…
Reference in New Issue
Block a user