opentofu/terraform/transform_diff.go
Martin Atkins a3403f2766 terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.

The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.

The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.

Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-10-16 19:11:09 -07:00

86 lines
2.3 KiB
Go

package terraform
import (
"fmt"
"log"
"github.com/hashicorp/terraform/dag"
"github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/states"
)
// DiffTransformer is a GraphTransformer that adds graph nodes representing
// each of the resource changes described in the given Changes object.
type DiffTransformer struct {
Concrete ConcreteResourceInstanceNodeFunc
Changes *plans.Changes
}
func (t *DiffTransformer) Transform(g *Graph) error {
if t.Changes == nil || len(t.Changes.Resources) == 0 {
// Nothing to do!
return nil
}
// Go through all the modules in the diff.
log.Printf("[TRACE] DiffTransformer starting")
for _, rc := range t.Changes.Resources {
addr := rc.Addr
dk := rc.DeposedKey
// Depending on the action we'll need some different combinations of
// nodes, because destroying uses a special node type separate from
// other actions.
var update, delete bool
switch rc.Action {
case plans.Delete:
delete = true
case plans.Replace:
update = true
delete = true
default:
update = true
}
if update {
// All actions except destroying the node type chosen by t.Concrete
abstract := NewNodeAbstractResourceInstance(addr)
var node dag.Vertex = abstract
if f := t.Concrete; f != nil {
node = f(abstract)
}
if dk != states.NotDeposed {
// The only valid action for deposed objects is to destroy them.
// Entering this branch suggests a bug in the plan phase that
// proposed this change.
return fmt.Errorf("invalid %s action for deposed object on %s: only Delete is allowed", rc.Action, addr)
}
log.Printf("[TRACE] DiffTransformer: %s will be represented by %s", addr, dag.VertexName(node))
g.Add(node)
}
if delete {
// Destroying always uses this destroy-specific node type.
abstract := NewNodeAbstractResourceInstance(addr)
node := &NodeDestroyResourceInstance{
NodeAbstractResourceInstance: abstract,
DeposedKey: dk,
}
if dk == states.NotDeposed {
log.Printf("[TRACE] DiffTransformer: %s will be represented for destruction by %s", addr, dag.VertexName(node))
} else {
log.Printf("[TRACE] DiffTransformer: %s deposed object %s will be represented for destruction by %s", addr, dk, dag.VertexName(node))
}
g.Add(node)
}
}
log.Printf("[TRACE] DiffTransformer complete")
return nil
}