terraform: test that mapping lookups work
This commit is contained in:
parent
c988be9ce7
commit
c9a20c3c58
|
@ -283,13 +283,18 @@ func (r *Resource) mergerMerge(m merger) merger {
|
|||
|
||||
// DefaultsMap returns a map of default values for this variable.
|
||||
func (v *Variable) DefaultsMap() map[string]string {
|
||||
n := fmt.Sprintf("var.%s", v.Name)
|
||||
|
||||
switch v.Type() {
|
||||
case VariableTypeString:
|
||||
return map[string]string{v.Name: v.Default.(string)}
|
||||
return map[string]string{n: v.Default.(string)}
|
||||
case VariableTypeMap:
|
||||
return flatmap.Flatten(map[string]interface{}{
|
||||
v.Name: v.Default.(map[string]string),
|
||||
result := flatmap.Flatten(map[string]interface{}{
|
||||
n: v.Default.(map[string]string),
|
||||
})
|
||||
result[n] = v.Name
|
||||
|
||||
return result
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ func TestVariableDefaultsMap(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
"foo",
|
||||
map[string]string{"foo": "foo"},
|
||||
map[string]string{"var.foo": "foo"},
|
||||
},
|
||||
|
||||
{
|
||||
|
@ -113,8 +113,9 @@ func TestVariableDefaultsMap(t *testing.T) {
|
|||
"bar": "baz",
|
||||
},
|
||||
map[string]string{
|
||||
"foo.foo": "bar",
|
||||
"foo.bar": "baz",
|
||||
"var.foo": "foo",
|
||||
"var.foo.foo": "bar",
|
||||
"var.foo.bar": "baz",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -51,6 +51,20 @@ func (t *libuclConfigurable) Config() (*Config, error) {
|
|||
if len(rawConfig.Variable) > 0 {
|
||||
config.Variables = make([]*Variable, 0, len(rawConfig.Variable))
|
||||
for k, v := range rawConfig.Variable {
|
||||
// Defaults turn into a slice of map[string]interface{} and
|
||||
// we need to make sure to convert that down into the
|
||||
// proper type for Config.
|
||||
if ms, ok := v.Default.([]map[string]interface{}); ok {
|
||||
def := make(map[string]interface{})
|
||||
for _, m := range ms {
|
||||
for k, v := range m {
|
||||
def[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
v.Default = def
|
||||
}
|
||||
|
||||
newVar := &Variable{
|
||||
Name: k,
|
||||
Default: v.Default,
|
||||
|
|
|
@ -30,6 +30,7 @@ type Context struct {
|
|||
providers map[string]ResourceProviderFactory
|
||||
provisioners map[string]ResourceProvisionerFactory
|
||||
variables map[string]string
|
||||
defaultVars map[string]string
|
||||
|
||||
l sync.Mutex // Lock acquired during any task
|
||||
parCh chan struct{} // Semaphore used to limit parallelism
|
||||
|
@ -72,6 +73,14 @@ func NewContext(opts *ContextOpts) *Context {
|
|||
}
|
||||
parCh := make(chan struct{}, par)
|
||||
|
||||
// Calculate all the default variables
|
||||
defaultVars := make(map[string]string)
|
||||
for _, v := range opts.Config.Variables {
|
||||
for k, val := range v.DefaultsMap() {
|
||||
defaultVars[k] = val
|
||||
}
|
||||
}
|
||||
|
||||
return &Context{
|
||||
config: opts.Config,
|
||||
diff: opts.Diff,
|
||||
|
@ -80,6 +89,7 @@ func NewContext(opts *ContextOpts) *Context {
|
|||
providers: opts.Providers,
|
||||
provisioners: opts.Provisioners,
|
||||
variables: opts.Variables,
|
||||
defaultVars: defaultVars,
|
||||
|
||||
parCh: parCh,
|
||||
sh: sh,
|
||||
|
@ -296,8 +306,13 @@ func (c *Context) computeVars(raw *config.RawConfig) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Go through each variable and find it
|
||||
// Start building up the variables. First, defaults
|
||||
vs := make(map[string]string)
|
||||
for k, v := range c.defaultVars {
|
||||
vs[k] = v
|
||||
}
|
||||
|
||||
// Next, the actual computed variables
|
||||
for n, rawV := range raw.Variables {
|
||||
switch v := rawV.(type) {
|
||||
case *config.ResourceVariable:
|
||||
|
@ -314,7 +329,10 @@ func (c *Context) computeVars(raw *config.RawConfig) error {
|
|||
|
||||
vs[n] = attr
|
||||
case *config.UserVariable:
|
||||
vs[n] = c.variables[v.Name]
|
||||
val, ok := c.variables[v.Name]
|
||||
if ok {
|
||||
vs[n] = val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1045,7 +1045,7 @@ func TestContextApply_vars(t *testing.T) {
|
|||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
Variables: map[string]string{
|
||||
"foo": "bar",
|
||||
"foo": "us-west-2",
|
||||
},
|
||||
})
|
||||
|
||||
|
|
|
@ -221,7 +221,8 @@ aws_instance.foo:
|
|||
const testTerraformApplyVarsStr = `
|
||||
aws_instance.bar:
|
||||
ID = foo
|
||||
foo = bar
|
||||
bar = foo
|
||||
foo = us-west-2
|
||||
type = aws_instance
|
||||
aws_instance.foo:
|
||||
ID = foo
|
||||
|
|
|
@ -1,7 +1,15 @@
|
|||
variable "amis" {
|
||||
default = {
|
||||
"us-east-1": "foo",
|
||||
"us-west-2": "foo",
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_instance" "foo" {
|
||||
num = "2"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {
|
||||
foo = "${var.foo}"
|
||||
bar = "${lookup(var.amis, var.foo)}"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue