2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
2017-09-01 04:19:06 +02:00
|
|
|
"bytes"
|
2017-01-19 05:47:56 +01:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-08-30 03:18:49 +02:00
|
|
|
"sort"
|
2017-01-19 05:47:56 +01:00
|
|
|
"strings"
|
|
|
|
|
2018-12-12 17:01:18 +01:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/command/format"
|
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 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/plans"
|
|
|
|
"github.com/hashicorp/terraform/plans/planfile"
|
2019-03-06 01:18:55 +01:00
|
|
|
"github.com/hashicorp/terraform/states"
|
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 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
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 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (b *Local) opPlan(
|
2018-02-10 00:10:52 +01:00
|
|
|
stopCtx context.Context,
|
|
|
|
cancelCtx context.Context,
|
2017-01-19 05:47:56 +01:00
|
|
|
op *backend.Operation,
|
|
|
|
runningOp *backend.RunningOperation) {
|
2018-03-21 02:43:02 +01:00
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
log.Printf("[INFO] backend/local: starting Plan operation")
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
var diags tfdiags.Diagnostics
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
if op.PlanFile != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Can't re-plan a saved plan",
|
2018-10-31 16:45:03 +01:00
|
|
|
"The plan command was given a saved plan file as its input. This command generates "+
|
|
|
|
"a new plan, and so it requires a configuration directory as its argument.",
|
2018-03-21 02:43:02 +01:00
|
|
|
))
|
|
|
|
b.ReportResult(runningOp, diags)
|
2017-01-30 04:51:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
// Local planning requires a config, unless we're planning to destroy.
|
|
|
|
if !op.Destroy && !op.HasConfig() {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"No configuration files",
|
2018-10-31 16:45:03 +01:00
|
|
|
"Plan requires configuration to be present. Planning without a configuration would "+
|
|
|
|
"mark everything for destruction, which is normally not what is desired. If you "+
|
|
|
|
"would like to destroy everything, run plan with the -destroy option. Otherwise, "+
|
|
|
|
"create a Terraform configuration file (.tf file) and try again.",
|
2018-03-21 02:43:02 +01:00
|
|
|
))
|
|
|
|
b.ReportResult(runningOp, diags)
|
|
|
|
return
|
2017-01-30 04:51:54 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// Setup our count hook that keeps track of resource changes
|
|
|
|
countHook := new(CountHook)
|
|
|
|
if b.ContextOpts == nil {
|
|
|
|
b.ContextOpts = new(terraform.ContextOpts)
|
|
|
|
}
|
|
|
|
old := b.ContextOpts.Hooks
|
|
|
|
defer func() { b.ContextOpts.Hooks = old }()
|
|
|
|
b.ContextOpts.Hooks = append(b.ContextOpts.Hooks, countHook)
|
|
|
|
|
|
|
|
// Get our context
|
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 23:24:45 +02:00
|
|
|
tfCtx, configSnap, opState, ctxDiags := b.context(op)
|
|
|
|
diags = diags.Append(ctxDiags)
|
|
|
|
if ctxDiags.HasErrors() {
|
2018-03-21 02:43:02 +01:00
|
|
|
b.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the state
|
|
|
|
runningOp.State = tfCtx.State()
|
|
|
|
|
|
|
|
// If we're refreshing before plan, perform that
|
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 23:24:45 +02:00
|
|
|
baseState := runningOp.State
|
2017-01-19 05:47:56 +01:00
|
|
|
if op.PlanRefresh {
|
|
|
|
log.Printf("[INFO] backend/local: plan calling Refresh")
|
|
|
|
|
2017-01-19 06:41:59 +01:00
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(planRefreshing) + "\n"))
|
|
|
|
}
|
|
|
|
|
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 23:24:45 +02:00
|
|
|
refreshedState, err := tfCtx.Refresh()
|
2017-01-19 05:47:56 +01:00
|
|
|
if err != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(err)
|
|
|
|
b.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
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 23:24:45 +02:00
|
|
|
baseState = refreshedState // plan will be relative to our refreshed state
|
2017-09-01 04:19:06 +02:00
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output("\n------------------------------------------------------------------------")
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2017-12-02 23:31:28 +01:00
|
|
|
// Perform the plan in a goroutine so we can be interrupted
|
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 23:24:45 +02:00
|
|
|
var plan *plans.Plan
|
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 19:33:53 +02:00
|
|
|
var planDiags tfdiags.Diagnostics
|
2017-12-02 23:31:28 +01:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
log.Printf("[INFO] backend/local: plan calling Plan")
|
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 19:33:53 +02:00
|
|
|
plan, planDiags = tfCtx.Plan()
|
2017-12-02 23:31:28 +01:00
|
|
|
}()
|
|
|
|
|
2018-02-12 17:52:21 +01:00
|
|
|
if b.opWait(doneCh, stopCtx, cancelCtx, tfCtx, opState) {
|
2018-10-14 18:21:31 +02:00
|
|
|
// If we get in here then the operation was cancelled, which is always
|
|
|
|
// considered to be a failure.
|
|
|
|
log.Printf("[INFO] backend/local: plan operation was force-cancelled by interrupt")
|
|
|
|
runningOp.Result = backend.OperationFailure
|
2018-02-10 00:10:52 +01:00
|
|
|
return
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
2018-10-14 18:21:31 +02:00
|
|
|
log.Printf("[INFO] backend/local: plan operation completed")
|
2017-01-19 05:47:56 +01: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 19:33:53 +02:00
|
|
|
diags = diags.Append(planDiags)
|
|
|
|
if planDiags.HasErrors() {
|
2018-03-21 02:43:02 +01:00
|
|
|
b.ReportResult(runningOp, diags)
|
2017-12-02 23:31:28 +01:00
|
|
|
return
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
// Record state
|
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 23:24:45 +02:00
|
|
|
runningOp.PlanEmpty = plan.Changes.Empty()
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
// Save the plan to disk
|
|
|
|
if path := op.PlanOutPath; path != "" {
|
2018-10-09 21:19:24 +02:00
|
|
|
if op.PlanOutBackend == nil {
|
|
|
|
// This is always a bug in the operation caller; it's not valid
|
|
|
|
// to set PlanOutPath without also setting PlanOutBackend.
|
2018-10-31 16:45:03 +01:00
|
|
|
diags = diags.Append(fmt.Errorf(
|
|
|
|
"PlanOutPath set without also setting PlanOutBackend (this is a bug in Terraform)"),
|
|
|
|
)
|
2018-10-09 21:19:24 +02:00
|
|
|
b.ReportResult(runningOp, diags)
|
|
|
|
return
|
2018-09-29 00:57:27 +02:00
|
|
|
}
|
2018-10-09 21:19:24 +02:00
|
|
|
plan.Backend = *op.PlanOutBackend
|
2017-01-19 05:47:56 +01:00
|
|
|
|
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 23:24:45 +02:00
|
|
|
// We may have updated the state in the refresh step above, but we
|
|
|
|
// will freeze that updated state in the plan file for now and
|
|
|
|
// only write it if this plan is subsequently applied.
|
|
|
|
plannedStateFile := statemgr.PlannedStateUpdate(opState, baseState)
|
2017-03-20 18:05:24 +01:00
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
log.Printf("[INFO] backend/local: writing plan output to: %s", path)
|
2018-10-31 16:45:03 +01:00
|
|
|
err := planfile.Create(path, configSnap, plannedStateFile, plan)
|
2017-01-19 05:47:56 +01:00
|
|
|
if err != nil {
|
2018-03-21 02:43:02 +01:00
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Failed to write plan file",
|
|
|
|
fmt.Sprintf("The plan file could not be written: %s.", err),
|
|
|
|
))
|
|
|
|
b.ReportResult(runningOp, diags)
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform some output tasks if we have a CLI to output to.
|
|
|
|
if b.CLI != nil {
|
2018-08-29 21:12:18 +02:00
|
|
|
schemas := tfCtx.Schemas()
|
|
|
|
|
|
|
|
if plan.Changes.Empty() {
|
2017-09-01 04:19:06 +02:00
|
|
|
b.CLI.Output("\n" + b.Colorize().Color(strings.TrimSpace(planNoChanges)))
|
2017-01-19 05:47:56 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-06 01:18:55 +01:00
|
|
|
b.renderPlan(plan, baseState, schemas)
|
2017-09-01 04:19:06 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
// If we've accumulated any warnings along the way then we'll show them
|
|
|
|
// here just before we show the summary and next steps. If we encountered
|
|
|
|
// errors then we would've returned early at some other point above.
|
|
|
|
b.ShowDiagnostics(diags)
|
|
|
|
|
2017-09-09 02:14:37 +02:00
|
|
|
// Give the user some next-steps, unless we're running in an automation
|
|
|
|
// tool which is presumed to provide its own UI for further actions.
|
|
|
|
if !b.RunningInAutomation {
|
|
|
|
|
|
|
|
b.CLI.Output("\n------------------------------------------------------------------------")
|
|
|
|
|
|
|
|
if path := op.PlanOutPath; path == "" {
|
|
|
|
b.CLI.Output(fmt.Sprintf(
|
|
|
|
"\n" + strings.TrimSpace(planHeaderNoOutput) + "\n",
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
b.CLI.Output(fmt.Sprintf(
|
|
|
|
"\n"+strings.TrimSpace(planHeaderYesOutput)+"\n",
|
|
|
|
path, path,
|
|
|
|
))
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
2017-09-01 04:19:06 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2019-03-06 01:18:55 +01:00
|
|
|
func (b *Local) renderPlan(plan *plans.Plan, state *states.State, schemas *terraform.Schemas) {
|
2018-08-29 21:12:18 +02:00
|
|
|
counts := map[plans.Action]int{}
|
2018-12-12 17:01:18 +01:00
|
|
|
var rChanges []*plans.ResourceInstanceChangeSrc
|
2018-08-29 21:12:18 +02:00
|
|
|
for _, change := range plan.Changes.Resources {
|
2018-12-12 17:01:18 +01:00
|
|
|
if change.Action == plans.Delete && change.Addr.Resource.Resource.Mode == addrs.DataResourceMode {
|
|
|
|
// Avoid rendering data sources on deletion
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rChanges = append(rChanges, change)
|
2018-08-29 21:12:18 +02:00
|
|
|
counts[change.Action]++
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2017-09-01 04:19:06 +02:00
|
|
|
headerBuf := &bytes.Buffer{}
|
|
|
|
fmt.Fprintf(headerBuf, "\n%s\n", strings.TrimSpace(planHeaderIntro))
|
2018-08-29 21:12:18 +02:00
|
|
|
if counts[plans.Create] > 0 {
|
2018-12-14 14:45:47 +01:00
|
|
|
fmt.Fprintf(headerBuf, "%s create\n", format.DiffActionSymbol(plans.Create))
|
2017-09-01 04:19:06 +02:00
|
|
|
}
|
2018-08-29 21:12:18 +02:00
|
|
|
if counts[plans.Update] > 0 {
|
2018-12-14 14:45:47 +01:00
|
|
|
fmt.Fprintf(headerBuf, "%s update in-place\n", format.DiffActionSymbol(plans.Update))
|
2017-09-01 04:19:06 +02:00
|
|
|
}
|
2018-08-29 21:12:18 +02:00
|
|
|
if counts[plans.Delete] > 0 {
|
2018-12-14 14:45:47 +01:00
|
|
|
fmt.Fprintf(headerBuf, "%s destroy\n", format.DiffActionSymbol(plans.Delete))
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
2018-09-22 02:08:52 +02:00
|
|
|
if counts[plans.DeleteThenCreate] > 0 {
|
2018-12-14 14:45:47 +01:00
|
|
|
fmt.Fprintf(headerBuf, "%s destroy and then create replacement\n", format.DiffActionSymbol(plans.DeleteThenCreate))
|
2017-09-01 04:19:06 +02:00
|
|
|
}
|
2018-09-22 02:08:52 +02:00
|
|
|
if counts[plans.CreateThenDelete] > 0 {
|
2018-12-14 14:45:47 +01:00
|
|
|
fmt.Fprintf(headerBuf, "%s create replacement and then destroy\n", format.DiffActionSymbol(plans.CreateThenDelete))
|
2018-09-22 02:08:52 +02:00
|
|
|
}
|
2018-08-29 21:12:18 +02:00
|
|
|
if counts[plans.Read] > 0 {
|
2018-12-14 14:45:47 +01:00
|
|
|
fmt.Fprintf(headerBuf, "%s read (data resources)\n", format.DiffActionSymbol(plans.Read))
|
2017-09-01 04:19:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
b.CLI.Output(b.Colorize().Color(headerBuf.String()))
|
|
|
|
|
|
|
|
b.CLI.Output("Terraform will perform the following actions:\n")
|
|
|
|
|
2018-08-30 03:18:49 +02:00
|
|
|
// Note: we're modifying the backing slice of this plan object in-place
|
|
|
|
// here. The ordering of resource changes in a plan is not significant,
|
|
|
|
// but we can only do this safely here because we can assume that nobody
|
|
|
|
// is concurrently modifying our changes while we're trying to print it.
|
|
|
|
sort.Slice(rChanges, func(i, j int) bool {
|
|
|
|
iA := rChanges[i].Addr
|
|
|
|
jA := rChanges[j].Addr
|
|
|
|
if iA.String() == jA.String() {
|
|
|
|
return rChanges[i].DeposedKey < rChanges[j].DeposedKey
|
|
|
|
}
|
|
|
|
return iA.Less(jA)
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, rcs := range rChanges {
|
2018-08-29 21:12:18 +02:00
|
|
|
if rcs.Action == plans.NoOp {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
providerSchema := schemas.ProviderSchema(rcs.ProviderAddr.ProviderConfig.Type)
|
|
|
|
if providerSchema == nil {
|
|
|
|
// Should never happen
|
|
|
|
b.CLI.Output(fmt.Sprintf("(schema missing for %s)\n", rcs.ProviderAddr))
|
|
|
|
continue
|
|
|
|
}
|
2018-11-28 00:30:18 +01:00
|
|
|
rSchema, _ := providerSchema.SchemaForResourceAddr(rcs.Addr.Resource.Resource)
|
2018-08-29 21:12:18 +02:00
|
|
|
if rSchema == nil {
|
|
|
|
// Should never happen
|
|
|
|
b.CLI.Output(fmt.Sprintf("(schema missing for %s)\n", rcs.Addr))
|
|
|
|
continue
|
|
|
|
}
|
2019-03-06 01:18:55 +01:00
|
|
|
|
|
|
|
// check if the change is due to a tainted resource
|
|
|
|
tainted := false
|
|
|
|
if !state.Empty() {
|
2019-06-03 23:50:32 +02:00
|
|
|
if is := state.ResourceInstance(rcs.Addr); is != nil {
|
|
|
|
if obj := is.GetGeneration(rcs.DeposedKey.Generation()); obj != nil {
|
|
|
|
tainted = obj.Status == states.ObjectTainted
|
|
|
|
}
|
2019-03-06 01:18:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 21:12:18 +02:00
|
|
|
b.CLI.Output(format.ResourceChange(
|
|
|
|
rcs,
|
2019-03-06 01:18:55 +01:00
|
|
|
tainted,
|
2018-08-29 21:12:18 +02:00
|
|
|
rSchema,
|
|
|
|
b.CLIColor,
|
|
|
|
))
|
|
|
|
}
|
2017-09-01 04:19:06 +02:00
|
|
|
|
2018-08-29 21:12:18 +02:00
|
|
|
// stats is similar to counts above, but:
|
|
|
|
// - it considers only resource changes
|
|
|
|
// - it simplifies "replace" into both a create and a delete
|
|
|
|
stats := map[plans.Action]int{}
|
2018-08-30 03:18:49 +02:00
|
|
|
for _, change := range rChanges {
|
2018-08-29 21:12:18 +02:00
|
|
|
switch change.Action {
|
2018-09-22 02:08:52 +02:00
|
|
|
case plans.CreateThenDelete, plans.DeleteThenCreate:
|
2018-08-30 03:18:49 +02:00
|
|
|
stats[plans.Create]++
|
|
|
|
stats[plans.Delete]++
|
2018-08-29 21:12:18 +02:00
|
|
|
default:
|
2018-08-30 03:18:49 +02:00
|
|
|
stats[change.Action]++
|
2018-08-29 21:12:18 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-01 04:19:06 +02:00
|
|
|
b.CLI.Output(b.Colorize().Color(fmt.Sprintf(
|
|
|
|
"[reset][bold]Plan:[reset] "+
|
|
|
|
"%d to add, %d to change, %d to destroy.",
|
2018-08-29 21:12:18 +02:00
|
|
|
stats[plans.Create], stats[plans.Update], stats[plans.Delete],
|
2017-09-01 04:19:06 +02:00
|
|
|
)))
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
2017-01-30 04:51:54 +01:00
|
|
|
const planErrNoConfig = `
|
|
|
|
No configuration files found!
|
|
|
|
|
|
|
|
Plan requires configuration to be present. Planning without a configuration
|
|
|
|
would mark everything for destruction, which is normally not what is desired.
|
|
|
|
If you would like to destroy everything, please run plan with the "-destroy"
|
|
|
|
flag or create a single empty configuration file. Otherwise, please create
|
|
|
|
a Terraform configuration file in the path being executed and try again.
|
|
|
|
`
|
|
|
|
|
2017-09-01 04:19:06 +02:00
|
|
|
const planHeaderIntro = `
|
|
|
|
An execution plan has been generated and is shown below.
|
|
|
|
Resource actions are indicated with the following symbols:
|
|
|
|
`
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
const planHeaderNoOutput = `
|
2017-09-01 04:19:06 +02:00
|
|
|
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
|
|
|
|
can't guarantee that exactly these actions will be performed if
|
|
|
|
"terraform apply" is subsequently run.
|
2017-01-19 05:47:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const planHeaderYesOutput = `
|
2017-09-01 04:19:06 +02:00
|
|
|
This plan was saved to: %s
|
2017-01-19 05:47:56 +01:00
|
|
|
|
2017-09-01 04:19:06 +02:00
|
|
|
To perform exactly these actions, run the following command to apply:
|
|
|
|
terraform apply %q
|
2017-01-19 05:47:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const planNoChanges = `
|
|
|
|
[reset][bold][green]No changes. Infrastructure is up-to-date.[reset][green]
|
|
|
|
|
2017-02-14 21:09:45 +01:00
|
|
|
This means that Terraform did not detect any differences between your
|
2017-09-01 04:19:06 +02:00
|
|
|
configuration and real physical resources that exist. As a result, no
|
|
|
|
actions need to be performed.
|
2017-01-19 05:47:56 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const planRefreshing = `
|
|
|
|
[reset][bold]Refreshing Terraform state in-memory prior to plan...[reset]
|
|
|
|
The refreshed state will be used to calculate this plan, but will not be
|
|
|
|
persisted to local or remote state storage.
|
|
|
|
`
|