2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-09-20 07:16:53 -05:00
|
|
|
package tofu
|
2015-01-21 16:20:51 -06:00
|
|
|
|
|
|
|
import (
|
2016-11-05 19:53:12 -05:00
|
|
|
"log"
|
2015-01-21 16:20:51 -06:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs"
|
|
|
|
"github.com/opentofu/opentofu/internal/dag"
|
2015-01-21 16:20:51 -06:00
|
|
|
)
|
|
|
|
|
2016-11-05 18:26:12 -05:00
|
|
|
// ConfigTransformer is a GraphTransformer that adds all the resources
|
|
|
|
// from the configuration to the graph.
|
|
|
|
//
|
|
|
|
// The module used to configure this transformer must be the root module.
|
|
|
|
//
|
|
|
|
// Only resources are added to the graph. Variables, outputs, and
|
|
|
|
// providers must be added via other transforms.
|
|
|
|
//
|
|
|
|
// Unlike ConfigTransformerOld, this transformer creates a graph with
|
|
|
|
// all resources including module resources, rather than creating module
|
|
|
|
// nodes that are then "flattened".
|
2015-01-26 23:23:27 -06:00
|
|
|
type ConfigTransformer struct {
|
2016-11-05 19:53:12 -05:00
|
|
|
Concrete ConcreteResourceNodeFunc
|
|
|
|
|
2017-01-22 14:57:19 -06:00
|
|
|
// Module is the module to add resources from.
|
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-04-30 12:33:53 -05:00
|
|
|
Config *configs.Config
|
2017-01-22 14:57:19 -06:00
|
|
|
|
|
|
|
// Mode will only add resources that match the given mode
|
|
|
|
ModeFilter bool
|
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-04-30 12:33:53 -05:00
|
|
|
Mode addrs.ResourceMode
|
2022-05-27 09:55:38 -05:00
|
|
|
|
2022-06-01 14:29:59 -05:00
|
|
|
// Do not apply this transformer.
|
|
|
|
skip bool
|
2022-06-06 13:46:59 -05:00
|
|
|
|
2023-05-11 01:38:37 -05:00
|
|
|
// importTargets specifies a slice of addresses that will have state
|
|
|
|
// imported for them.
|
2022-06-06 13:46:59 -05:00
|
|
|
importTargets []*ImportTarget
|
2023-05-11 01:38:37 -05:00
|
|
|
|
2023-05-24 06:58:26 -05:00
|
|
|
// generateConfigPathForImportTargets tells the graph where to write any
|
|
|
|
// generated config for import targets that are not contained within config.
|
2023-05-11 01:38:37 -05:00
|
|
|
//
|
2023-05-24 06:58:26 -05:00
|
|
|
// If this is empty and an import target has no config, the graph will
|
2023-05-11 01:38:37 -05:00
|
|
|
// simply import the state for the target and any follow-up operations will
|
|
|
|
// try to delete the imported resource unless the config is updated
|
|
|
|
// manually.
|
2023-05-24 06:58:26 -05:00
|
|
|
generateConfigPathForImportTargets string
|
2020-03-07 11:48:39 -06:00
|
|
|
}
|
|
|
|
|
2015-01-26 23:23:27 -06:00
|
|
|
func (t *ConfigTransformer) Transform(g *Graph) error {
|
2022-06-01 14:29:59 -05:00
|
|
|
if t.skip {
|
2016-11-05 18:26:12 -05:00
|
|
|
return nil
|
2015-01-21 16:20:51 -06:00
|
|
|
}
|
2016-11-05 18:26:12 -05:00
|
|
|
|
2022-06-01 14:29:59 -05:00
|
|
|
// If no configuration is available, we don't do anything
|
|
|
|
if t.Config == nil {
|
2022-05-27 09:55:38 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-05 19:53:12 -05:00
|
|
|
// Start the transformation process
|
2023-05-24 06:58:26 -05:00
|
|
|
return t.transform(g, t.Config, t.generateConfigPathForImportTargets)
|
2016-11-05 19:53:12 -05:00
|
|
|
}
|
|
|
|
|
2023-05-24 06:58:26 -05:00
|
|
|
func (t *ConfigTransformer) transform(g *Graph, config *configs.Config, generateConfigPath string) error {
|
2016-11-05 19:53:12 -05:00
|
|
|
// If no config, do nothing
|
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-04-30 12:33:53 -05:00
|
|
|
if config == nil {
|
2015-02-11 19:47:30 -06:00
|
|
|
return nil
|
2015-01-30 15:31:39 -06:00
|
|
|
}
|
|
|
|
|
2024-06-06 05:20:41 -05:00
|
|
|
// If the module is being overridden, do nothing. We don't want to create anything
|
|
|
|
// from the underlying module.
|
|
|
|
if config.Module.IsOverridden {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-05 19:53:12 -05:00
|
|
|
// Add our resources
|
2023-05-24 06:58:26 -05:00
|
|
|
if err := t.transformSingle(g, config, generateConfigPath); err != nil {
|
2016-11-05 19:53:12 -05:00
|
|
|
return err
|
2015-04-30 18:27:20 -05:00
|
|
|
}
|
2015-01-21 16:20:51 -06:00
|
|
|
|
2023-05-11 01:38:37 -05:00
|
|
|
// Transform all the children without generating config.
|
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-04-30 12:33:53 -05:00
|
|
|
for _, c := range config.Children {
|
2023-05-24 06:58:26 -05:00
|
|
|
if err := t.transform(g, c, ""); err != nil {
|
2016-11-05 19:53:12 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-01-21 16:39:16 -06:00
|
|
|
}
|
|
|
|
|
2016-11-05 19:53:12 -05:00
|
|
|
return nil
|
|
|
|
}
|
2015-01-21 16:20:51 -06:00
|
|
|
|
2023-05-24 06:58:26 -05:00
|
|
|
func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config, generateConfigPath string) error {
|
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-04-30 12:33:53 -05:00
|
|
|
path := config.Path
|
|
|
|
module := config.Module
|
|
|
|
log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", path)
|
|
|
|
|
|
|
|
allResources := make([]*configs.Resource, 0, len(module.ManagedResources)+len(module.DataResources))
|
|
|
|
for _, r := range module.ManagedResources {
|
|
|
|
allResources = append(allResources, r)
|
|
|
|
}
|
|
|
|
for _, r := range module.DataResources {
|
|
|
|
allResources = append(allResources, r)
|
|
|
|
}
|
2015-01-21 16:20:51 -06:00
|
|
|
|
2023-05-11 01:38:37 -05:00
|
|
|
// Take a copy of the import targets, so we can edit them as we go.
|
2023-05-12 17:05:00 -05:00
|
|
|
// Only include import targets that are targeting the current module.
|
2023-05-11 01:38:37 -05:00
|
|
|
var importTargets []*ImportTarget
|
2023-05-12 17:05:00 -05:00
|
|
|
for _, target := range t.importTargets {
|
2024-02-08 08:05:12 -06:00
|
|
|
if targetModule := target.StaticAddr().Module; targetModule.Equal(config.Path) {
|
2023-05-12 17:05:00 -05:00
|
|
|
importTargets = append(importTargets, target)
|
|
|
|
}
|
|
|
|
}
|
2023-05-11 01:38:37 -05:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
for _, r := range allResources {
|
|
|
|
relAddr := r.Addr()
|
2015-01-23 17:11:11 -06:00
|
|
|
|
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-04-30 12:33:53 -05:00
|
|
|
if t.ModeFilter && relAddr.Mode != t.Mode {
|
|
|
|
// Skip non-matching modes
|
2017-01-22 14:57:19 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-06 13:46:59 -05:00
|
|
|
// If any of the import targets can apply to this node's instances,
|
|
|
|
// filter them down to the applicable addresses.
|
|
|
|
var imports []*ImportTarget
|
|
|
|
configAddr := relAddr.InModule(path)
|
2023-05-11 01:38:37 -05:00
|
|
|
|
|
|
|
var matchedIndices []int
|
|
|
|
for ix, i := range importTargets {
|
2024-02-08 08:05:12 -06:00
|
|
|
if target := i.StaticAddr(); target.Equal(configAddr) {
|
2023-05-11 01:38:37 -05:00
|
|
|
// This import target has been claimed by an actual resource,
|
|
|
|
// let's make a note of this to remove it from the targets.
|
|
|
|
matchedIndices = append(matchedIndices, ix)
|
2022-06-06 13:46:59 -05:00
|
|
|
imports = append(imports, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:38:37 -05:00
|
|
|
for ix := len(matchedIndices) - 1; ix >= 0; ix-- {
|
|
|
|
tIx := matchedIndices[ix]
|
|
|
|
|
|
|
|
// We do this backwards, since it means we don't have to adjust the
|
|
|
|
// later indices as we change the length of import targets.
|
|
|
|
//
|
|
|
|
// We need to do this separately, as a single resource could match
|
|
|
|
// multiple import targets.
|
|
|
|
importTargets = append(importTargets[:tIx], importTargets[tIx+1:]...)
|
|
|
|
}
|
|
|
|
|
2020-03-07 11:48:39 -06:00
|
|
|
abstract := &NodeAbstractResource{
|
2020-03-12 13:47:28 -05:00
|
|
|
Addr: addrs.ConfigResource{
|
|
|
|
Resource: relAddr,
|
|
|
|
Module: path,
|
|
|
|
},
|
2022-06-06 13:46:59 -05:00
|
|
|
importTargets: imports,
|
2020-03-07 11:48:39 -06:00
|
|
|
}
|
|
|
|
|
2016-11-05 19:53:12 -05:00
|
|
|
var node dag.Vertex = abstract
|
|
|
|
if f := t.Concrete; f != nil {
|
|
|
|
node = f(abstract)
|
2015-01-21 16:20:51 -06:00
|
|
|
}
|
2016-11-05 19:53:12 -05:00
|
|
|
|
|
|
|
g.Add(node)
|
2015-01-21 16:20:51 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 17:05:00 -05:00
|
|
|
// If any import targets were not claimed by resources, then let's add them
|
|
|
|
// into the graph now.
|
|
|
|
//
|
2024-04-15 05:06:35 -05:00
|
|
|
// We should only reach this point if config generation is enabled, since we validate that all import targets have
|
|
|
|
// a resource in validateImportTargets when config generation is disabled
|
2023-05-12 17:05:00 -05:00
|
|
|
//
|
|
|
|
// We'll add the nodes that we know will fail, and catch them again later
|
|
|
|
// in the processing when we are in a position to raise a much more helpful
|
|
|
|
// error message.
|
|
|
|
for _, i := range importTargets {
|
2024-02-08 08:05:12 -06:00
|
|
|
if len(generateConfigPath) > 0 {
|
|
|
|
// Create a node with the resource and import target. This node will take care of the config generation
|
|
|
|
abstract := &NodeAbstractResource{
|
2024-04-15 05:06:35 -05:00
|
|
|
// We've already validated in validateImportTargets that the address is fully resolvable
|
|
|
|
Addr: i.ResolvedAddr().ConfigResource(),
|
2024-02-08 08:05:12 -06:00
|
|
|
importTargets: []*ImportTarget{i},
|
|
|
|
generateConfigPath: generateConfigPath,
|
|
|
|
}
|
2023-05-12 17:05:00 -05:00
|
|
|
|
2024-02-08 08:05:12 -06:00
|
|
|
var node dag.Vertex = abstract
|
|
|
|
if f := t.Concrete; f != nil {
|
|
|
|
node = f(abstract)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Add(node)
|
|
|
|
} else {
|
2024-04-15 05:06:35 -05:00
|
|
|
// Technically we shouldn't reach this point, as we've already validated that a resource exists
|
|
|
|
// in validateImportTargets
|
|
|
|
return importResourceWithoutConfigDiags(i.StaticAddr().String(), i.Config)
|
2024-02-08 08:05:12 -06:00
|
|
|
}
|
2023-05-11 01:38:37 -05:00
|
|
|
}
|
|
|
|
|
2016-11-05 19:53:12 -05:00
|
|
|
return nil
|
2015-01-21 16:20:51 -06:00
|
|
|
}
|