From d64ba66edb299b3322a1da7db199186b14be9390 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Jun 2014 18:22:42 -0700 Subject: [PATCH] command: adhere to new API --- command/apply.go | 7 ++--- command/plan.go | 7 ++--- terraform/plan.go | 8 +++++- terraform/state.go | 4 +++ terraform/terraform.go | 1 + terraform/terraform_test.go | 56 +++++++++++++++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 11 deletions(-) diff --git a/command/apply.go b/command/apply.go index f4a8450007..1ab8188ee0 100644 --- a/command/apply.go +++ b/command/apply.go @@ -86,16 +86,13 @@ func (c *ApplyCommand) Run(args []string) int { return 1 } - tfconfig := c.TFConfig - tfconfig.Config = b - - tf, err := terraform.New(tfconfig) + tf, err := terraform.New(c.TFConfig) if err != nil { c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err)) return 1 } - plan, err := tf.Plan(state) + plan, err := tf.Plan(b, state, nil) if err != nil { c.Ui.Error(fmt.Sprintf("Error running plan: %s", err)) return 1 diff --git a/command/plan.go b/command/plan.go index e72e894c63..2437e9b8dc 100644 --- a/command/plan.go +++ b/command/plan.go @@ -60,16 +60,13 @@ func (c *PlanCommand) Run(args []string) int { return 1 } - tfconfig := c.TFConfig - tfconfig.Config = b - - tf, err := terraform.New(tfconfig) + tf, err := terraform.New(c.TFConfig) if err != nil { c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err)) return 1 } - plan, err := tf.Plan(state) + plan, err := tf.Plan(b, state, nil) if err != nil { c.Ui.Error(fmt.Sprintf("Error running plan: %s", err)) return 1 diff --git a/terraform/plan.go b/terraform/plan.go index d765775cf3..f2ecb123d4 100644 --- a/terraform/plan.go +++ b/terraform/plan.go @@ -1,6 +1,7 @@ package terraform import ( + "bytes" "encoding/gob" "errors" "fmt" @@ -22,7 +23,12 @@ type Plan struct { } func (p *Plan) String() string { - return p.Diff.String() + buf := new(bytes.Buffer) + buf.WriteString("DIFF:\n\n") + buf.WriteString(p.Diff.String()) + buf.WriteString("\nSTATE:\n\n") + buf.WriteString(p.State.String()) + return buf.String() } func (p *Plan) init() { diff --git a/terraform/state.go b/terraform/state.go index 4b4f2906e1..8fa84b8888 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -49,6 +49,10 @@ func (s *State) Orphans(c *config.Config) []string { } func (s *State) String() string { + if len(s.Resources) == 0 { + return "" + } + var buf bytes.Buffer names := make([]string, 0, len(s.Resources)) diff --git a/terraform/terraform.go b/terraform/terraform.go index 795338ca99..ea265c0e32 100644 --- a/terraform/terraform.go +++ b/terraform/terraform.go @@ -109,6 +109,7 @@ func (t *Terraform) plan( p := &Plan{ Config: c, Vars: vs, + State: s, } err := g.Walk(t.planWalkFn(p, vs)) return p, err diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 33cafc35de..f0fdbd616e 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -161,6 +161,34 @@ func TestTerraformPlan_computed(t *testing.T) { } } +func TestTerraformPlan_state(t *testing.T) { + c := testConfig(t, "plan-good") + tf := testTerraform2(t, nil) + + s := &State{ + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + ID: "bar", + }, + }, + } + + plan, err := tf.Plan(c, s, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + if len(plan.Diff.Resources) < 2 { + t.Fatalf("bad: %#v", plan.Diff.Resources) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanStateStr) + if actual != expected { + t.Fatalf("bad:\n%s", actual) + } +} + func TestTerraformRefresh(t *testing.T) { rpAWS := new(MockResourceProvider) rpAWS.ResourcesReturn = []ResourceType{ @@ -431,15 +459,23 @@ aws_instance.foo: ` const testTerraformPlanStr = ` +DIFF: + UPDATE: aws_instance.bar foo: "" => "2" type: "" => "aws_instance" UPDATE: aws_instance.foo num: "" => "2" type: "" => "aws_instance" + +STATE: + + ` const testTerraformPlanComputedStr = ` +DIFF: + UPDATE: aws_instance.bar foo: "" => "" type: "" => "aws_instance" @@ -447,4 +483,24 @@ UPDATE: aws_instance.foo id: "" => "" num: "" => "2" type: "" => "aws_instance" + +STATE: + + +` + +const testTerraformPlanStateStr = ` +DIFF: + +UPDATE: aws_instance.bar + foo: "" => "2" + type: "" => "aws_instance" +UPDATE: aws_instance.foo + num: "" => "2" + type: "" => "" + +STATE: + +aws_instance.foo: + ID = bar `