2017-07-01 18:48:37 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2020-09-09 21:59:29 +02:00
|
|
|
"fmt"
|
2020-03-07 00:27:11 +01:00
|
|
|
"log"
|
|
|
|
|
2020-09-09 21:59:29 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs"
|
2021-05-17 18:30:37 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/dag"
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
"github.com/hashicorp/terraform/lang"
|
2020-09-09 21:59:29 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-07-01 18:48:37 +02:00
|
|
|
)
|
|
|
|
|
2020-05-12 16:31:59 +02:00
|
|
|
// nodeExpandLocal represents a named local value in a configuration module,
|
2020-03-07 00:27:11 +01:00
|
|
|
// which has not yet been expanded.
|
2020-05-12 16:31:59 +02:00
|
|
|
type nodeExpandLocal struct {
|
2020-03-07 00:27:11 +01:00
|
|
|
Addr addrs.LocalValue
|
|
|
|
Module addrs.Module
|
|
|
|
Config *configs.Local
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-05-12 16:31:59 +02:00
|
|
|
_ GraphNodeReferenceable = (*nodeExpandLocal)(nil)
|
|
|
|
_ GraphNodeReferencer = (*nodeExpandLocal)(nil)
|
|
|
|
_ GraphNodeDynamicExpandable = (*nodeExpandLocal)(nil)
|
|
|
|
_ graphNodeTemporaryValue = (*nodeExpandLocal)(nil)
|
2020-05-29 16:31:10 +02:00
|
|
|
_ graphNodeExpandsInstances = (*nodeExpandLocal)(nil)
|
2020-05-22 04:11:58 +02:00
|
|
|
)
|
2020-05-19 02:59:17 +02:00
|
|
|
|
2020-05-22 04:11:58 +02:00
|
|
|
func (n *nodeExpandLocal) expandsInstances() {}
|
2020-05-19 02:59:17 +02:00
|
|
|
|
2020-04-02 19:26:53 +02:00
|
|
|
// graphNodeTemporaryValue
|
2020-05-12 16:31:59 +02:00
|
|
|
func (n *nodeExpandLocal) temporaryValue() bool {
|
2020-04-02 19:26:53 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-12 16:31:59 +02:00
|
|
|
func (n *nodeExpandLocal) Name() string {
|
2020-03-07 00:27:11 +01:00
|
|
|
path := n.Module.String()
|
2020-05-12 16:31:59 +02:00
|
|
|
addr := n.Addr.String() + " (expand)"
|
|
|
|
|
2020-03-07 00:27:11 +01:00
|
|
|
if path != "" {
|
|
|
|
return path + "." + addr
|
|
|
|
}
|
2020-05-12 16:31:59 +02:00
|
|
|
return addr
|
2020-03-07 00:27:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeModulePath
|
2020-05-12 16:31:59 +02:00
|
|
|
func (n *nodeExpandLocal) ModulePath() addrs.Module {
|
2020-03-07 00:27:11 +01:00
|
|
|
return n.Module
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferenceable
|
2020-05-12 16:31:59 +02:00
|
|
|
func (n *nodeExpandLocal) ReferenceableAddrs() []addrs.Referenceable {
|
2020-03-07 00:27:11 +01:00
|
|
|
return []addrs.Referenceable{n.Addr}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferencer
|
2020-05-12 16:31:59 +02:00
|
|
|
func (n *nodeExpandLocal) References() []*addrs.Reference {
|
2020-03-07 00:27:11 +01:00
|
|
|
refs, _ := lang.ReferencesInExpr(n.Config.Expr)
|
2020-07-18 00:48:35 +02:00
|
|
|
return refs
|
2020-03-07 00:27:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-12 16:31:59 +02:00
|
|
|
func (n *nodeExpandLocal) DynamicExpand(ctx EvalContext) (*Graph, error) {
|
2020-03-07 00:27:11 +01:00
|
|
|
var g Graph
|
|
|
|
expander := ctx.InstanceExpander()
|
|
|
|
for _, module := range expander.ExpandModule(n.Module) {
|
|
|
|
o := &NodeLocal{
|
|
|
|
Addr: n.Addr.Absolute(module),
|
|
|
|
Config: n.Config,
|
|
|
|
}
|
|
|
|
log.Printf("[TRACE] Expanding local: adding %s as %T", o.Addr.String(), o)
|
|
|
|
g.Add(o)
|
|
|
|
}
|
|
|
|
return &g, nil
|
|
|
|
}
|
|
|
|
|
2017-07-01 18:48:37 +02:00
|
|
|
// NodeLocal represents a named local value in a particular module.
|
|
|
|
//
|
|
|
|
// Local value nodes only have one operation, common to all walk types:
|
|
|
|
// evaluate the result and place it in state.
|
|
|
|
type NodeLocal struct {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
Addr addrs.AbsLocalValue
|
|
|
|
Config *configs.Local
|
2017-07-01 18:48:37 +02: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 19:33:53 +02:00
|
|
|
var (
|
2020-03-05 22:13:54 +01:00
|
|
|
_ GraphNodeModuleInstance = (*NodeLocal)(nil)
|
|
|
|
_ GraphNodeReferenceable = (*NodeLocal)(nil)
|
|
|
|
_ GraphNodeReferencer = (*NodeLocal)(nil)
|
2020-09-09 21:59:29 +02:00
|
|
|
_ GraphNodeExecutable = (*NodeLocal)(nil)
|
2020-04-02 19:26:53 +02:00
|
|
|
_ graphNodeTemporaryValue = (*NodeLocal)(nil)
|
2020-03-05 22:13:54 +01:00
|
|
|
_ dag.GraphNodeDotter = (*NodeLocal)(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 19:33:53 +02:00
|
|
|
)
|
2017-07-01 18:48:37 +02:00
|
|
|
|
2020-04-02 19:26:53 +02:00
|
|
|
// graphNodeTemporaryValue
|
|
|
|
func (n *NodeLocal) temporaryValue() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
func (n *NodeLocal) Name() string {
|
|
|
|
return n.Addr.String()
|
2017-07-01 18:48:37 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 22:13:54 +01:00
|
|
|
// GraphNodeModuleInstance
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
func (n *NodeLocal) Path() addrs.ModuleInstance {
|
|
|
|
return n.Addr.Module
|
2017-07-01 18:48:37 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 03:00:16 +01:00
|
|
|
// GraphNodeModulePath
|
|
|
|
func (n *NodeLocal) ModulePath() addrs.Module {
|
|
|
|
return n.Addr.Module.Module()
|
|
|
|
}
|
|
|
|
|
2017-07-01 18:48:37 +02:00
|
|
|
// GraphNodeReferenceable
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
func (n *NodeLocal) ReferenceableAddrs() []addrs.Referenceable {
|
|
|
|
return []addrs.Referenceable{n.Addr.LocalValue}
|
2017-07-01 18:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferencer
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
func (n *NodeLocal) References() []*addrs.Reference {
|
|
|
|
refs, _ := lang.ReferencesInExpr(n.Config.Expr)
|
2020-07-18 00:48:35 +02:00
|
|
|
return refs
|
2017-07-01 18:48:37 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 21:59:29 +02:00
|
|
|
// GraphNodeExecutable
|
|
|
|
// NodeLocal.Execute is an Execute implementation that evaluates the
|
|
|
|
// expression for a local value and writes it into a transient part of
|
|
|
|
// the state.
|
2020-10-28 18:47:04 +01:00
|
|
|
func (n *NodeLocal) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {
|
2020-09-09 21:59:29 +02:00
|
|
|
expr := n.Config.Expr
|
|
|
|
addr := n.Addr.LocalValue
|
|
|
|
|
|
|
|
// We ignore diags here because any problems we might find will be found
|
|
|
|
// again in EvaluateExpr below.
|
|
|
|
refs, _ := lang.ReferencesInExpr(expr)
|
|
|
|
for _, ref := range refs {
|
|
|
|
if ref.Subject == addr {
|
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Self-referencing local value",
|
|
|
|
Detail: fmt.Sprintf("Local value %s cannot use its own result as part of its expression.", addr),
|
|
|
|
Subject: ref.SourceRange.ToHCL().Ptr(),
|
|
|
|
Context: expr.Range().Ptr(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if diags.HasErrors() {
|
2020-10-28 18:47:04 +01:00
|
|
|
return diags
|
2020-09-09 21:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val, moreDiags := ctx.EvaluateExpr(expr, cty.DynamicPseudoType, nil)
|
|
|
|
diags = diags.Append(moreDiags)
|
|
|
|
if moreDiags.HasErrors() {
|
2020-10-28 18:47:04 +01:00
|
|
|
return diags
|
2017-07-01 18:48:37 +02:00
|
|
|
}
|
2020-09-09 21:59:29 +02:00
|
|
|
|
|
|
|
state := ctx.State()
|
|
|
|
if state == nil {
|
2020-10-28 18:47:04 +01:00
|
|
|
diags = diags.Append(fmt.Errorf("cannot write local value to nil state"))
|
|
|
|
return diags
|
2020-09-09 21:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetLocalValue(addr.Absolute(ctx.Path()), val)
|
|
|
|
|
2020-10-28 18:47:04 +01:00
|
|
|
return diags
|
2017-07-01 18:48:37 +02:00
|
|
|
}
|
2018-05-03 04:55:53 +02:00
|
|
|
|
|
|
|
// dag.GraphNodeDotter impl.
|
|
|
|
func (n *NodeLocal) DotNode(name string, opts *dag.DotOpts) *dag.DotNode {
|
|
|
|
return &dag.DotNode{
|
|
|
|
Name: name,
|
|
|
|
Attrs: map[string]string{
|
|
|
|
"label": n.Name(),
|
|
|
|
"shape": "note",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|