2014-05-24 14:27:58 -05:00
package command
import (
2014-07-13 11:34:35 -05:00
"bytes"
2014-06-18 18:42:13 -05:00
"fmt"
2014-06-18 23:36:44 -05:00
"os"
2014-07-13 11:34:35 -05:00
"sort"
2014-05-24 14:27:58 -05:00
"strings"
2015-10-15 15:48:58 -05:00
"github.com/hashicorp/go-getter"
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/hashicorp/terraform/addrs"
2017-01-18 22:50:45 -06:00
"github.com/hashicorp/terraform/backend"
2018-10-13 11:24:03 -05:00
"github.com/hashicorp/terraform/config/hcl2shim"
"github.com/hashicorp/terraform/repl"
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
"github.com/hashicorp/terraform/states"
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/hashicorp/terraform/tfdiags"
2014-05-24 14:27:58 -05:00
)
// ApplyCommand is a Command implementation that applies a Terraform
// configuration and actually builds or changes infrastructure.
type ApplyCommand struct {
2014-07-12 22:21:46 -05:00
Meta
2014-09-30 23:49:24 -05:00
// If true, then this apply command will become the "destroy"
// command. It is just like apply but only processes a destroy.
Destroy bool
2014-05-24 14:27:58 -05:00
}
2014-06-18 18:42:13 -05:00
func ( c * ApplyCommand ) Run ( args [ ] string ) int {
2016-06-20 20:06:28 -05:00
var destroyForce , refresh , autoApprove bool
2017-03-07 22:09:48 -06:00
args , err := c . Meta . process ( args , true )
if err != nil {
return 1
}
2014-07-12 22:21:46 -05:00
2014-09-30 23:49:24 -05:00
cmdName := "apply"
if c . Destroy {
cmdName = "destroy"
}
2018-11-21 08:35:27 -06:00
cmdFlags := c . Meta . extendedFlagSet ( cmdName )
2018-01-31 18:14:42 -06:00
cmdFlags . BoolVar ( & autoApprove , "auto-approve" , false , "skip interactive approval of plan before applying" )
2014-10-03 18:08:50 -05:00
if c . Destroy {
2018-01-31 18:14:42 -06:00
cmdFlags . BoolVar ( & destroyForce , "force" , false , "deprecated: same as auto-approve" )
2014-10-03 18:08:50 -05:00
}
2014-07-26 19:50:13 -05:00
cmdFlags . BoolVar ( & refresh , "refresh" , true , "refresh" )
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
cmdFlags . IntVar ( & c . Meta . parallelism , "parallelism" , DefaultParallelism , "parallelism" )
2017-01-18 22:50:45 -06:00
cmdFlags . StringVar ( & c . Meta . statePath , "state" , "" , "path" )
2014-10-11 20:05:23 -05:00
cmdFlags . StringVar ( & c . Meta . stateOutPath , "state-out" , "" , "path" )
cmdFlags . StringVar ( & c . Meta . backupPath , "backup" , "" , "path" )
2017-02-06 09:07:32 -06:00
cmdFlags . BoolVar ( & c . Meta . stateLock , "lock" , true , "lock state" )
2017-04-01 15:19:59 -05:00
cmdFlags . DurationVar ( & c . Meta . stateLockTimeout , "lock-timeout" , 0 , "lock timeout" )
2014-06-18 18:42:13 -05:00
cmdFlags . Usage = func ( ) { c . Ui . Error ( c . Help ( ) ) }
if err := cmdFlags . Parse ( args ) ; err != nil {
return 1
}
2018-10-16 09:47:32 -05:00
var diags tfdiags . Diagnostics
2017-01-18 22:50:45 -06:00
// Get the args. The "maybeInit" flag tracks whether we may need to
// initialize the configuration from a remote path. This is true as long
// as we have an argument.
2014-06-18 18:42:13 -05:00
args = cmdFlags . Args ( )
2017-01-18 22:50:45 -06:00
maybeInit := len ( args ) == 1
configPath , err := ModulePath ( args )
if err != nil {
c . Ui . Error ( err . Error ( ) )
2014-06-18 18:42:13 -05:00
return 1
}
2017-06-15 13:26:12 -05:00
// Check for user-supplied plugin path
if c . pluginPath , err = c . loadPluginPath ( ) ; err != nil {
c . Ui . Error ( fmt . Sprintf ( "Error loading plugin path: %s" , err ) )
return 1
}
2015-01-16 12:22:20 -06:00
if ! c . Destroy && maybeInit {
2017-01-18 22:50:45 -06:00
// We need the pwd for the getter operation below
pwd , err := os . Getwd ( )
if err != nil {
c . Ui . Error ( fmt . Sprintf ( "Error getting pwd: %s" , err ) )
return 1
}
2014-09-30 23:49:24 -05:00
// Do a detect to determine if we need to do an init + apply.
2015-10-15 15:48:58 -05:00
if detected , err := getter . Detect ( configPath , pwd , getter . Detectors ) ; err != nil {
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
c . Ui . Error ( fmt . Sprintf ( "Invalid path: %s" , err ) )
2014-09-30 23:49:24 -05:00
return 1
} else if ! strings . HasPrefix ( detected , "file" ) {
// If this isn't a file URL then we're doing an init +
// apply.
var init InitCommand
init . Meta = c . Meta
if code := init . Run ( [ ] string { detected } ) ; code != 0 {
return code
}
2014-09-29 17:55:28 -05:00
2014-09-30 23:49:24 -05:00
// Change the config path to be the cwd
configPath = pwd
}
2014-09-29 17:55:28 -05:00
}
2017-01-18 22:50:45 -06:00
// Check if the path is a plan
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
planFile , err := c . PlanFile ( configPath )
2014-07-01 12:02:13 -05:00
if err != nil {
c . Ui . Error ( err . Error ( ) )
return 1
2014-06-18 18:42:13 -05: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 16:24:45 -05:00
if c . Destroy && planFile != nil {
c . Ui . Error ( fmt . Sprintf ( "Destroy can't be called with a plan file." ) )
2014-09-30 23:49:24 -05:00
return 1
}
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
if planFile != nil {
2017-01-18 22:50:45 -06:00
// Reset the config path for backend loading
configPath = ""
2018-10-16 09:47:32 -05:00
if ! c . variableArgs . Empty ( ) {
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
"Can't set variables when applying a saved plan" ,
"The -var and -var-file options cannot be used when applying a saved plan file, because a saved plan includes the variable values that were set when it was created." ,
) )
c . showDiagnostics ( diags )
return 1
}
}
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-06 18:41:48 -06: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 16:24:45 -05:00
// Load the backend
var be backend . Enhanced
var beDiags tfdiags . Diagnostics
if planFile == nil {
backendConfig , configDiags := c . loadBackendConfig ( configPath )
2018-03-27 17:31:05 -05:00
diags = diags . Append ( configDiags )
if configDiags . HasErrors ( ) {
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-06 18:41:48 -06:00
c . showDiagnostics ( diags )
2017-01-18 22:50:45 -06:00
return 1
}
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
be , beDiags = c . Backend ( & BackendOpts {
Config : backendConfig ,
} )
} else {
plan , err := planFile . ReadPlan ( )
if err != nil {
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
"Failed to read plan from plan file" ,
fmt . Sprintf ( "Cannot read the plan from the given plan file: %s." , err ) ,
) )
2018-09-30 11:29:51 -05:00
c . showDiagnostics ( diags )
return 1
}
if plan . Backend . Config == nil {
// Should never happen; always indicates a bug in the creation of the plan file
diags = diags . Append ( tfdiags . Sourceless (
tfdiags . Error ,
"Failed to read plan from plan file" ,
fmt . Sprintf ( "The given plan file does not have a valid backend configuration. This is a bug in the Terraform command that generated this plan file." ) ,
) )
c . showDiagnostics ( diags )
return 1
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
}
be , beDiags = c . BackendForPlan ( plan . Backend )
}
2018-03-27 17:31:05 -05:00
diags = diags . Append ( beDiags )
if beDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
2017-01-18 22:50:45 -06:00
return 1
}
2018-03-27 17:31:05 -05:00
// Before we delegate to the backend, we'll print any warning diagnostics
// we've accumulated here, since the backend will start fresh with its own
// diagnostics.
c . showDiagnostics ( diags )
diags = nil
2017-01-18 22:50:45 -06:00
// Build the operation
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
opReq := c . Operation ( be )
2018-10-05 08:25:17 -05:00
opReq . AutoApprove = autoApprove
2018-03-27 17:31:05 -05:00
opReq . ConfigDir = configPath
2018-10-31 10:45:03 -05:00
opReq . Destroy = c . Destroy
opReq . DestroyForce = destroyForce
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
opReq . PlanFile = planFile
2017-01-18 22:50:45 -06:00
opReq . PlanRefresh = refresh
opReq . Type = backend . OperationTypeApply
2018-10-31 10:45:03 -05:00
2018-03-27 17:31:05 -05:00
opReq . ConfigLoader , err = c . initConfigLoader ( )
2017-01-18 22:50:45 -06:00
if err != nil {
2018-03-27 17:31:05 -05:00
c . showDiagnostics ( err )
return 1
2014-06-18 18:42:13 -05:00
}
2018-10-31 10:45:03 -05:00
2018-10-12 15:19:58 -05:00
{
var moreDiags tfdiags . Diagnostics
opReq . Variables , moreDiags = c . collectVariableValues ( )
diags = diags . Append ( moreDiags )
if moreDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
return 1
}
}
2014-06-18 18:42:13 -05: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 16:24:45 -05:00
op , err := c . RunOperation ( be , opReq )
2018-03-27 17:31:05 -05:00
if err != nil {
c . showDiagnostics ( err )
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-06 18:41:48 -06:00
return 1
}
2018-03-27 17:31:05 -05:00
if op . Result != backend . OperationSuccess {
return op . Result . ExitStatus ( )
}
command: validate config as part of loading it
Previously we required callers to separately call .Validate on the root
module to determine if there were any value errors, but we did that
inconsistently and would thus see crashes in some cases where later code
would try to use invalid configuration as if it were valid.
Now we run .Validate automatically after config loading, returning the
resulting diagnostics. Since we return a diagnostics here, it's possible
to return both warnings and errors.
We return the loaded module even if it's invalid, so callers are free to
ignore returned errors and try to work with the config anyway, though they
will need to be defensive against invalid configuration themselves in
that case.
As a result of this, all of the commands that load configuration now need
to use diagnostic printing to signal errors. For the moment this just
allows us to return potentially-multiple config errors/warnings in full
fidelity, but also sets us up for later when more subsystems are able
to produce rich diagnostics so we can show them all together.
Finally, this commit also removes some stale, commented-out code for the
"legacy" (pre-0.8) graph implementation, which has not been available
for some time.
2017-12-06 18:41:48 -06:00
2015-06-15 10:40:56 -05:00
if ! c . Destroy {
2018-10-14 17:33:30 -05:00
if outputs := outputsAsString ( op . State , addrs . RootModuleInstance , true ) ; outputs != "" {
c . Ui . Output ( c . Colorize ( ) . Color ( outputs ) )
}
2014-07-13 11:34:35 -05:00
}
2018-03-27 17:31:05 -05:00
return op . Result . ExitStatus ( )
2014-05-24 14:27:58 -05:00
}
func ( c * ApplyCommand ) Help ( ) string {
2014-10-01 00:01:11 -05:00
if c . Destroy {
return c . helpDestroy ( )
}
return c . helpApply ( )
}
func ( c * ApplyCommand ) Synopsis ( ) string {
if c . Destroy {
return "Destroy Terraform-managed infrastructure"
}
return "Builds or changes infrastructure"
}
func ( c * ApplyCommand ) helpApply ( ) string {
2014-05-24 14:27:58 -05:00
helpText := `
2016-04-11 12:24:08 -05:00
Usage : terraform apply [ options ] [ DIR - OR - PLAN ]
2014-05-24 14:27:58 -05:00
2014-07-11 23:30:40 -05:00
Builds or changes infrastructure according to Terraform configuration
2014-09-29 17:57:35 -05:00
files in DIR .
2016-04-11 12:24:08 -05:00
By default , apply scans the current directory for the configuration
and applies the changes appropriately . However , a path to another
configuration or an execution plan can be provided . Execution plans can be
used to only execute a pre - determined set of actions .
2014-05-24 14:27:58 -05:00
Options :
2014-07-27 17:09:04 -05:00
- backup = path Path to backup the existing state file before
modifying . Defaults to the "-state-out" path with
2014-08-07 02:19:56 -05:00
".backup" extension . Set to "-" to disable backup .
2014-07-27 17:09:04 -05:00
2018-03-26 12:21:47 -05:00
- auto - approve Skip interactive approval of plan before applying .
2017-02-06 09:07:32 -06:00
- lock = true Lock the state file when locking is supported .
2017-02-03 13:15:08 -06:00
2017-04-01 15:19:59 -05:00
- lock - timeout = 0 s Duration to retry a state lock .
2014-09-29 14:46:58 -05:00
- input = true Ask for input for variables if not directly set .
2014-07-12 22:21:46 -05:00
- no - color If specified , output won ' t contain any color .
2017-02-08 13:49:08 -06:00
- parallelism = n Limit the number of parallel resource operations .
2015-10-05 15:06:08 -05:00
Defaults to 10.
2015-05-06 10:58:42 -05:00
2014-07-26 19:50:13 -05:00
- refresh = true Update state prior to checking for differences . This
has no effect if a plan file is given to apply .
2014-07-11 23:30:40 -05:00
- state = path Path to read and save state ( unless state - out
is specified ) . Defaults to "terraform.tfstate" .
2014-06-18 23:36:44 -05:00
2014-07-11 23:30:40 -05:00
- state - out = path Path to write state to that is different than
"-state" . This can be used to preserve the old
state .
2014-05-24 14:27:58 -05:00
2015-03-24 11:18:15 -05:00
- target = resource Resource to target . Operation will be limited to this
resource and its dependencies . This flag can be used
multiple times .
2014-07-18 13:37:27 -05:00
- var ' foo = bar ' Set a variable in the Terraform configuration . This
flag can be set multiple times .
- var - file = foo Set variables in the Terraform configuration from
2017-06-21 20:22:07 -05:00
a file . If "terraform.tfvars" or any ".auto.tfvars"
files are present , they will be automatically loaded .
2014-07-18 13:37:27 -05:00
2014-05-24 14:27:58 -05:00
`
return strings . TrimSpace ( helpText )
}
2014-10-01 00:01:11 -05:00
func ( c * ApplyCommand ) helpDestroy ( ) string {
helpText := `
Usage : terraform destroy [ options ] [ DIR ]
Destroy Terraform - managed infrastructure .
Options :
- backup = path Path to backup the existing state file before
modifying . Defaults to the "-state-out" path with
".backup" extension . Set to "-" to disable backup .
2018-01-31 18:14:42 -06:00
- auto - approve Skip interactive approval before destroying .
- force Deprecated : same as auto - approve .
2014-10-01 00:01:11 -05:00
2017-02-06 09:07:32 -06:00
- lock = true Lock the state file when locking is supported .
2017-02-03 13:15:08 -06:00
2017-04-01 15:19:59 -05:00
- lock - timeout = 0 s Duration to retry a state lock .
2014-10-01 00:01:11 -05:00
- no - color If specified , output won ' t contain any color .
2015-10-05 17:18:03 -05:00
- parallelism = n Limit the number of concurrent operations .
Defaults to 10.
2014-10-01 00:01:11 -05:00
- refresh = true Update state prior to checking for differences . This
has no effect if a plan file is given to apply .
- state = path Path to read and save state ( unless state - out
is specified ) . Defaults to "terraform.tfstate" .
- state - out = path Path to write state to that is different than
"-state" . This can be used to preserve the old
state .
2015-03-24 11:18:15 -05:00
- target = resource Resource to target . Operation will be limited to this
resource and its dependencies . This flag can be used
multiple times .
2014-10-01 00:01:11 -05:00
- var ' foo = bar ' Set a variable in the Terraform configuration . This
flag can be set multiple times .
- var - file = foo Set variables in the Terraform configuration from
2017-06-21 20:22:07 -05:00
a file . If "terraform.tfvars" or any ".auto.tfvars"
files are present , they will be automatically loaded .
2014-10-01 00:01:11 -05:00
`
return strings . TrimSpace ( helpText )
2014-06-27 16:43:23 -05:00
}
2015-06-15 10:40:56 -05:00
2018-10-14 17:33:30 -05:00
func outputsAsString ( state * states . State , modPath addrs . ModuleInstance , includeHeader bool ) string {
2015-06-15 10:40:56 -05:00
if state == nil {
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 16:24:45 -05:00
ms := state . Module ( modPath )
2016-11-03 00:33:34 -05:00
if ms == nil {
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 16:24:45 -05:00
outputs := ms . OutputValues
2015-06-15 10:40:56 -05:00
outputBuf := new ( bytes . Buffer )
if len ( outputs ) > 0 {
2016-05-10 13:56:26 -05:00
if includeHeader {
outputBuf . WriteString ( "[reset][bold][green]\nOutputs:\n\n" )
}
2015-06-15 10:40:56 -05:00
// Output the outputs in alphabetical order
keyLen := 0
2016-04-11 12:40:06 -05:00
ks := make ( [ ] string , 0 , len ( outputs ) )
2015-06-15 10:40:56 -05:00
for key , _ := range outputs {
2016-04-11 12:40:06 -05:00
ks = append ( ks , key )
2015-06-15 10:40:56 -05:00
if len ( key ) > keyLen {
keyLen = len ( key )
}
}
2016-04-11 12:40:06 -05:00
sort . Strings ( ks )
2015-06-15 10:40:56 -05:00
2016-04-11 12:40:06 -05:00
for _ , k := range ks {
2018-10-14 17:33:30 -05:00
v := outputs [ k ]
if v . Sensitive {
2016-04-11 12:40:06 -05:00
outputBuf . WriteString ( fmt . Sprintf ( "%s = <sensitive>\n" , k ) )
continue
}
2018-10-13 11:24:03 -05:00
// Our formatter still wants an old-style raw interface{} value, so
// for now we'll just shim it.
// FIXME: Port the formatter to work with cty.Value directly.
legacyVal := hcl2shim . ConfigValueFromHCL2 ( v . Value )
result , err := repl . FormatResult ( legacyVal )
if err != nil {
// We can't really return errors from here, so we'll just have
// to stub this out. This shouldn't happen in practice anyway.
result = "<error during formatting>"
}
outputBuf . WriteString ( fmt . Sprintf ( "%s = %s\n" , k , result ) )
2015-06-15 10:40:56 -05:00
}
}
return strings . TrimSpace ( outputBuf . String ( ) )
}
2017-04-25 10:43:59 -05:00
const outputInterrupt = ` Interrupt received .
Please wait for Terraform to exit or data loss may occur .
Gracefully shutting down ... `