command: adhere to new API

This commit is contained in:
Mitchell Hashimoto 2014-06-25 18:22:42 -07:00
parent 4b263992cc
commit d64ba66edb
6 changed files with 72 additions and 11 deletions

View File

@ -86,16 +86,13 @@ func (c *ApplyCommand) Run(args []string) int {
return 1 return 1
} }
tfconfig := c.TFConfig tf, err := terraform.New(c.TFConfig)
tfconfig.Config = b
tf, err := terraform.New(tfconfig)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err)) c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err))
return 1 return 1
} }
plan, err := tf.Plan(state) plan, err := tf.Plan(b, state, nil)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err)) c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
return 1 return 1

View File

@ -60,16 +60,13 @@ func (c *PlanCommand) Run(args []string) int {
return 1 return 1
} }
tfconfig := c.TFConfig tf, err := terraform.New(c.TFConfig)
tfconfig.Config = b
tf, err := terraform.New(tfconfig)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err)) c.Ui.Error(fmt.Sprintf("Error initializing Terraform: %s", err))
return 1 return 1
} }
plan, err := tf.Plan(state) plan, err := tf.Plan(b, state, nil)
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err)) c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
return 1 return 1

View File

@ -1,6 +1,7 @@
package terraform package terraform
import ( import (
"bytes"
"encoding/gob" "encoding/gob"
"errors" "errors"
"fmt" "fmt"
@ -22,7 +23,12 @@ type Plan struct {
} }
func (p *Plan) String() string { 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() { func (p *Plan) init() {

View File

@ -49,6 +49,10 @@ func (s *State) Orphans(c *config.Config) []string {
} }
func (s *State) String() string { func (s *State) String() string {
if len(s.Resources) == 0 {
return "<no state>"
}
var buf bytes.Buffer var buf bytes.Buffer
names := make([]string, 0, len(s.Resources)) names := make([]string, 0, len(s.Resources))

View File

@ -109,6 +109,7 @@ func (t *Terraform) plan(
p := &Plan{ p := &Plan{
Config: c, Config: c,
Vars: vs, Vars: vs,
State: s,
} }
err := g.Walk(t.planWalkFn(p, vs)) err := g.Walk(t.planWalkFn(p, vs))
return p, err return p, err

View File

@ -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) { func TestTerraformRefresh(t *testing.T) {
rpAWS := new(MockResourceProvider) rpAWS := new(MockResourceProvider)
rpAWS.ResourcesReturn = []ResourceType{ rpAWS.ResourcesReturn = []ResourceType{
@ -431,15 +459,23 @@ aws_instance.foo:
` `
const testTerraformPlanStr = ` const testTerraformPlanStr = `
DIFF:
UPDATE: aws_instance.bar UPDATE: aws_instance.bar
foo: "" => "2" foo: "" => "2"
type: "" => "aws_instance" type: "" => "aws_instance"
UPDATE: aws_instance.foo UPDATE: aws_instance.foo
num: "" => "2" num: "" => "2"
type: "" => "aws_instance" type: "" => "aws_instance"
STATE:
<no state>
` `
const testTerraformPlanComputedStr = ` const testTerraformPlanComputedStr = `
DIFF:
UPDATE: aws_instance.bar UPDATE: aws_instance.bar
foo: "" => "<computed>" foo: "" => "<computed>"
type: "" => "aws_instance" type: "" => "aws_instance"
@ -447,4 +483,24 @@ UPDATE: aws_instance.foo
id: "" => "<computed>" id: "" => "<computed>"
num: "" => "2" num: "" => "2"
type: "" => "aws_instance" type: "" => "aws_instance"
STATE:
<no 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
` `