2016-09-13 12:56:37 -05:00
|
|
|
package terraform
|
|
|
|
|
2016-09-13 19:11:34 -05:00
|
|
|
import (
|
2018-09-11 16:22:25 -05:00
|
|
|
"log"
|
2018-09-12 18:56:06 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
"github.com/hashicorp/terraform/lang"
|
2016-09-13 19:11:34 -05:00
|
|
|
)
|
|
|
|
|
2020-03-20 14:45:39 -05:00
|
|
|
// nodeExpandApplyableResource handles the first layer of resource
|
2020-03-26 10:52:41 -05:00
|
|
|
// expansion during apply. Even though the resource instances themselves are
|
|
|
|
// already expanded from the plan, we still need to expand the
|
|
|
|
// NodeApplyableResource nodes into their respective modules.
|
2020-03-20 14:45:39 -05:00
|
|
|
type nodeExpandApplyableResource struct {
|
|
|
|
*NodeAbstractResource
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ GraphNodeDynamicExpandable = (*nodeExpandApplyableResource)(nil)
|
|
|
|
_ GraphNodeReferenceable = (*nodeExpandApplyableResource)(nil)
|
|
|
|
_ GraphNodeReferencer = (*nodeExpandApplyableResource)(nil)
|
|
|
|
_ GraphNodeConfigResource = (*nodeExpandApplyableResource)(nil)
|
|
|
|
_ GraphNodeAttachResourceConfig = (*nodeExpandApplyableResource)(nil)
|
2020-05-21 21:11:58 -05:00
|
|
|
_ graphNodeExpandsInstances = (*nodeExpandApplyableResource)(nil)
|
2020-06-10 14:39:29 -05:00
|
|
|
_ GraphNodeTargetable = (*nodeExpandApplyableResource)(nil)
|
2020-03-20 14:45:39 -05:00
|
|
|
)
|
|
|
|
|
2020-10-09 16:24:10 -05:00
|
|
|
func (n *nodeExpandApplyableResource) expandsInstances() {
|
|
|
|
}
|
2020-05-18 19:59:17 -05:00
|
|
|
|
2020-03-20 14:45:39 -05:00
|
|
|
func (n *nodeExpandApplyableResource) References() []*addrs.Reference {
|
|
|
|
return (&NodeApplyableResource{NodeAbstractResource: n.NodeAbstractResource}).References()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nodeExpandApplyableResource) Name() string {
|
2020-05-12 09:28:33 -05:00
|
|
|
return n.NodeAbstractResource.Name() + " (expand)"
|
2020-03-20 14:45:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nodeExpandApplyableResource) DynamicExpand(ctx EvalContext) (*Graph, error) {
|
|
|
|
var g Graph
|
|
|
|
|
|
|
|
expander := ctx.InstanceExpander()
|
2020-03-27 16:16:08 -05:00
|
|
|
moduleInstances := expander.ExpandModule(n.Addr.Module)
|
|
|
|
var resources []addrs.AbsResource
|
|
|
|
for _, module := range moduleInstances {
|
|
|
|
resAddr := n.Addr.Resource.Absolute(module)
|
|
|
|
resources = append(resources, resAddr)
|
2020-03-20 14:45:39 -05:00
|
|
|
g.Add(&NodeApplyableResource{
|
|
|
|
NodeAbstractResource: n.NodeAbstractResource,
|
|
|
|
Addr: n.Addr.Resource.Absolute(module),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &g, nil
|
|
|
|
}
|
|
|
|
|
2018-09-11 16:22:25 -05:00
|
|
|
// NodeApplyableResource represents a resource that is "applyable":
|
|
|
|
// it may need to have its record in the state adjusted to match configuration.
|
|
|
|
//
|
|
|
|
// Unlike in the plan walk, this resource node does not DynamicExpand. Instead,
|
|
|
|
// it should be inserted into the same graph as any instances of the nodes
|
|
|
|
// with dependency edges ensuring that the resource is evaluated before any
|
|
|
|
// of its instances, which will turn ensure that the whole-resource record
|
|
|
|
// in the state is suitably prepared to receive any updates to instances.
|
|
|
|
type NodeApplyableResource struct {
|
|
|
|
*NodeAbstractResource
|
2020-03-20 14:45:39 -05:00
|
|
|
|
|
|
|
Addr addrs.AbsResource
|
2016-09-15 11:32:37 -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 = (*NodeApplyableResource)(nil)
|
2020-03-15 10:32:06 -05:00
|
|
|
_ GraphNodeConfigResource = (*NodeApplyableResource)(nil)
|
2020-09-24 14:30:58 -05:00
|
|
|
_ GraphNodeExecutable = (*NodeApplyableResource)(nil)
|
2018-09-11 16:22:25 -05:00
|
|
|
_ GraphNodeProviderConsumer = (*NodeApplyableResource)(nil)
|
|
|
|
_ GraphNodeAttachResourceConfig = (*NodeApplyableResource)(nil)
|
2018-09-12 18:56:06 -05:00
|
|
|
_ GraphNodeReferencer = (*NodeApplyableResource)(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
|
|
|
)
|
|
|
|
|
2020-03-23 07:56:55 -05:00
|
|
|
func (n *NodeApplyableResource) Path() addrs.ModuleInstance {
|
|
|
|
return n.Addr.Module
|
|
|
|
}
|
|
|
|
|
2018-09-12 18:56:06 -05:00
|
|
|
func (n *NodeApplyableResource) References() []*addrs.Reference {
|
|
|
|
if n.Config == nil {
|
|
|
|
log.Printf("[WARN] NodeApplyableResource %q: no configuration, so can't determine References", dag.VertexName(n))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*addrs.Reference
|
|
|
|
|
|
|
|
// Since this node type only updates resource-level metadata, we only
|
|
|
|
// need to worry about the parts of the configuration that affect
|
|
|
|
// our "each mode": the count and for_each meta-arguments.
|
|
|
|
refs, _ := lang.ReferencesInExpr(n.Config.Count)
|
|
|
|
result = append(result, refs...)
|
|
|
|
refs, _ = lang.ReferencesInExpr(n.Config.ForEach)
|
|
|
|
result = append(result, refs...)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-09-24 14:30:58 -05:00
|
|
|
// GraphNodeExecutable
|
|
|
|
func (n *NodeApplyableResource) Execute(ctx EvalContext, op walkOperation) error {
|
2020-03-09 14:43:56 -05:00
|
|
|
if n.Config == nil {
|
2018-09-11 16:22:25 -05:00
|
|
|
// Nothing to do, then.
|
2020-03-09 14:43:56 -05:00
|
|
|
log.Printf("[TRACE] NodeApplyableResource: no configuration present for %s", n.Name())
|
2020-09-24 14:30:58 -05:00
|
|
|
return nil
|
2016-10-21 00:03:48 -05:00
|
|
|
}
|
2016-09-13 19:52:09 -05:00
|
|
|
|
2020-10-01 07:12:10 -05:00
|
|
|
err := n.writeResourceState(ctx, n.Addr)
|
2020-09-25 10:15:53 -05:00
|
|
|
return err
|
2016-09-13 19:52:09 -05:00
|
|
|
}
|