11 "github.com/hashicorp/terraform/config/module"
15 gob.Register(make([]interface{}, 0))
16 gob.Register(make([]map[string]interface{}, 0))
17 gob.Register(make(map[string]interface{}))
18 gob.Register(make(map[string]string))
21 // Plan represents a single Terraform execution plan, which contains
22 // all the information necessary to make an infrastructure change.
24 // A plan has to contain basically the entire state of the world
25 // necessary to make a change: the state, diff, config, backend config, etc.
26 // This is so that it can run alone without any other data.
31 Vars map[string]interface{}
34 // Backend is the backend that this plan should use and store data with.
40 // Context returns a Context with the data encapsulated in this plan.
42 // The following fields in opts are overridden by the plan: Config,
43 // Diff, State, Variables.
44 func (p *Plan) Context(opts *ContextOpts) (*Context, error) {
46 opts.Module = p.Module
48 opts.Targets = p.Targets
50 opts.Variables = make(map[string]interface{})
51 for k, v := range p.Vars {
55 return NewContext(opts)
58 func (p *Plan) String() string {
59 buf := new(bytes.Buffer)
60 buf.WriteString("DIFF:\n\n")
61 buf.WriteString(p.Diff.String())
62 buf.WriteString("\n\nSTATE:\n\n")
63 buf.WriteString(p.State.String())
67 func (p *Plan) init() {
80 p.Vars = make(map[string]interface{})
85 // The format byte is prefixed into the plan file format so that we have
86 // the ability in the future to change the file format if we want for any
88 const planFormatMagic = "tfplan"
89 const planFormatVersion byte = 1
91 // ReadPlan reads a plan structure out of a reader in the format that
92 // was written by WritePlan.
93 func ReadPlan(src io.Reader) (*Plan, error) {
98 // Verify the magic bytes
99 magic := make([]byte, len(planFormatMagic))
101 n, err = src.Read(magic[n:])
103 return nil, fmt.Errorf("error while reading magic bytes: %s", err)
106 if string(magic) != planFormatMagic {
107 return nil, fmt.Errorf("not a valid plan file")
110 // Verify the version is something we can read
111 var formatByte [1]byte
112 n, err = src.Read(formatByte[:])
116 if n != len(formatByte) {
117 return nil, errors.New("failed to read plan version byte")
120 if formatByte[0] != planFormatVersion {
121 return nil, fmt.Errorf("unknown plan file version: %d", formatByte[0])
124 dec := gob.NewDecoder(src)
125 if err := dec.Decode(&result); err != nil {
132 // WritePlan writes a plan somewhere in a binary format.
133 func WritePlan(d *Plan, dst io.Writer) error {
134 // Write the magic bytes so we can determine the file format later
135 n, err := dst.Write([]byte(planFormatMagic))
139 if n != len(planFormatMagic) {
140 return errors.New("failed to write plan format magic bytes")
143 // Write a version byte so we can iterate on version at some point
144 n, err = dst.Write([]byte{planFormatVersion})
149 return errors.New("failed to write plan version byte")
152 return gob.NewEncoder(dst).Encode(d)