opentofu/terraform/transform_attach_state.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

67 lines
2.0 KiB
Go

package terraform
import (
"log"
"github.com/hashicorp/terraform/dag"
"github.com/hashicorp/terraform/states"
)
// GraphNodeAttachResourceState is an interface that can be implemented
// to request that a ResourceState is attached to the node.
//
// Due to a historical naming inconsistency, the type ResourceState actually
// represents the state for a particular _instance_, while InstanceState
// represents the values for that instance during a particular phase
// (e.g. primary vs. deposed). Consequently, GraphNodeAttachResourceState
// is supported only for nodes that represent resource instances, even though
// the name might suggest it is for containing resources.
type GraphNodeAttachResourceState interface {
GraphNodeResourceInstance
// Sets the state
AttachResourceState(*states.Resource)
}
// AttachStateTransformer goes through the graph and attaches
// state to nodes that implement the interfaces above.
type AttachStateTransformer struct {
State *states.State // State is the root state
}
func (t *AttachStateTransformer) Transform(g *Graph) error {
// If no state, then nothing to do
if t.State == nil {
log.Printf("[DEBUG] Not attaching any node states: overall state is nil")
return nil
}
for _, v := range g.Vertices() {
// Nodes implement this interface to request state attachment.
an, ok := v.(GraphNodeAttachResourceState)
if !ok {
continue
}
addr := an.ResourceInstanceAddr()
rs := t.State.Resource(addr.ContainingResource())
if rs == nil {
log.Printf("[DEBUG] Resource state not found for node %q, instance %s", dag.VertexName(v), addr)
continue
}
is := rs.Instance(addr.Resource.Key)
if is == nil {
// We don't actually need this here, since we'll attach the whole
// resource state, but we still check because it'd be weird
// for the specific instance we're attaching to not to exist.
log.Printf("[DEBUG] Resource instance state not found for node %q, instance %s", dag.VertexName(v), addr)
continue
}
an.AttachResourceState(rs)
}
return nil
}