mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 01:11:10 -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.
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package terraform
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/configs"
|
|
"github.com/hashicorp/terraform/states"
|
|
)
|
|
|
|
// OrphanOutputTransformer finds the outputs that aren't present
|
|
// in the given config that are in the state and adds them to the graph
|
|
// for deletion.
|
|
type OrphanOutputTransformer struct {
|
|
Config *configs.Config // Root of config tree
|
|
State *states.State // State is the root state
|
|
}
|
|
|
|
func (t *OrphanOutputTransformer) Transform(g *Graph) error {
|
|
if t.State == nil {
|
|
log.Printf("[DEBUG] No state, no orphan outputs")
|
|
return nil
|
|
}
|
|
|
|
for _, ms := range t.State.Modules {
|
|
if err := t.transform(g, ms); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *OrphanOutputTransformer) transform(g *Graph, ms *states.Module) error {
|
|
if ms == nil {
|
|
return nil
|
|
}
|
|
|
|
moduleAddr := ms.Addr
|
|
|
|
// Get the config for this path, which is nil if the entire module has been
|
|
// removed.
|
|
var outputs map[string]*configs.Output
|
|
if c := t.Config.DescendentForInstance(moduleAddr); c != nil {
|
|
outputs = c.Module.Outputs
|
|
}
|
|
|
|
// An output is "orphaned" if it's present in the state but not declared
|
|
// in the configuration.
|
|
for name := range ms.OutputValues {
|
|
if _, exists := outputs[name]; exists {
|
|
continue
|
|
}
|
|
|
|
g.Add(&NodeOutputOrphan{
|
|
Addr: addrs.OutputValue{Name: name}.Absolute(moduleAddr),
|
|
})
|
|
}
|
|
|
|
return nil
|
|
}
|