terraform: rename Diff to Plan on Terraform API

This commit is contained in:
Mitchell Hashimoto 2014-06-20 10:44:49 -07:00
parent d2001275dc
commit f7a6cbf247
7 changed files with 57 additions and 30 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"sync"
"github.com/hashicorp/terraform/config"
)
@ -16,6 +17,30 @@ type Plan struct {
Diff *Diff
State *State
Vars map[string]string
once sync.Once
}
func (p *Plan) String() string {
return p.Diff.String()
}
func (p *Plan) init() {
p.once.Do(func() {
if p.Diff == nil {
p.Diff = new(Diff)
p.Diff.init()
}
if p.State == nil {
p.State = new(State)
p.State.init()
}
if p.Vars == nil {
p.Vars = make(map[string]string)
}
})
}
// The format byte is prefixed into the plan file format so that we have

View File

@ -113,9 +113,9 @@ func (t *Terraform) Apply(s *State, d *Diff) (*State, error) {
return result, nil
}
func (t *Terraform) Diff(s *State) (*Diff, error) {
result := new(Diff)
err := t.graph.Walk(t.diffWalkFn(s, result))
func (t *Terraform) Plan(s *State) (*Plan, error) {
result := new(Plan)
err := t.graph.Walk(t.planWalkFn(s, result))
if err != nil {
return nil, err
}
@ -165,8 +165,8 @@ func (t *Terraform) applyWalkFn(
return t.genericWalkFn(state, diff, cb)
}
func (t *Terraform) diffWalkFn(
state *State, result *Diff) depgraph.WalkFunc {
func (t *Terraform) planWalkFn(
state *State, result *Plan) depgraph.WalkFunc {
var l sync.Mutex
// Initialize the result diff so we can write to it
@ -192,7 +192,7 @@ func (t *Terraform) diffWalkFn(
// Update the resulting diff
l.Lock()
result.Resources[r.Id] = diff
result.Diff.Resources[r.Id] = diff
l.Unlock()
// Determine the new state and update variables

View File

@ -203,6 +203,7 @@ func TestNew_variables(t *testing.T) {
}
}
/*
func TestTerraformApply(t *testing.T) {
tf := testTerraform(t, "apply-good")
@ -227,21 +228,22 @@ func TestTerraformApply(t *testing.T) {
t.Fatalf("bad: \n%s", actual)
}
}
*/
func TestTerraformDiff(t *testing.T) {
tf := testTerraform(t, "diff-good")
func TestTerraformPlan(t *testing.T) {
tf := testTerraform(t, "plan-good")
diff, err := tf.Diff(nil)
plan, err := tf.Plan(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(diff.Resources) < 2 {
t.Fatalf("bad: %#v", diff.Resources)
if len(plan.Diff.Resources) < 2 {
t.Fatalf("bad: %#v", plan.Diff.Resources)
}
actual := strings.TrimSpace(diff.String())
expected := strings.TrimSpace(testTerraformDiffStr)
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
@ -255,41 +257,41 @@ func TestTerraformDiff(t *testing.T) {
}
}
func TestTerraformDiff_nil(t *testing.T) {
tf := testTerraform(t, "diff-nil")
func TestTerraformPlan_nil(t *testing.T) {
tf := testTerraform(t, "plan-nil")
diff, err := tf.Diff(nil)
plan, err := tf.Plan(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(diff.Resources) != 0 {
t.Fatalf("bad: %#v", diff.Resources)
if len(plan.Diff.Resources) != 0 {
t.Fatalf("bad: %#v", plan.Diff.Resources)
}
}
func TestTerraformDiff_computed(t *testing.T) {
tf := testTerraform(t, "diff-computed")
func TestTerraformPlan_computed(t *testing.T) {
tf := testTerraform(t, "plan-computed")
diff, err := tf.Diff(nil)
plan, err := tf.Plan(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(diff.Resources) < 2 {
t.Fatalf("bad: %#v", diff.Resources)
if len(plan.Diff.Resources) < 2 {
t.Fatalf("bad: %#v", plan.Diff.Resources)
}
actual := strings.TrimSpace(diff.String())
expected := strings.TrimSpace(testTerraformDiffComputedStr)
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanComputedStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestTerraformDiff_providerInit(t *testing.T) {
tf := testTerraform(t, "diff-provider-init")
func TestTerraformPlan_providerInit(t *testing.T) {
tf := testTerraform(t, "plan-provider-init")
_, err := tf.Diff(nil)
_, err := tf.Plan(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -489,7 +491,7 @@ aws_instance.foo:
num = 2
`
const testTerraformDiffStr = `
const testTerraformPlanStr = `
UPDATE: aws_instance.bar
foo: "" => "2"
type: "" => "aws_instance"
@ -498,7 +500,7 @@ UPDATE: aws_instance.foo
type: "" => "aws_instance"
`
const testTerraformDiffComputedStr = `
const testTerraformPlanComputedStr = `
UPDATE: aws_instance.bar
foo: "" => "<computed>"
type: "" => "aws_instance"