terraform: test that mapping lookups work

This commit is contained in:
Mitchell Hashimoto 2014-07-22 08:06:09 -07:00
parent c988be9ce7
commit c9a20c3c58
7 changed files with 57 additions and 10 deletions

View File

@ -283,13 +283,18 @@ func (r *Resource) mergerMerge(m merger) merger {
// DefaultsMap returns a map of default values for this variable. // DefaultsMap returns a map of default values for this variable.
func (v *Variable) DefaultsMap() map[string]string { func (v *Variable) DefaultsMap() map[string]string {
n := fmt.Sprintf("var.%s", v.Name)
switch v.Type() { switch v.Type() {
case VariableTypeString: case VariableTypeString:
return map[string]string{v.Name: v.Default.(string)} return map[string]string{n: v.Default.(string)}
case VariableTypeMap: case VariableTypeMap:
return flatmap.Flatten(map[string]interface{}{ result := flatmap.Flatten(map[string]interface{}{
v.Name: v.Default.(map[string]string), n: v.Default.(map[string]string),
}) })
result[n] = v.Name
return result
default: default:
return nil return nil
} }

View File

@ -104,7 +104,7 @@ func TestVariableDefaultsMap(t *testing.T) {
}{ }{
{ {
"foo", "foo",
map[string]string{"foo": "foo"}, map[string]string{"var.foo": "foo"},
}, },
{ {
@ -113,8 +113,9 @@ func TestVariableDefaultsMap(t *testing.T) {
"bar": "baz", "bar": "baz",
}, },
map[string]string{ map[string]string{
"foo.foo": "bar", "var.foo": "foo",
"foo.bar": "baz", "var.foo.foo": "bar",
"var.foo.bar": "baz",
}, },
}, },
} }

View File

@ -51,6 +51,20 @@ func (t *libuclConfigurable) Config() (*Config, error) {
if len(rawConfig.Variable) > 0 { if len(rawConfig.Variable) > 0 {
config.Variables = make([]*Variable, 0, len(rawConfig.Variable)) config.Variables = make([]*Variable, 0, len(rawConfig.Variable))
for k, v := range 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{ newVar := &Variable{
Name: k, Name: k,
Default: v.Default, Default: v.Default,

View File

@ -30,6 +30,7 @@ type Context struct {
providers map[string]ResourceProviderFactory providers map[string]ResourceProviderFactory
provisioners map[string]ResourceProvisionerFactory provisioners map[string]ResourceProvisionerFactory
variables map[string]string variables map[string]string
defaultVars map[string]string
l sync.Mutex // Lock acquired during any task l sync.Mutex // Lock acquired during any task
parCh chan struct{} // Semaphore used to limit parallelism parCh chan struct{} // Semaphore used to limit parallelism
@ -72,6 +73,14 @@ func NewContext(opts *ContextOpts) *Context {
} }
parCh := make(chan struct{}, par) 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{ return &Context{
config: opts.Config, config: opts.Config,
diff: opts.Diff, diff: opts.Diff,
@ -80,6 +89,7 @@ func NewContext(opts *ContextOpts) *Context {
providers: opts.Providers, providers: opts.Providers,
provisioners: opts.Provisioners, provisioners: opts.Provisioners,
variables: opts.Variables, variables: opts.Variables,
defaultVars: defaultVars,
parCh: parCh, parCh: parCh,
sh: sh, sh: sh,
@ -296,8 +306,13 @@ func (c *Context) computeVars(raw *config.RawConfig) error {
return nil return nil
} }
// Go through each variable and find it // Start building up the variables. First, defaults
vs := make(map[string]string) 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 { for n, rawV := range raw.Variables {
switch v := rawV.(type) { switch v := rawV.(type) {
case *config.ResourceVariable: case *config.ResourceVariable:
@ -314,7 +329,10 @@ func (c *Context) computeVars(raw *config.RawConfig) error {
vs[n] = attr vs[n] = attr
case *config.UserVariable: case *config.UserVariable:
vs[n] = c.variables[v.Name] val, ok := c.variables[v.Name]
if ok {
vs[n] = val
}
} }
} }

View File

@ -1045,7 +1045,7 @@ func TestContextApply_vars(t *testing.T) {
"aws": testProviderFuncFixed(p), "aws": testProviderFuncFixed(p),
}, },
Variables: map[string]string{ Variables: map[string]string{
"foo": "bar", "foo": "us-west-2",
}, },
}) })

View File

@ -221,7 +221,8 @@ aws_instance.foo:
const testTerraformApplyVarsStr = ` const testTerraformApplyVarsStr = `
aws_instance.bar: aws_instance.bar:
ID = foo ID = foo
foo = bar bar = foo
foo = us-west-2
type = aws_instance type = aws_instance
aws_instance.foo: aws_instance.foo:
ID = foo ID = foo

View File

@ -1,7 +1,15 @@
variable "amis" {
default = {
"us-east-1": "foo",
"us-west-2": "foo",
}
}
resource "aws_instance" "foo" { resource "aws_instance" "foo" {
num = "2" num = "2"
} }
resource "aws_instance" "bar" { resource "aws_instance" "bar" {
foo = "${var.foo}" foo = "${var.foo}"
bar = "${lookup(var.amis, var.foo)}"
} }