2014-06-20 12:33:26 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2014-06-25 20:22:42 -05:00
|
|
|
"bytes"
|
2014-06-20 12:33:26 -05:00
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-06-20 12:44:49 -05:00
|
|
|
"sync"
|
2014-06-20 12:33:26 -05:00
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
2014-06-20 12:33:26 -05:00
|
|
|
)
|
|
|
|
|
2014-07-02 13:28:23 -05:00
|
|
|
func init() {
|
2014-07-02 18:17:26 -05:00
|
|
|
gob.Register(make([]interface{}, 0))
|
2014-07-02 19:04:48 -05:00
|
|
|
gob.Register(make([]map[string]interface{}, 0))
|
2014-08-05 00:08:55 -05:00
|
|
|
gob.Register(make(map[string]interface{}))
|
2014-07-24 09:17:30 -05:00
|
|
|
gob.Register(make(map[string]string))
|
2014-07-02 13:28:23 -05:00
|
|
|
}
|
|
|
|
|
2014-06-20 12:33:26 -05:00
|
|
|
// Plan represents a single Terraform execution plan, which contains
|
|
|
|
// all the information necessary to make an infrastructure change.
|
2017-01-18 22:50:57 -06:00
|
|
|
//
|
|
|
|
// A plan has to contain basically the entire state of the world
|
|
|
|
// necessary to make a change: the state, diff, config, backend config, etc.
|
|
|
|
// This is so that it can run alone without any other data.
|
2014-06-20 12:33:26 -05:00
|
|
|
type Plan struct {
|
2017-08-16 14:09:38 -05:00
|
|
|
// Diff describes the resource actions that must be taken when this
|
|
|
|
// plan is applied.
|
|
|
|
Diff *Diff
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
// Config represents the entire configuration that was present when this
|
2017-08-16 14:09:38 -05:00
|
|
|
// plan was created.
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
Config *configs.Config
|
2017-08-16 14:09:38 -05:00
|
|
|
|
|
|
|
// State is the Terraform state that was current when this plan was
|
|
|
|
// created.
|
|
|
|
//
|
|
|
|
// It is not allowed to apply a plan that has a stale state, since its
|
|
|
|
// diff could be outdated.
|
|
|
|
State *State
|
|
|
|
|
|
|
|
// Vars retains the variables that were set when creating the plan, so
|
|
|
|
// that the same variables can be applied during apply.
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
Vars map[string]cty.Value
|
2017-08-16 14:09:38 -05:00
|
|
|
|
|
|
|
// Targets, if non-empty, contains a set of resource address strings that
|
|
|
|
// identify graph nodes that were selected as targets for plan.
|
|
|
|
//
|
|
|
|
// When targets are set, any graph node that is not directly targeted or
|
|
|
|
// indirectly targeted via dependencies is excluded from the graph.
|
2016-02-23 10:24:53 -06:00
|
|
|
Targets []string
|
2014-06-20 12:44:49 -05:00
|
|
|
|
2017-08-16 14:09:38 -05:00
|
|
|
// TerraformVersion is the version of Terraform that was used to create
|
|
|
|
// this plan.
|
|
|
|
//
|
|
|
|
// It is not allowed to apply a plan created with a different version of
|
|
|
|
// Terraform, since the other fields of this structure may be interpreted
|
|
|
|
// in different ways between versions.
|
2017-06-05 19:08:02 -05:00
|
|
|
TerraformVersion string
|
2017-08-16 14:09:38 -05:00
|
|
|
|
|
|
|
// ProviderSHA256s is a map giving the SHA256 hashes of the exact binaries
|
|
|
|
// used as plugins for each provider during plan.
|
|
|
|
//
|
|
|
|
// These must match between plan and apply to ensure that the diff is
|
|
|
|
// correctly interpreted, since different provider versions may have
|
|
|
|
// different attributes or attribute value constraints.
|
|
|
|
ProviderSHA256s map[string][]byte
|
2017-06-05 19:08:02 -05:00
|
|
|
|
2017-01-18 22:50:57 -06:00
|
|
|
// Backend is the backend that this plan should use and store data with.
|
|
|
|
Backend *BackendState
|
|
|
|
|
2017-09-27 18:26:14 -05:00
|
|
|
// Destroy indicates that this plan was created for a full destroy operation
|
|
|
|
Destroy bool
|
|
|
|
|
2014-06-20 12:44:49 -05:00
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plan) String() string {
|
2014-06-25 20:22:42 -05:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.WriteString("DIFF:\n\n")
|
|
|
|
buf.WriteString(p.Diff.String())
|
2014-09-23 13:43:21 -05:00
|
|
|
buf.WriteString("\n\nSTATE:\n\n")
|
2014-06-25 20:22:42 -05:00
|
|
|
buf.WriteString(p.State.String())
|
|
|
|
return buf.String()
|
2014-06-20 12:44:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 12:33:53 -05:00
|
|
|
p.Vars = make(map[string]cty.Value)
|
2014-06-20 12:44:49 -05:00
|
|
|
}
|
|
|
|
})
|
2014-06-20 12:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The format byte is prefixed into the plan file format so that we have
|
|
|
|
// the ability in the future to change the file format if we want for any
|
|
|
|
// reason.
|
2014-07-11 23:43:24 -05:00
|
|
|
const planFormatMagic = "tfplan"
|
2017-06-05 19:08:02 -05:00
|
|
|
const planFormatVersion byte = 2
|
2014-06-20 12:33:26 -05:00
|
|
|
|
|
|
|
// ReadPlan reads a plan structure out of a reader in the format that
|
|
|
|
// was written by WritePlan.
|
|
|
|
func ReadPlan(src io.Reader) (*Plan, error) {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
return nil, fmt.Errorf("terraform.ReadPlan is no longer in use; use planfile.Open instead")
|
2014-06-20 12:33:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// WritePlan writes a plan somewhere in a binary format.
|
|
|
|
func WritePlan(d *Plan, dst io.Writer) error {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
return fmt.Errorf("terraform.WritePlan is no longer in use; use planfile.Create instead")
|
2014-06-20 12:33:26 -05:00
|
|
|
}
|