terraform: encode module dependencies

This commit is contained in:
Armon Dadgar 2014-11-21 16:44:20 -08:00
parent ec1c026b80
commit 9c6280f64b
1 changed files with 56 additions and 31 deletions

View File

@ -303,45 +303,70 @@ func Graph(opts *GraphOpts) (*depgraph.Graph, error) {
// allows orphaned resources to be destroyed in the proper order. // allows orphaned resources to be destroyed in the proper order.
func graphEncodeDependencies(g *depgraph.Graph) { func graphEncodeDependencies(g *depgraph.Graph) {
for _, n := range g.Nouns { for _, n := range g.Nouns {
// Ignore any non-resource nodes switch rn := n.Meta.(type) {
rn, ok := n.Meta.(*GraphNodeResource) case *GraphNodeResource:
if !ok { // If we are using create-before-destroy, there
continue // are some special depedencies injected on the
} // deposed node that would cause a circular depedency
r := rn.Resource // chain if persisted. We must only handle the new node,
// node the deposed node.
r := rn.Resource
if r.Flags&FlagDeposed != 0 {
continue
}
// If we are using create-before-destroy, there // Update the dependencies
// are some special depedencies injected on the var inject []string
// deposed node that would cause a circular depedency for _, dep := range n.Deps {
// chain if persisted. We must only handle the new node, switch target := dep.Target.Meta.(type) {
// node the deposed node. case *GraphNodeModule:
if r.Flags&FlagDeposed != 0 { inject = append(inject, dep.Target.Name)
continue
}
// Update the dependencies case *GraphNodeResource:
var inject []string if target.Resource.Id == r.Id {
for _, dep := range n.Deps { continue
switch target := dep.Target.Meta.(type) { }
case *GraphNodeModule: inject = append(inject, target.Resource.Id)
inject = append(inject, dep.Target.Name)
case *GraphNodeResource: case *GraphNodeResourceProvider:
if target.Resource.Id == r.Id { // Do nothing
continue
default:
panic(fmt.Sprintf("Unknown graph node: %#v", dep.Target))
} }
inject = append(inject, target.Resource.Id) }
case *GraphNodeResourceProvider: // Update the dependencies
// Do nothing r.Dependencies = inject
default: case *GraphNodeModule:
panic(fmt.Sprintf("Unknown graph node: %#v", dep.Target)) // Update the dependencies
var inject []string
for _, dep := range n.Deps {
switch target := dep.Target.Meta.(type) {
case *GraphNodeModule:
if dep.Target.Name == n.Name {
continue
}
inject = append(inject, dep.Target.Name)
case *GraphNodeResource:
inject = append(inject, target.Resource.Id)
case *GraphNodeResourceProvider:
// Do nothing
default:
panic(fmt.Sprintf("Unknown graph node: %#v", dep.Target))
}
}
// Update the dependencies
if rn.State != nil {
rn.State.Dependencies = inject
} }
} }
// Update the dependencies
r.Dependencies = inject
} }
} }