diff --git a/terraform/context.go b/terraform/context.go index 1eaeada139..a48e46863d 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -362,7 +362,7 @@ func (c *Context) Apply() (*State, error) { graph, err = c.Graph(&ContextGraphOpts{Validate: true}) } else { graph, err = (&ApplyGraphBuilder{ - Config: c.module, + Module: c.module, Diff: c.diff, State: c.state, Providers: c.providersList(), diff --git a/terraform/graph_builder_apply.go b/terraform/graph_builder_apply.go index d96c2d3f21..8dde58519e 100644 --- a/terraform/graph_builder_apply.go +++ b/terraform/graph_builder_apply.go @@ -12,8 +12,8 @@ import ( // 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. type ApplyGraphBuilder struct { - // Config is the root module for the graph to build. - Config *module.Tree + // Module is the root module for the graph to build. + Module *module.Tree // Diff is the diff to apply. Diff *Diff @@ -42,7 +42,7 @@ func (b *ApplyGraphBuilder) Steps() []GraphTransformer { // Creates all the nodes represented in the diff. &DiffTransformer{ Diff: b.Diff, - Config: b.Config, + Module: b.Module, State: b.State, }, diff --git a/terraform/test-fixtures/transform-diff-basic/main.tf b/terraform/test-fixtures/transform-diff-basic/main.tf new file mode 100644 index 0000000000..919f140bba --- /dev/null +++ b/terraform/test-fixtures/transform-diff-basic/main.tf @@ -0,0 +1 @@ +resource "aws_instance" "foo" {} diff --git a/terraform/transform_diff.go b/terraform/transform_diff.go index c154fa7f8d..f0455f5e39 100644 --- a/terraform/transform_diff.go +++ b/terraform/transform_diff.go @@ -12,14 +12,14 @@ import ( // This transform is used for example by the ApplyGraphBuilder to ensure // 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 // complete graph (such as create-before-destroy information). The graph // is built based on the diff first, though, ensuring that only resources // that are being modified are present in the graph. type DiffTransformer struct { Diff *Diff - Config *module.Tree + Module *module.Tree State *State } @@ -70,7 +70,7 @@ func (t *DiffTransformer) Transform(g *Graph) error { // Annotate all nodes with their config and state for _, n := range nodes { // 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 { // Get a resource address so we can compare addr, err := parseResourceAddressConfig(r) diff --git a/terraform/transform_diff_test.go b/terraform/transform_diff_test.go new file mode 100644 index 0000000000..c3e3176e64 --- /dev/null +++ b/terraform/transform_diff_test.go @@ -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 +`