terraform: prune resources and variables
This commit is contained in:
parent
6550f564bf
commit
4d361c839e
|
@ -215,6 +215,49 @@ func TestContext2Apply_createBeforeDestroyUpdate(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext2Apply_destroyComputed(t *testing.T) {
|
||||||
|
m := testModule(t, "apply-destroy-computed")
|
||||||
|
p := testProvider("aws")
|
||||||
|
p.ApplyFn = testApplyFn
|
||||||
|
p.DiffFn = testDiffFn
|
||||||
|
state := &State{
|
||||||
|
Modules: []*ModuleState{
|
||||||
|
&ModuleState{
|
||||||
|
Path: rootModulePath,
|
||||||
|
Resources: map[string]*ResourceState{
|
||||||
|
"aws_instance.foo": &ResourceState{
|
||||||
|
Type: "aws_instance",
|
||||||
|
Primary: &InstanceState{
|
||||||
|
ID: "foo",
|
||||||
|
Attributes: map[string]string{
|
||||||
|
"output": "value",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ctx := testContext2(t, &ContextOpts{
|
||||||
|
Module: m,
|
||||||
|
Providers: map[string]ResourceProviderFactory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
State: state,
|
||||||
|
Destroy: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
if p, err := ctx.Plan(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
} else {
|
||||||
|
t.Logf(p.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := ctx.Apply(); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestContext2Apply_minimal(t *testing.T) {
|
func TestContext2Apply_minimal(t *testing.T) {
|
||||||
m := testModule(t, "apply-minimal")
|
m := testModule(t, "apply-minimal")
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
|
|
|
@ -164,6 +164,9 @@ func (b *BuiltinGraphBuilder) Steps(path []string) []GraphTransformer {
|
||||||
Then: &PruneDestroyTransformer{Diff: b.Diff, State: b.State},
|
Then: &PruneDestroyTransformer{Diff: b.Diff, State: b.State},
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
// Remove the noop nodes
|
||||||
|
&PruneNoopTransformer{Diff: b.Diff, State: b.State},
|
||||||
|
|
||||||
// Insert nodes to close opened plugin connections
|
// Insert nodes to close opened plugin connections
|
||||||
&CloseProviderTransformer{},
|
&CloseProviderTransformer{},
|
||||||
&CloseProvisionerTransformer{},
|
&CloseProvisionerTransformer{},
|
||||||
|
|
|
@ -249,6 +249,38 @@ func (n *GraphNodeConfigResource) DestroyNode(mode GraphNodeDestroyMode) GraphNo
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GraphNodeNoopPrunable
|
||||||
|
func (n *GraphNodeConfigResource) Noop(opts *NoopOpts) bool {
|
||||||
|
// We don't have any noop optimizations for destroy nodes yet
|
||||||
|
if n.DestroyMode != DestroyNone {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is no diff, then we aren't a noop
|
||||||
|
if opts.Diff == nil || opts.Diff.Empty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have no module diff, we're certainly a noop
|
||||||
|
if opts.ModDiff == nil || opts.ModDiff.Empty() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Grab the ID which is the prefix (in the case count > 0 at some point)
|
||||||
|
prefix := n.Resource.Id()
|
||||||
|
|
||||||
|
// Go through the diff and if there are any with our name on it, keep us
|
||||||
|
found := false
|
||||||
|
for k, _ := range opts.ModDiff.Resources {
|
||||||
|
if strings.HasPrefix(k, prefix) {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return !found
|
||||||
|
}
|
||||||
|
|
||||||
// Same as GraphNodeConfigResource, but for flattening
|
// Same as GraphNodeConfigResource, but for flattening
|
||||||
type GraphNodeConfigResourceFlat struct {
|
type GraphNodeConfigResourceFlat struct {
|
||||||
*GraphNodeConfigResource
|
*GraphNodeConfigResource
|
||||||
|
|
|
@ -75,6 +75,28 @@ func (n *GraphNodeConfigVariable) DestroyEdgeInclude(v dag.Vertex) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GraphNodeNoopPrunable
|
||||||
|
func (n *GraphNodeConfigVariable) Noop(opts *NoopOpts) bool {
|
||||||
|
// If we have no diff, always keep this in the graph. We have to do
|
||||||
|
// this primarily for validation: we want to validate that variable
|
||||||
|
// interpolations are valid even if there are no resources that
|
||||||
|
// depend on them.
|
||||||
|
if opts.Diff == nil || opts.Diff.Empty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range opts.Graph.UpEdges(opts.Vertex).List() {
|
||||||
|
// This is terrible, but I can't think of a better way to do this.
|
||||||
|
if dag.VertexName(v) == rootNodeName {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// GraphNodeProxy impl.
|
// GraphNodeProxy impl.
|
||||||
func (n *GraphNodeConfigVariable) Proxy() bool {
|
func (n *GraphNodeConfigVariable) Proxy() bool {
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -370,7 +370,7 @@ MISSING:
|
||||||
// be unknown. Instead, we return that the value is computed so
|
// be unknown. Instead, we return that the value is computed so
|
||||||
// that the graph can continue to refresh other nodes. It doesn't
|
// that the graph can continue to refresh other nodes. It doesn't
|
||||||
// matter because the config isn't interpolated anyways.
|
// matter because the config isn't interpolated anyways.
|
||||||
if i.Operation == walkRefresh {
|
if i.Operation == walkRefresh || i.Operation == walkPlanDestroy {
|
||||||
return config.UnknownVariableValue, nil
|
return config.UnknownVariableValue, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -446,7 +446,7 @@ func (i *Interpolater) computeResourceMultiVariable(
|
||||||
// be unknown. Instead, we return that the value is computed so
|
// be unknown. Instead, we return that the value is computed so
|
||||||
// that the graph can continue to refresh other nodes. It doesn't
|
// that the graph can continue to refresh other nodes. It doesn't
|
||||||
// matter because the config isn't interpolated anyways.
|
// matter because the config isn't interpolated anyways.
|
||||||
if i.Operation == walkRefresh {
|
if i.Operation == walkRefresh || i.Operation == walkPlanDestroy {
|
||||||
return config.UnknownVariableValue, nil
|
return config.UnknownVariableValue, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
variable "value" {}
|
||||||
|
|
||||||
|
resource "aws_instance" "bar" {
|
||||||
|
value = "${var.value}"
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
resource "aws_instance" "foo" {}
|
||||||
|
|
||||||
|
module "child" {
|
||||||
|
source = "./child"
|
||||||
|
value = "${aws_instance.foo.output}"
|
||||||
|
}
|
|
@ -12,10 +12,12 @@ type GraphNodeNoopPrunable interface {
|
||||||
|
|
||||||
// NoopOpts are the options available to determine if your node is a noop.
|
// NoopOpts are the options available to determine if your node is a noop.
|
||||||
type NoopOpts struct {
|
type NoopOpts struct {
|
||||||
Graph *Graph
|
Graph *Graph
|
||||||
Vertex dag.Vertex
|
Vertex dag.Vertex
|
||||||
Diff *ModuleDiff
|
Diff *Diff
|
||||||
State *ModuleState
|
State *State
|
||||||
|
ModDiff *ModuleDiff
|
||||||
|
ModState *ModuleState
|
||||||
}
|
}
|
||||||
|
|
||||||
// PruneNoopTransformer is a graph transform that prunes nodes that
|
// PruneNoopTransformer is a graph transform that prunes nodes that
|
||||||
|
@ -52,6 +54,7 @@ func (t *PruneNoopTransformer) Transform(g *Graph) error {
|
||||||
|
|
||||||
// Do a depth first walk from the leaves and remove things.
|
// Do a depth first walk from the leaves and remove things.
|
||||||
return g.ReverseDepthFirstWalk(leaves, func(v dag.Vertex, depth int) error {
|
return g.ReverseDepthFirstWalk(leaves, func(v dag.Vertex, depth int) error {
|
||||||
|
println("NAME: " + v.(dag.NamedVertex).Name())
|
||||||
// We need a prunable
|
// We need a prunable
|
||||||
pn, ok := v.(GraphNodeNoopPrunable)
|
pn, ok := v.(GraphNodeNoopPrunable)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -75,10 +78,12 @@ func (t *PruneNoopTransformer) Transform(g *Graph) error {
|
||||||
|
|
||||||
// Determine if its a noop. If it isn't, just return
|
// Determine if its a noop. If it isn't, just return
|
||||||
noop := pn.Noop(&NoopOpts{
|
noop := pn.Noop(&NoopOpts{
|
||||||
Graph: g,
|
Graph: g,
|
||||||
Vertex: v,
|
Vertex: v,
|
||||||
Diff: modDiff,
|
Diff: t.Diff,
|
||||||
State: modState,
|
State: t.State,
|
||||||
|
ModDiff: modDiff,
|
||||||
|
ModState: modState,
|
||||||
})
|
})
|
||||||
if !noop {
|
if !noop {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -2,6 +2,8 @@ package terraform
|
||||||
|
|
||||||
import "github.com/hashicorp/terraform/dag"
|
import "github.com/hashicorp/terraform/dag"
|
||||||
|
|
||||||
|
const rootNodeName = "root"
|
||||||
|
|
||||||
// RootTransformer is a GraphTransformer that adds a root to the graph.
|
// RootTransformer is a GraphTransformer that adds a root to the graph.
|
||||||
type RootTransformer struct{}
|
type RootTransformer struct{}
|
||||||
|
|
||||||
|
@ -32,7 +34,7 @@ func (t *RootTransformer) Transform(g *Graph) error {
|
||||||
type graphNodeRoot struct{}
|
type graphNodeRoot struct{}
|
||||||
|
|
||||||
func (n graphNodeRoot) Name() string {
|
func (n graphNodeRoot) Name() string {
|
||||||
return "root"
|
return rootNodeName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n graphNodeRoot) Flatten(p []string) (dag.Vertex, error) {
|
func (n graphNodeRoot) Flatten(p []string) (dag.Vertex, error) {
|
||||||
|
|
Loading…
Reference in New Issue