terraform: add variables as graph nodes (no eval yet)
This commit is contained in:
parent
a8165fea11
commit
1152ff562b
|
@ -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 = `
|
const testGraphNodeModuleExpandStr = `
|
||||||
aws_instance.bar
|
aws_instance.bar
|
||||||
aws_instance.foo
|
aws_instance.foo
|
||||||
|
|
|
@ -12,4 +12,5 @@ const (
|
||||||
GraphNodeConfigTypeProvider
|
GraphNodeConfigTypeProvider
|
||||||
GraphNodeConfigTypeModule
|
GraphNodeConfigTypeModule
|
||||||
GraphNodeConfigTypeOutput
|
GraphNodeConfigTypeOutput
|
||||||
|
GraphNodeConfigTypeVariable
|
||||||
)
|
)
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -4,9 +4,9 @@ package terraform
|
||||||
|
|
||||||
import "fmt"
|
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 {
|
func (i GraphNodeConfigType) String() string {
|
||||||
if i < 0 || i+1 >= GraphNodeConfigType(len(_GraphNodeConfigType_index)) {
|
if i < 0 || i+1 >= GraphNodeConfigType(len(_GraphNodeConfigType_index)) {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
variable "value" {}
|
||||||
|
|
||||||
resource "aws_instance" "foo" {
|
resource "aws_instance" "foo" {
|
||||||
num = "2"
|
num = "2"
|
||||||
compute = "dynamical"
|
compute = "dynamical"
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
variable "pass" {}
|
||||||
|
variable "value" {}
|
||||||
|
|
||||||
resource "aws_instance" "foo" {
|
resource "aws_instance" "foo" {
|
||||||
num = "2"
|
num = "2"
|
||||||
compute = "dynamical"
|
compute = "dynamical"
|
||||||
|
|
|
@ -7,3 +7,5 @@ provider "aws" {
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_instance" "foo" {}
|
resource "aws_instance" "foo" {}
|
||||||
|
|
||||||
|
variable "foo" {}
|
||||||
|
|
|
@ -37,7 +37,16 @@ func (t *ConfigTransformer) Transform(g *Graph) error {
|
||||||
|
|
||||||
// Create the node list we'll use for the graph
|
// Create the node list we'll use for the graph
|
||||||
nodes := make([]graphNodeConfig, 0,
|
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
|
// Write all the provider configs out
|
||||||
for _, pc := range config.ProviderConfigs {
|
for _, pc := range config.ProviderConfigs {
|
||||||
|
@ -99,6 +108,8 @@ func varNameForVar(raw config.InterpolatedVariable) string {
|
||||||
return fmt.Sprintf("module.%s", v.Name)
|
return fmt.Sprintf("module.%s", v.Name)
|
||||||
case *config.ResourceVariable:
|
case *config.ResourceVariable:
|
||||||
return v.ResourceId()
|
return v.ResourceId()
|
||||||
|
case *config.UserVariable:
|
||||||
|
return fmt.Sprintf("var.%s", v.Name)
|
||||||
default:
|
default:
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,12 +111,14 @@ func TestConfigTransformer_errMissingDeps(t *testing.T) {
|
||||||
const testGraphBasicStr = `
|
const testGraphBasicStr = `
|
||||||
aws_instance.web
|
aws_instance.web
|
||||||
aws_security_group.firewall
|
aws_security_group.firewall
|
||||||
|
var.foo
|
||||||
aws_load_balancer.weblb
|
aws_load_balancer.weblb
|
||||||
aws_instance.web
|
aws_instance.web
|
||||||
aws_security_group.firewall
|
aws_security_group.firewall
|
||||||
openstack_floating_ip.random
|
openstack_floating_ip.random
|
||||||
provider.aws
|
provider.aws
|
||||||
openstack_floating_ip.random
|
openstack_floating_ip.random
|
||||||
|
var.foo
|
||||||
`
|
`
|
||||||
|
|
||||||
const testGraphDependsOnStr = `
|
const testGraphDependsOnStr = `
|
||||||
|
|
|
@ -218,7 +218,9 @@ provider.foo
|
||||||
const testTransformDisableProviderBasicStr = `
|
const testTransformDisableProviderBasicStr = `
|
||||||
module.child
|
module.child
|
||||||
provider.aws (disabled)
|
provider.aws (disabled)
|
||||||
|
var.foo
|
||||||
provider.aws (disabled)
|
provider.aws (disabled)
|
||||||
|
var.foo
|
||||||
`
|
`
|
||||||
|
|
||||||
const testTransformDisableProviderKeepStr = `
|
const testTransformDisableProviderKeepStr = `
|
||||||
|
@ -226,5 +228,7 @@ aws_instance.foo
|
||||||
provider.aws
|
provider.aws
|
||||||
module.child
|
module.child
|
||||||
provider.aws
|
provider.aws
|
||||||
|
var.foo
|
||||||
provider.aws
|
provider.aws
|
||||||
|
var.foo
|
||||||
`
|
`
|
||||||
|
|
|
@ -2,6 +2,7 @@ package terraform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/config"
|
"github.com/hashicorp/terraform/config"
|
||||||
"github.com/hashicorp/terraform/dag"
|
"github.com/hashicorp/terraform/dag"
|
||||||
|
@ -169,6 +170,18 @@ func (n *graphNodeExpandedResource) ProvidedBy() []string {
|
||||||
return []string{resourceProvider(n.Resource.Type, n.Resource.Provider)}
|
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.
|
// GraphNodeEvalable impl.
|
||||||
func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
||||||
var diff *InstanceDiff
|
var diff *InstanceDiff
|
||||||
|
@ -257,7 +270,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
||||||
Name: n.stateId(),
|
Name: n.stateId(),
|
||||||
ResourceType: n.Resource.Type,
|
ResourceType: n.Resource.Type,
|
||||||
Provider: n.Resource.Provider,
|
Provider: n.Resource.Provider,
|
||||||
Dependencies: n.DependentOn(),
|
Dependencies: n.StateDependencies(),
|
||||||
State: &state,
|
State: &state,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -298,7 +311,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
||||||
Name: n.stateId(),
|
Name: n.stateId(),
|
||||||
ResourceType: n.Resource.Type,
|
ResourceType: n.Resource.Type,
|
||||||
Provider: n.Resource.Provider,
|
Provider: n.Resource.Provider,
|
||||||
Dependencies: n.DependentOn(),
|
Dependencies: n.StateDependencies(),
|
||||||
State: &state,
|
State: &state,
|
||||||
},
|
},
|
||||||
&EvalDiffTainted{
|
&EvalDiffTainted{
|
||||||
|
@ -445,7 +458,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
||||||
Name: n.stateId(),
|
Name: n.stateId(),
|
||||||
ResourceType: n.Resource.Type,
|
ResourceType: n.Resource.Type,
|
||||||
Provider: n.Resource.Provider,
|
Provider: n.Resource.Provider,
|
||||||
Dependencies: n.DependentOn(),
|
Dependencies: n.StateDependencies(),
|
||||||
State: &state,
|
State: &state,
|
||||||
},
|
},
|
||||||
&EvalApplyProvisioners{
|
&EvalApplyProvisioners{
|
||||||
|
@ -489,7 +502,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
||||||
Name: n.stateId(),
|
Name: n.stateId(),
|
||||||
ResourceType: n.Resource.Type,
|
ResourceType: n.Resource.Type,
|
||||||
Provider: n.Resource.Provider,
|
Provider: n.Resource.Provider,
|
||||||
Dependencies: n.DependentOn(),
|
Dependencies: n.StateDependencies(),
|
||||||
State: &state,
|
State: &state,
|
||||||
Index: -1,
|
Index: -1,
|
||||||
},
|
},
|
||||||
|
@ -507,7 +520,7 @@ func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
||||||
Name: n.stateId(),
|
Name: n.stateId(),
|
||||||
ResourceType: n.Resource.Type,
|
ResourceType: n.Resource.Type,
|
||||||
Provider: n.Resource.Provider,
|
Provider: n.Resource.Provider,
|
||||||
Dependencies: n.DependentOn(),
|
Dependencies: n.StateDependencies(),
|
||||||
State: &state,
|
State: &state,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -618,7 +631,7 @@ func (n *graphNodeExpandedResourceDestroy) EvalTree() EvalNode {
|
||||||
Name: n.stateId(),
|
Name: n.stateId(),
|
||||||
ResourceType: n.Resource.Type,
|
ResourceType: n.Resource.Type,
|
||||||
Provider: n.Resource.Provider,
|
Provider: n.Resource.Provider,
|
||||||
Dependencies: n.DependentOn(),
|
Dependencies: n.StateDependencies(),
|
||||||
State: &state,
|
State: &state,
|
||||||
},
|
},
|
||||||
&EvalApplyPost{
|
&EvalApplyPost{
|
||||||
|
|
Loading…
Reference in New Issue