terraform: variables to modules work

This commit is contained in:
Mitchell Hashimoto 2014-09-23 16:44:21 -07:00
parent a3b668bf7d
commit 9e871d5617
5 changed files with 95 additions and 16 deletions

View File

@ -32,7 +32,6 @@ 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
@ -80,16 +79,6 @@ func NewContext(opts *ContextOpts) *Context {
config = opts.Module.Config() config = opts.Module.Config()
} }
// Calculate all the default variables
defaultVars := make(map[string]string)
if config != nil {
for _, v := range config.Variables {
for k, val := range v.DefaultsMap() {
defaultVars[k] = val
}
}
}
return &Context{ return &Context{
config: config, config: config,
diff: opts.Diff, diff: opts.Diff,
@ -99,7 +88,6 @@ 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,
@ -394,10 +382,34 @@ func (c *Context) validateWalkFn(rws *[]string, res *[]error) depgraph.WalkFunc
} }
func (c *Context) walkContext(op walkOperation, path []string) *walkContext { 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{ return &walkContext{
Context: c, Context: c,
Operation: op, Operation: op,
Path: path, Path: path,
Variables: c.variables,
defaultVariables: defaultVars,
} }
} }
@ -408,6 +420,9 @@ type walkContext struct {
Meta interface{} Meta interface{}
Operation walkOperation Operation walkOperation
Path []string Path []string
Variables map[string]string
defaultVariables map[string]string
// This is only set manually by subsequent context creations // This is only set manually by subsequent context creations
// in genericWalkFunc. // in genericWalkFunc.
@ -865,6 +880,17 @@ func (c *walkContext) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc {
// Preserve the meta // Preserve the meta
wc.Meta = c.Meta wc.Meta = c.Meta
// Set the variables
if m.Config != nil {
wc.Variables = make(map[string]string)
rc := NewResourceConfig(m.Config.RawConfig)
rc.interpolate(c)
for k, v := range rc.Config {
wc.Variables[k] = v.(string)
}
}
return wc.Walk() return wc.Walk()
case *GraphNodeResource: case *GraphNodeResource:
// Continue, we care about this the most // Continue, we care about this the most
@ -1054,9 +1080,9 @@ func (c *walkContext) computeVars(raw *config.RawConfig) error {
return nil return nil
} }
// Start building up the variables. First, defaults // Copy the default variables
vs := make(map[string]string) vs := make(map[string]string)
for k, v := range c.Context.defaultVars { for k, v := range c.defaultVariables {
vs[k] = v vs[k] = v
} }
@ -1084,7 +1110,7 @@ func (c *walkContext) computeVars(raw *config.RawConfig) error {
vs[n] = attr vs[n] = attr
case *config.UserVariable: case *config.UserVariable:
val, ok := c.Context.variables[v.Name] val, ok := c.Variables[v.Name]
if ok { if ok {
vs[n] = val vs[n] = val
continue continue
@ -1092,7 +1118,7 @@ func (c *walkContext) computeVars(raw *config.RawConfig) error {
// Look up if we have any variables with this prefix because // Look up if we have any variables with this prefix because
// those are map overrides. Include those. // those are map overrides. Include those.
for k, val := range c.Context.variables { for k, val := range c.Variables {
if strings.HasPrefix(k, v.Name+".") { if strings.HasPrefix(k, v.Name+".") {
vs["var."+k] = val vs["var."+k] = val
} }

View File

@ -1451,6 +1451,29 @@ func TestContextPlan_modules(t *testing.T) {
} }
} }
func TestContextPlan_moduleInput(t *testing.T) {
m := testModule(t, "plan-module-input")
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
plan, err := ctx.Plan(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanModuleInputStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestContextPlan_moduleOrphans(t *testing.T) { func TestContextPlan_moduleOrphans(t *testing.T) {
m := testModule(t, "plan-modules-remove") m := testModule(t, "plan-modules-remove")
p := testProvider("aws") p := testProvider("aws")

View File

@ -511,6 +511,23 @@ STATE:
<no state> <no state>
` `
const testTerraformPlanModuleInputStr = `
DIFF:
CREATE: aws_instance.bar
foo: "" => "2"
type: "" => "aws_instance"
module.child:
CREATE: aws_instance.foo
foo: "" => "42"
type: "" => "aws_instance"
STATE:
<no state>
`
const testTerraformPlanModuleOrphansStr = ` const testTerraformPlanModuleOrphansStr = `
DIFF: DIFF:

View File

@ -0,0 +1,5 @@
variable "input" {}
resource "aws_instance" "foo" {
foo = "${var.input}"
}

View File

@ -0,0 +1,8 @@
module "child" {
input = "42"
source = "./child"
}
resource "aws_instance" "bar" {
foo = "2"
}