2015-02-09 12:14:09 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-08 19:29:23 -05:00
|
|
|
"log"
|
2015-02-09 12:14:09 -06:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
2020-10-28 12:47:04 -05:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2015-02-09 12:14:09 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// GraphNodeProvisioner is an interface that nodes that can be a provisioner
|
|
|
|
// must implement. The ProvisionerName returned is the name of the provisioner
|
|
|
|
// they satisfy.
|
|
|
|
type GraphNodeProvisioner interface {
|
|
|
|
ProvisionerName() string
|
|
|
|
}
|
|
|
|
|
2015-06-19 14:52:50 -05:00
|
|
|
// GraphNodeCloseProvisioner is an interface that nodes that can be a close
|
|
|
|
// provisioner must implement. The CloseProvisionerName returned is the name
|
|
|
|
// of the provisioner they satisfy.
|
|
|
|
type GraphNodeCloseProvisioner interface {
|
|
|
|
CloseProvisionerName() string
|
|
|
|
}
|
|
|
|
|
2015-02-09 12:14:09 -06:00
|
|
|
// GraphNodeProvisionerConsumer is an interface that nodes that require
|
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
|
|
|
// a provisioner must implement. ProvisionedBy must return the names of the
|
|
|
|
// provisioners to use.
|
2015-02-09 12:14:09 -06:00
|
|
|
type GraphNodeProvisionerConsumer interface {
|
|
|
|
ProvisionedBy() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProvisionerTransformer is a GraphTransformer that maps resources to
|
|
|
|
// provisioners within the graph. This will error if there are any resources
|
|
|
|
// that don't map to proper resources.
|
|
|
|
type ProvisionerTransformer struct{}
|
|
|
|
|
|
|
|
func (t *ProvisionerTransformer) Transform(g *Graph) error {
|
|
|
|
// Go through the other nodes and match them to provisioners they need
|
|
|
|
var err error
|
|
|
|
m := provisionerVertexMap(g)
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if pv, ok := v.(GraphNodeProvisionerConsumer); ok {
|
2016-02-04 06:46:45 -06:00
|
|
|
for _, p := range pv.ProvisionedBy() {
|
2019-08-21 18:06:25 -05:00
|
|
|
if m[p] == nil {
|
2015-02-09 12:14:09 -06:00
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"%s: provisioner %s couldn't be found",
|
2016-02-04 06:46:45 -06:00
|
|
|
dag.VertexName(v), p))
|
2015-02-09 12:14:09 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-21 18:06:25 -05:00
|
|
|
log.Printf("[TRACE] ProvisionerTransformer: %s is provisioned by %s (%q)", dag.VertexName(v), p, dag.VertexName(m[p]))
|
|
|
|
g.Connect(dag.BasicEdge(v, m[p]))
|
2015-02-09 12:14:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MissingProvisionerTransformer is a GraphTransformer that adds nodes
|
2016-02-04 06:46:45 -06:00
|
|
|
// for missing provisioners into the graph.
|
2015-02-09 12:14:09 -06:00
|
|
|
type MissingProvisionerTransformer struct {
|
|
|
|
// Provisioners is the list of provisioners we support.
|
|
|
|
Provisioners []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *MissingProvisionerTransformer) Transform(g *Graph) error {
|
2016-01-28 09:14:15 -06:00
|
|
|
// Create a set of our supported provisioners
|
|
|
|
supported := make(map[string]struct{}, len(t.Provisioners))
|
|
|
|
for _, v := range t.Provisioners {
|
|
|
|
supported[v] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the map of provisioners we already have in our graph
|
2015-02-09 12:14:09 -06:00
|
|
|
m := provisionerVertexMap(g)
|
2016-01-28 09:14:15 -06:00
|
|
|
|
|
|
|
// Go through all the provisioner consumers and make sure we add
|
|
|
|
// that provisioner if it is missing.
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
pv, ok := v.(GraphNodeProvisionerConsumer)
|
|
|
|
if !ok {
|
2015-02-09 12:14:09 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-28 09:14:15 -06:00
|
|
|
for _, p := range pv.ProvisionedBy() {
|
2019-08-21 18:06:25 -05:00
|
|
|
if _, ok := m[p]; ok {
|
2016-01-28 09:14:15 -06:00
|
|
|
// This provisioner already exists as a configure node
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := supported[p]; !ok {
|
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
|
|
|
// If we don't support the provisioner type, we skip it.
|
2016-01-28 09:14:15 -06:00
|
|
|
// Validation later will catch this as an error.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-11-03 12:25:11 -05:00
|
|
|
// Build the vertex
|
2017-01-26 23:02:55 -06:00
|
|
|
var newV dag.Vertex = &NodeProvisioner{
|
|
|
|
NameValue: p,
|
|
|
|
}
|
2016-11-03 12:25:11 -05:00
|
|
|
|
2016-02-04 06:46:45 -06:00
|
|
|
// Add the missing provisioner node to the graph
|
2019-08-21 18:06:25 -05:00
|
|
|
m[p] = g.Add(newV)
|
|
|
|
log.Printf("[TRACE] MissingProviderTransformer: added implicit provisioner %s, first implied by %s", p, dag.VertexName(v))
|
2016-01-28 09:14:15 -06:00
|
|
|
}
|
2015-02-09 12:14:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 06:46:45 -06:00
|
|
|
// CloseProvisionerTransformer is a GraphTransformer that adds nodes to the
|
|
|
|
// graph that will close open provisioner connections that aren't needed
|
|
|
|
// anymore. A provisioner connection is not needed anymore once all depended
|
|
|
|
// resources in the graph are evaluated.
|
|
|
|
type CloseProvisionerTransformer struct{}
|
2015-02-09 12:14:09 -06:00
|
|
|
|
2016-02-04 06:46:45 -06:00
|
|
|
func (t *CloseProvisionerTransformer) Transform(g *Graph) error {
|
|
|
|
m := closeProvisionerVertexMap(g)
|
2015-02-09 12:14:09 -06:00
|
|
|
for _, v := range g.Vertices() {
|
2016-02-04 06:46:45 -06:00
|
|
|
if pv, ok := v.(GraphNodeProvisionerConsumer); ok {
|
|
|
|
for _, p := range pv.ProvisionedBy() {
|
|
|
|
source := m[p]
|
|
|
|
|
|
|
|
if source == nil {
|
|
|
|
// Create a new graphNodeCloseProvisioner and add it to the graph
|
|
|
|
source = &graphNodeCloseProvisioner{ProvisionerNameValue: p}
|
|
|
|
g.Add(source)
|
2015-02-09 12:14:09 -06:00
|
|
|
|
2016-02-04 06:46:45 -06:00
|
|
|
// Make sure we also add the new graphNodeCloseProvisioner to the map
|
|
|
|
// so we don't create and add any duplicate graphNodeCloseProvisioners.
|
|
|
|
m[p] = source
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Connect(dag.BasicEdge(source, v))
|
|
|
|
}
|
2015-02-09 12:14:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-19 14:52:50 -05:00
|
|
|
func provisionerVertexMap(g *Graph) map[string]dag.Vertex {
|
|
|
|
m := make(map[string]dag.Vertex)
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if pv, ok := v.(GraphNodeProvisioner); ok {
|
2019-08-21 18:06:25 -05:00
|
|
|
m[pv.ProvisionerName()] = v
|
2015-06-19 14:52:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeProvisionerVertexMap(g *Graph) map[string]dag.Vertex {
|
|
|
|
m := make(map[string]dag.Vertex)
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if pv, ok := v.(GraphNodeCloseProvisioner); ok {
|
|
|
|
m[pv.CloseProvisionerName()] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type graphNodeCloseProvisioner struct {
|
|
|
|
ProvisionerNameValue string
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:47:04 -05:00
|
|
|
var _ GraphNodeExecutable = (*graphNodeCloseProvisioner)(nil)
|
|
|
|
|
2015-06-19 14:52:50 -05:00
|
|
|
func (n *graphNodeCloseProvisioner) Name() string {
|
|
|
|
return fmt.Sprintf("provisioner.%s (close)", n.ProvisionerNameValue)
|
|
|
|
}
|
|
|
|
|
2020-09-14 07:43:14 -05:00
|
|
|
// GraphNodeExecutable impl.
|
2020-10-28 12:47:04 -05:00
|
|
|
func (n *graphNodeCloseProvisioner) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {
|
|
|
|
return diags.Append(ctx.CloseProvisioner(n.ProvisionerNameValue))
|
2015-06-19 14:52:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeCloseProvisioner) CloseProvisionerName() string {
|
|
|
|
return n.ProvisionerNameValue
|
|
|
|
}
|