2015-01-28 06:48:46 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-19 01:19:43 +02:00
|
|
|
"log"
|
2015-06-25 05:58:52 +02:00
|
|
|
"strings"
|
2015-01-28 06:48:46 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
2015-01-28 07:14:01 +01:00
|
|
|
// GraphNodeProvider is an interface that nodes that can be a provider
|
2017-11-01 23:34:18 +01:00
|
|
|
// must implement.
|
|
|
|
// ProviderName returns the name of the provider this satisfies.
|
|
|
|
// Name returns the full name of the provider in the config.
|
2015-01-28 07:14:01 +01:00
|
|
|
type GraphNodeProvider interface {
|
|
|
|
ProviderName() string
|
2017-11-01 23:34:18 +01:00
|
|
|
Name() string
|
2015-01-28 07:14:01 +01:00
|
|
|
}
|
|
|
|
|
2015-06-19 21:52:50 +02:00
|
|
|
// GraphNodeCloseProvider is an interface that nodes that can be a close
|
|
|
|
// provider must implement. The CloseProviderName returned is the name of
|
|
|
|
// the provider they satisfy.
|
|
|
|
type GraphNodeCloseProvider interface {
|
|
|
|
CloseProviderName() string
|
|
|
|
}
|
|
|
|
|
2015-01-28 07:14:01 +01:00
|
|
|
// GraphNodeProviderConsumer is an interface that nodes that require
|
|
|
|
// a provider must implement. ProvidedBy must return the name of the provider
|
|
|
|
// to use.
|
|
|
|
type GraphNodeProviderConsumer interface {
|
2017-10-31 20:58:24 +01:00
|
|
|
// TODO: make this return s string instead of a []string
|
2015-02-10 08:32:28 +01:00
|
|
|
ProvidedBy() []string
|
2017-11-01 23:34:18 +01:00
|
|
|
// Set the resolved provider address for this resource.
|
|
|
|
SetProvider(string)
|
2015-01-28 07:14:01 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 06:48:46 +01:00
|
|
|
// ProviderTransformer is a GraphTransformer that maps resources to
|
|
|
|
// providers within the graph. This will error if there are any resources
|
|
|
|
// that don't map to proper resources.
|
|
|
|
type ProviderTransformer struct{}
|
|
|
|
|
|
|
|
func (t *ProviderTransformer) Transform(g *Graph) error {
|
|
|
|
// Go through the other nodes and match them to providers they need
|
|
|
|
var err error
|
2015-01-28 07:14:01 +01:00
|
|
|
m := providerVertexMap(g)
|
2015-01-28 06:48:46 +01:00
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if pv, ok := v.(GraphNodeProviderConsumer); ok {
|
2017-10-31 20:58:24 +01:00
|
|
|
p := pv.ProvidedBy()[0]
|
|
|
|
|
|
|
|
key := providerMapKey(p, pv)
|
|
|
|
target := m[key]
|
|
|
|
|
|
|
|
sp, ok := pv.(GraphNodeSubPath)
|
|
|
|
if !ok && target == nil {
|
|
|
|
// no target, and no path to walk up
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"%s: provider %s couldn't be found",
|
|
|
|
dag.VertexName(v), p))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we don't have a provider at this level, walk up the path looking for one
|
|
|
|
for i := 1; target == nil; i++ {
|
2017-11-01 23:34:18 +01:00
|
|
|
path := normalizeModulePath(sp.Path())
|
|
|
|
if len(path) < i {
|
2017-10-31 20:58:24 +01:00
|
|
|
break
|
2015-02-10 08:32:28 +01:00
|
|
|
}
|
2015-01-28 06:48:46 +01:00
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
key = ResolveProviderName(p, path[:len(path)-i])
|
2017-10-31 20:58:24 +01:00
|
|
|
target = m[key]
|
2017-11-01 23:34:18 +01:00
|
|
|
if target != nil {
|
|
|
|
break
|
|
|
|
}
|
2017-10-31 20:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if target == nil {
|
|
|
|
err = multierror.Append(err, fmt.Errorf(
|
|
|
|
"%s: provider %s couldn't be found",
|
|
|
|
dag.VertexName(v), p))
|
|
|
|
break
|
2015-02-10 08:32:28 +01:00
|
|
|
}
|
2017-10-31 20:58:24 +01:00
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
pv.SetProvider(key)
|
2017-10-31 20:58:24 +01:00
|
|
|
g.Connect(dag.BasicEdge(v, target))
|
2015-01-28 06:48:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-19 21:52:50 +02:00
|
|
|
// CloseProviderTransformer is a GraphTransformer that adds nodes to the
|
|
|
|
// graph that will close open provider connections that aren't needed anymore.
|
|
|
|
// A provider connection is not needed anymore once all depended resources
|
|
|
|
// in the graph are evaluated.
|
|
|
|
type CloseProviderTransformer struct{}
|
|
|
|
|
|
|
|
func (t *CloseProviderTransformer) Transform(g *Graph) error {
|
2015-06-26 21:21:03 +02:00
|
|
|
pm := providerVertexMap(g)
|
2017-11-01 23:34:18 +01:00
|
|
|
cpm := make(map[string]*graphNodeCloseProvider)
|
2015-06-26 21:21:03 +02:00
|
|
|
var err error
|
2015-06-19 21:52:50 +02:00
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
for _, v := range pm {
|
|
|
|
p := v.(GraphNodeProvider)
|
|
|
|
|
|
|
|
// get the close provider of this type if we alread created it
|
|
|
|
closer := cpm[p.ProviderName()]
|
|
|
|
|
|
|
|
if closer == nil {
|
|
|
|
// create a closer for this provider type
|
|
|
|
closer = &graphNodeCloseProvider{ProviderNameValue: p.ProviderName()}
|
|
|
|
g.Add(closer)
|
|
|
|
cpm[p.ProviderName()] = closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close node depends on the provider itself
|
|
|
|
// this is added unconditionally, so it will connect to all instances
|
|
|
|
// of the provider. Extra edges will be removed by transitive
|
|
|
|
// reduction.
|
|
|
|
g.Connect(dag.BasicEdge(closer, p))
|
|
|
|
|
|
|
|
// connect all the provider's resources to the close node
|
|
|
|
for _, s := range g.UpEdges(p).List() {
|
|
|
|
if _, ok := s.(GraphNodeResource); ok {
|
|
|
|
g.Connect(dag.BasicEdge(closer, s))
|
2015-06-19 21:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 21:21:03 +02:00
|
|
|
return err
|
2015-06-19 21:52:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-31 18:13:24 +01:00
|
|
|
// MissingProviderTransformer is a GraphTransformer that adds nodes for all
|
|
|
|
// required providers into the graph. Specifically, it creates provider
|
|
|
|
// configuration nodes for all the providers that we support. These are pruned
|
|
|
|
// later during an optimization pass.
|
2015-01-28 07:14:01 +01:00
|
|
|
type MissingProviderTransformer struct {
|
|
|
|
// Providers is the list of providers we support.
|
|
|
|
Providers []string
|
2016-09-15 08:58:16 +02:00
|
|
|
|
2016-12-11 02:11:24 +01:00
|
|
|
// AllowAny will not check that a provider is supported before adding
|
|
|
|
// it to the graph.
|
|
|
|
AllowAny bool
|
|
|
|
|
2016-12-04 00:27:38 +01:00
|
|
|
// Concrete, if set, overrides how the providers are made.
|
|
|
|
Concrete ConcreteProviderNodeFunc
|
2015-01-28 06:48:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 07:14:01 +01:00
|
|
|
func (t *MissingProviderTransformer) Transform(g *Graph) error {
|
2016-09-15 08:58:16 +02:00
|
|
|
// Initialize factory
|
2016-12-04 00:27:38 +01:00
|
|
|
if t.Concrete == nil {
|
|
|
|
t.Concrete = func(a *NodeAbstractProvider) dag.Vertex {
|
2017-01-27 05:58:22 +01:00
|
|
|
return a
|
2016-09-15 08:58:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 05:58:52 +02:00
|
|
|
// Create a set of our supported providers
|
|
|
|
supported := make(map[string]struct{}, len(t.Providers))
|
|
|
|
for _, v := range t.Providers {
|
|
|
|
supported[v] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the map of providers we already have in our graph
|
2015-01-28 07:14:01 +01:00
|
|
|
m := providerVertexMap(g)
|
2015-06-25 05:58:52 +02:00
|
|
|
|
|
|
|
// Go through all the provider consumers and make sure we add
|
2016-04-30 07:09:34 +02:00
|
|
|
// that provider if it is missing. We use a for loop here instead
|
|
|
|
// of "range" since we'll modify check as we go to add more to check.
|
|
|
|
check := g.Vertices()
|
|
|
|
for i := 0; i < len(check); i++ {
|
|
|
|
v := check[i]
|
|
|
|
|
2015-06-25 05:58:52 +02:00
|
|
|
pv, ok := v.(GraphNodeProviderConsumer)
|
|
|
|
if !ok {
|
2015-01-28 07:14:01 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-30 07:09:34 +02:00
|
|
|
// If this node has a subpath, then we use that as a prefix
|
|
|
|
// into our map to check for an existing provider.
|
|
|
|
var path []string
|
|
|
|
if sp, ok := pv.(GraphNodeSubPath); ok {
|
|
|
|
raw := normalizeModulePath(sp.Path())
|
|
|
|
if len(raw) > len(rootModulePath) {
|
|
|
|
path = raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
p := pv.ProvidedBy()[0]
|
|
|
|
// always add the parent nodes to check, since configured providers
|
|
|
|
// may have already been added for modules.
|
|
|
|
if len(path) > 0 {
|
|
|
|
// We'll need the parent provider as well, so let's
|
|
|
|
// add a dummy node to check to make sure that we add
|
|
|
|
// that parent provider.
|
|
|
|
check = append(check, &graphNodeProviderConsumerDummy{
|
|
|
|
ProviderValue: p,
|
|
|
|
PathValue: path[:len(path)-1],
|
|
|
|
})
|
|
|
|
}
|
2017-10-31 20:58:24 +01:00
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
key := providerMapKey(p, pv)
|
|
|
|
if _, ok := m[key]; ok {
|
|
|
|
// This provider already exists as a configure node
|
|
|
|
continue
|
|
|
|
}
|
2015-06-25 05:58:52 +02:00
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
// If the provider has an alias in it, we just want the type
|
|
|
|
// TODO: jbardin -- stop adding aliased providers altogether
|
|
|
|
ptype := p
|
|
|
|
if idx := strings.IndexRune(p, '.'); idx != -1 {
|
|
|
|
ptype = p[:idx]
|
|
|
|
}
|
2015-06-25 05:58:52 +02:00
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
if !t.AllowAny {
|
|
|
|
if _, ok := supported[ptype]; !ok {
|
|
|
|
// If we don't support the provider type, skip it.
|
|
|
|
// Validation later will catch this as an error.
|
|
|
|
continue
|
2015-06-25 05:58:52 +02:00
|
|
|
}
|
2017-11-01 23:34:18 +01:00
|
|
|
}
|
2015-06-25 05:58:52 +02:00
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
// Add the missing provider node to the graph
|
|
|
|
provider := t.Concrete(&NodeAbstractProvider{
|
|
|
|
NameValue: p,
|
|
|
|
PathValue: path,
|
|
|
|
}).(dag.Vertex)
|
2017-10-31 20:58:24 +01:00
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
m[key] = g.Add(provider)
|
2015-01-28 07:14:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-14 23:43:14 +02:00
|
|
|
// ParentProviderTransformer connects provider nodes to their parents.
|
|
|
|
//
|
|
|
|
// This works by finding nodes that are both GraphNodeProviders and
|
|
|
|
// GraphNodeSubPath. It then connects the providers to their parent
|
|
|
|
// path.
|
|
|
|
type ParentProviderTransformer struct{}
|
|
|
|
|
|
|
|
func (t *ParentProviderTransformer) Transform(g *Graph) error {
|
|
|
|
// Make a mapping of path to dag.Vertex, where path is: "path.name"
|
|
|
|
m := make(map[string]dag.Vertex)
|
|
|
|
|
|
|
|
// Also create a map that maps a provider to its parent
|
|
|
|
parentMap := make(map[dag.Vertex]string)
|
|
|
|
for _, raw := range g.Vertices() {
|
|
|
|
// If it is the flat version, then make it the non-flat version.
|
|
|
|
// We eventually want to get rid of the flat version entirely so
|
|
|
|
// this is a stop-gap while it still exists.
|
|
|
|
var v dag.Vertex = raw
|
|
|
|
|
|
|
|
// Only care about providers
|
|
|
|
pn, ok := v.(GraphNodeProvider)
|
|
|
|
if !ok || pn.ProviderName() == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also require a subpath, if there is no subpath then we
|
|
|
|
// just totally ignore it. The expectation of this transform is
|
|
|
|
// that it is used with a graph builder that is already flattened.
|
|
|
|
var path []string
|
|
|
|
if pn, ok := raw.(GraphNodeSubPath); ok {
|
|
|
|
path = pn.Path()
|
|
|
|
}
|
|
|
|
path = normalizeModulePath(path)
|
|
|
|
|
2017-11-01 23:34:18 +01:00
|
|
|
key := ResolveProviderName(pn.ProviderName(), path)
|
2016-09-14 23:55:02 +02:00
|
|
|
m[key] = raw
|
2016-09-14 23:43:14 +02:00
|
|
|
|
|
|
|
// Determine the parent if we're non-root. This is length 1 since
|
|
|
|
// the 0 index should be "root" since we normalize above.
|
|
|
|
if len(path) > 1 {
|
|
|
|
path = path[:len(path)-1]
|
2017-11-01 23:34:18 +01:00
|
|
|
key := ResolveProviderName(pn.ProviderName(), path)
|
2016-09-14 23:43:14 +02:00
|
|
|
parentMap[raw] = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect!
|
|
|
|
for v, key := range parentMap {
|
|
|
|
if parent, ok := m[key]; ok {
|
|
|
|
g.Connect(dag.BasicEdge(v, parent))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-30 22:03:12 +01:00
|
|
|
// PruneProviderTransformer is a GraphTransformer that prunes all the
|
|
|
|
// providers that aren't needed from the graph. A provider is unneeded if
|
|
|
|
// no resource or module is using that provider.
|
|
|
|
type PruneProviderTransformer struct{}
|
|
|
|
|
|
|
|
func (t *PruneProviderTransformer) Transform(g *Graph) error {
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
// We only care about the providers
|
2015-05-02 00:18:40 +02:00
|
|
|
if pn, ok := v.(GraphNodeProvider); !ok || pn.ProviderName() == "" {
|
2015-01-30 22:03:12 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Does anything depend on this? If not, then prune it.
|
|
|
|
if s := g.UpEdges(v); s.Len() == 0 {
|
2016-04-19 01:19:43 +02:00
|
|
|
if nv, ok := v.(dag.NamedVertex); ok {
|
|
|
|
log.Printf("[DEBUG] Pruning provider with no dependencies: %s", nv.Name())
|
|
|
|
}
|
2015-01-30 22:03:12 +01:00
|
|
|
g.Remove(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-30 08:25:11 +02:00
|
|
|
// providerMapKey is a helper that gives us the key to use for the
|
|
|
|
// maps returned by things such as providerVertexMap.
|
|
|
|
func providerMapKey(k string, v dag.Vertex) string {
|
2017-11-01 23:34:18 +01:00
|
|
|
// we create a dummy provider to
|
|
|
|
var path []string
|
2016-04-30 08:25:11 +02:00
|
|
|
if sp, ok := v.(GraphNodeSubPath); ok {
|
2017-11-01 23:34:18 +01:00
|
|
|
path = normalizeModulePath(sp.Path())
|
2016-04-30 08:25:11 +02:00
|
|
|
}
|
2017-11-01 23:34:18 +01:00
|
|
|
return ResolveProviderName(k, path)
|
2016-04-30 08:25:11 +02:00
|
|
|
}
|
|
|
|
|
2015-06-19 21:52:50 +02:00
|
|
|
func providerVertexMap(g *Graph) map[string]dag.Vertex {
|
|
|
|
m := make(map[string]dag.Vertex)
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if pv, ok := v.(GraphNodeProvider); ok {
|
2017-11-01 23:34:18 +01:00
|
|
|
m[pv.Name()] = v
|
2015-06-19 21:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeProviderVertexMap(g *Graph) map[string]dag.Vertex {
|
|
|
|
m := make(map[string]dag.Vertex)
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if pv, ok := v.(GraphNodeCloseProvider); ok {
|
|
|
|
m[pv.CloseProviderName()] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type graphNodeCloseProvider struct {
|
|
|
|
ProviderNameValue string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeCloseProvider) Name() string {
|
|
|
|
return fmt.Sprintf("provider.%s (close)", n.ProviderNameValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable impl.
|
|
|
|
func (n *graphNodeCloseProvider) EvalTree() EvalNode {
|
|
|
|
return CloseProviderEvalTree(n.ProviderNameValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDependable impl.
|
|
|
|
func (n *graphNodeCloseProvider) DependableName() []string {
|
|
|
|
return []string{n.Name()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeCloseProvider) CloseProviderName() string {
|
|
|
|
return n.ProviderNameValue
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDotter impl.
|
2016-11-10 15:28:42 +01:00
|
|
|
func (n *graphNodeCloseProvider) DotNode(name string, opts *dag.DotOpts) *dag.DotNode {
|
2015-06-25 00:36:45 +02:00
|
|
|
if !opts.Verbose {
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-10 15:28:42 +01:00
|
|
|
return &dag.DotNode{
|
|
|
|
Name: name,
|
|
|
|
Attrs: map[string]string{
|
|
|
|
"label": n.Name(),
|
|
|
|
"shape": "diamond",
|
|
|
|
},
|
|
|
|
}
|
2015-06-19 21:52:50 +02:00
|
|
|
}
|
|
|
|
|
2017-02-17 18:27:47 +01:00
|
|
|
// RemovableIfNotTargeted
|
|
|
|
func (n *graphNodeCloseProvider) RemoveIfNotTargeted() bool {
|
|
|
|
// We need to add this so that this node will be removed if
|
|
|
|
// it isn't targeted or a dependency of a target.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-04-30 07:59:09 +02:00
|
|
|
// graphNodeProviderConsumerDummy is a struct that never enters the real
|
|
|
|
// graph (though it could to no ill effect). It implements
|
|
|
|
// GraphNodeProviderConsumer and GraphNodeSubpath as a way to force
|
|
|
|
// certain transformations.
|
|
|
|
type graphNodeProviderConsumerDummy struct {
|
|
|
|
ProviderValue string
|
|
|
|
PathValue []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeProviderConsumerDummy) Path() []string {
|
|
|
|
return n.PathValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeProviderConsumerDummy) ProvidedBy() []string {
|
|
|
|
return []string{n.ProviderValue}
|
|
|
|
}
|
2017-11-01 23:34:18 +01:00
|
|
|
|
|
|
|
func (n *graphNodeProviderConsumerDummy) SetProvider(string) {}
|