From 672bf58337babd5b186973566501d953b9944e0a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 24 Sep 2014 14:37:24 -0700 Subject: [PATCH] command: compiles, tests don't pass yet --- command/format_plan.go | 6 +++--- command/meta.go | 2 +- command/plan_test.go | 8 +++++--- terraform/diff.go | 11 +++++++++++ terraform/diff_test.go | 21 +++++++++++++++++++++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/command/format_plan.go b/command/format_plan.go index 0b17e9a26..e6485c9e0 100644 --- a/command/format_plan.go +++ b/command/format_plan.go @@ -27,15 +27,15 @@ func FormatPlan(p *terraform.Plan, c *colorstring.Colorize) string { // We want to output the resources in sorted order to make things // easier to scan through, so get all the resource names and sort them. - names := make([]string, 0, len(p.Diff.Resources)) - for name, _ := range p.Diff.Resources { + names := make([]string, 0, len(p.Diff.RootModule().Resources)) + for name, _ := range p.Diff.RootModule().Resources { names = append(names, name) } sort.Strings(names) // Go through each sorted name and start building the output for _, name := range names { - rdiff := p.Diff.Resources[name] + rdiff := p.Diff.RootModule().Resources[name] // Determine the color for the text (green for adding, yellow // for change, red for delete), and symbol, and output the diff --git a/command/meta.go b/command/meta.go index c7708eceb..4f66f47d5 100644 --- a/command/meta.go +++ b/command/meta.go @@ -99,7 +99,7 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) { return nil, false, fmt.Errorf("Error downloading modules: %s", err) } - opts.Config = mod.Config() + opts.Module = mod opts.State = state ctx := terraform.NewContext(opts) return ctx, false, nil diff --git a/command/plan_test.go b/command/plan_test.go index 76049f491..d981c2294 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -80,9 +80,11 @@ func TestPlan_destroy(t *testing.T) { } plan := testReadPlan(t, outPath) - for _, r := range plan.Diff.Resources { - if !r.Destroy { - t.Fatalf("bad: %#v", r) + for _, m := range plan.Diff.Modules { + for _, r := range m.Resources { + if !r.Destroy { + t.Fatalf("bad: %#v", r) + } } } diff --git a/terraform/diff.go b/terraform/diff.go index ac1cad0ea..7c25eb399 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -54,6 +54,17 @@ func (d *Diff) RootModule() *ModuleDiff { return root } +// Empty returns true if the diff has no changes. +func (d *Diff) Empty() bool { + for _, m := range d.Modules { + if !m.Empty() { + return false + } + } + + return true +} + func (d *Diff) String() string { var buf bytes.Buffer for _, m := range d.Modules { diff --git a/terraform/diff_test.go b/terraform/diff_test.go index bb9bd1594..920e24123 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -5,6 +5,27 @@ import ( "testing" ) +func TestDiffEmpty(t *testing.T) { + diff := new(Diff) + if !diff.Empty() { + t.Fatal("should be empty") + } + + mod := diff.AddModule(rootModulePath) + mod.Resources["nodeA"] = &InstanceDiff{ + Attributes: map[string]*ResourceAttrDiff{ + "foo": &ResourceAttrDiff{ + Old: "foo", + New: "bar", + }, + }, + } + + if diff.Empty() { + t.Fatal("should not be empty") + } +} + func TestModuleDiff_Empty(t *testing.T) { diff := new(ModuleDiff) if !diff.Empty() {