terraform: Simplify sub-graph finalization

This commit is contained in:
Armon Dadgar 2014-11-17 15:50:26 -08:00
parent 2d117326ed
commit a2ba45edf5
1 changed files with 23 additions and 38 deletions

View File

@ -1662,13 +1662,18 @@ func (n *GraphNodeResource) Expand() (*depgraph.Graph, error) {
// Add all the variable dependencies // Add all the variable dependencies
graphAddVariableDeps(g) graphAddVariableDeps(g)
// If we're just expanding the apply, then filter those out and // Filter the nodes depending on the expansion type
// return them now. switch n.ExpandMode {
if n.ExpandMode == ResourceExpandApply { case ResourceExpandApply:
return n.finalizeGraph(g, false) n.filterResources(g, false)
case ResourceExpandDestroy:
n.filterResources(g, true)
default:
panic(fmt.Sprintf("Unhandled expansion mode %d", n.ExpandMode))
} }
return n.finalizeGraph(g, true) // Return the finalized graph
return g, n.finalizeGraph(g)
} }
// expand expands this resource and adds the resources to the graph. It // expand expands this resource and adds the resources to the graph. It
@ -1790,8 +1795,11 @@ func (n *GraphNodeResource) copyResource(id string) *Resource {
return &resource return &resource
} }
func (n *GraphNodeResource) finalizeGraph( // filterResources is used to remove resources from the sub-graph based
g *depgraph.Graph, destroy bool) (*depgraph.Graph, error) { // on the ExpandMode. This is because there is a Destroy sub-graph, and
// Apply sub-graph, and we cannot includes the same instances in both
// sub-graphs.
func (n *GraphNodeResource) filterResources(g *depgraph.Graph, destroy bool) {
result := make([]*depgraph.Noun, 0, len(g.Nouns)) result := make([]*depgraph.Noun, 0, len(g.Nouns))
for _, n := range g.Nouns { for _, n := range g.Nouns {
rn, ok := n.Meta.(*GraphNodeResource) rn, ok := n.Meta.(*GraphNodeResource)
@ -1799,44 +1807,22 @@ func (n *GraphNodeResource) finalizeGraph(
continue continue
} }
// If the diff is nil, then we're not destroying, so append only if destroy {
// in that case. if rn.Resource.Diff != nil && rn.Resource.Diff.Destroy {
if rn.Resource.Diff == nil {
if !destroy {
result = append(result, n) result = append(result, n)
} }
continue continue
} }
// If we are destroying, append it only if we care about destroys if rn.Resource.Diff == nil || !rn.Resource.Diff.Destroy {
if rn.Resource.Diff.Destroy {
if destroy {
result = append(result, n)
}
continue
}
// If this is an oprhan, we only care about it if we're destroying.
if rn.Resource.Flags&FlagOrphan != 0 {
if destroy {
result = append(result, n)
}
continue
}
// If we're not destroying, then add it only if we don't
// care about deploys.
if !destroy {
result = append(result, n) result = append(result, n)
} }
} }
// Set the nouns to be only those we care about
g.Nouns = result g.Nouns = result
}
// finalizeGraph is used to ensure the generated graph is valid
func (n *GraphNodeResource) finalizeGraph(g *depgraph.Graph) error {
// Remove the dependencies that don't exist // Remove the dependencies that don't exist
graphRemoveInvalidDeps(g) graphRemoveInvalidDeps(g)
@ -1845,10 +1831,9 @@ func (n *GraphNodeResource) finalizeGraph(
// Validate // Validate
if err := g.Validate(); err != nil { if err := g.Validate(); err != nil {
return nil, err return err
} }
return nil
return g, nil
} }
// matchingPrefixes takes a resource type and a set of resource // matchingPrefixes takes a resource type and a set of resource