terraform: add variables as graph nodes (no eval yet)

This commit is contained in:
Mitchell Hashimoto 2015-04-30 16:27:20 -07:00
parent a8165fea11
commit 1152ff562b
11 changed files with 81 additions and 9 deletions

View File

@ -141,6 +141,12 @@ func TestGraphNodeConfigResource_ProvisionedBy(t *testing.T) {
}
}
func TestGraphNodeConfigVariable_impl(t *testing.T) {
var _ dag.Vertex = new(GraphNodeConfigVariable)
var _ dag.NamedVertex = new(GraphNodeConfigVariable)
var _ graphNodeConfig = new(GraphNodeConfigVariable)
}
const testGraphNodeModuleExpandStr = `
aws_instance.bar
aws_instance.foo

View File

@ -12,4 +12,5 @@ const (
GraphNodeConfigTypeProvider
GraphNodeConfigTypeModule
GraphNodeConfigTypeOutput
GraphNodeConfigTypeVariable
)

View File

@ -0,0 +1,28 @@
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/config"
)
// GraphNodeConfigVariable represents a Variable in the config.
type GraphNodeConfigVariable struct {
Variable *config.Variable
}
func (n *GraphNodeConfigVariable) Name() string {
return fmt.Sprintf("var.%s", n.Variable.Name)
}
func (n *GraphNodeConfigVariable) ConfigType() GraphNodeConfigType {
return GraphNodeConfigTypeVariable
}
func (n *GraphNodeConfigVariable) DependableName() []string {
return []string{n.Name()}
}
func (n *GraphNodeConfigVariable) DependentOn() []string {
return nil
}

View File

@ -4,9 +4,9 @@ package terraform
import "fmt"
const _GraphNodeConfigType_name = "GraphNodeConfigTypeInvalidGraphNodeConfigTypeResourceGraphNodeConfigTypeProviderGraphNodeConfigTypeModuleGraphNodeConfigTypeOutput"
const _GraphNodeConfigType_name = "GraphNodeConfigTypeInvalidGraphNodeConfigTypeResourceGraphNodeConfigTypeProviderGraphNodeConfigTypeModuleGraphNodeConfigTypeOutputGraphNodeConfigTypeVariable"
var _GraphNodeConfigType_index = [...]uint8{0, 26, 53, 80, 105, 130}
var _GraphNodeConfigType_index = [...]uint8{0, 26, 53, 80, 105, 130, 157}
func (i GraphNodeConfigType) String() string {
if i < 0 || i+1 >= GraphNodeConfigType(len(_GraphNodeConfigType_index)) {

View File

@ -1,3 +1,5 @@
variable "value" {}
resource "aws_instance" "foo" {
num = "2"
compute = "dynamical"

View File

@ -1,3 +1,6 @@
variable "pass" {}
variable "value" {}
resource "aws_instance" "foo" {
num = "2"
compute = "dynamical"

View File

@ -7,3 +7,5 @@ provider "aws" {
}
resource "aws_instance" "foo" {}
variable "foo" {}

View File

@ -37,7 +37,16 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
// Create the node list we'll use for the graph
nodes := make([]graphNodeConfig, 0,
(len(config.ProviderConfigs)+len(config.Modules)+len(config.Resources))*2)
(len(config.Variables)+
len(config.ProviderConfigs)+
len(config.Modules)+
len(config.Resources)+
len(config.Outputs))*2)
// Write all the variables out
for _, v := range config.Variables {
nodes = append(nodes, &GraphNodeConfigVariable{Variable: v})
}
// Write all the provider configs out
for _, pc := range config.ProviderConfigs {
@ -99,6 +108,8 @@ func varNameForVar(raw config.InterpolatedVariable) string {
return fmt.Sprintf("module.%s", v.Name)
case *config.ResourceVariable:
return v.ResourceId()
case *config.UserVariable:
return fmt.Sprintf("var.%s", v.Name)
default:
return ""
}

View File

@ -111,12 +111,14 @@ func TestConfigTransformer_errMissingDeps(t *testing.T) {
const testGraphBasicStr = `
aws_instance.web
aws_security_group.firewall
var.foo
aws_load_balancer.weblb
aws_instance.web
aws_security_group.firewall
openstack_floating_ip.random
provider.aws
openstack_floating_ip.random
var.foo
`
const testGraphDependsOnStr = `

View File

@ -218,7 +218,9 @@ provider.foo
const testTransformDisableProviderBasicStr = `
module.child
provider.aws (disabled)
var.foo
provider.aws (disabled)
var.foo
`
const testTransformDisableProviderKeepStr = `
@ -226,5 +228,7 @@ aws_instance.foo
provider.aws
module.child
provider.aws
var.foo
provider.aws
var.foo
`

View File

@ -2,6 +2,7 @@ package terraform
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/dag"
@ -169,6 +170,18 @@ func (n *graphNodeExpandedResource) ProvidedBy() []string {
return []string{resourceProvider(n.Resource.Type, n.Resource.Provider)}
}
func (n *graphNodeExpandedResource) StateDependencies() []string {
depsRaw := n.DependentOn()
deps := make([]string, 0, len(depsRaw))
for _, d := range depsRaw {
if !strings.HasPrefix(d, "var.") {
deps = append(deps, d)
}
}
return deps
}
// GraphNodeEvalable impl.
func (n *graphNodeExpandedResource) EvalTree() EvalNode {
var diff *InstanceDiff
@ -257,7 +270,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
Name: n.stateId(),
ResourceType: n.Resource.Type,
Provider: n.Resource.Provider,
Dependencies: n.DependentOn(),
Dependencies: n.StateDependencies(),
State: &state,
},
},
@ -298,7 +311,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
Name: n.stateId(),
ResourceType: n.Resource.Type,
Provider: n.Resource.Provider,
Dependencies: n.DependentOn(),
Dependencies: n.StateDependencies(),
State: &state,
},
&EvalDiffTainted{
@ -445,7 +458,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
Name: n.stateId(),
ResourceType: n.Resource.Type,
Provider: n.Resource.Provider,
Dependencies: n.DependentOn(),
Dependencies: n.StateDependencies(),
State: &state,
},
&EvalApplyProvisioners{
@ -489,7 +502,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
Name: n.stateId(),
ResourceType: n.Resource.Type,
Provider: n.Resource.Provider,
Dependencies: n.DependentOn(),
Dependencies: n.StateDependencies(),
State: &state,
Index: -1,
},
@ -507,7 +520,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
Name: n.stateId(),
ResourceType: n.Resource.Type,
Provider: n.Resource.Provider,
Dependencies: n.DependentOn(),
Dependencies: n.StateDependencies(),
State: &state,
},
},
@ -618,7 +631,7 @@ func (n *graphNodeExpandedResourceDestroy) EvalTree() EvalNode {
Name: n.stateId(),
ResourceType: n.Resource.Type,
Provider: n.Resource.Provider,
Dependencies: n.DependentOn(),
Dependencies: n.StateDependencies(),
State: &state,
},
&EvalApplyPost{