2016-09-20 11:32:34 -05:00
|
|
|
package terraform
|
|
|
|
|
2016-09-20 12:16:49 -05:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
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
|
|
|
"github.com/hashicorp/terraform/addrs"
|
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
|
|
|
"github.com/hashicorp/terraform/states"
|
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
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
2016-09-20 12:16:49 -05:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
2016-09-20 11:32:34 -05:00
|
|
|
// GraphNodeDestroyer must be implemented by nodes that destroy resources.
|
|
|
|
type GraphNodeDestroyer interface {
|
2016-09-20 12:16:49 -05:00
|
|
|
dag.Vertex
|
|
|
|
|
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
|
|
|
// DestroyAddr is the address of the resource that is being
|
2016-09-20 11:32:34 -05:00
|
|
|
// destroyed by this node. If this returns nil, then this node
|
|
|
|
// is not destroying anything.
|
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
|
|
|
DestroyAddr() *addrs.AbsResourceInstance
|
2016-09-20 11:32:34 -05:00
|
|
|
}
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
// GraphNodeCreator must be implemented by nodes that create OR update resources.
|
|
|
|
type GraphNodeCreator interface {
|
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
|
|
|
// CreateAddr is the address of the resource being created or updated
|
|
|
|
CreateAddr() *addrs.AbsResourceInstance
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
|
|
|
|
2016-09-20 11:32:34 -05:00
|
|
|
// DestroyEdgeTransformer is a GraphTransformer that creates the proper
|
|
|
|
// references for destroy resources. Destroy resources are more complex
|
|
|
|
// in that they must be depend on the destruction of resources that
|
|
|
|
// in turn depend on the CREATION of the node being destroy.
|
|
|
|
//
|
|
|
|
// That is complicated. Visually:
|
|
|
|
//
|
|
|
|
// B_d -> A_d -> A -> B
|
|
|
|
//
|
|
|
|
// Notice that A destroy depends on B destroy, while B create depends on
|
|
|
|
// A create. They're inverted. This must be done for example because often
|
|
|
|
// dependent resources will block parent resources from deleting. Concrete
|
|
|
|
// example: VPC with subnets, the VPC can't be deleted while there are
|
|
|
|
// still subnets.
|
2016-09-20 12:16:49 -05:00
|
|
|
type DestroyEdgeTransformer struct {
|
2016-12-10 19:11:24 -06:00
|
|
|
// These are needed to properly build the graph of dependencies
|
|
|
|
// to determine what a destroy node depends on. Any of these can be 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
|
|
|
Config *configs.Config
|
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
|
|
|
State *states.State
|
2018-05-09 19:05:18 -05:00
|
|
|
|
2018-05-31 14:39:45 -05:00
|
|
|
// If configuration is present then Schemas is required in order to
|
2018-05-09 19:05:18 -05:00
|
|
|
// obtain schema information from providers and provisioners in order
|
|
|
|
// to properly resolve implicit dependencies.
|
2018-05-31 14:39:45 -05:00
|
|
|
Schemas *Schemas
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
2016-09-20 11:32:34 -05:00
|
|
|
|
|
|
|
func (t *DestroyEdgeTransformer) Transform(g *Graph) error {
|
|
|
|
// Build a map of what is being destroyed (by address string) to
|
2019-10-17 15:05:27 -05:00
|
|
|
// the list of destroyers.
|
2016-09-20 11:32:34 -05:00
|
|
|
destroyers := make(map[string][]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
|
|
|
destroyerAddrs := make(map[string]addrs.AbsResourceInstance)
|
2019-10-17 15:05:27 -05:00
|
|
|
|
2019-10-30 14:59:34 -05:00
|
|
|
// Record the creators, which will need to depend on the destroyers if they
|
|
|
|
// are only being updated.
|
|
|
|
creators := make(map[string]GraphNodeCreator)
|
|
|
|
|
2019-10-17 15:05:27 -05:00
|
|
|
// destroyersByResource records each destroyer by the AbsResourceAddress.
|
|
|
|
// We use this because dependencies are only referenced as resources, but we
|
|
|
|
// will want to connect all the individual instances for correct ordering.
|
|
|
|
destroyersByResource := make(map[string][]GraphNodeDestroyer)
|
2016-09-20 11:32:34 -05:00
|
|
|
for _, v := range g.Vertices() {
|
2019-10-30 14:59:34 -05:00
|
|
|
switch n := v.(type) {
|
|
|
|
case GraphNodeDestroyer:
|
|
|
|
addrP := n.DestroyAddr()
|
|
|
|
if addrP == nil {
|
|
|
|
log.Printf("[WARN] DestroyEdgeTransformer: %q (%T) has no destroy address", dag.VertexName(n), v)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := *addrP
|
|
|
|
|
|
|
|
key := addr.String()
|
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: %q (%T) destroys %s", dag.VertexName(n), v, key)
|
|
|
|
destroyers[key] = append(destroyers[key], n)
|
|
|
|
destroyerAddrs[key] = addr
|
|
|
|
|
|
|
|
resAddr := addr.Resource.Absolute(addr.Module).String()
|
|
|
|
destroyersByResource[resAddr] = append(destroyersByResource[resAddr], n)
|
|
|
|
case GraphNodeCreator:
|
|
|
|
addr := n.CreateAddr()
|
|
|
|
creators[addr.String()] = n
|
2016-09-20 11:32:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we aren't destroying anything, there will be no edges to make
|
|
|
|
// so just exit early and avoid future work.
|
|
|
|
if len(destroyers) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-30 14:59:34 -05:00
|
|
|
// Connect destroy despendencies as stored in the state
|
2019-10-17 15:05:27 -05:00
|
|
|
for _, ds := range destroyers {
|
|
|
|
for _, des := range ds {
|
2019-10-30 14:59:34 -05:00
|
|
|
ri, ok := des.(GraphNodeResourceInstance)
|
2019-10-17 15:05:27 -05:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resAddr := range ri.StateDependencies() {
|
|
|
|
for _, desDep := range destroyersByResource[resAddr.String()] {
|
2019-10-30 14:59:34 -05:00
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: %s has stored dependency of %s\n", dag.VertexName(desDep), dag.VertexName(des))
|
2019-10-17 15:05:27 -05:00
|
|
|
g.Connect(dag.BasicEdge(desDep, des))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 14:59:34 -05:00
|
|
|
// connect creators to any destroyers on which they may depend
|
|
|
|
for _, c := range creators {
|
|
|
|
ri, ok := c.(GraphNodeResourceInstance)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resAddr := range ri.StateDependencies() {
|
|
|
|
for _, desDep := range destroyersByResource[resAddr.String()] {
|
2019-11-17 08:56:44 -06:00
|
|
|
// TODO: don't connect this if c is CreateBeforeDestroy.
|
|
|
|
// This will require getting the actual change action at
|
|
|
|
// this point, since the lifecycle may have been forced
|
|
|
|
// by a dependent. This should prevent needing to prune
|
|
|
|
// the edge back out in CBDEdgeTransformer, and allow
|
|
|
|
// non-CBD nodes to depend on CBD destroys directly.
|
2019-10-30 14:59:34 -05:00
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: %s has stored dependency of %s\n", dag.VertexName(c), dag.VertexName(desDep))
|
|
|
|
g.Connect(dag.BasicEdge(c, desDep))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
// Go through and connect creators to destroyers. Going along with
|
|
|
|
// our example, this makes: A_d => A
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
cn, ok := v.(GraphNodeCreator)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := cn.CreateAddr()
|
|
|
|
if addr == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:05:27 -05:00
|
|
|
for _, d := range destroyers[addr.String()] {
|
2016-09-21 12:55:07 -05:00
|
|
|
// For illustrating our example
|
|
|
|
a_d := d.(dag.Vertex)
|
|
|
|
a := v
|
|
|
|
|
2016-09-21 13:03:04 -05:00
|
|
|
log.Printf(
|
2018-05-09 19:05:18 -05:00
|
|
|
"[TRACE] DestroyEdgeTransformer: connecting creator %q with destroyer %q",
|
2016-09-21 13:03:04 -05:00
|
|
|
dag.VertexName(a), dag.VertexName(a_d))
|
|
|
|
|
2016-09-21 12:55:07 -05:00
|
|
|
g.Connect(&DestroyEdge{S: a, T: a_d})
|
2018-06-01 08:13:14 -05:00
|
|
|
|
|
|
|
// Attach the destroy node to the creator
|
|
|
|
// There really shouldn't be more than one destroyer, but even if
|
|
|
|
// there are, any of them will represent the correct
|
|
|
|
// CreateBeforeDestroy status.
|
|
|
|
if n, ok := cn.(GraphNodeAttachDestroyer); ok {
|
|
|
|
if d, ok := d.(GraphNodeDestroyerCBD); ok {
|
|
|
|
n.AttachDestroyNode(d)
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 12:55:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 12:16:49 -05:00
|
|
|
// This is strange but is the easiest way to get the dependencies
|
|
|
|
// of a node that is being destroyed. We use another graph to make sure
|
|
|
|
// the resource is in the graph and ask for references. We have to do this
|
|
|
|
// because the node that is being destroyed may NOT be in the graph.
|
|
|
|
//
|
|
|
|
// Example: resource A is force new, then destroy A AND create A are
|
|
|
|
// in the graph. BUT if resource A is just pure destroy, then only
|
|
|
|
// destroy A is in the graph, and create A is not.
|
2016-12-10 19:11:24 -06:00
|
|
|
providerFn := func(a *NodeAbstractProvider) dag.Vertex {
|
|
|
|
return &NodeApplyableProvider{NodeAbstractProvider: a}
|
|
|
|
}
|
2016-09-20 12:16:49 -05:00
|
|
|
steps := []GraphTransformer{
|
2017-09-29 10:12:57 -05:00
|
|
|
// Add the local values
|
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
|
|
|
&LocalTransformer{Config: t.Config},
|
2017-09-29 10:12:57 -05:00
|
|
|
|
2016-12-10 19:11:24 -06:00
|
|
|
// Add outputs and metadata
|
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
|
|
|
&OutputTransformer{Config: t.Config},
|
|
|
|
&AttachResourceConfigTransformer{Config: t.Config},
|
2016-09-20 12:16:49 -05:00
|
|
|
&AttachStateTransformer{State: t.State},
|
2016-12-10 19:11:24 -06:00
|
|
|
|
2016-12-14 23:39:13 -06:00
|
|
|
// Add all the variables. We can depend on resources through
|
|
|
|
// variables due to module parameters, and we need to properly
|
|
|
|
// determine that.
|
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
|
|
|
&RootVariableTransformer{Config: t.Config},
|
|
|
|
&ModuleVariableTransformer{Config: t.Config},
|
2016-12-14 23:39:13 -06:00
|
|
|
|
2018-05-29 18:18:33 -05:00
|
|
|
TransformProviders(nil, providerFn, t.Config),
|
|
|
|
|
2018-06-01 15:00:52 -05:00
|
|
|
// Must attach schemas before ReferenceTransformer so that we can
|
|
|
|
// analyze the configuration to find references.
|
2018-05-31 15:00:17 -05:00
|
|
|
&AttachSchemaTransformer{Schemas: t.Schemas},
|
|
|
|
|
2016-11-11 16:29:19 -06:00
|
|
|
&ReferenceTransformer{},
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
|
|
|
|
2016-11-08 11:35:57 -06:00
|
|
|
// Go through all the nodes being destroyed and create a graph.
|
|
|
|
// The resulting graph is only of things being CREATED. For example,
|
|
|
|
// following our example, the resulting graph would be:
|
|
|
|
//
|
|
|
|
// A, B (with no edges)
|
|
|
|
//
|
|
|
|
var tempG Graph
|
2016-11-11 16:29:19 -06:00
|
|
|
var tempDestroyed []dag.Vertex
|
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 d := range destroyers {
|
|
|
|
// d is the string key for the resource being destroyed. We actually
|
|
|
|
// want the address value, which we stashed earlier.
|
|
|
|
addr := destroyerAddrs[d]
|
2016-09-20 12:16:49 -05:00
|
|
|
|
|
|
|
// This part is a little bit weird but is the best way to
|
|
|
|
// find the dependencies we need to: build a graph and use the
|
|
|
|
// attach config and state transformers then ask for references.
|
2018-05-09 19:05:18 -05:00
|
|
|
abstract := NewNodeAbstractResourceInstance(addr)
|
2017-02-17 16:29:22 -06:00
|
|
|
tempG.Add(abstract)
|
|
|
|
tempDestroyed = append(tempDestroyed, abstract)
|
|
|
|
|
|
|
|
// We also add the destroy version here since the destroy can
|
|
|
|
// depend on things that the creation doesn't (destroy provisioners).
|
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
|
|
|
destroy := &NodeDestroyResourceInstance{NodeAbstractResourceInstance: abstract}
|
2017-02-17 16:29:22 -06:00
|
|
|
tempG.Add(destroy)
|
|
|
|
tempDestroyed = append(tempDestroyed, destroy)
|
2016-11-08 11:35:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the graph transforms so we have the information we need to
|
|
|
|
// build references.
|
2018-05-29 20:43:57 -05:00
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: constructing temporary graph for analysis of references, starting from:\n%s", tempG.StringWithNodeTypes())
|
2016-11-08 11:35:57 -06:00
|
|
|
for _, s := range steps {
|
2018-05-09 19:05:18 -05:00
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: running %T on temporary graph", s)
|
2016-11-08 11:35:57 -06:00
|
|
|
if err := s.Transform(&tempG); err != nil {
|
2018-05-09 19:05:18 -05:00
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: %T failed: %s", s, err)
|
2016-11-08 11:35:57 -06:00
|
|
|
return err
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
2016-11-08 11:35:57 -06:00
|
|
|
}
|
2018-05-29 20:43:57 -05:00
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: temporary reference graph:\n%s", tempG.String())
|
2016-09-20 12:16:49 -05:00
|
|
|
|
2016-11-08 11:35:57 -06:00
|
|
|
// Go through all the nodes in the graph and determine what they
|
|
|
|
// depend on.
|
2016-11-11 16:29:19 -06:00
|
|
|
for _, v := range tempDestroyed {
|
2016-11-14 11:34:21 -06:00
|
|
|
// Find all ancestors of this to determine the edges we'll depend on
|
2016-11-11 16:29:19 -06:00
|
|
|
vs, err := tempG.Ancestors(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
refs := make([]dag.Vertex, 0, vs.Len())
|
|
|
|
for _, raw := range vs.List() {
|
|
|
|
refs = append(refs, raw.(dag.Vertex))
|
|
|
|
}
|
|
|
|
|
2016-12-10 19:11:24 -06:00
|
|
|
refNames := make([]string, len(refs))
|
|
|
|
for i, ref := range refs {
|
|
|
|
refNames[i] = dag.VertexName(ref)
|
|
|
|
}
|
2016-09-20 12:16:49 -05:00
|
|
|
log.Printf(
|
2016-12-10 19:11:24 -06:00
|
|
|
"[TRACE] DestroyEdgeTransformer: creation node %q references %s",
|
|
|
|
dag.VertexName(v), refNames)
|
2016-11-08 11:35:57 -06:00
|
|
|
|
|
|
|
// If we have no references, then we won't need to do anything
|
|
|
|
if len(refs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the destroy node for this. In the example of our struct,
|
|
|
|
// we are currently at B and we're looking for B_d.
|
2018-05-29 20:43:57 -05:00
|
|
|
rn, ok := v.(GraphNodeResourceInstance)
|
2016-11-08 11:35:57 -06:00
|
|
|
if !ok {
|
2018-05-29 20:43:57 -05:00
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: skipping %s, since it's not a resource", dag.VertexName(v))
|
2016-11-08 11:35:57 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:43:57 -05:00
|
|
|
addr := rn.ResourceInstanceAddr()
|
2016-11-08 11:35:57 -06:00
|
|
|
dns := destroyers[addr.String()]
|
|
|
|
|
2016-09-20 12:16:49 -05:00
|
|
|
// We have dependencies, check if any are being destroyed
|
|
|
|
// to build the list of things that we must depend on!
|
|
|
|
//
|
|
|
|
// In the example of the struct, if we have:
|
|
|
|
//
|
|
|
|
// B_d => A_d => A => B
|
|
|
|
//
|
2016-11-08 11:35:57 -06:00
|
|
|
// Then at this point in the algorithm we started with B_d,
|
|
|
|
// we built B (to get dependencies), and we found A. We're now looking
|
|
|
|
// to see if A_d exists.
|
2016-09-20 12:16:49 -05:00
|
|
|
var depDestroyers []dag.Vertex
|
2016-11-08 11:35:57 -06:00
|
|
|
for _, v := range refs {
|
2018-05-29 20:43:57 -05:00
|
|
|
rn, ok := v.(GraphNodeResourceInstance)
|
2016-11-08 11:35:57 -06:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:43:57 -05:00
|
|
|
addr := rn.ResourceInstanceAddr()
|
2016-11-08 11:35:57 -06:00
|
|
|
key := addr.String()
|
|
|
|
if ds, ok := destroyers[key]; ok {
|
2016-09-20 12:16:49 -05:00
|
|
|
for _, d := range ds {
|
|
|
|
depDestroyers = append(depDestroyers, d.(dag.Vertex))
|
|
|
|
log.Printf(
|
|
|
|
"[TRACE] DestroyEdgeTransformer: destruction of %q depends on %s",
|
2016-11-08 11:35:57 -06:00
|
|
|
key, dag.VertexName(d))
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through and make the connections. Use the variable
|
|
|
|
// names "a_d" and "b_d" to reference our example.
|
|
|
|
for _, a_d := range dns {
|
|
|
|
for _, b_d := range depDestroyers {
|
2016-11-08 14:27:33 -06:00
|
|
|
if b_d != a_d {
|
2018-05-29 20:43:57 -05:00
|
|
|
log.Printf("[TRACE] DestroyEdgeTransformer: %q depends on %q", dag.VertexName(b_d), dag.VertexName(a_d))
|
2016-11-08 14:27:33 -06:00
|
|
|
g.Connect(dag.BasicEdge(b_d, a_d))
|
|
|
|
}
|
2016-09-20 12:16:49 -05:00
|
|
|
}
|
2016-09-20 11:32:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 19:34:55 -05:00
|
|
|
return t.pruneResources(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are only destroy instances for a particular resource, there's no
|
|
|
|
// reason for the resource node to prepare the state. Remove Resource nodes so
|
|
|
|
// that they don't fail by trying to evaluate a resource that is only being
|
|
|
|
// destroyed along with its dependencies.
|
|
|
|
func (t *DestroyEdgeTransformer) pruneResources(g *Graph) error {
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
n, ok := v.(*NodeApplyableResource)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there are only destroy dependencies, we don't need this node
|
|
|
|
des, err := g.Descendents(n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
descendents := des.List()
|
|
|
|
nonDestroyInstanceFound := false
|
|
|
|
for _, v := range descendents {
|
|
|
|
if _, ok := v.(*NodeApplyableResourceInstance); ok {
|
|
|
|
nonDestroyInstanceFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nonDestroyInstanceFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect all the through-edges, then delete the node
|
|
|
|
for _, d := range g.DownEdges(n).List() {
|
|
|
|
for _, u := range g.UpEdges(n).List() {
|
|
|
|
g.Connect(dag.BasicEdge(u, d))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("DestroyEdgeTransformer: pruning unused resource node %s", dag.VertexName(n))
|
|
|
|
g.Remove(n)
|
|
|
|
}
|
2016-09-20 11:32:34 -05:00
|
|
|
return nil
|
|
|
|
}
|