terraform: don't use Meta node anymore

This commit is contained in:
Mitchell Hashimoto 2014-10-01 18:06:25 -07:00
parent 95f43d8230
commit 4fe0c4ada4
3 changed files with 184 additions and 151 deletions

View File

@ -167,6 +167,19 @@ func (d *ModuleDiff) Empty() bool {
return true
}
// Instances returns the instance diffs for the id given. This can return
// multiple instance diffs if there are counts within the resource.
func (d *ModuleDiff) Instances(id string) []*InstanceDiff {
var result []*InstanceDiff
for k, diff := range d.Resources {
if strings.HasPrefix(k, id) && !diff.Empty() {
result = append(result, diff)
}
}
return result
}
// IsRoot says whether or not this module diff is for the root module.
func (d *ModuleDiff) IsRoot() bool {
return reflect.DeepEqual(d.Path, rootModulePath)

View File

@ -91,6 +91,10 @@ type GraphNodeResource struct {
Config *config.Resource
Resource *Resource
ResourceProviderNode string
// Expand, if true, indicates that this resource needs to be expanded
// at walk-time to multiple resources.
ExpandMode ResourceExpandMode
}
// GraphNodeResourceMeta is a node type in the graph that represents the
@ -123,6 +127,16 @@ type graphSharedProvider struct {
parentNoun *depgraph.Noun
}
// ResourceExpandMode specifies the expand behavior of the GraphNodeResource
// node.
type ResourceExpandMode byte
const (
ResourceExpandNone ResourceExpandMode = iota
ResourceExpandApply
ResourceExpandDestroy
)
// Graph builds a dependency graph of all the resources for infrastructure
// change.
//
@ -372,8 +386,28 @@ func graphAddConfigResources(
meta := g.Meta.(*GraphMeta)
// This tracks all the resource nouns
nouns := make(map[string]*depgraph.Noun)
for _, r := range c.Resources {
nounsList := make([]*depgraph.Noun, len(c.Resources))
for i, r := range c.Resources {
name := r.Id()
nounsList[i] = &depgraph.Noun{
Name: name,
Meta: &GraphNodeResource{
Index: -1,
Config: r,
Resource: &Resource{
Id: name,
Info: &InstanceInfo{
Id: name,
ModulePath: meta.ModulePath,
Type: r.Type,
},
},
ExpandMode: ResourceExpandApply,
},
}
/*
TODO: probably did something important, bring it back somehow
resourceNouns := make([]*depgraph.Noun, r.Count)
for i := 0; i < r.Count; i++ {
name := r.Id()
@ -468,12 +502,7 @@ func graphAddConfigResources(
for _, n := range resourceNouns {
nouns[n.Name] = n
}
}
// Build the list of nouns that we iterate over
nounsList := make([]*depgraph.Noun, 0, len(nouns))
for _, n := range nouns {
nounsList = append(nounsList, n)
*/
}
g.Name = "terraform"
@ -501,15 +530,28 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
continue
}
rd, ok := d.Resources[rn.Resource.Id]
if !ok {
change := false
destroy := false
diffs := d.Instances(rn.Resource.Id)
if len(diffs) == 0 {
continue
}
if rd.Empty() {
continue
for _, d := range diffs {
if d.Destroy {
destroy = true
}
if rd.Destroy {
if len(d.Attributes) > 0 {
change = true
}
}
var rd *InstanceDiff
if rn.ExpandMode == ResourceExpandNone {
rd = diffs[0]
}
if destroy {
// If we're destroying, we create a new destroy node with
// the proper dependencies. Perform a dirty copy operation.
newNode := new(GraphNodeResource)
@ -520,6 +562,11 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
// Make the diff _just_ the destroy.
newNode.Resource.Diff = &InstanceDiff{Destroy: true}
// Make sure ExpandDestroy is set if Expand
if newNode.ExpandMode == ResourceExpandApply {
newNode.ExpandMode = ResourceExpandDestroy
}
// Create the new node
newN := &depgraph.Noun{
Name: fmt.Sprintf("%s (destroy)", newNode.Resource.Id),
@ -533,17 +580,19 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
// Append it to the list so we handle it later
nlist = append(nlist, newN)
if rd != nil {
// Mark the old diff to not destroy since we handle that in
// the dedicated node.
newDiff := new(InstanceDiff)
*newDiff = *rd
newDiff.Destroy = false
rd = newDiff
}
// The dependency ordering depends on if the CreateBeforeDestroy
// flag is enabled. If so, we must create the replacement first,
// and then destroy the old instance.
if rn.Config != nil && rn.Config.Lifecycle.CreateBeforeDestroy && !rd.Empty() {
if rn.Config != nil && rn.Config.Lifecycle.CreateBeforeDestroy && change {
dep := &depgraph.Dependency{
Name: n.Name,
Source: newN,
@ -877,7 +926,7 @@ func graphAddOrphanDeps(g *depgraph.Graph, mod *ModuleState) {
}
for _, depName := range rs.Dependencies {
if compareName != depName {
if !strings.HasPrefix(depName, compareName) {
continue
}
dep := &depgraph.Dependency{
@ -886,6 +935,7 @@ func graphAddOrphanDeps(g *depgraph.Graph, mod *ModuleState) {
Target: n2,
}
n.Deps = append(n.Deps, dep)
break
}
}
}

View File

@ -7,7 +7,7 @@ import (
"testing"
)
func TestGraph(t *testing.T) {
func TestGraph_basic(t *testing.T) {
m := testModule(t, "graph-basic")
g, err := Graph(&GraphOpts{Module: m})
@ -447,6 +447,8 @@ func TestGraphAddDiff(t *testing.T) {
t.Fatalf("bad:\n\n%s", actual)
}
/*
TODO: test this somewhere
// Verify that the state has been added
n := g.Noun("aws_instance.foo")
rn := n.Meta.(*GraphNodeResource)
@ -456,6 +458,7 @@ func TestGraphAddDiff(t *testing.T) {
if !reflect.DeepEqual(actual2, expected2) {
t.Fatalf("bad: %#v", actual2)
}
*/
}
func TestGraphAddDiff_destroy(t *testing.T) {
@ -609,13 +612,11 @@ func TestGraphAddDiff_destroy_counts(t *testing.T) {
}
// Verify that the state has been added
n := g.Noun("aws_instance.web.0 (destroy)")
n := g.Noun("aws_instance.web (destroy)")
rn := n.Meta.(*GraphNodeResource)
expected2 := &InstanceDiff{Destroy: true}
actual2 := rn.Resource.Diff
if !reflect.DeepEqual(actual2, expected2) {
t.Fatalf("bad: %#v", actual2)
if rn.ExpandMode != ResourceExpandDestroy {
t.Fatalf("bad: %#v", rn)
}
// Verify that our original structure has not been modified
@ -816,13 +817,13 @@ func TestGraphEncodeDependencies_count(t *testing.T) {
// This should encode the dependency information into the state
graphEncodeDependencies(g)
web := g.Noun("aws_instance.web.0").Meta.(*GraphNodeResource).Resource
web := g.Noun("aws_instance.web").Meta.(*GraphNodeResource).Resource
if len(web.Dependencies) != 0 {
t.Fatalf("bad: %#v", web)
}
weblb := g.Noun("aws_load_balancer.weblb").Meta.(*GraphNodeResource).Resource
if len(weblb.Dependencies) != 3 {
if len(weblb.Dependencies) != 1 {
t.Fatalf("bad: %#v", weblb)
}
}
@ -956,12 +957,6 @@ root
const testTerraformGraphCountStr = `
root: root
aws_instance.web
aws_instance.web -> aws_instance.web.0
aws_instance.web -> aws_instance.web.1
aws_instance.web -> aws_instance.web.2
aws_instance.web.0
aws_instance.web.1
aws_instance.web.2
aws_load_balancer.weblb
aws_load_balancer.weblb -> aws_instance.web
root
@ -982,12 +977,7 @@ root
const testTerraformGraphDependsCountStr = `
root: root
aws_instance.db
aws_instance.db -> aws_instance.db.0
aws_instance.db -> aws_instance.db.1
aws_instance.db.0
aws_instance.db.0 -> aws_instance.web
aws_instance.db.1
aws_instance.db.1 -> aws_instance.web
aws_instance.db -> aws_instance.web
aws_instance.web
root
root -> aws_instance.db
@ -1024,21 +1014,9 @@ root
const testTerraformGraphDiffDestroyCountsStr = `
root: root
aws_instance.web
aws_instance.web -> aws_instance.web.0
aws_instance.web -> aws_instance.web.1
aws_instance.web -> aws_instance.web.2
aws_instance.web.0
aws_instance.web.0 -> aws_instance.web.0 (destroy)
aws_instance.web.0 (destroy)
aws_instance.web.0 (destroy) -> aws_load_balancer.weblb (destroy)
aws_instance.web.1
aws_instance.web.1 -> aws_instance.web.1 (destroy)
aws_instance.web.1 (destroy)
aws_instance.web.1 (destroy) -> aws_load_balancer.weblb (destroy)
aws_instance.web.2
aws_instance.web.2 -> aws_instance.web.2 (destroy)
aws_instance.web.2 (destroy)
aws_instance.web.2 (destroy) -> aws_load_balancer.weblb (destroy)
aws_instance.web -> aws_instance.web (destroy)
aws_instance.web (destroy)
aws_instance.web (destroy) -> aws_load_balancer.weblb (destroy)
aws_load_balancer.weblb
aws_load_balancer.weblb -> aws_instance.web
aws_load_balancer.weblb -> aws_load_balancer.weblb (destroy)
@ -1197,16 +1175,8 @@ root
const testTerraformGraphCountOrphanStr = `
root: root
aws_instance.web
aws_instance.web -> aws_instance.web.0
aws_instance.web -> aws_instance.web.1
aws_instance.web -> aws_instance.web.2
aws_instance.web.0
aws_instance.web.1
aws_instance.web.2
aws_load_balancer.old
aws_load_balancer.old -> aws_instance.web.0
aws_load_balancer.old -> aws_instance.web.1
aws_load_balancer.old -> aws_instance.web.2
aws_load_balancer.old -> aws_instance.web
aws_load_balancer.weblb
aws_load_balancer.weblb -> aws_instance.web
root