Cleaning up the PruneProvisionerTransformer

And renamed some types so they better reflect what they are for.
This commit is contained in:
Sander van Harmelen 2016-02-04 13:46:45 +01:00 committed by Sander van Harmelen
parent 5c3da47d8e
commit 1bec11472a
4 changed files with 69 additions and 101 deletions

View File

@ -146,11 +146,9 @@ func (b *BuiltinGraphBuilder) Steps(path []string) []GraphTransformer {
// their dependencies. // their dependencies.
&TargetsTransformer{Targets: b.Targets, Destroy: b.Destroy}, &TargetsTransformer{Targets: b.Targets, Destroy: b.Destroy},
// Prune the providers and provisioners. This must happen // Prune the providers. This must happen only once because flattened
// only once because flattened modules might depend on empty // modules might depend on empty providers.
// providers.
&PruneProviderTransformer{}, &PruneProviderTransformer{},
&PruneProvisionerTransformer{},
// Create the destruction nodes // Create the destruction nodes
&DestroyTransformer{FullDestroy: b.Destroy}, &DestroyTransformer{FullDestroy: b.Destroy},

View File

@ -265,6 +265,7 @@ provider.aws (close)
const testBuiltinGraphBuilderMultiLevelStr = ` const testBuiltinGraphBuilderMultiLevelStr = `
module.foo.module.bar.output.value module.foo.module.bar.output.value
module.foo.module.bar.var.bar module.foo.module.bar.var.bar
module.foo.var.foo
module.foo.module.bar.plan-destroy module.foo.module.bar.plan-destroy
module.foo.module.bar.var.bar module.foo.module.bar.var.bar
module.foo.var.foo module.foo.var.foo
@ -273,7 +274,9 @@ module.foo.var.foo
root root
module.foo.module.bar.output.value module.foo.module.bar.output.value
module.foo.module.bar.plan-destroy module.foo.module.bar.plan-destroy
module.foo.module.bar.var.bar
module.foo.plan-destroy module.foo.plan-destroy
module.foo.var.foo
` `
const testBuiltinGraphBuilderOrphanDepsStr = ` const testBuiltinGraphBuilderOrphanDepsStr = `

View File

@ -39,16 +39,15 @@ func (t *ProvisionerTransformer) Transform(g *Graph) error {
m := provisionerVertexMap(g) m := provisionerVertexMap(g)
for _, v := range g.Vertices() { for _, v := range g.Vertices() {
if pv, ok := v.(GraphNodeProvisionerConsumer); ok { if pv, ok := v.(GraphNodeProvisionerConsumer); ok {
for _, provisionerName := range pv.ProvisionedBy() { for _, p := range pv.ProvisionedBy() {
target := m[provisionerName] if m[p] == nil {
if target == nil {
err = multierror.Append(err, fmt.Errorf( err = multierror.Append(err, fmt.Errorf(
"%s: provisioner %s couldn't be found", "%s: provisioner %s couldn't be found",
dag.VertexName(v), provisionerName)) dag.VertexName(v), p))
continue continue
} }
g.Connect(dag.BasicEdge(v, target)) g.Connect(dag.BasicEdge(v, m[p]))
} }
} }
} }
@ -56,41 +55,8 @@ func (t *ProvisionerTransformer) Transform(g *Graph) error {
return err return err
} }
// CloseProvisionerTransformer is a GraphTransformer that adds nodes to the
// graph that will close open provisioner connections that aren't needed
// anymore. A provisioner connection is not needed anymore once all depended
// resources in the graph are evaluated.
type CloseProvisionerTransformer struct{}
func (t *CloseProvisionerTransformer) Transform(g *Graph) error {
m := closeProvisionerVertexMap(g)
for _, v := range g.Vertices() {
if pv, ok := v.(GraphNodeProvisionerConsumer); ok {
for _, p := range pv.ProvisionedBy() {
source := m[p]
if source == nil {
// Create a new graphNodeCloseProvisioner and add it to the graph
source = &graphNodeCloseProvisioner{ProvisionerNameValue: p}
g.Add(source)
// Make sure we also add the new graphNodeCloseProvisioner to the map
// so we don't create and add any duplicate graphNodeCloseProvisioners.
m[p] = source
}
g.Connect(dag.BasicEdge(source, v))
}
}
}
return nil
}
// MissingProvisionerTransformer is a GraphTransformer that adds nodes // MissingProvisionerTransformer is a GraphTransformer that adds nodes
// for missing provisioners into the graph. Specifically, it creates provisioner // for missing provisioners into the graph.
// configuration nodes for all the provisioners that we support. These are
// pruned later during an optimization pass.
type MissingProvisionerTransformer struct { type MissingProvisionerTransformer struct {
// Provisioners is the list of provisioners we support. // Provisioners is the list of provisioners we support.
Provisioners []string Provisioners []string
@ -126,29 +92,39 @@ func (t *MissingProvisionerTransformer) Transform(g *Graph) error {
continue continue
} }
// Add our own missing provisioner node to the graph // Add the missing provisioner node to the graph
m[p] = g.Add(&graphNodeMissingProvisioner{ProvisionerNameValue: p}) m[p] = g.Add(&graphNodeProvisioner{ProvisionerNameValue: p})
} }
} }
return nil return nil
} }
// PruneProvisionerTransformer is a GraphTransformer that prunes all the // CloseProvisionerTransformer is a GraphTransformer that adds nodes to the
// provisioners that aren't needed from the graph. A provisioner is unneeded if // graph that will close open provisioner connections that aren't needed
// no resource or module is using that provisioner. // anymore. A provisioner connection is not needed anymore once all depended
type PruneProvisionerTransformer struct{} // resources in the graph are evaluated.
type CloseProvisionerTransformer struct{}
func (t *PruneProvisionerTransformer) Transform(g *Graph) error { func (t *CloseProvisionerTransformer) Transform(g *Graph) error {
m := closeProvisionerVertexMap(g)
for _, v := range g.Vertices() { for _, v := range g.Vertices() {
// We only care about the provisioners if pv, ok := v.(GraphNodeProvisionerConsumer); ok {
if _, ok := v.(GraphNodeProvisioner); !ok { for _, p := range pv.ProvisionedBy() {
continue source := m[p]
}
// Does anything depend on this? If not, then prune it. if source == nil {
if s := g.UpEdges(v); s.Len() == 0 { // Create a new graphNodeCloseProvisioner and add it to the graph
g.Remove(v) source = &graphNodeCloseProvisioner{ProvisionerNameValue: p}
g.Add(source)
// Make sure we also add the new graphNodeCloseProvisioner to the map
// so we don't create and add any duplicate graphNodeCloseProvisioners.
m[p] = source
}
g.Connect(dag.BasicEdge(source, v))
}
} }
} }
@ -194,49 +170,49 @@ func (n *graphNodeCloseProvisioner) CloseProvisionerName() string {
return n.ProvisionerNameValue return n.ProvisionerNameValue
} }
type graphNodeMissingProvisioner struct { type graphNodeProvisioner struct {
ProvisionerNameValue string ProvisionerNameValue string
} }
func (n *graphNodeMissingProvisioner) Name() string { func (n *graphNodeProvisioner) Name() string {
return fmt.Sprintf("provisioner.%s", n.ProvisionerNameValue) return fmt.Sprintf("provisioner.%s", n.ProvisionerNameValue)
} }
// GraphNodeEvalable impl. // GraphNodeEvalable impl.
func (n *graphNodeMissingProvisioner) EvalTree() EvalNode { func (n *graphNodeProvisioner) EvalTree() EvalNode {
return &EvalInitProvisioner{Name: n.ProvisionerNameValue} return &EvalInitProvisioner{Name: n.ProvisionerNameValue}
} }
func (n *graphNodeMissingProvisioner) ProvisionerName() string { func (n *graphNodeProvisioner) ProvisionerName() string {
return n.ProvisionerNameValue return n.ProvisionerNameValue
} }
// GraphNodeFlattenable impl. // GraphNodeFlattenable impl.
func (n *graphNodeMissingProvisioner) Flatten(p []string) (dag.Vertex, error) { func (n *graphNodeProvisioner) Flatten(p []string) (dag.Vertex, error) {
return &graphNodeMissingProvisionerFlat{ return &graphNodeProvisionerFlat{
graphNodeMissingProvisioner: n, graphNodeProvisioner: n,
PathValue: p, PathValue: p,
}, nil }, nil
} }
// Same as graphNodeMissingProvisioner, but for flattening // Same as graphNodeMissingProvisioner, but for flattening
type graphNodeMissingProvisionerFlat struct { type graphNodeProvisionerFlat struct {
*graphNodeMissingProvisioner *graphNodeProvisioner
PathValue []string PathValue []string
} }
func (n *graphNodeMissingProvisionerFlat) Name() string { func (n *graphNodeProvisionerFlat) Name() string {
return fmt.Sprintf( return fmt.Sprintf(
"%s.%s", modulePrefixStr(n.PathValue), n.graphNodeMissingProvisioner.Name()) "%s.%s", modulePrefixStr(n.PathValue), n.graphNodeProvisioner.Name())
} }
func (n *graphNodeMissingProvisionerFlat) Path() []string { func (n *graphNodeProvisionerFlat) Path() []string {
return n.PathValue return n.PathValue
} }
func (n *graphNodeMissingProvisionerFlat) ProvisionerName() string { func (n *graphNodeProvisionerFlat) ProvisionerName() string {
return fmt.Sprintf( return fmt.Sprintf(
"%s.%s", modulePrefixStr(n.PathValue), "%s.%s", modulePrefixStr(n.PathValue),
n.graphNodeMissingProvisioner.ProvisionerName()) n.graphNodeProvisioner.ProvisionerName())
} }

