terraform: don't use Meta node anymore
This commit is contained in:
parent
95f43d8230
commit
4fe0c4ada4
|
@ -167,6 +167,19 @@ func (d *ModuleDiff) Empty() bool {
|
||||||
return true
|
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.
|
// IsRoot says whether or not this module diff is for the root module.
|
||||||
func (d *ModuleDiff) IsRoot() bool {
|
func (d *ModuleDiff) IsRoot() bool {
|
||||||
return reflect.DeepEqual(d.Path, rootModulePath)
|
return reflect.DeepEqual(d.Path, rootModulePath)
|
||||||
|
|
|
@ -91,6 +91,10 @@ type GraphNodeResource struct {
|
||||||
Config *config.Resource
|
Config *config.Resource
|
||||||
Resource *Resource
|
Resource *Resource
|
||||||
ResourceProviderNode string
|
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
|
// GraphNodeResourceMeta is a node type in the graph that represents the
|
||||||
|
@ -123,6 +127,16 @@ type graphSharedProvider struct {
|
||||||
parentNoun *depgraph.Noun
|
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
|
// Graph builds a dependency graph of all the resources for infrastructure
|
||||||
// change.
|
// change.
|
||||||
//
|
//
|
||||||
|
@ -372,8 +386,28 @@ func graphAddConfigResources(
|
||||||
meta := g.Meta.(*GraphMeta)
|
meta := g.Meta.(*GraphMeta)
|
||||||
|
|
||||||
// This tracks all the resource nouns
|
// This tracks all the resource nouns
|
||||||
nouns := make(map[string]*depgraph.Noun)
|
nounsList := make([]*depgraph.Noun, len(c.Resources))
|
||||||
for _, r := range 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)
|
resourceNouns := make([]*depgraph.Noun, r.Count)
|
||||||
for i := 0; i < r.Count; i++ {
|
for i := 0; i < r.Count; i++ {
|
||||||
name := r.Id()
|
name := r.Id()
|
||||||
|
@ -468,12 +502,7 @@ func graphAddConfigResources(
|
||||||
for _, n := range resourceNouns {
|
for _, n := range resourceNouns {
|
||||||
nouns[n.Name] = n
|
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"
|
g.Name = "terraform"
|
||||||
|
@ -501,15 +530,28 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
rd, ok := d.Resources[rn.Resource.Id]
|
change := false
|
||||||
if !ok {
|
destroy := false
|
||||||
|
diffs := d.Instances(rn.Resource.Id)
|
||||||
|
if len(diffs) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if rd.Empty() {
|
for _, d := range diffs {
|
||||||
continue
|
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
|
// If we're destroying, we create a new destroy node with
|
||||||
// the proper dependencies. Perform a dirty copy operation.
|
// the proper dependencies. Perform a dirty copy operation.
|
||||||
newNode := new(GraphNodeResource)
|
newNode := new(GraphNodeResource)
|
||||||
|
@ -520,6 +562,11 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
|
||||||
// Make the diff _just_ the destroy.
|
// Make the diff _just_ the destroy.
|
||||||
newNode.Resource.Diff = &InstanceDiff{Destroy: true}
|
newNode.Resource.Diff = &InstanceDiff{Destroy: true}
|
||||||
|
|
||||||
|
// Make sure ExpandDestroy is set if Expand
|
||||||
|
if newNode.ExpandMode == ResourceExpandApply {
|
||||||
|
newNode.ExpandMode = ResourceExpandDestroy
|
||||||
|
}
|
||||||
|
|
||||||
// Create the new node
|
// Create the new node
|
||||||
newN := &depgraph.Noun{
|
newN := &depgraph.Noun{
|
||||||
Name: fmt.Sprintf("%s (destroy)", newNode.Resource.Id),
|
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
|
// Append it to the list so we handle it later
|
||||||
nlist = append(nlist, newN)
|
nlist = append(nlist, newN)
|
||||||
|
|
||||||
|
if rd != nil {
|
||||||
// Mark the old diff to not destroy since we handle that in
|
// Mark the old diff to not destroy since we handle that in
|
||||||
// the dedicated node.
|
// the dedicated node.
|
||||||
newDiff := new(InstanceDiff)
|
newDiff := new(InstanceDiff)
|
||||||
*newDiff = *rd
|
*newDiff = *rd
|
||||||
newDiff.Destroy = false
|
newDiff.Destroy = false
|
||||||
rd = newDiff
|
rd = newDiff
|
||||||
|
}
|
||||||
|
|
||||||
// The dependency ordering depends on if the CreateBeforeDestroy
|
// The dependency ordering depends on if the CreateBeforeDestroy
|
||||||
// flag is enabled. If so, we must create the replacement first,
|
// flag is enabled. If so, we must create the replacement first,
|
||||||
// and then destroy the old instance.
|
// 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{
|
dep := &depgraph.Dependency{
|
||||||
Name: n.Name,
|
Name: n.Name,
|
||||||
Source: newN,
|
Source: newN,
|
||||||
|
@ -877,7 +926,7 @@ func graphAddOrphanDeps(g *depgraph.Graph, mod *ModuleState) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, depName := range rs.Dependencies {
|
for _, depName := range rs.Dependencies {
|
||||||
if compareName != depName {
|
if !strings.HasPrefix(depName, compareName) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
dep := &depgraph.Dependency{
|
dep := &depgraph.Dependency{
|
||||||
|
@ -886,6 +935,7 @@ func graphAddOrphanDeps(g *depgraph.Graph, mod *ModuleState) {
|
||||||
Target: n2,
|
Target: n2,
|
||||||
}
|
}
|
||||||
n.Deps = append(n.Deps, dep)
|
n.Deps = append(n.Deps, dep)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGraph(t *testing.T) {
|
func TestGraph_basic(t *testing.T) {
|
||||||
m := testModule(t, "graph-basic")
|
m := testModule(t, "graph-basic")
|
||||||
|
|
||||||
g, err := Graph(&GraphOpts{Module: m})
|
g, err := Graph(&GraphOpts{Module: m})
|
||||||
|
@ -447,6 +447,8 @@ func TestGraphAddDiff(t *testing.T) {
|
||||||
t.Fatalf("bad:\n\n%s", actual)
|
t.Fatalf("bad:\n\n%s", actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO: test this somewhere
|
||||||
// Verify that the state has been added
|
// Verify that the state has been added
|
||||||
n := g.Noun("aws_instance.foo")
|
n := g.Noun("aws_instance.foo")
|
||||||
rn := n.Meta.(*GraphNodeResource)
|
rn := n.Meta.(*GraphNodeResource)
|
||||||
|
@ -456,6 +458,7 @@ func TestGraphAddDiff(t *testing.T) {
|
||||||
if !reflect.DeepEqual(actual2, expected2) {
|
if !reflect.DeepEqual(actual2, expected2) {
|
||||||
t.Fatalf("bad: %#v", actual2)
|
t.Fatalf("bad: %#v", actual2)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGraphAddDiff_destroy(t *testing.T) {
|
func TestGraphAddDiff_destroy(t *testing.T) {
|
||||||
|
@ -609,13 +612,11 @@ func TestGraphAddDiff_destroy_counts(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the state has been added
|
// 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)
|
rn := n.Meta.(*GraphNodeResource)
|
||||||
|
|
||||||
expected2 := &InstanceDiff{Destroy: true}
|
if rn.ExpandMode != ResourceExpandDestroy {
|
||||||
actual2 := rn.Resource.Diff
|
t.Fatalf("bad: %#v", rn)
|
||||||
if !reflect.DeepEqual(actual2, expected2) {
|
|
||||||
t.Fatalf("bad: %#v", actual2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that our original structure has not been modified
|
// 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
|
// This should encode the dependency information into the state
|
||||||
graphEncodeDependencies(g)
|
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 {
|
if len(web.Dependencies) != 0 {
|
||||||
t.Fatalf("bad: %#v", web)
|
t.Fatalf("bad: %#v", web)
|
||||||
}
|
}
|
||||||
|
|
||||||
weblb := g.Noun("aws_load_balancer.weblb").Meta.(*GraphNodeResource).Resource
|
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)
|
t.Fatalf("bad: %#v", weblb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -956,12 +957,6 @@ root
|
||||||
const testTerraformGraphCountStr = `
|
const testTerraformGraphCountStr = `
|
||||||
root: root
|
root: root
|
||||||
aws_instance.web
|
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_load_balancer.weblb -> aws_instance.web
|
aws_load_balancer.weblb -> aws_instance.web
|
||||||
root
|
root
|
||||||
|
@ -982,12 +977,7 @@ root
|
||||||
const testTerraformGraphDependsCountStr = `
|
const testTerraformGraphDependsCountStr = `
|
||||||
root: root
|
root: root
|
||||||
aws_instance.db
|
aws_instance.db
|
||||||
aws_instance.db -> aws_instance.db.0
|
aws_instance.db -> aws_instance.web
|
||||||
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.web
|
aws_instance.web
|
||||||
root
|
root
|
||||||
root -> aws_instance.db
|
root -> aws_instance.db
|
||||||
|
@ -1024,21 +1014,9 @@ root
|
||||||
const testTerraformGraphDiffDestroyCountsStr = `
|
const testTerraformGraphDiffDestroyCountsStr = `
|
||||||
root: root
|
root: root
|
||||||
aws_instance.web
|
aws_instance.web
|
||||||
aws_instance.web -> aws_instance.web.0
|
aws_instance.web -> aws_instance.web (destroy)
|
||||||
aws_instance.web -> aws_instance.web.1
|
aws_instance.web (destroy)
|
||||||
aws_instance.web -> aws_instance.web.2
|
aws_instance.web (destroy) -> aws_load_balancer.weblb (destroy)
|
||||||
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_load_balancer.weblb
|
aws_load_balancer.weblb
|
||||||
aws_load_balancer.weblb -> aws_instance.web
|
aws_load_balancer.weblb -> aws_instance.web
|
||||||
aws_load_balancer.weblb -> aws_load_balancer.weblb (destroy)
|
aws_load_balancer.weblb -> aws_load_balancer.weblb (destroy)
|
||||||
|
@ -1197,16 +1175,8 @@ root
|
||||||
const testTerraformGraphCountOrphanStr = `
|
const testTerraformGraphCountOrphanStr = `
|
||||||
root: root
|
root: root
|
||||||
aws_instance.web
|
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_load_balancer.old -> aws_instance.web.0
|
aws_load_balancer.old -> aws_instance.web
|
||||||
aws_load_balancer.old -> aws_instance.web.1
|
|
||||||
aws_load_balancer.old -> aws_instance.web.2
|
|
||||||
aws_load_balancer.weblb
|
aws_load_balancer.weblb
|
||||||
aws_load_balancer.weblb -> aws_instance.web
|
aws_load_balancer.weblb -> aws_instance.web
|
||||||
root
|
root
|
||||||
|
|
Loading…
Reference in New Issue