mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
a3403f2766
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.
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
package terraform
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
// EvalCountFixZeroOneBoundaryGlobal is an EvalNode that fixes up the state
|
|
// when there is a resource count with zero/one boundary, i.e. fixing
|
|
// a resource named "aws_instance.foo" to "aws_instance.foo.0" and vice-versa.
|
|
//
|
|
// This works on the global state.
|
|
type EvalCountFixZeroOneBoundaryGlobal struct{}
|
|
|
|
// TODO: test
|
|
func (n *EvalCountFixZeroOneBoundaryGlobal) Eval(ctx EvalContext) (interface{}, error) {
|
|
return nil, fmt.Errorf("EvalCountFixZeroOneBoundaryGlobal not yet updated for new state types")
|
|
/*
|
|
// Get the state and lock it since we'll potentially modify it
|
|
state, lock := ctx.State()
|
|
lock.Lock()
|
|
defer lock.Unlock()
|
|
|
|
// Prune the state since we require a clean state to work
|
|
state.prune()
|
|
|
|
// Go through each modules since the boundaries are restricted to a
|
|
// module scope.
|
|
for _, m := range state.Modules {
|
|
if err := n.fixModule(m); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return nil, nil
|
|
*/
|
|
}
|
|
|
|
func (n *EvalCountFixZeroOneBoundaryGlobal) fixModule(m *ModuleState) error {
|
|
// Counts keeps track of keys and their counts
|
|
counts := make(map[string]int)
|
|
for k, _ := range m.Resources {
|
|
// Parse the key
|
|
key, err := ParseResourceStateKey(k)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Set the index to -1 so that we can keep count
|
|
key.Index = -1
|
|
|
|
// Increment
|
|
counts[key.String()]++
|
|
}
|
|
|
|
// Go through the counts and do the fixup for each resource
|
|
for raw, count := range counts {
|
|
// Search and replace this resource
|
|
search := raw
|
|
replace := raw + ".0"
|
|
if count < 2 {
|
|
search, replace = replace, search
|
|
}
|
|
log.Printf("[TRACE] EvalCountFixZeroOneBoundaryGlobal: count %d, search %q, replace %q", count, search, replace)
|
|
|
|
// Look for the resource state. If we don't have one, then it is okay.
|
|
rs, ok := m.Resources[search]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
// If the replacement key exists, we just keep both
|
|
if _, ok := m.Resources[replace]; ok {
|
|
continue
|
|
}
|
|
|
|
m.Resources[replace] = rs
|
|
delete(m.Resources, search)
|
|
}
|
|
|
|
return nil
|
|
}
|