terraform/terraform/graph_config_node.go

136 lines
3.4 KiB
Go
Raw Normal View History

2015-01-21 23:39:16 +01:00
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
2015-01-23 02:12:32 +01:00
"github.com/hashicorp/terraform/dag"
2015-01-21 23:39:16 +01:00
)
// graphNodeConfig is an interface that all graph nodes for the
// configuration graph need to implement in order to build the variable
// dependencies properly.
type graphNodeConfig interface {
dag.NamedVertex
2015-01-21 23:39:16 +01:00
2015-01-27 23:56:01 +01:00
// All graph nodes should be dependent on other things, and able to
// be depended on.
GraphNodeDependable
GraphNodeDependent
2015-01-21 23:39:16 +01:00
}
// GraphNodeConfigModule represents a module within the configuration graph.
type GraphNodeConfigModule struct {
Module *config.Module
Tree *module.Tree
}
func (n *GraphNodeConfigModule) DependableName() []string {
return []string{n.Name()}
}
2015-01-27 23:56:01 +01:00
func (n *GraphNodeConfigModule) DependentOn() []string {
vars := n.Module.RawConfig.Variables
result := make([]string, 0, len(vars))
for _, v := range vars {
2015-01-27 23:56:01 +01:00
if vn := varNameForVar(v); vn != "" {
result = append(result, vn)
}
}
return result
}
2015-01-27 23:56:01 +01:00
func (n *GraphNodeConfigModule) Name() string {
return fmt.Sprintf("module.%s", n.Module.Name)
}
// GraphNodeConfigProvider represents a configured provider within the
// configuration graph. These are only immediately in the graph when an
// explicit `provider` configuration block is in the configuration.
2015-01-21 23:39:16 +01:00
type GraphNodeConfigProvider struct {
Provider *config.ProviderConfig
}
func (n *GraphNodeConfigProvider) Name() string {
return fmt.Sprintf("provider.%s", n.Provider.Name)
}
2015-01-27 23:56:01 +01:00
func (n *GraphNodeConfigProvider) DependableName() []string {
return []string{n.Name()}
}
func (n *GraphNodeConfigProvider) DependentOn() []string {
vars := n.Provider.RawConfig.Variables
result := make([]string, 0, len(vars))
for _, v := range vars {
2015-01-27 23:56:01 +01:00
if vn := varNameForVar(v); vn != "" {
result = append(result, vn)
}
}
return result
2015-01-21 23:39:16 +01:00
}
2015-02-03 10:43:18 +01:00
// GraphNodeEvalable impl.
func (n *GraphNodeConfigProvider) EvalTree() EvalNode {
return ProviderEvalTree(n.Provider.Name, n.Provider.RawConfig)
2015-02-03 10:43:18 +01:00
}
// GraphNodeProvider implementation
func (n *GraphNodeConfigProvider) ProviderName() string {
return n.Provider.Name
}
2015-01-21 23:39:16 +01:00
// GraphNodeConfigResource represents a resource within the config graph.
type GraphNodeConfigResource struct {
Resource *config.Resource
}
func (n *GraphNodeConfigResource) DependableName() []string {
return []string{n.Resource.Id()}
}
2015-01-27 23:56:01 +01:00
func (n *GraphNodeConfigResource) DependentOn() []string {
result := make([]string, len(n.Resource.DependsOn),
len(n.Resource.RawCount.Variables)+
len(n.Resource.RawConfig.Variables)+
len(n.Resource.DependsOn))
copy(result, n.Resource.DependsOn)
for _, v := range n.Resource.RawCount.Variables {
2015-01-27 23:56:01 +01:00
if vn := varNameForVar(v); vn != "" {
result = append(result, vn)
}
}
for _, v := range n.Resource.RawConfig.Variables {
2015-01-27 23:56:01 +01:00
if vn := varNameForVar(v); vn != "" {
result = append(result, vn)
}
2015-01-21 23:39:16 +01:00
}
return result
2015-01-21 23:39:16 +01:00
}
2015-01-27 23:56:01 +01:00
func (n *GraphNodeConfigResource) Name() string {
return n.Resource.Id()
}
2015-02-05 00:44:23 +01:00
// GraphNodeEvalable impl.
func (n *GraphNodeConfigResource) EvalTree() EvalNode {
2015-02-05 02:23:26 +01:00
return &EvalSequence{
Nodes: []EvalNode{
&EvalValidateResource{
Provider: &EvalGetProvider{Name: n.ProvidedBy()},
Config: &EvalInterpolate{Config: n.Resource.RawConfig},
2015-02-05 02:23:26 +01:00
ProviderType: n.ProvidedBy(),
},
},
2015-02-05 00:44:23 +01:00
}
}
// GraphNodeProviderConsumer
func (n *GraphNodeConfigResource) ProvidedBy() string {
return resourceProvider(n.Resource.Type)
}