opentofu/terraform/transform_deposed.go
Martin Atkins c937c06a03 terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.

The three main goals here are:
- Use the configuration models from the "configs" package instead of the
  older models in the "config" package, which is now deprecated and
  preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
  new "lang" package, instead of the Interpolator type and related
  functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
  rather than hand-constructed strings. This is not critical to support
  the above, but was a big help during the implementation of these other
  points since it made it much more explicit what kind of address is
  expected in each context.

Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.

I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-10-16 18:46:46 -07:00

196 lines
4.9 KiB
Go

package terraform
import (
"fmt"
"github.com/hashicorp/terraform/addrs"
)
// DeposedTransformer is a GraphTransformer that adds deposed resources
// to the graph.
type DeposedTransformer struct {
// State is the global state. We'll automatically find the correct
// ModuleState based on the Graph.Path that is being transformed.
State *State
// View, if non-empty, is the ModuleState.View used around the state
// to find deposed resources.
View string
// The provider used by the resourced which were deposed
ResolvedProvider addrs.AbsProviderConfig
}
func (t *DeposedTransformer) 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 deposed
// resources, since they live in the state.
return nil
}
// If we have a view, apply it now
if t.View != "" {
state = state.View(t.View)
}
// Go through all the resources in our state to look for deposed resources
for k, rs := range state.Resources {
// If we have no deposed resources, then move on
if len(rs.Deposed) == 0 {
continue
}
legacyAddr, err := parseResourceAddressInternal(k)
if err != nil {
return fmt.Errorf("invalid instance key %q in state: %s", k, err)
}
addr := legacyAddr.AbsResourceInstanceAddr()
providerAddr, err := rs.ProviderAddr()
if err != nil {
return fmt.Errorf("invalid instance provider address %q in state: %s", rs.Provider, err)
}
for i := range rs.Deposed {
g.Add(&graphNodeDeposedResource{
Addr: addr,
Index: i,
RecordedProvider: providerAddr,
})
}
}
return nil
}
// graphNodeDeposedResource is the graph vertex representing a deposed resource.
type graphNodeDeposedResource struct {
Addr addrs.AbsResourceInstance
Index int // Index into the "deposed" list in state
RecordedProvider addrs.AbsProviderConfig
ResolvedProvider addrs.AbsProviderConfig
}
var (
_ GraphNodeProviderConsumer = (*graphNodeDeposedResource)(nil)
_ GraphNodeEvalable = (*graphNodeDeposedResource)(nil)
)
func (n *graphNodeDeposedResource) Name() string {
return fmt.Sprintf("%s (deposed #%d)", n.Addr.String(), n.Index)
}
func (n *graphNodeDeposedResource) ProvidedBy() (addrs.AbsProviderConfig, bool) {
return n.RecordedProvider, true
}
func (n *graphNodeDeposedResource) SetProvider(addr addrs.AbsProviderConfig) {
// Because our ProvidedBy returns exact=true, this is actually rather
// pointless and should always just be the address we asked for.
n.RecordedProvider = addr
}
// GraphNodeEvalable impl.
func (n *graphNodeDeposedResource) EvalTree() EvalNode {
addr := n.Addr
var provider ResourceProvider
var state *InstanceState
seq := &EvalSequence{Nodes: make([]EvalNode, 0, 5)}
stateKey := NewLegacyResourceInstanceAddress(addr).stateId()
// Refresh the resource
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
Ops: []walkOperation{walkRefresh},
Node: &EvalSequence{
Nodes: []EvalNode{
&EvalGetProvider{
Addr: n.ResolvedProvider,
Output: &provider,
},
&EvalReadStateDeposed{
Name: stateKey,
Output: &state,
Index: n.Index,
},
&EvalRefresh{
Addr: addr.Resource,
Provider: &provider,
State: &state,
Output: &state,
},
&EvalWriteStateDeposed{
Name: stateKey,
ResourceType: n.Addr.Resource.Resource.Type,
Provider: n.ResolvedProvider.String(), // FIXME: Change underlying struct to use addrs.AbsProviderConfig itself
State: &state,
Index: n.Index,
},
},
},
})
// Apply
var diff *InstanceDiff
var err error
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
Ops: []walkOperation{walkApply, walkDestroy},
Node: &EvalSequence{
Nodes: []EvalNode{
&EvalGetProvider{
Addr: n.ResolvedProvider,
Output: &provider,
},
&EvalReadStateDeposed{
Name: stateKey,
Output: &state,
Index: n.Index,
},
&EvalDiffDestroy{
Addr: addr.Resource,
State: &state,
Output: &diff,
},
// Call pre-apply hook
&EvalApplyPre{
Addr: addr.Resource,
State: &state,
Diff: &diff,
},
&EvalApply{
Addr: addr.Resource,
State: &state,
Diff: &diff,
Provider: &provider,
Output: &state,
Error: &err,
},
// Always write the resource back to the state deposed... if it
// was successfully destroyed it will be pruned. If it was not, it will
// be caught on the next run.
&EvalWriteStateDeposed{
Name: stateKey,
ResourceType: n.Addr.Resource.Resource.Type,
Provider: n.ResolvedProvider.String(), // FIXME: Change underlying struct to use addrs.AbsProviderConfig itself
State: &state,
Index: n.Index,
},
&EvalApplyPost{
Addr: addr.Resource,
State: &state,
Error: &err,
},
&EvalReturnError{
Error: &err,
},
&EvalUpdateStateHook{},
},
},
})
return seq
}