terraform: PlanOpts for expanding plan configuration

This commit is contained in:
Mitchell Hashimoto 2014-06-28 13:10:11 -07:00
parent e44f2548db
commit 5c836ab861
5 changed files with 51 additions and 29 deletions

View File

@ -160,7 +160,10 @@ func (c *ApplyCommand) configToPlan(
return nil, fmt.Errorf("Error loading config: %s", err)
}
plan, err := tf.Plan(config, state, nil)
plan, err := tf.Plan(&terraform.PlanOpts{
Config: config,
State: state,
})
if err != nil {
return nil, fmt.Errorf("Error running plan: %s", err)
}

View File

@ -79,7 +79,10 @@ func (c *PlanCommand) Run(args []string) int {
}
}
plan, err := tf.Plan(b, state, nil)
plan, err := tf.Plan(&terraform.PlanOpts{
Config: b,
State: state,
})
if err != nil {
c.Ui.Error(fmt.Sprintf("Error running plan: %s", err))
return 1

View File

@ -11,6 +11,19 @@ import (
"github.com/hashicorp/terraform/config"
)
// PlanOpts are the options used to generate an execution plan for
// Terraform.
type PlanOpts struct {
// If set to true, then the generated plan will destroy all resources
// that are created. Otherwise, it will move towards the desired state
// specified in the configuration.
Destroy bool
Config *config.Config
State *State
Vars map[string]string
}
// Plan represents a single Terraform execution plan, which contains
// all the information necessary to make an infrastructure change.
type Plan struct {

View File

@ -79,14 +79,13 @@ func (t *Terraform) Graph(c *config.Config, s *State) (*depgraph.Graph, error) {
return g, nil
}
func (t *Terraform) Plan(
c *config.Config, s *State, vs map[string]string) (*Plan, error) {
g, err := t.Graph(c, s)
func (t *Terraform) Plan(opts *PlanOpts) (*Plan, error) {
g, err := t.Graph(opts.Config, opts.State)
if err != nil {
return nil, err
}
return t.plan(g, c, s, vs)
return t.plan(g, opts)
}
// Refresh goes through all the resources in the state and refreshes them
@ -108,17 +107,13 @@ func (t *Terraform) apply(
return s, err
}
func (t *Terraform) plan(
g *depgraph.Graph,
c *config.Config,
s *State,
vs map[string]string) (*Plan, error) {
func (t *Terraform) plan(g *depgraph.Graph, opts *PlanOpts) (*Plan, error) {
p := &Plan{
Config: c,
Vars: vs,
State: s,
Config: opts.Config,
Vars: opts.Vars,
State: opts.State,
}
err := g.Walk(t.planWalkFn(p, vs))
err := g.Walk(t.planWalkFn(p, opts))
return p, err
}
@ -247,8 +242,7 @@ func (t *Terraform) applyWalkFn(
return t.genericWalkFn(vs, cb)
}
func (t *Terraform) planWalkFn(
result *Plan, vs map[string]string) depgraph.WalkFunc {
func (t *Terraform) planWalkFn(result *Plan, opts *PlanOpts) depgraph.WalkFunc {
var l sync.Mutex
// Initialize the result
@ -303,7 +297,7 @@ func (t *Terraform) planWalkFn(
return vars, nil
}
return t.genericWalkFn(vs, cb)
return t.genericWalkFn(opts.Vars, cb)
}
func (t *Terraform) genericWalkFn(

View File

@ -17,7 +17,7 @@ func TestTerraformApply(t *testing.T) {
c := testConfig(t, "apply-good")
tf := testTerraform2(t, nil)
p, err := tf.Plan(c, nil, nil)
p, err := tf.Plan(&PlanOpts{Config: c})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -44,7 +44,7 @@ func TestTerraformApply_compute(t *testing.T) {
c := testConfig(t, "apply-compute")
tf := testTerraform2(t, nil)
p, err := tf.Plan(c, nil, nil)
p, err := tf.Plan(&PlanOpts{Config: c})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -70,7 +70,7 @@ func TestTerraformApply_hook(t *testing.T) {
Hooks: []Hook{h},
})
p, err := tf.Plan(c, nil, nil)
p, err := tf.Plan(&PlanOpts{Config: c})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -91,7 +91,7 @@ func TestTerraformApply_unknownAttribute(t *testing.T) {
c := testConfig(t, "apply-unknown")
tf := testTerraform2(t, nil)
p, err := tf.Plan(c, nil, nil)
p, err := tf.Plan(&PlanOpts{Config: c})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -112,7 +112,10 @@ func TestTerraformApply_vars(t *testing.T) {
c := testConfig(t, "apply-vars")
tf := testTerraform2(t, nil)
p, err := tf.Plan(c, nil, map[string]string{"foo": "baz"})
p, err := tf.Plan(&PlanOpts{
Config: c,
Vars: map[string]string{"foo": "baz"},
})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -136,7 +139,7 @@ func TestTerraformPlan(t *testing.T) {
c := testConfig(t, "plan-good")
tf := testTerraform2(t, nil)
plan, err := tf.Plan(c, nil, nil)
plan, err := tf.Plan(&PlanOpts{Config: c})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -156,7 +159,7 @@ func TestTerraformPlan_nil(t *testing.T) {
c := testConfig(t, "plan-nil")
tf := testTerraform2(t, nil)
plan, err := tf.Plan(c, nil, nil)
plan, err := tf.Plan(&PlanOpts{Config: c})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -169,7 +172,7 @@ func TestTerraformPlan_computed(t *testing.T) {
c := testConfig(t, "plan-computed")
tf := testTerraform2(t, nil)
plan, err := tf.Plan(c, nil, nil)
plan, err := tf.Plan(&PlanOpts{Config: c})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -192,7 +195,7 @@ func TestTerraformPlan_hook(t *testing.T) {
Hooks: []Hook{h},
})
if _, err := tf.Plan(c, nil, nil); err != nil {
if _, err := tf.Plan(&PlanOpts{Config: c}); err != nil {
t.Fatalf("err: %s", err)
}
if !h.PreDiffCalled {
@ -216,7 +219,10 @@ func TestTerraformPlan_orphan(t *testing.T) {
},
}
plan, err := tf.Plan(c, s, nil)
plan, err := tf.Plan(&PlanOpts{
Config: c,
State: s,
})
if err != nil {
t.Fatalf("err: %s", err)
}
@ -240,7 +246,10 @@ func TestTerraformPlan_state(t *testing.T) {
},
}
plan, err := tf.Plan(c, s, nil)
plan, err := tf.Plan(&PlanOpts{
Config: c,
State: s,
})
if err != nil {
t.Fatalf("err: %s", err)
}