diff --git a/terraform/context.go b/terraform/context.go index 3a240fe91..c483094cf 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -1135,6 +1135,11 @@ func (c *walkContext) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc { // If we're expanding, then expand the nodes, and then rewalk the graph if rn.ExpandMode > ResourceExpandNone { + // Interpolate the count + rc := NewResourceConfig(rn.Config.RawCount) + rc.interpolate(c) + + // Expand the node to the actual resources ns, err := rn.Expand() if err != nil { return err @@ -1148,14 +1153,14 @@ func (c *walkContext) genericWalkFn(cb genericWalkFunc) depgraph.WalkFunc { for _, n := range ns { wg.Add(1) - go func() { + go func(n *depgraph.Noun) { defer wg.Done() if err := walkFn(n); err != nil { l.Lock() defer l.Unlock() errs = append(errs, err) } - }() + }(n) } // Wait for the subgraph @@ -1515,13 +1520,22 @@ func (c *walkContext) computeResourceMultiVariable( // TODO: Not use only root module module := c.Context.state.RootModule() + // TODO: handle computed here + count, err := cr.Count() + if err != nil { + return "", fmt.Errorf( + "Error reading %s count: %s", + v.ResourceId(), + err) + } + var values []string - for i := 0; i < cr.Count; i++ { + for i := 0; i < count; i++ { id := fmt.Sprintf("%s.%d", v.ResourceId(), i) // If we're dealing with only a single resource, then the // ID doesn't have a trailing index. - if cr.Count == 1 { + if count == 1 { id = v.ResourceId() } diff --git a/terraform/graph.go b/terraform/graph.go index a20ca76d4..22f8c43f9 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -603,11 +603,13 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error { // Add a depedency from the root, since the create node // does not depend on us - g.Root.Deps = append(g.Root.Deps, &depgraph.Dependency{ - Name: newN.Name, - Source: g.Root, - Target: newN, - }) + if g.Root != nil { + g.Root.Deps = append(g.Root.Deps, &depgraph.Dependency{ + Name: newN.Name, + Source: g.Root, + Target: newN, + }) + } // Set the ReplacePrimary flag on the new instance so that // it will become the new primary, and Diposed flag on the @@ -1590,9 +1592,18 @@ func (p *graphSharedProvider) MergeConfig( // Expand will expand this node into a subgraph if Expand is set. func (n *GraphNodeResource) Expand() ([]*depgraph.Noun, error) { - count := 1 + // Expand the count out, which should be interpolated at this point + count, err := n.Config.Count() + if err != nil { + return nil, err + } + log.Printf("[DEBUG] %s: expanding to count = %d", n.Resource.Id, count) + // TODO: can we DRY this up? g := new(depgraph.Graph) + g.Meta = &GraphMeta{ + ModulePath: n.Resource.Info.ModulePath, + } // Determine the nodes to create. If we're just looking for the // nodes to create, return that.