2016-09-16 19:29:36 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
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 16:24:45 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2021-05-17 14:33:17 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2021-05-17 12:11:06 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
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 16:24:45 -05:00
|
|
|
|
2021-05-17 14:00:50 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 14:17:09 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
2021-05-17 14:43:35 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2016-09-16 19:29:36 -05:00
|
|
|
)
|
|
|
|
|
2018-09-26 19:52:34 -05:00
|
|
|
// NodeDestroyResourceInstance represents a resource instance that is to be
|
|
|
|
// destroyed.
|
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
|
|
|
type NodeDestroyResourceInstance struct {
|
|
|
|
*NodeAbstractResourceInstance
|
|
|
|
|
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 16:24:45 -05:00
|
|
|
// If DeposedKey is set to anything other than states.NotDeposed then
|
|
|
|
// this node destroys a deposed object of the associated instance
|
|
|
|
// rather than its current object.
|
|
|
|
DeposedKey states.DeposedKey
|
2016-09-16 19:29:36 -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
|
|
|
var (
|
2020-03-23 07:56:55 -05:00
|
|
|
_ GraphNodeModuleInstance = (*NodeDestroyResourceInstance)(nil)
|
2020-03-15 10:32:06 -05:00
|
|
|
_ GraphNodeConfigResource = (*NodeDestroyResourceInstance)(nil)
|
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
|
|
|
_ GraphNodeResourceInstance = (*NodeDestroyResourceInstance)(nil)
|
|
|
|
_ GraphNodeDestroyer = (*NodeDestroyResourceInstance)(nil)
|
|
|
|
_ GraphNodeDestroyerCBD = (*NodeDestroyResourceInstance)(nil)
|
|
|
|
_ GraphNodeReferenceable = (*NodeDestroyResourceInstance)(nil)
|
|
|
|
_ GraphNodeReferencer = (*NodeDestroyResourceInstance)(nil)
|
2020-09-16 10:33:55 -05:00
|
|
|
_ GraphNodeExecutable = (*NodeDestroyResourceInstance)(nil)
|
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
|
|
|
_ GraphNodeProviderConsumer = (*NodeDestroyResourceInstance)(nil)
|
|
|
|
_ GraphNodeProvisionerConsumer = (*NodeDestroyResourceInstance)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
func (n *NodeDestroyResourceInstance) Name() string {
|
2018-09-20 14:30:52 -05:00
|
|
|
if n.DeposedKey != states.NotDeposed {
|
|
|
|
return fmt.Sprintf("%s (destroy deposed %s)", n.ResourceInstanceAddr(), n.DeposedKey)
|
|
|
|
}
|
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
|
|
|
return n.ResourceInstanceAddr().String() + " (destroy)"
|
2016-09-16 19:29:36 -05:00
|
|
|
}
|
|
|
|
|
2021-03-08 13:51:44 -06:00
|
|
|
func (n *NodeDestroyResourceInstance) ProvidedBy() (addr addrs.ProviderConfig, exact bool) {
|
|
|
|
if n.Addr.Resource.Resource.Mode == addrs.DataResourceMode {
|
|
|
|
// indicate that this node does not require a configured provider
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
return n.NodeAbstractResourceInstance.ProvidedBy()
|
|
|
|
}
|
|
|
|
|
2016-09-20 12:23:48 -05:00
|
|
|
// GraphNodeDestroyer
|
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
|
|
|
func (n *NodeDestroyResourceInstance) DestroyAddr() *addrs.AbsResourceInstance {
|
|
|
|
addr := n.ResourceInstanceAddr()
|
|
|
|
return &addr
|
2016-09-20 12:23:48 -05:00
|
|
|
}
|
|
|
|
|
2016-09-21 20:48:21 -05:00
|
|
|
// GraphNodeDestroyerCBD
|
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
|
|
|
func (n *NodeDestroyResourceInstance) CreateBeforeDestroy() bool {
|
2020-09-16 08:52:48 -05:00
|
|
|
// State takes precedence during destroy.
|
|
|
|
// If the resource was removed, there is no config to check.
|
|
|
|
// If CBD was forced from descendent, it should be saved in the state
|
|
|
|
// already.
|
|
|
|
if s := n.instanceState; s != nil {
|
|
|
|
if s.Current != nil {
|
|
|
|
return s.Current.CreateBeforeDestroy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 14:03:32 -06:00
|
|
|
if n.Config != nil && n.Config.Managed != nil {
|
|
|
|
return n.Config.Managed.CreateBeforeDestroy
|
2016-09-21 20:48:21 -05:00
|
|
|
}
|
2016-09-16 22:26:10 -05:00
|
|
|
|
2019-12-12 14:03:32 -06:00
|
|
|
return false
|
2016-09-16 22:26:10 -05:00
|
|
|
}
|
|
|
|
|
2016-12-02 08:46:42 -06:00
|
|
|
// GraphNodeDestroyerCBD
|
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
|
|
|
func (n *NodeDestroyResourceInstance) ModifyCreateBeforeDestroy(v bool) error {
|
2016-12-02 08:46:42 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-22 12:10:57 -05:00
|
|
|
// GraphNodeReferenceable, overriding NodeAbstractResource
|
2018-05-30 14:01:05 -05:00
|
|
|
func (n *NodeDestroyResourceInstance) ReferenceableAddrs() []addrs.Referenceable {
|
|
|
|
normalAddrs := n.NodeAbstractResourceInstance.ReferenceableAddrs()
|
|
|
|
destroyAddrs := make([]addrs.Referenceable, len(normalAddrs))
|
|
|
|
|
|
|
|
phaseType := addrs.ResourceInstancePhaseDestroy
|
|
|
|
if n.CreateBeforeDestroy() {
|
|
|
|
phaseType = addrs.ResourceInstancePhaseDestroyCBD
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, normalAddr := range normalAddrs {
|
|
|
|
switch ta := normalAddr.(type) {
|
|
|
|
case addrs.Resource:
|
|
|
|
destroyAddrs[i] = ta.Phase(phaseType)
|
|
|
|
case addrs.ResourceInstance:
|
|
|
|
destroyAddrs[i] = ta.Phase(phaseType)
|
|
|
|
default:
|
|
|
|
destroyAddrs[i] = normalAddr
|
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
|
|
|
}
|
2016-10-12 20:43:26 -05:00
|
|
|
}
|
2018-05-30 14:01:05 -05:00
|
|
|
|
|
|
|
return destroyAddrs
|
2016-09-22 12:10:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferencer, overriding NodeAbstractResource
|
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
|
|
|
func (n *NodeDestroyResourceInstance) References() []*addrs.Reference {
|
2017-02-17 15:13:44 -06:00
|
|
|
// If we have a config, then we need to include destroy-time dependencies
|
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 c := n.Config; c != nil && c.Managed != nil {
|
|
|
|
var result []*addrs.Reference
|
|
|
|
|
|
|
|
// We include conn info and config for destroy time provisioners
|
|
|
|
// as dependencies that we have.
|
|
|
|
for _, p := range c.Managed.Provisioners {
|
|
|
|
schema := n.ProvisionerSchemas[p.Type]
|
|
|
|
|
|
|
|
if p.When == configs.ProvisionerWhenDestroy {
|
2018-05-04 21:56:40 -05:00
|
|
|
if p.Connection != nil {
|
|
|
|
result = append(result, ReferencesFromConfig(p.Connection.Config, connectionBlockSupersetSchema)...)
|
|
|
|
}
|
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
|
|
|
result = append(result, ReferencesFromConfig(p.Config, schema)...)
|
2017-02-17 15:13:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-09-22 12:10:57 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 10:33:55 -05:00
|
|
|
// GraphNodeExecutable
|
2020-10-28 12:47:04 -05:00
|
|
|
func (n *NodeDestroyResourceInstance) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {
|
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
|
|
|
addr := n.ResourceInstanceAddr()
|
2016-09-16 22:26:10 -05:00
|
|
|
|
2021-03-08 13:51:44 -06:00
|
|
|
// Eval info is different depending on what kind of resource this is
|
|
|
|
switch addr.Resource.Resource.Mode {
|
|
|
|
case addrs.ManagedResourceMode:
|
|
|
|
return n.managedResourceExecute(ctx)
|
|
|
|
case addrs.DataResourceMode:
|
|
|
|
return n.dataResourceExecute(ctx)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unsupported resource mode %s", n.Config.Mode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NodeDestroyResourceInstance) managedResourceExecute(ctx EvalContext) (diags tfdiags.Diagnostics) {
|
|
|
|
addr := n.ResourceInstanceAddr()
|
|
|
|
|
2016-09-16 22:26:10 -05:00
|
|
|
// Get our state
|
2020-07-10 09:16:45 -05:00
|
|
|
is := n.instanceState
|
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 16:24:45 -05:00
|
|
|
if is == nil {
|
|
|
|
log.Printf("[WARN] NodeDestroyResourceInstance for %s with no state", addr)
|
2016-09-16 22:26:10 -05:00
|
|
|
}
|
|
|
|
|
2020-09-16 10:33:55 -05:00
|
|
|
// These vars are updated through pointers at various stages below.
|
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 16:24:45 -05:00
|
|
|
var changeApply *plans.ResourceInstanceChange
|
|
|
|
var state *states.ResourceInstanceObject
|
2020-09-16 10:33:55 -05:00
|
|
|
|
2020-12-10 08:55:50 -06:00
|
|
|
_, providerSchema, err := getProvider(ctx, n.ResolvedProvider)
|
2020-10-28 12:47:04 -05:00
|
|
|
diags = diags.Append(err)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return diags
|
2020-10-06 16:14:53 -05:00
|
|
|
}
|
2020-09-16 10:33:55 -05:00
|
|
|
|
2020-10-06 16:14:53 -05:00
|
|
|
changeApply, err = n.readDiff(ctx, providerSchema)
|
2020-10-28 12:47:04 -05:00
|
|
|
diags = diags.Append(err)
|
2021-10-06 11:51:03 -05:00
|
|
|
if changeApply == nil || diags.HasErrors() {
|
2020-10-28 12:47:04 -05:00
|
|
|
return diags
|
2020-10-06 16:14:53 -05:00
|
|
|
}
|
2020-09-16 10:33:55 -05:00
|
|
|
|
Eval() Refactor: Plan Edition (#27177)
* terraforn: refactor EvalRefresh
EvalRefresh.Eval(ctx) is now Refresh(evalRefreshReqest, ctx). While none
of the inner logic of the function has changed, it now returns a
states.ResourceInstanceObject instead of updating a pointer. This is a
human-centric change, meant to make the logic flow (in the calling
functions) easier to follow.
* terraform: refactor EvalReadDataPlan and Apply
This is a very minor refactor that removes the (currently) redundant
types EvalReadDataPlan and EvalReadDataApply in favor of using
EvalReadData with a Plan and Apply functions.
This is in effect an aesthetic change; since there is no longer an
Eval() abstraction we can rename functions to make their functionality
as obvious as possible.
* terraform: refactor EvalCheckPlannedChange
EvalCheckPlannedChange was only used by NodeApplyableResourceInstance
and has been refactored into a method on that type called
checkPlannedChange.
* terraform: refactor EvalDiff.Eval
EvalDiff.Eval is now a method on NodeResourceAbstracted called Plan
which takes as a parameter an EvalPlanRequest. Instead of updating
pointers it returns a new plan and state.
I removed as many redundant fields from the original EvalDiff struct as
possible.
* terraform: refactor EvalReduceDiff
EvalReduceDiff is now reducePlan, a regular function (without a method)
that returns a value.
* terraform: refactor EvalDiffDestroy
EvalDiffDestroy.Eval is now NodeAbstractResourceInstance.PlanDestroy
which takes ctx, state and optional DeposedKey and returns a change.
I've removed the state return value since it was only ever returning a
nil state.
* terraform: refactor EvalWriteDiff
EvalWriteDiff.Eval is now NodeAbstractResourceInstance.WriteChange.
* rename files to something more logical
* terrafrom: refresh refactor, continued!
I had originally made Refresh a stand-alone function since it was
(obnoxiously) called from a graphNodeImportStateSub, but after some
(greatly appreciated) prompting in the PR I instead made it a method on
the NodeAbstractResourceInstance, in keeping with the other refactored
eval nodes, and then built a NodeAbstractResourceInstance inside import.
Since I did that I could also remove my duplicated 'writeState' code
inside graphNodeImportStateSub and use n.writeResourceInstanceState, so
double thanks!
* unexport eval methods
* re-refactor Plan, it made more sense on NodeAbstractResourceInstance. Sorry
* Remove uninformative `Eval`s from EvalReadData, consolidate to a single
file, and rename file to match function names.
* manual rebase
2020-12-08 07:50:30 -06:00
|
|
|
changeApply = reducePlan(addr.Resource, changeApply, true)
|
|
|
|
// reducePlan may have simplified our planned change
|
2020-10-06 16:14:53 -05:00
|
|
|
// into a NoOp if it does not require destroying.
|
|
|
|
if changeApply == nil || changeApply.Action == plans.NoOp {
|
2020-10-28 12:47:04 -05:00
|
|
|
return diags
|
2020-10-06 16:14:53 -05:00
|
|
|
}
|
2020-09-16 10:33:55 -05:00
|
|
|
|
2021-04-28 12:43:45 -05:00
|
|
|
state, readDiags := n.readResourceInstanceState(ctx, addr)
|
|
|
|
diags = diags.Append(readDiags)
|
2020-10-28 12:47:04 -05:00
|
|
|
if diags.HasErrors() {
|
|
|
|
return diags
|
2020-10-06 16:14:53 -05:00
|
|
|
}
|
2020-09-16 10:33:55 -05:00
|
|
|
|
2020-10-06 16:14:53 -05:00
|
|
|
// Exit early if the state object is null after reading the state
|
|
|
|
if state == nil || state.Value.IsNull() {
|
2020-10-28 12:47:04 -05:00
|
|
|
return diags
|
2020-10-06 16:14:53 -05:00
|
|
|
}
|
2017-01-20 20:07:51 -06:00
|
|
|
|
Mildwonkey/eval apply (#27222)
* rename files for consistency with contents
* terraform: refactor EvalValidateSelfref
The EvalValidateSelfref eval node implementation was removed in favor of a regular function.
* terraform: refactor EvalValidateProvisioner
EvalValidateProvisioner is now a method on NodeValidatableResource.
* terraform: refactor EvalValidateResource
EvalValidateResource is now a method on NodeValidatableResource, and the
functions called by (the new) validateResource are now standalone
functions.
This particular refactor gets the prize for "most complicated test
refactoring".
* terraform: refactor EvalMaybeTainted
EvalMaybeTainted was a relatively simple operation which never returned
an error, so I've refactored it into a plain function and moved it into
the only file its called from.
* terraform: eval-related cleanup
De-exported preApplyHook, which got missed in my general cleanup sweeps.
Removed resourceHasUserVisibleApply in favor of moving the logic inline
- it was a single-line check so calling the function was (nearly) as
much code as just checking if the resource was managed.
* terraform: refactor EvalApplyProvisioners
EvalApplyProvisioners.Eval is now a method on
NodeResourceAbstractInstance. There were two "apply"ish functions, so I
named the first "evalApplyProvisioners" since it mainly determined if
provisioners should be run before passing off execution to
applyProvisioners.
* terraform: refactor EvalApply
EvalApply is now a method on NodeAbstractResourceInstance. This was one
of the trickier Eval()s to refactor, and my goal was to change as little
as possible to avoid unintended side effects.
One notable change: there was a createNew boolean that was only used in
NodeApplyableResourceInstance.managedResourceExecute, and that boolean
was populated from the change (which was available from
managedResourceExecute), so I removed it from apply entirely. Out of an
abundance of caution I assigned the value to createNew in (roughtly) the same spot,
in case I was missing some place where the change might get modified.
TODO: Destroy nodes passed nil configs into apply, and I am curious if
we can get the same functionality by checking if the planned change is a
destroy, instead of passing a config into apply. That felt too risky for
this refactor but it is something I would like to explore at a future
point.
There are also a few updates to log output in this PR, since I spent
some time staring at logs and noticed various spots I missed.
2020-12-10 07:05:53 -06:00
|
|
|
diags = diags.Append(n.preApplyHook(ctx, changeApply))
|
2020-10-27 17:16:28 -05:00
|
|
|
if diags.HasErrors() {
|
2020-10-28 12:47:04 -05:00
|
|
|
return diags
|
2020-10-06 16:14:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run destroy provisioners if not tainted
|
2021-03-08 13:51:44 -06:00
|
|
|
if state.Status != states.ObjectTainted {
|
2021-01-13 14:27:17 -06:00
|
|
|
applyProvisionersDiags := n.evalApplyProvisioners(ctx, state, false, configs.ProvisionerWhenDestroy)
|
|
|
|
diags = diags.Append(applyProvisionersDiags)
|
2021-01-13 14:08:53 -06:00
|
|
|
// keep the diags separate from the main set until we handle the cleanup
|
|
|
|
|
2021-01-13 14:27:17 -06:00
|
|
|
if diags.HasErrors() {
|
2020-10-06 16:14:53 -05:00
|
|
|
// If we have a provisioning error, then we just call
|
|
|
|
// the post-apply hook now.
|
2021-01-13 14:27:17 -06:00
|
|
|
diags = diags.Append(n.postApplyHook(ctx, state, diags.Err()))
|
|
|
|
return diags
|
2020-09-16 10:33:55 -05:00
|
|
|
}
|
2020-10-06 16:14:53 -05:00
|
|
|
}
|
2020-09-16 10:33:55 -05:00
|
|
|
|
2020-10-06 16:14:53 -05:00
|
|
|
// Managed resources need to be destroyed, while data sources
|
|
|
|
// are only removed from state.
|
2021-03-08 13:51:44 -06:00
|
|
|
// we pass a nil configuration to apply because we are destroying
|
|
|
|
s, d := n.apply(ctx, state, changeApply, nil, false)
|
|
|
|
state, diags = s, diags.Append(d)
|
|
|
|
// we don't return immediately here on error, so that the state can be
|
|
|
|
// finalized
|
|
|
|
|
2021-03-26 08:40:33 -05:00
|
|
|
err = n.writeResourceInstanceState(ctx, state, workingState)
|
2021-03-08 13:51:44 -06:00
|
|
|
if err != nil {
|
|
|
|
return diags.Append(err)
|
2020-10-06 16:14:53 -05:00
|
|
|
}
|
|
|
|
|
2021-01-13 14:08:53 -06:00
|
|
|
// create the err value for postApplyHook
|
2021-01-13 14:27:17 -06:00
|
|
|
diags = diags.Append(n.postApplyHook(ctx, state, diags.Err()))
|
2020-12-10 08:55:50 -06:00
|
|
|
diags = diags.Append(updateStateHook(ctx))
|
2020-10-28 12:47:04 -05:00
|
|
|
return diags
|
2016-09-16 19:29:36 -05:00
|
|
|
}
|
2021-03-08 13:51:44 -06:00
|
|
|
|
|
|
|
func (n *NodeDestroyResourceInstance) dataResourceExecute(ctx EvalContext) (diags tfdiags.Diagnostics) {
|
|
|
|
log.Printf("[TRACE] NodeDestroyResourceInstance: removing state object for %s", n.Addr)
|
|
|
|
ctx.State().SetResourceInstanceCurrent(n.Addr, nil, n.ResolvedProvider)
|
|
|
|
return diags.Append(updateStateHook(ctx))
|
|
|
|
}
|