terraform: more efficient variable buildup

This commit is contained in:
Mitchell Hashimoto 2014-06-05 11:08:27 -07:00
parent d77a72ba84
commit e8f235f515
1 changed files with 13 additions and 23 deletions

View File

@ -155,6 +155,12 @@ func (t *Terraform) diffWalkFn(
// This is the value that will be used for computed properties // This is the value that will be used for computed properties
computedId := "computed" computedId := "computed"
// Initialize the variables for application
vars := make(map[string]string)
for k, v := range t.variables {
vars[k] = v
}
return func(n *depgraph.Noun) error { return func(n *depgraph.Noun) error {
// If it is the root node, ignore // If it is the root node, ignore
if n.Name == config.ResourceGraphRoot { if n.Name == config.ResourceGraphRoot {
@ -169,9 +175,8 @@ func (t *Terraform) diffWalkFn(
l.RLock() l.RLock()
rs := state.resources[r.Id()] rs := state.resources[r.Id()]
vs := t.replaceVariables(r, state) if len(vars) > 0 {
if len(vs) > 0 { r = r.ReplaceVariables(vars)
r = r.ReplaceVariables(vs)
} }
l.RUnlock() l.RUnlock()
@ -192,31 +197,16 @@ func (t *Terraform) diffWalkFn(
// Update the resulting diff // Update the resulting diff
result.Resources[r.Id()] = diff.Attributes result.Resources[r.Id()] = diff.Attributes
// Update the state for child dependencies // Determine the new state and update variables
state.resources[r.Id()] = rs.MergeDiff(diff.Attributes, computedId) rs = rs.MergeDiff(diff.Attributes, computedId)
for ak, av := range rs.Attributes {
vars[fmt.Sprintf("%s.%s", r.Id(), ak)] = av
}
return nil return nil
} }
} }
// replaceVariables will return the mapping of variable replacements to
// apply for a given resource with a given state.
func (t *Terraform) replaceVariables(
r *config.Resource,
s *State) map[string]string {
result := make(map[string]string)
for k, v := range t.variables {
result[k] = v
}
for n, rs := range s.resources {
for attrK, attrV := range rs.Attributes {
result[fmt.Sprintf("%s.%s", n, attrK)] = attrV
}
}
return result
}
// matchingPrefixes takes a resource type and a set of resource // matchingPrefixes takes a resource type and a set of resource
// providers we know about by prefix and returns a list of prefixes // providers we know about by prefix and returns a list of prefixes
// that might be valid for that resource. // that might be valid for that resource.