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,108 +386,123 @@ func graphAddConfigResources(
meta := g.Meta.(*GraphMeta)
// This tracks all the resource nouns
nouns := make(map[string]*depgraph.Noun)
for _, r := range c.Resources {
resourceNouns := make([]*depgraph.Noun, r.Count)
for i := 0; i < r.Count; i++ {
name := r.Id()
index := -1
// If we have a count that is more than one, then make sure
// we suffix with the number of the resource that this is.
if r.Count > 1 {
name = fmt.Sprintf("%s.%d", name, i)
index = i
}
var state *ResourceState
if mod != nil {
// Lookup the resource state
state = mod.Resources[name]
if state == nil {
if r.Count == 1 {
// If the count is one, check the state for ".0"
// appended, which might exist if we go from
// count > 1 to count == 1.
state = mod.Resources[r.Id()+".0"]
} else if i == 0 {
// If count is greater than one, check for state
// with just the ID, which might exist if we go
// from count == 1 to count > 1
state = mod.Resources[r.Id()]
}
// TODO(mitchellh): If one of the above works, delete
// the old style and just copy it to the new style.
}
}
if state == nil {
state = &ResourceState{
Type: r.Type,
}
}
flags := FlagPrimary
if len(state.Tainted) > 0 {
flags |= FlagHasTainted
}
resourceNouns[i] = &depgraph.Noun{
Name: name,
Meta: &GraphNodeResource{
Index: index,
Config: r,
Resource: &Resource{
Id: name,
Info: &InstanceInfo{
Id: name,
ModulePath: meta.ModulePath,
Type: r.Type,
},
State: state.Primary,
Config: NewResourceConfig(r.RawConfig),
Flags: flags,
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,
},
}
// If we have more than one, then create a meta node to track
// the resources.
if r.Count > 1 {
metaNoun := &depgraph.Noun{
Name: r.Id(),
Meta: &GraphNodeResourceMeta{
ID: r.Id(),
Name: r.Name,
Type: r.Type,
Count: r.Count,
},
/*
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()
index := -1
// If we have a count that is more than one, then make sure
// we suffix with the number of the resource that this is.
if r.Count > 1 {
name = fmt.Sprintf("%s.%d", name, i)
index = i
}
var state *ResourceState
if mod != nil {
// Lookup the resource state
state = mod.Resources[name]
if state == nil {
if r.Count == 1 {
// If the count is one, check the state for ".0"
// appended, which might exist if we go from
// count > 1 to count == 1.
state = mod.Resources[r.Id()+".0"]
} else if i == 0 {
// If count is greater than one, check for state
// with just the ID, which might exist if we go
// from count == 1 to count > 1
state = mod.Resources[r.Id()]
}
// TODO(mitchellh): If one of the above works, delete
// the old style and just copy it to the new style.
}
}
if state == nil {
state = &ResourceState{
Type: r.Type,
}
}
flags := FlagPrimary
if len(state.Tainted) > 0 {
flags |= FlagHasTainted
}
resourceNouns[i] = &depgraph.Noun{
Name: name,
Meta: &GraphNodeResource{
Index: index,
Config: r,
Resource: &Resource{
Id: name,
Info: &InstanceInfo{
Id: name,
ModulePath: meta.ModulePath,
Type: r.Type,
},
State: state.Primary,
Config: NewResourceConfig(r.RawConfig),
Flags: flags,
},
},
}
}
// If we have more than one, then create a meta node to track
// the resources.
if r.Count > 1 {
metaNoun := &depgraph.Noun{
Name: r.Id(),
Meta: &GraphNodeResourceMeta{
ID: r.Id(),
Name: r.Name,
Type: r.Type,
Count: r.Count,
},
}
// Create the dependencies on this noun
for _, n := range resourceNouns {
metaNoun.Deps = append(metaNoun.Deps, &depgraph.Dependency{
Name: n.Name,
Source: metaNoun,
Target: n,
})
}
// Assign it to the map so that we have it
nouns[metaNoun.Name] = metaNoun
}
// Create the dependencies on this noun
for _, n := range resourceNouns {
metaNoun.Deps = append(metaNoun.Deps, &depgraph.Dependency{
Name: n.Name,
Source: metaNoun,
Target: n,
})
nouns[n.Name] = n
}
// Assign it to the map so that we have it
nouns[metaNoun.Name] = metaNoun
}
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 len(d.Attributes) > 0 {
change = true
}
}
if rd.Destroy {
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)
// 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
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