2019-11-22 03:45:43 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2020-04-28 23:15:40 +02:00
|
|
|
"log"
|
2020-04-01 22:37:55 +02:00
|
|
|
|
2019-11-22 03:45:43 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
2020-04-04 04:30:12 +02:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
2020-02-24 23:42:32 +01:00
|
|
|
"github.com/hashicorp/terraform/lang"
|
2020-06-16 18:39:50 +02:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2019-11-22 03:45:43 +01:00
|
|
|
)
|
|
|
|
|
2020-04-04 04:30:12 +02:00
|
|
|
type ConcreteModuleNodeFunc func(n *nodeExpandModule) dag.Vertex
|
|
|
|
|
2019-11-22 03:45:43 +01:00
|
|
|
// nodeExpandModule represents a module call in the configuration that
|
|
|
|
// might expand into multiple module instances depending on how it is
|
|
|
|
// configured.
|
|
|
|
type nodeExpandModule struct {
|
2020-02-24 23:42:32 +01:00
|
|
|
Addr addrs.Module
|
2019-11-22 03:45:43 +01:00
|
|
|
Config *configs.Module
|
2020-02-24 23:42:32 +01:00
|
|
|
ModuleCall *configs.ModuleCall
|
2019-11-22 03:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-09-09 21:59:29 +02:00
|
|
|
_ GraphNodeExecutable = (*nodeExpandModule)(nil)
|
2020-05-22 03:53:36 +02:00
|
|
|
_ GraphNodeReferencer = (*nodeExpandModule)(nil)
|
|
|
|
_ GraphNodeReferenceOutside = (*nodeExpandModule)(nil)
|
2020-05-22 04:11:58 +02:00
|
|
|
_ graphNodeExpandsInstances = (*nodeExpandModule)(nil)
|
2019-11-22 03:45:43 +01:00
|
|
|
)
|
|
|
|
|
2020-05-22 04:11:58 +02:00
|
|
|
func (n *nodeExpandModule) expandsInstances() {}
|
2020-05-19 02:59:17 +02:00
|
|
|
|
2019-11-22 03:45:43 +01:00
|
|
|
func (n *nodeExpandModule) Name() string {
|
2020-05-12 16:28:33 +02:00
|
|
|
return n.Addr.String() + " (expand)"
|
2019-11-22 03:45:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 03:00:16 +01:00
|
|
|
// GraphNodeModulePath implementation
|
|
|
|
func (n *nodeExpandModule) ModulePath() addrs.Module {
|
2020-05-22 03:53:36 +02:00
|
|
|
return n.Addr
|
2020-03-05 03:00:16 +01:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:45:43 +01:00
|
|
|
// GraphNodeReferencer implementation
|
|
|
|
func (n *nodeExpandModule) References() []*addrs.Reference {
|
2020-02-24 23:42:32 +01:00
|
|
|
var refs []*addrs.Reference
|
|
|
|
|
|
|
|
if n.ModuleCall == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 01:45:59 +02:00
|
|
|
refs = append(refs, n.DependsOn()...)
|
2020-04-28 23:15:40 +02:00
|
|
|
|
2019-11-22 03:45:43 +01:00
|
|
|
// Expansion only uses the count and for_each expressions, so this
|
|
|
|
// particular graph node only refers to those.
|
|
|
|
// Individual variable values in the module call definition might also
|
|
|
|
// refer to other objects, but that's handled by
|
|
|
|
// NodeApplyableModuleVariable.
|
|
|
|
//
|
|
|
|
// Because our Path method returns the module instance that contains
|
|
|
|
// our call, these references will be correctly interpreted as being
|
|
|
|
// in the calling module's namespace, not the namespaces of any of the
|
|
|
|
// child module instances we might expand to during our evaluation.
|
2020-02-24 23:42:32 +01:00
|
|
|
|
|
|
|
if n.ModuleCall.Count != nil {
|
2020-04-28 23:15:40 +02:00
|
|
|
countRefs, _ := lang.ReferencesInExpr(n.ModuleCall.Count)
|
|
|
|
refs = append(refs, countRefs...)
|
2020-02-24 23:42:32 +01:00
|
|
|
}
|
|
|
|
if n.ModuleCall.ForEach != nil {
|
2020-04-28 23:15:40 +02:00
|
|
|
forEachRefs, _ := lang.ReferencesInExpr(n.ModuleCall.ForEach)
|
|
|
|
refs = append(refs, forEachRefs...)
|
2020-02-24 23:42:32 +01:00
|
|
|
}
|
2020-07-18 00:48:35 +02:00
|
|
|
return refs
|
2019-11-22 03:45:43 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 01:45:59 +02:00
|
|
|
func (n *nodeExpandModule) DependsOn() []*addrs.Reference {
|
|
|
|
if n.ModuleCall == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var refs []*addrs.Reference
|
|
|
|
for _, traversal := range n.ModuleCall.DependsOn {
|
|
|
|
ref, diags := addrs.ParseRef(traversal)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
// We ignore this here, because this isn't a suitable place to return
|
|
|
|
// errors. This situation should be caught and rejected during
|
|
|
|
// validation.
|
|
|
|
log.Printf("[ERROR] Can't parse %#v from depends_on as reference: %s", traversal, diags.Err())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
refs = append(refs, ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2020-05-22 03:53:36 +02:00
|
|
|
// GraphNodeReferenceOutside
|
|
|
|
func (n *nodeExpandModule) ReferenceOutside() (selfPath, referencePath addrs.Module) {
|
|
|
|
return n.Addr, n.Addr.Parent()
|
|
|
|
}
|
|
|
|
|
2020-09-09 21:59:29 +02:00
|
|
|
// GraphNodeExecutable
|
2020-10-28 18:47:04 +01:00
|
|
|
func (n *nodeExpandModule) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {
|
2020-09-09 21:59:29 +02:00
|
|
|
expander := ctx.InstanceExpander()
|
|
|
|
_, call := n.Addr.Call()
|
|
|
|
|
|
|
|
// nodeExpandModule itself does not have visibility into how its ancestors
|
|
|
|
// were expanded, so we use the expander here to provide all possible paths
|
|
|
|
// to our module, and register module instances with each of them.
|
|
|
|
for _, module := range expander.ExpandModule(n.Addr.Parent()) {
|
|
|
|
ctx = ctx.WithPath(module)
|
|
|
|
switch {
|
|
|
|
case n.ModuleCall.Count != nil:
|
2020-10-28 18:47:04 +01:00
|
|
|
count, ctDiags := evaluateCountExpression(n.ModuleCall.Count, ctx)
|
|
|
|
diags = diags.Append(ctDiags)
|
2020-09-09 21:59:29 +02:00
|
|
|
if diags.HasErrors() {
|
2020-10-28 18:47:04 +01:00
|
|
|
return diags
|
2020-09-09 21:59:29 +02:00
|
|
|
}
|
|
|
|
expander.SetModuleCount(module, call, count)
|
|
|
|
|
|
|
|
case n.ModuleCall.ForEach != nil:
|
2020-10-28 18:47:04 +01:00
|
|
|
forEach, feDiags := evaluateForEachExpression(n.ModuleCall.ForEach, ctx)
|
|
|
|
diags = diags.Append(feDiags)
|
2020-09-09 21:59:29 +02:00
|
|
|
if diags.HasErrors() {
|
2020-10-28 18:47:04 +01:00
|
|
|
return diags
|
2020-09-09 21:59:29 +02:00
|
|
|
}
|
|
|
|
expander.SetModuleForEach(module, call, forEach)
|
|
|
|
|
|
|
|
default:
|
|
|
|
expander.SetModuleSingle(module, call)
|
|
|
|
}
|
2019-11-22 03:45:43 +01:00
|
|
|
}
|
2020-09-09 21:59:29 +02:00
|
|
|
|
2020-10-28 18:47:04 +01:00
|
|
|
return diags
|
2020-09-09 21:59:29 +02:00
|
|
|
|
2019-11-22 03:45:43 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 15:30:25 +02:00
|
|
|
// nodeCloseModule represents an expanded module during apply, and is visited
|
2020-04-01 22:37:55 +02:00
|
|
|
// after all other module instance nodes. This node will depend on all module
|
|
|
|
// instance resource and outputs, and anything depending on the module should
|
|
|
|
// wait on this node.
|
|
|
|
// Besides providing a root node for dependency ordering, nodeCloseModule also
|
|
|
|
// cleans up state after all the module nodes have been evaluated, removing
|
2021-01-20 16:17:29 +01:00
|
|
|
// empty resources and modules from the state.
|
|
|
|
// The root module instance also closes any remaining provisioner plugins which
|
|
|
|
// do not have a lifecycle controlled by individual graph nodes.
|
2020-04-01 22:37:55 +02:00
|
|
|
type nodeCloseModule struct {
|
2020-04-02 21:49:32 +02:00
|
|
|
Addr addrs.Module
|
2020-04-01 22:37:55 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 18:52:25 +02:00
|
|
|
var (
|
2020-05-20 19:17:31 +02:00
|
|
|
_ GraphNodeReferenceable = (*nodeCloseModule)(nil)
|
|
|
|
_ GraphNodeReferenceOutside = (*nodeCloseModule)(nil)
|
2020-10-28 18:47:04 +01:00
|
|
|
_ GraphNodeExecutable = (*nodeCloseModule)(nil)
|
2020-04-06 18:52:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (n *nodeCloseModule) ModulePath() addrs.Module {
|
2020-05-19 02:59:17 +02:00
|
|
|
return n.Addr
|
2020-04-06 18:52:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 19:17:31 +02:00
|
|
|
func (n *nodeCloseModule) ReferenceOutside() (selfPath, referencePath addrs.Module) {
|
|
|
|
return n.Addr.Parent(), n.Addr
|
|
|
|
}
|
|
|
|
|
2020-04-06 18:52:25 +02:00
|
|
|
func (n *nodeCloseModule) ReferenceableAddrs() []addrs.Referenceable {
|
|
|
|
_, call := n.Addr.Call()
|
|
|
|
return []addrs.Referenceable{
|
|
|
|
call,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 22:37:55 +02:00
|
|
|
func (n *nodeCloseModule) Name() string {
|
2020-04-02 21:49:32 +02:00
|
|
|
if len(n.Addr) == 0 {
|
|
|
|
return "root"
|
|
|
|
}
|
2020-04-01 22:37:55 +02:00
|
|
|
return n.Addr.String() + " (close)"
|
|
|
|
}
|
|
|
|
|
2020-10-28 18:47:04 +01:00
|
|
|
func (n *nodeCloseModule) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {
|
2021-01-19 18:58:00 +01:00
|
|
|
if n.Addr.IsRoot() {
|
|
|
|
// If this is the root module, we are cleaning up the walk, so close
|
|
|
|
// any running provisioners
|
|
|
|
diags = diags.Append(ctx.CloseProvisioners())
|
|
|
|
}
|
|
|
|
|
2020-09-09 21:59:29 +02:00
|
|
|
switch op {
|
|
|
|
case walkApply, walkDestroy:
|
|
|
|
state := ctx.State().Lock()
|
|
|
|
defer ctx.State().Unlock()
|
2020-04-01 22:37:55 +02:00
|
|
|
|
2020-09-09 21:59:29 +02:00
|
|
|
for modKey, mod := range state.Modules {
|
|
|
|
if !n.Addr.Equal(mod.Addr.Module()) {
|
|
|
|
continue
|
2020-04-01 22:37:55 +02:00
|
|
|
}
|
2020-02-24 23:42:32 +01:00
|
|
|
|
2020-09-09 21:59:29 +02:00
|
|
|
// clean out any empty resources
|
|
|
|
for resKey, res := range mod.Resources {
|
|
|
|
if len(res.Instances) == 0 {
|
|
|
|
delete(mod.Resources, resKey)
|
|
|
|
}
|
2020-04-05 16:46:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 21:59:29 +02:00
|
|
|
// empty child modules are always removed
|
|
|
|
if len(mod.Resources) == 0 && !mod.Addr.IsRoot() {
|
|
|
|
delete(state.Modules, modKey)
|
2020-04-05 16:46:36 +02:00
|
|
|
}
|
2020-03-06 23:16:07 +01:00
|
|
|
}
|
2020-09-09 21:59:29 +02:00
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return nil
|
2020-02-24 23:42:32 +01:00
|
|
|
}
|
2019-11-22 03:45:43 +01:00
|
|
|
}
|
2020-04-04 04:30:12 +02:00
|
|
|
|
|
|
|
// nodeValidateModule wraps a nodeExpand module for validation, ensuring that
|
|
|
|
// no expansion is attempted during evaluation, when count and for_each
|
|
|
|
// expressions may not be known.
|
|
|
|
type nodeValidateModule struct {
|
|
|
|
nodeExpandModule
|
|
|
|
}
|
|
|
|
|
2020-10-28 18:47:04 +01:00
|
|
|
var _ GraphNodeExecutable = (*nodeValidateModule)(nil)
|
|
|
|
|
2020-04-04 04:30:12 +02:00
|
|
|
// GraphNodeEvalable
|
2020-10-28 18:47:04 +01:00
|
|
|
func (n *nodeValidateModule) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {
|
2020-04-04 04:30:12 +02:00
|
|
|
_, call := n.Addr.Call()
|
|
|
|
expander := ctx.InstanceExpander()
|
|
|
|
|
|
|
|
// Modules all evaluate to single instances during validation, only to
|
|
|
|
// create a proper context within which to evaluate. All parent modules
|
|
|
|
// will be a single instance, but still get our address in the expected
|
|
|
|
// manner anyway to ensure they've been registered correctly.
|
|
|
|
for _, module := range expander.ExpandModule(n.Addr.Parent()) {
|
2020-05-05 22:44:22 +02:00
|
|
|
ctx = ctx.WithPath(module)
|
|
|
|
|
|
|
|
// Validate our for_each and count expressions at a basic level
|
|
|
|
// We skip validation on known, because there will be unknown values before
|
|
|
|
// a full expansion, presuming these errors will be caught in later steps
|
|
|
|
switch {
|
|
|
|
case n.ModuleCall.Count != nil:
|
2020-06-16 18:39:50 +02:00
|
|
|
_, countDiags := evaluateCountExpressionValue(n.ModuleCall.Count, ctx)
|
|
|
|
diags = diags.Append(countDiags)
|
2020-05-05 22:44:22 +02:00
|
|
|
|
|
|
|
case n.ModuleCall.ForEach != nil:
|
2020-10-29 01:52:03 +01:00
|
|
|
_, forEachDiags := evaluateForEachExpressionValue(n.ModuleCall.ForEach, ctx, true)
|
2020-06-16 18:39:50 +02:00
|
|
|
diags = diags.Append(forEachDiags)
|
2020-05-05 22:44:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 18:39:50 +02:00
|
|
|
diags = diags.Append(validateDependsOn(ctx, n.ModuleCall.DependsOn))
|
|
|
|
|
2020-04-04 04:30:12 +02:00
|
|
|
// now set our own mode to single
|
2020-05-05 22:44:22 +02:00
|
|
|
expander.SetModuleSingle(module, call)
|
2020-04-04 04:30:12 +02:00
|
|
|
}
|
2020-06-16 18:39:50 +02:00
|
|
|
|
2020-10-28 18:47:04 +01:00
|
|
|
return diags
|
2020-04-04 04:30:12 +02:00
|
|
|
}
|