2016-11-07 22:23:06 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 18:30:37 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/dag"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2016-11-07 22:23:06 +01:00
|
|
|
)
|
|
|
|
|
2020-03-27 22:16:08 +01:00
|
|
|
// OrphanResourceInstanceCountTransformer is a GraphTransformer that adds orphans
|
2016-11-07 22:23:06 +01:00
|
|
|
// for an expanded count to the graph. The determination of this depends
|
|
|
|
// on the count argument given.
|
|
|
|
//
|
|
|
|
// Orphans are found by comparing the count to what is found in the state.
|
2016-11-09 17:10:09 +01:00
|
|
|
// This transform assumes that if an element in the state is within the count
|
2016-11-07 22:23:06 +01:00
|
|
|
// bounds given, that it is not an orphan.
|
2020-03-27 22:16:08 +01:00
|
|
|
type OrphanResourceInstanceCountTransformer struct {
|
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 19:33:53 +02:00
|
|
|
Concrete ConcreteResourceInstanceNodeFunc
|
2016-11-07 22:23:06 +01:00
|
|
|
|
2020-03-25 20:07:35 +01:00
|
|
|
Addr addrs.AbsResource // Addr of the resource to look for orphans
|
2019-11-22 03:45:43 +01:00
|
|
|
InstanceAddrs []addrs.AbsResourceInstance // Addresses that currently exist in config
|
|
|
|
State *states.State // Full global state
|
2016-11-07 22:23:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 22:16:08 +01:00
|
|
|
func (t *OrphanResourceInstanceCountTransformer) Transform(g *Graph) error {
|
2020-03-25 20:07:35 +01:00
|
|
|
rs := t.State.Resource(t.Addr)
|
|
|
|
if rs == nil {
|
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-08-14 23:24:45 +02:00
|
|
|
return nil // Resource doesn't exist in state, so nothing to do!
|
2016-11-07 22:23:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 20:07:35 +01:00
|
|
|
// This is an O(n*m) analysis, which we accept for now because the
|
|
|
|
// number of instances of a single resource ought to always be small in any
|
|
|
|
// reasonable Terraform configuration.
|
|
|
|
Have:
|
|
|
|
for key := range rs.Instances {
|
|
|
|
thisAddr := rs.Addr.Instance(key)
|
|
|
|
for _, wantAddr := range t.InstanceAddrs {
|
|
|
|
if wantAddr.Equal(thisAddr) {
|
|
|
|
continue Have
|
2019-11-22 03:45:43 +01:00
|
|
|
}
|
2020-03-25 20:07:35 +01:00
|
|
|
}
|
|
|
|
// If thisAddr is not in t.InstanceAddrs then we've found an "orphan"
|
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 19:33:53 +02:00
|
|
|
|
2020-03-25 20:07:35 +01:00
|
|
|
abstract := NewNodeAbstractResourceInstance(thisAddr)
|
|
|
|
var node dag.Vertex = abstract
|
|
|
|
if f := t.Concrete; f != nil {
|
|
|
|
node = f(abstract)
|
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 19:33:53 +02:00
|
|
|
}
|
2020-03-27 22:16:08 +01:00
|
|
|
log.Printf("[TRACE] OrphanResourceInstanceCountTransformer: adding %s as %T", thisAddr, node)
|
2020-03-25 20:07:35 +01:00
|
|
|
g.Add(node)
|
2016-11-07 22:23:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|