View File

@ -19,14 +19,14 @@ func TestMissingProvisionerTransformer(t *testing.T) {
} }
{ {
transform := &MissingProvisionerTransformer{Provisioners: []string{"foo"}} transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
if err := transform.Transform(&g); err != nil { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
} }
{ {
transform := &CloseProvisionerTransformer{} transform := &ProvisionerTransformer{}
if err := transform.Transform(&g); err != nil { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -39,8 +39,8 @@ func TestMissingProvisionerTransformer(t *testing.T) {
} }
} }
func TestPruneProvisionerTransformer(t *testing.T) { func TestCloseProvisionerTransformer(t *testing.T) {
mod := testModule(t, "transform-provisioner-prune") mod := testModule(t, "transform-provisioner-basic")
g := Graph{Path: RootModulePath} g := Graph{Path: RootModulePath}
{ {
@ -51,8 +51,7 @@ func TestPruneProvisionerTransformer(t *testing.T) {
} }
{ {
transform := &MissingProvisionerTransformer{ transform := &MissingProvisionerTransformer{Provisioners: []string{"shell"}}
Provisioners: []string{"foo", "bar"}}
if err := transform.Transform(&g); err != nil { if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -72,28 +71,20 @@ func TestPruneProvisionerTransformer(t *testing.T) {
} }
} }
{
transform := &PruneProvisionerTransformer{}
if err := transform.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
}
actual := strings.TrimSpace(g.String()) actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformPruneProvisionerBasicStr) expected := strings.TrimSpace(testTransformCloseProvisionerBasicStr)
if actual != expected { if actual != expected {
t.Fatalf("bad:\n\n%s", actual) t.Fatalf("bad:\n\n%s", actual)
} }
} }
func TestGraphNodeProvisioner_impl(t *testing.T) {
func TestGraphNodeMissingProvisioner_impl(t *testing.T) { var _ dag.Vertex = new(graphNodeProvisioner)
var _ dag.Vertex = new(graphNodeMissingProvisioner) var _ dag.NamedVertex = new(graphNodeProvisioner)
var _ dag.NamedVertex = new(graphNodeMissingProvisioner) var _ GraphNodeProvisioner = new(graphNodeProvisioner)
var _ GraphNodeProvisioner = new(graphNodeMissingProvisioner)
} }
func TestGraphNodeMissingProvisioner_ProvisionerName(t *testing.T) { func TestGraphNodeProvisioner_ProvisionerName(t *testing.T) {
n := &graphNodeMissingProvisioner{ProvisionerNameValue: "foo"} n := &graphNodeProvisioner{ProvisionerNameValue: "foo"}
if v := n.ProvisionerName(); v != "foo" { if v := n.ProvisionerName(); v != "foo" {
t.Fatalf("bad: %#v", v) t.Fatalf("bad: %#v", v)
} }
@ -101,14 +92,14 @@ func TestGraphNodeMissingProvisioner_ProvisionerName(t *testing.T) {
const testTransformMissingProvisionerBasicStr = ` const testTransformMissingProvisionerBasicStr = `
aws_instance.web aws_instance.web
provisioner.shell
provisioner.shell
`
const testTransformCloseProvisionerBasicStr = `
aws_instance.web
provisioner.shell
provisioner.shell
provisioner.shell (close) provisioner.shell (close)
aws_instance.web aws_instance.web
` `
const testTransformPruneProvisionerBasicStr = `
aws_instance.web
provisioner.foo
provisioner.foo
provisioner.foo (close)
aws_instance.web
`