From 1152ff562b365f235582502344bd50b019d581d4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 30 Apr 2015 16:27:20 -0700 Subject: [PATCH] terraform: add variables as graph nodes (no eval yet) --- terraform/graph_config_node_test.go | 6 ++++ terraform/graph_config_node_type.go | 1 + terraform/graph_config_node_variable.go | 28 +++++++++++++++++++ terraform/graphnodeconfigtype_string.go | 4 +-- .../apply-provisioner-compute/main.tf | 2 ++ .../apply-provisioner-conninfo/main.tf | 3 ++ .../plan-module-provider-defaults-var/main.tf | 2 ++ terraform/transform_config.go | 13 ++++++++- terraform/transform_config_test.go | 2 ++ terraform/transform_provider_test.go | 4 +++ terraform/transform_resource.go | 25 +++++++++++++---- 11 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 terraform/graph_config_node_variable.go diff --git a/terraform/graph_config_node_test.go b/terraform/graph_config_node_test.go index 13848663d..22653dffd 100644 --- a/terraform/graph_config_node_test.go +++ b/terraform/graph_config_node_test.go @@ -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 diff --git a/terraform/graph_config_node_type.go b/terraform/graph_config_node_type.go index f0196096f..42dd8dc9f 100644 --- a/terraform/graph_config_node_type.go +++ b/terraform/graph_config_node_type.go @@ -12,4 +12,5 @@ const ( GraphNodeConfigTypeProvider GraphNodeConfigTypeModule GraphNodeConfigTypeOutput + GraphNodeConfigTypeVariable ) diff --git a/terraform/graph_config_node_variable.go b/terraform/graph_config_node_variable.go new file mode 100644 index 000000000..ad1bf3857 --- /dev/null +++ b/terraform/graph_config_node_variable.go @@ -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 +} diff --git a/terraform/graphnodeconfigtype_string.go b/terraform/graphnodeconfigtype_string.go index d0748979e..de24c2dcd 100644 --- a/terraform/graphnodeconfigtype_string.go +++ b/terraform/graphnodeconfigtype_string.go @@ -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)) { diff --git a/terraform/test-fixtures/apply-provisioner-compute/main.tf b/terraform/test-fixtures/apply-provisioner-compute/main.tf index e0ff5507a..e503eddd5 100644 --- a/terraform/test-fixtures/apply-provisioner-compute/main.tf +++ b/terraform/test-fixtures/apply-provisioner-compute/main.tf @@ -1,3 +1,5 @@ +variable "value" {} + resource "aws_instance" "foo" { num = "2" compute = "dynamical" diff --git a/terraform/test-fixtures/apply-provisioner-conninfo/main.tf b/terraform/test-fixtures/apply-provisioner-conninfo/main.tf index f0bfe43ab..029895fa1 100644 --- a/terraform/test-fixtures/apply-provisioner-conninfo/main.tf +++ b/terraform/test-fixtures/apply-provisioner-conninfo/main.tf @@ -1,3 +1,6 @@ +variable "pass" {} +variable "value" {} + resource "aws_instance" "foo" { num = "2" compute = "dynamical" diff --git a/terraform/test-fixtures/plan-module-provider-defaults-var/main.tf b/terraform/test-fixtures/plan-module-provider-defaults-var/main.tf index e6e3f1c29..d3c34908b 100644 --- a/terraform/test-fixtures/plan-module-provider-defaults-var/main.tf +++ b/terraform/test-fixtures/plan-module-provider-defaults-var/main.tf @@ -7,3 +7,5 @@ provider "aws" { } resource "aws_instance" "foo" {} + +variable "foo" {} diff --git a/terraform/transform_config.go b/terraform/transform_config.go index 3b115688b..44cc9f57c 100644 --- a/terraform/transform_config.go +++ b/terraform/transform_config.go @@ -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 "" } diff --git a/terraform/transform_config_test.go b/terraform/transform_config_test.go index 75570d3f6..4ba88a358 100644 --- a/terraform/transform_config_test.go +++ b/terraform/transform_config_test.go @@ -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 = ` diff --git a/terraform/transform_provider_test.go b/terraform/transform_provider_test.go index 719cef75c..fdf764c3f 100644 --- a/terraform/transform_provider_test.go +++ b/terraform/transform_provider_test.go @@ -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 ` diff --git a/terraform/transform_resource.go b/terraform/transform_resource.go index 498fe20ac..0289e1a1d 100644 --- a/terraform/transform_resource.go +++ b/terraform/transform_resource.go @@ -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{