Skip to content

Commit 718fb42

Browse files
committed
terraform: Plan should use module.Tree
1 parent 672bf58 commit 718fb42

5 files changed

Lines changed: 103 additions & 24 deletions

File tree

config/module/tree_gob.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package module
2+
3+
import (
4+
"bytes"
5+
"encoding/gob"
6+
7+
"github.com/hashicorp/terraform/config"
8+
)
9+
10+
func (t *Tree) GobDecode(bs []byte) error {
11+
t.lock.Lock()
12+
defer t.lock.Unlock()
13+
14+
// Decode the gob data
15+
var data treeGob
16+
dec := gob.NewDecoder(bytes.NewReader(bs))
17+
if err := dec.Decode(&data); err != nil {
18+
return err
19+
}
20+
21+
// Set the fields
22+
t.name = data.Name
23+
t.config = data.Config
24+
t.children = data.Children
25+
26+
return nil
27+
}
28+
29+
func (t *Tree) GobEncode() ([]byte, error) {
30+
data := &treeGob{
31+
Config: t.config,
32+
Children: t.children,
33+
Name: t.name,
34+
}
35+
36+
var buf bytes.Buffer
37+
enc := gob.NewEncoder(&buf)
38+
if err := enc.Encode(data); err != nil {
39+
return nil, err
40+
}
41+
42+
return buf.Bytes(), nil
43+
}
44+
45+
// treeGob is used as a structure to Gob encode a tree.
46+
//
47+
// This structure is private so it can't be referenced but the fields are
48+
// public, allowing Gob to properly encode this. When we decode this, we are
49+
// able to turn it into a Tree.
50+
type treeGob struct {
51+
Config *config.Config
52+
Children map[string]*Tree
53+
Name string
54+
}

config/module/tree_gob_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package module
2+
3+
import (
4+
"bytes"
5+
"encoding/gob"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestTreeEncodeDecodeGob(t *testing.T) {
11+
storage := testStorage(t)
12+
tree := NewTree("", testConfig(t, "basic"))
13+
14+
// This should get things
15+
if err := tree.Load(storage, GetModeGet); err != nil {
16+
t.Fatalf("err: %s", err)
17+
}
18+
19+
// Encode it.
20+
var buf bytes.Buffer
21+
enc := gob.NewEncoder(&buf)
22+
if err := enc.Encode(tree); err != nil {
23+
t.Fatalf("err: %s", err)
24+
}
25+
26+
dec := gob.NewDecoder(&buf)
27+
var actual Tree
28+
if err := dec.Decode(&actual); err != nil {
29+
t.Fatalf("err: %s", err)
30+
}
31+
32+
actualStr := strings.TrimSpace(actual.String())
33+
expectedStr := strings.TrimSpace(tree.String())
34+
if actualStr != expectedStr {
35+
t.Fatalf("\n%s\n\nexpected:\n\n%s", actualStr, expectedStr)
36+
}
37+
}

terraform/context.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ type genericWalkFunc func(*walkContext, *Resource) error
2424
//
2525
// Additionally, a context can be created from a Plan using Plan.Context.
2626
type Context struct {
27-
config *config.Config
2827
module *module.Tree
2928
diff *Diff
3029
hooks []Hook
@@ -74,13 +73,7 @@ func NewContext(opts *ContextOpts) *Context {
7473
}
7574
parCh := make(chan struct{}, par)
7675

77-
var config *config.Config
78-
if opts.Module != nil {
79-
config = opts.Module.Config()
80-
}
81-
8276
return &Context{
83-
config: config,
8477
diff: opts.Diff,
8578
hooks: hooks,
8679
module: opts.Module,
@@ -139,7 +132,7 @@ func (c *Context) Plan(opts *PlanOpts) (*Plan, error) {
139132
defer c.releaseRun(v)
140133

141134
p := &Plan{
142-
Config: c.config,
135+
Module: c.module,
143136
Vars: c.variables,
144137
State: c.state,
145138
}
@@ -223,12 +216,12 @@ func (c *Context) Validate() ([]string, []error) {
223216
var rerr *multierror.Error
224217

225218
// Validate the configuration itself
226-
if err := c.config.Validate(); err != nil {
219+
if err := c.module.Config().Validate(); err != nil {
227220
rerr = multierror.ErrorAppend(rerr, err)
228221
}
229222

230223
// Validate the user variables
231-
if errs := smcUserVariables(c.config, c.variables); len(errs) > 0 {
224+
if errs := smcUserVariables(c.module.Config(), c.variables); len(errs) > 0 {
232225
rerr = multierror.ErrorAppend(rerr, errs...)
233226
}
234227

@@ -1260,7 +1253,7 @@ func (c *walkContext) computeResourceMultiVariable(
12601253
// Get the resource from the configuration so we can know how
12611254
// many of the resource there is.
12621255
var cr *config.Resource
1263-
for _, r := range c.Context.config.Resources {
1256+
for _, r := range c.Context.module.Config().Resources {
12641257
if r.Id() == v.ResourceId() {
12651258
cr = r
12661259
break

terraform/plan.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99
"sync"
1010

11-
"github.com/hashicorp/terraform/config"
1211
"github.com/hashicorp/terraform/config/module"
1312
)
1413

@@ -31,8 +30,8 @@ type PlanOpts struct {
3130
// Plan represents a single Terraform execution plan, which contains
3231
// all the information necessary to make an infrastructure change.
3332
type Plan struct {
34-
Config *config.Config
3533
Diff *Diff
34+
Module *module.Tree
3635
State *State
3736
Vars map[string]string
3837

@@ -45,7 +44,7 @@ type Plan struct {
4544
// Diff, State, Variables.
4645
func (p *Plan) Context(opts *ContextOpts) *Context {
4746
opts.Diff = p.Diff
48-
opts.Module = module.NewTree("", p.Config) // TODO: compat
47+
opts.Module = p.Module
4948
opts.State = p.State
5049
opts.Variables = p.Vars
5150
return NewContext(opts)
@@ -62,10 +61,6 @@ func (p *Plan) String() string {
6261

6362
func (p *Plan) init() {
6463
p.once.Do(func() {
65-
if p.Config == nil {
66-
p.Config = new(config.Config)
67-
}
68-
6964
if p.Diff == nil {
7065
p.Diff = new(Diff)
7166
p.Diff.init()

terraform/plan_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package terraform
22

33
import (
44
"bytes"
5-
"reflect"
5+
"strings"
66

77
"testing"
88
)
99

1010
func TestReadWritePlan(t *testing.T) {
1111
plan := &Plan{
12-
Config: testConfig(t, "new-good"),
12+
Module: testModule(t, "new-good"),
1313
Diff: &Diff{
1414
Modules: []*ModuleDiff{
1515
&ModuleDiff{
@@ -65,9 +65,9 @@ func TestReadWritePlan(t *testing.T) {
6565
t.Fatalf("err: %s", err)
6666
}
6767

68-
println(reflect.DeepEqual(actual.Config.Resources, plan.Config.Resources))
69-
70-
if !reflect.DeepEqual(actual, plan) {
71-
t.Fatalf("bad: %#v", actual)
68+
actualStr := strings.TrimSpace(actual.String())
69+
expectedStr := strings.TrimSpace(plan.String())
70+
if actualStr != expectedStr {
71+
t.Fatalf("bad:\n\n%s\n\nexpected:\n\n%s", actualStr, expectedStr)
7272
}
7373
}

0 commit comments

Comments
 (0)