terraform: Handle module depedency inversion

This commit is contained in:
Armon Dadgar 2014-11-24 14:38:06 -08:00
parent d5fd4dabe8
commit afef564108
1 changed files with 82 additions and 67 deletions

View File

@ -274,7 +274,7 @@ func Graph(opts *GraphOpts) (*depgraph.Graph, error) {
// If we have a diff, then make sure to add that in // If we have a diff, then make sure to add that in
if modDiff != nil { if modDiff != nil {
if err := graphAddDiff(g, modDiff); err != nil { if err := graphAddDiff(g, opts.Diff, modDiff); err != nil {
return nil, err return nil, err
} }
} }
@ -544,10 +544,22 @@ func graphAddConfigResources(
// destroying the VPC's subnets first, whereas creating a VPC requires // destroying the VPC's subnets first, whereas creating a VPC requires
// doing it before the subnets are created. This function handles inserting // doing it before the subnets are created. This function handles inserting
// these nodes for you. // these nodes for you.
func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error { func graphAddDiff(g *depgraph.Graph, gDiff *Diff, d *ModuleDiff) error {
var nlist []*depgraph.Noun var nlist []*depgraph.Noun
var modules []*depgraph.Noun
injected := make(map[*depgraph.Dependency]struct{}) injected := make(map[*depgraph.Dependency]struct{})
for _, n := range g.Nouns { for _, n := range g.Nouns {
// A module is being destroyed if all it's resources are being
// destroyed (via a destroy plan) or if it is orphaned. Only in
// those cases do we need to handle depedency inversion.
if mod, ok := n.Meta.(*GraphNodeModule); ok {
md := gDiff.ModuleByPath(mod.Path)
if mod.Flags&FlagOrphan != 0 || (md != nil && md.Destroy) {
modules = append(modules, n)
}
continue
}
rn, ok := n.Meta.(*GraphNodeResource) rn, ok := n.Meta.(*GraphNodeResource)
if !ok { if !ok {
continue continue
@ -693,9 +705,11 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
rn.Resource.Diff = rd rn.Resource.Diff = rd
} }
// Go through each noun and make sure we calculate all the dependencies // Go through each resource and module and make sure we
// properly. // calculate all the dependencies properly.
for _, n := range nlist { invertDeps := [][]*depgraph.Noun{nlist, modules}
for _, list := range invertDeps {
for _, n := range list {
deps := n.Deps deps := n.Deps
num := len(deps) num := len(deps)
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
@ -766,6 +780,7 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
} }
n.Deps = deps[:num] n.Deps = deps[:num]
} }
}
// Add the nouns to the graph // Add the nouns to the graph
g.Nouns = append(g.Nouns, nlist...) g.Nouns = append(g.Nouns, nlist...)