terraform: rename Config to Module, tests for diff transform

This commit is contained in:
Mitchell Hashimoto 2016-09-14 11:44:41 -07:00
parent b0bae6dbfe
commit 9ea9e52185
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 68 additions and 7 deletions

View File

@ -362,7 +362,7 @@ func (c *Context) Apply() (*State, error) {
graph, err = c.Graph(&ContextGraphOpts{Validate: true}) graph, err = c.Graph(&ContextGraphOpts{Validate: true})
} else { } else {
graph, err = (&ApplyGraphBuilder{ graph, err = (&ApplyGraphBuilder{
Config: c.module, Module: c.module,
Diff: c.diff, Diff: c.diff,
State: c.state, State: c.state,
Providers: c.providersList(), Providers: c.providersList(),

View File

@ -12,8 +12,8 @@ import (
// that aren't explicitly in the diff. There are other scenarios where the // that aren't explicitly in the diff. There are other scenarios where the
// diff can be deviated, so this is just one layer of protection. // diff can be deviated, so this is just one layer of protection.
type ApplyGraphBuilder struct { type ApplyGraphBuilder struct {
// Config is the root module for the graph to build. // Module is the root module for the graph to build.
Config *module.Tree Module *module.Tree
// Diff is the diff to apply. // Diff is the diff to apply.
Diff *Diff Diff *Diff
@ -42,7 +42,7 @@ func (b *ApplyGraphBuilder) Steps() []GraphTransformer {
// Creates all the nodes represented in the diff. // Creates all the nodes represented in the diff.
&DiffTransformer{ &DiffTransformer{
Diff: b.Diff, Diff: b.Diff,
Config: b.Config, Module: b.Module,
State: b.State, State: b.State,
}, },

View File

@ -0,0 +1 @@
resource "aws_instance" "foo" {}

View File

@ -12,14 +12,14 @@ import (
// This transform is used for example by the ApplyGraphBuilder to ensure // This transform is used for example by the ApplyGraphBuilder to ensure
// that only resources that are being modified are represented in the graph. // that only resources that are being modified are represented in the graph.
// //
// Config and State is still required for the DiffTransformer for annotations // Module and State is still required for the DiffTransformer for annotations
// since the Diff doesn't contain all the information required to build the // since the Diff doesn't contain all the information required to build the
// complete graph (such as create-before-destroy information). The graph // complete graph (such as create-before-destroy information). The graph
// is built based on the diff first, though, ensuring that only resources // is built based on the diff first, though, ensuring that only resources
// that are being modified are present in the graph. // that are being modified are present in the graph.
type DiffTransformer struct { type DiffTransformer struct {
Diff *Diff Diff *Diff
Config *module.Tree Module *module.Tree
State *State State *State
} }
@ -70,7 +70,7 @@ func (t *DiffTransformer) Transform(g *Graph) error {
// Annotate all nodes with their config and state // Annotate all nodes with their config and state
for _, n := range nodes { for _, n := range nodes {
// Grab the configuration at this path. // Grab the configuration at this path.
if t := t.Config.Child(n.Addr.Path); t != nil { if t := t.Module.Child(n.Addr.Path); t != nil {
for _, r := range t.Config().Resources { for _, r := range t.Config().Resources {
// Get a resource address so we can compare // Get a resource address so we can compare
addr, err := parseResourceAddressConfig(r) addr, err := parseResourceAddressConfig(r)

View File

@ -0,0 +1,60 @@
package terraform
import (
"strings"
"testing"
)
func TestDiffTransformer_nilDiff(t *testing.T) {
g := Graph{Path: RootModulePath}
tf := &DiffTransformer{}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
if len(g.Vertices()) > 0 {
t.Fatal("graph should be empty")
}
}
func TestDiffTransformer(t *testing.T) {
g := Graph{Path: RootModulePath}
tf := &DiffTransformer{
Module: testModule(t, "transform-diff-basic"),
Diff: &Diff{
Modules: []*ModuleDiff{
&ModuleDiff{
Path: []string{"root"},
Resources: map[string]*InstanceDiff{
"aws_instance.foo": &InstanceDiff{
Attributes: map[string]*ResourceAttrDiff{
"name": &ResourceAttrDiff{
Old: "",
New: "foo",
},
},
},
},
},
},
},
}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformDiffBasicStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
v := g.Vertices()[0].(*NodeApplyableResource)
if v.Config == nil {
t.Fatal("no config")
}
}
const testTransformDiffBasicStr = `
aws_instance.foo
`