terraform: pulling out everything into Interpolater
This commit is contained in:
parent
76ce6e45f7
commit
3908b6319a
|
@ -10,7 +10,6 @@ import (
|
|||
"sync/atomic"
|
||||
|
||||
"github.com/hashicorp/terraform/config"
|
||||
"github.com/hashicorp/terraform/config/lang/ast"
|
||||
"github.com/hashicorp/terraform/config/module"
|
||||
"github.com/hashicorp/terraform/depgraph"
|
||||
"github.com/hashicorp/terraform/helper/multierror"
|
||||
|
@ -373,34 +372,11 @@ func (c *Context) releaseRun(ch chan<- struct{}) {
|
|||
}
|
||||
|
||||
func (c *Context) walkContext(op walkOperation, path []string) *walkContext {
|
||||
// Get the config structure
|
||||
m := c.module
|
||||
for _, n := range path[1:] {
|
||||
cs := m.Children()
|
||||
m = cs[n]
|
||||
}
|
||||
var conf *config.Config
|
||||
if m != nil {
|
||||
conf = m.Config()
|
||||
}
|
||||
|
||||
// Calculate the default variable values
|
||||
defaultVars := make(map[string]string)
|
||||
if conf != nil {
|
||||
for _, v := range conf.Variables {
|
||||
for k, val := range v.DefaultsMap() {
|
||||
defaultVars[k] = val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &walkContext{
|
||||
Context: c,
|
||||
Operation: op,
|
||||
Path: path,
|
||||
Variables: c.variables,
|
||||
|
||||
defaultVariables: defaultVars,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1521,16 +1497,6 @@ func (c *walkContext) computeVars(
|
|||
return err
|
||||
}
|
||||
|
||||
// Copy the default variables
|
||||
for k, v := range c.defaultVariables {
|
||||
if _, ok := vs[k]; !ok {
|
||||
vs[k] = ast.Variable{
|
||||
Value: v,
|
||||
Type: ast.TypeString,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Interpolate the variables
|
||||
return raw.Interpolate(vs)
|
||||
}
|
||||
|
|
|
@ -148,6 +148,26 @@ func TestContext2Validate_providerConfig_good(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContext2Validate_requiredVar(t *testing.T) {
|
||||
m := testModule(t, "validate-required-var")
|
||||
p := testProvider("aws")
|
||||
c := testContext2(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
w, e := c.Validate()
|
||||
if len(w) > 0 {
|
||||
t.Fatalf("bad: %#v", w)
|
||||
}
|
||||
// TODO: fail
|
||||
if len(e) == 0 {
|
||||
t.Fatalf("bad: %s", e)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func TestContextValidate_goodModule(t *testing.T) {
|
||||
p := testProvider("aws")
|
||||
|
@ -395,21 +415,6 @@ func TestContextValidate_resourceNameSymbol(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContextValidate_requiredVar(t *testing.T) {
|
||||
m := testModule(t, "validate-required-var")
|
||||
c := testContext(t, &ContextOpts{
|
||||
Module: m,
|
||||
})
|
||||
|
||||
w, e := c.Validate()
|
||||
if len(w) > 0 {
|
||||
t.Fatalf("bad: %#v", w)
|
||||
}
|
||||
if len(e) == 0 {
|
||||
t.Fatalf("bad: %#v", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextValidate_provisionerConfig_bad(t *testing.T) {
|
||||
m := testModule(t, "validate-bad-prov-conf")
|
||||
p := testProvider("aws")
|
||||
|
|
|
@ -34,6 +34,23 @@ func (i *Interpolater) Values(
|
|||
scope *InterpolationScope,
|
||||
vars map[string]config.InterpolatedVariable) (map[string]ast.Variable, error) {
|
||||
result := make(map[string]ast.Variable, len(vars))
|
||||
|
||||
// Copy the default variables
|
||||
if i.Module != nil && scope != nil {
|
||||
mod := i.Module
|
||||
if len(scope.Path) > 1 {
|
||||
mod = i.Module.Child(scope.Path[1:])
|
||||
}
|
||||
for _, v := range mod.Config().Variables {
|
||||
for k, val := range v.DefaultsMap() {
|
||||
result[k] = ast.Variable{
|
||||
Value: val,
|
||||
Type: ast.TypeString,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for n, rawV := range vars {
|
||||
var err error
|
||||
switch v := rawV.(type) {
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
module "child" {
|
||||
source = "./child"
|
||||
}
|
Loading…
Reference in New Issue