2015-04-29 13:18:58 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2018-01-29 18:17:31 -06:00
|
|
|
"log"
|
|
|
|
|
2020-02-24 16:42:32 -06:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
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"
|
2018-01-29 18:17:31 -06:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
2020-10-09 10:09:36 -05:00
|
|
|
"github.com/hashicorp/terraform/plans"
|
2015-04-29 13:18:58 -05:00
|
|
|
)
|
|
|
|
|
2016-09-15 13:39:11 -05:00
|
|
|
// OutputTransformer is a GraphTransformer that adds all the outputs
|
|
|
|
// in the configuration to the graph.
|
|
|
|
//
|
|
|
|
// This is done for the apply graph builder even if dependent nodes
|
|
|
|
// aren't changing since there is no downside: the state will be available
|
|
|
|
// even if the dependent items aren't changing.
|
|
|
|
type OutputTransformer struct {
|
2020-10-09 10:09:36 -05:00
|
|
|
Config *configs.Config
|
|
|
|
Changes *plans.Changes
|
|
|
|
|
|
|
|
// if this is a planed destroy, root outputs are still in the configuration
|
|
|
|
// so we need to record that we wish to remove them
|
|
|
|
Destroy bool
|
2015-04-29 13:18:58 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 13:39:11 -05:00
|
|
|
func (t *OutputTransformer) Transform(g *Graph) error {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
return t.transform(g, t.Config)
|
2016-09-16 01:20:35 -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
|
|
|
func (t *OutputTransformer) transform(g *Graph, c *configs.Config) error {
|
|
|
|
// If we have no config then there can be no outputs.
|
|
|
|
if c == nil {
|
2016-09-16 01:20:35 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform all the children. We must do this first because
|
|
|
|
// we can reference module outputs and they must show up in the
|
|
|
|
// reference map.
|
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 _, cc := range c.Children {
|
|
|
|
if err := t.transform(g, cc); err != nil {
|
2016-09-16 01:20:35 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 16:24:10 -05:00
|
|
|
// Add outputs to the graph, which will be dynamically expanded
|
2020-02-24 16:42:32 -06:00
|
|
|
// into NodeApplyableOutputs to reflect possible expansion
|
|
|
|
// through the presence of "count" or "for_each" on the modules.
|
2020-10-09 10:09:36 -05:00
|
|
|
|
|
|
|
var changes []*plans.OutputChangeSrc
|
|
|
|
if t.Changes != nil {
|
|
|
|
changes = t.Changes.Outputs
|
|
|
|
}
|
|
|
|
|
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 _, o := range c.Module.Outputs {
|
2020-10-09 16:24:10 -05:00
|
|
|
addr := addrs.OutputValue{Name: o.Name}
|
2016-09-16 01:20:35 -05:00
|
|
|
|
2020-10-09 16:24:10 -05:00
|
|
|
var rootChange *plans.OutputChangeSrc
|
|
|
|
for _, c := range changes {
|
|
|
|
if c.Addr.Module.IsRoot() && c.Addr.OutputValue.Name == o.Name {
|
|
|
|
rootChange = c
|
|
|
|
}
|
2018-01-29 18:17:31 -06:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:58:43 -05:00
|
|
|
destroy := t.Destroy
|
|
|
|
if rootChange != nil {
|
|
|
|
destroy = rootChange.Action == plans.Delete
|
|
|
|
}
|
|
|
|
|
2020-10-12 11:10:01 -05:00
|
|
|
// If this is a root output, we add the apply or destroy node directly,
|
|
|
|
// as the root modules does not expand.
|
|
|
|
|
2020-10-09 16:24:10 -05:00
|
|
|
var node dag.Vertex
|
|
|
|
switch {
|
2020-10-09 17:58:43 -05:00
|
|
|
case c.Path.IsRoot() && destroy:
|
2020-10-09 16:24:10 -05:00
|
|
|
node = &NodeDestroyableOutput{
|
|
|
|
Addr: addr.Absolute(addrs.RootModuleInstance),
|
|
|
|
Config: o,
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:58:43 -05:00
|
|
|
case c.Path.IsRoot():
|
|
|
|
node = &NodeApplyableOutput{
|
|
|
|
Addr: addr.Absolute(addrs.RootModuleInstance),
|
|
|
|
Config: o,
|
|
|
|
Change: rootChange,
|
2020-10-09 16:24:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
node = &nodeExpandOutput{
|
|
|
|
Addr: addr,
|
|
|
|
Module: c.Path,
|
|
|
|
Config: o,
|
|
|
|
Changes: changes,
|
|
|
|
Destroy: t.Destroy,
|
|
|
|
}
|
2020-03-06 17:02:20 -06:00
|
|
|
}
|
|
|
|
|
2020-10-09 16:24:10 -05:00
|
|
|
log.Printf("[TRACE] OutputTransformer: adding %s as %T", o.Name, node)
|
2018-01-29 18:17:31 -06:00
|
|
|
g.Add(node)
|
|
|
|
}
|
2020-10-09 16:24:10 -05:00
|
|
|
|
2018-01-29 18:17:31 -06:00
|
|
|
return nil
|
|
|
|
}
|