2016-05-04 12:06:16 -05:00
package command
import (
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
"errors"
2016-05-04 12:06:16 -05:00
"fmt"
"log"
2016-11-02 12:30:28 -05:00
"os"
2016-05-04 12:06:16 -05:00
"strings"
2016-05-04 12:32:08 -05:00
2019-09-09 17:58:44 -05:00
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
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
"github.com/hashicorp/terraform/addrs"
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 12:01:30 -06:00
"github.com/hashicorp/terraform/command/arguments"
"github.com/hashicorp/terraform/command/views"
2018-03-27 17:31:05 -05:00
"github.com/hashicorp/terraform/configs"
2021-05-17 10:42:17 -05:00
"github.com/hashicorp/terraform/internal/backend"
2021-05-17 12:11:06 -05:00
"github.com/hashicorp/terraform/internal/tfdiags"
2016-05-04 12:32:08 -05:00
"github.com/hashicorp/terraform/terraform"
2016-05-04 12:06:16 -05:00
)
// ImportCommand is a cli.Command implementation that imports resources
// into the Terraform state.
type ImportCommand struct {
Meta
}
func ( c * ImportCommand ) Run ( args [ ] string ) int {
2016-11-02 13:11:42 -05:00
// Get the pwd since its our default -config flag value
pwd , err := os . Getwd ( )
if err != nil {
c . Ui . Error ( fmt . Sprintf ( "Error getting pwd: %s" , err ) )
return 1
}
var configPath string
2020-04-01 14:01:08 -05:00
args = c . Meta . process ( args )
2016-05-04 12:06:16 -05:00
2018-11-21 08:35:27 -06:00
cmdFlags := c . Meta . extendedFlagSet ( "import" )
2021-01-27 12:58:41 -06:00
cmdFlags . BoolVar ( & c . ignoreRemoteVersion , "ignore-remote-version" , false , "continue even if remote and local Terraform versions are incompatible" )
2019-03-06 08:25:36 -06:00
cmdFlags . IntVar ( & c . Meta . parallelism , "parallelism" , DefaultParallelism , "parallelism" )
2018-11-15 15:31:30 -06:00
cmdFlags . StringVar ( & c . Meta . statePath , "state" , "" , "path" )
2016-05-04 12:06:16 -05:00
cmdFlags . StringVar ( & c . Meta . stateOutPath , "state-out" , "" , "path" )
cmdFlags . StringVar ( & c . Meta . backupPath , "backup" , "" , "path" )
2016-11-02 13:11:42 -05:00
cmdFlags . StringVar ( & configPath , "config" , pwd , "path" )
2017-04-01 15:19:59 -05:00
cmdFlags . BoolVar ( & c . Meta . stateLock , "lock" , true , "lock state" )
cmdFlags . DurationVar ( & c . Meta . stateLockTimeout , "lock-timeout" , 0 , "lock timeout" )
2017-09-18 13:41:30 -05:00
cmdFlags . BoolVar ( & c . Meta . allowMissingConfig , "allow-missing-config" , false , "allow missing config" )
2016-05-04 12:06:16 -05:00
cmdFlags . Usage = func ( ) { c . Ui . Error ( c . Help ( ) ) }
if err := cmdFlags . Parse ( args ) ; err != nil {
return 1
}
args = cmdFlags . Args ( )
if len ( args ) != 2 {
c . Ui . Error ( "The import command expects two arguments." )
cmdFlags . Usage ( )
return 1
}
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
var diags tfdiags . Diagnostics
// Parse the provided resource address.
2018-10-09 19:47:53 -05:00
traversalSrc := [ ] byte ( args [ 0 ] )
traversal , travDiags := hclsyntax . ParseTraversalAbs ( traversalSrc , "<import-address>" , hcl . Pos { Line : 1 , Column : 1 } )
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
diags = diags . Append ( travDiags )
if travDiags . HasErrors ( ) {
2018-10-09 19:47:53 -05:00
c . registerSynthConfigSource ( "<import-address>" , traversalSrc ) // so we can include a source snippet
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
c . showDiagnostics ( diags )
c . Ui . Info ( importCommandInvalidAddressReference )
2017-05-16 20:20:08 -05:00
return 1
}
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
addr , addrDiags := addrs . ParseAbsResourceInstance ( traversal )
diags = diags . Append ( addrDiags )
if addrDiags . HasErrors ( ) {
2018-10-09 19:47:53 -05:00
c . registerSynthConfigSource ( "<import-address>" , traversalSrc ) // so we can include a source snippet
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
c . showDiagnostics ( diags )
c . Ui . Info ( importCommandInvalidAddressReference )
2017-05-16 20:20:08 -05:00
return 1
}
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
if addr . Resource . Resource . Mode != addrs . ManagedResourceMode {
diags = diags . Append ( errors . New ( "A managed resource address is required. Importing into a data resource is not allowed." ) )
c . showDiagnostics ( diags )
2017-05-16 20:20:08 -05:00
return 1
}
2018-03-27 17:31:05 -05:00
if ! c . dirIsConfigPath ( configPath ) {
diags = diags . Append ( & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : "No Terraform configuration files" ,
Detail : fmt . Sprintf (
"The directory %s does not contain any Terraform configuration files (.tf or .tf.json). To specify a different configuration directory, use the -config=\"...\" command line option." ,
configPath ,
) ,
} )
c . showDiagnostics ( diags )
return 1
}
2017-12-08 12:22:07 -06:00
2018-03-27 17:31:05 -05:00
// Load the full config, so we can verify that the target resource is
// already configured.
config , configDiags := c . loadConfig ( configPath )
diags = diags . Append ( configDiags )
if configDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
return 1
2017-01-18 22:50:45 -06:00
}
2017-05-16 20:26:20 -05:00
// Verify that the given address points to something that exists in config.
// This is to reduce the risk that a typo in the resource address will
// import something that Terraform will want to immediately destroy on
// the next plan, and generally acts as a reassurance of user intent.
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
targetConfig := config . DescendentForInstance ( addr . Module )
2018-03-27 17:31:05 -05:00
if targetConfig == 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
modulePath := addr . Module . String ( )
2017-12-08 12:22:07 -06:00
diags = diags . Append ( & hcl . Diagnostic {
Severity : hcl . DiagError ,
Summary : "Import to non-existent module" ,
Detail : fmt . Sprintf (
"%s is not defined in the configuration. Please add configuration for this module before importing into it." ,
modulePath ,
) ,
} )
c . showDiagnostics ( diags )
2017-05-16 20:26:20 -05:00
return 1
}
2018-03-27 17:31:05 -05:00
targetMod := targetConfig . Module
rcs := targetMod . ManagedResources
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
var rc * configs . Resource
resourceRelAddr := addr . Resource . Resource
2017-05-16 20:26:20 -05:00
for _ , thisRc := range rcs {
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
if resourceRelAddr . Type == thisRc . Type && resourceRelAddr . Name == thisRc . Name {
2017-05-16 20:26:20 -05:00
rc = thisRc
break
}
}
2017-09-18 13:41:30 -05:00
if ! c . Meta . allowMissingConfig && rc == 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
modulePath := addr . Module . String ( )
2017-05-16 20:26:20 -05:00
if modulePath == "" {
modulePath = "the root module"
}
2017-12-08 12:22:07 -06:00
c . showDiagnostics ( diags )
// This is not a diagnostic because currently our diagnostics printer
// doesn't support having a code example in the detail, and there's
// a code example in this message.
// TODO: Improve the diagnostics printer so we can use it for this
// message.
2017-05-16 20:26:20 -05:00
c . Ui . Error ( fmt . Sprintf (
importCommandMissingResourceFmt ,
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
addr , modulePath , resourceRelAddr . Type , resourceRelAddr . Name ,
2017-05-16 20:26:20 -05:00
) )
return 1
2017-05-01 16:47:53 -05:00
}
2017-06-26 17:28:45 -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
}
2017-01-18 22:50:45 -06:00
// Load the backend
2018-03-27 17:31:05 -05:00
b , backendDiags := c . Backend ( & BackendOpts {
Config : config . Module . Backend ,
2017-05-01 16:47:53 -05:00
} )
2018-03-27 17:31:05 -05:00
diags = diags . Append ( backendDiags )
if backendDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
2017-01-18 22:50:45 -06:00
return 1
}
2017-08-08 17:41:00 -05:00
// We require a backend.Local to build a context.
// This isn't necessarily a "local.Local" backend, which provides local
// operations, however that is the only current implementation. A
// "local.Local" backend also doesn't necessarily provide local state, as
// that may be delegated to a "remotestate.Backend".
2017-01-18 22:50:45 -06:00
local , ok := b . ( backend . Local )
if ! ok {
c . Ui . Error ( ErrUnsupportedLocalOp )
return 1
}
// 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 ( b )
2018-03-27 17:31:05 -05:00
opReq . ConfigDir = configPath
opReq . ConfigLoader , err = c . initConfigLoader ( )
if err != nil {
diags = diags . Append ( err )
c . showDiagnostics ( diags )
return 1
}
2021-02-11 19:52:10 -06:00
opReq . Hooks = [ ] terraform . Hook { c . uiHook ( ) }
2018-10-14 18:54:23 -05:00
{
var moreDiags tfdiags . Diagnostics
opReq . Variables , moreDiags = c . collectVariableValues ( )
diags = diags . Append ( moreDiags )
if moreDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
return 1
}
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 12:01:30 -06:00
opReq . View = views . NewOperation ( arguments . ViewHuman , c . RunningInAutomation , c . View )
2017-01-18 22:50:45 -06:00
backend: Validate remote backend Terraform version
When using the enhanced remote backend, a subset of all Terraform
operations are supported. Of these, only plan and apply can be executed
on the remote infrastructure (e.g. Terraform Cloud). Other operations
run locally and use the remote backend for state storage.
This causes problems when the local version of Terraform does not match
the configured version from the remote workspace. If the two versions
are incompatible, an `import` or `state mv` operation can cause the
remote workspace to be unusable until a manual fix is applied.
To prevent this from happening accidentally, this commit introduces a
check that the local Terraform version and the configured remote
workspace Terraform version are compatible. This check is skipped for
commands which do not write state, and can also be disabled by the use
of a new command-line flag, `-ignore-remote-version`.
Terraform version compatibility is defined as:
- For all releases before 0.14.0, local must exactly equal remote, as
two different versions cannot share state;
- 0.14.0 to 1.0.x are compatible, as we will not change the state
version number until at least Terraform 1.1.0;
- Versions after 1.1.0 must have the same major and minor versions, as
we will not change the state version number in a patch release.
If the two versions are incompatible, a diagnostic is displayed,
advising that the error can be suppressed with `-ignore-remote-version`.
When this flag is used, the diagnostic is still displayed, but as a
warning instead of an error.
Commands which will not write state can assert this fact by calling the
helper `meta.ignoreRemoteBackendVersionConflict`, which will disable the
checks. Those which can write state should instead call the helper
`meta.remoteBackendVersionCheck`, which will return diagnostics for
display.
In addition to these explicit paths for managing the version check, we
have an implicit check in the remote backend's state manager
initialization method. Both of the above helpers will disable this
check. This fallback is in place to ensure that future code paths which
access state cannot accidentally skip the remote version check.
2020-11-13 15:43:56 -06:00
// Check remote Terraform version is compatible
remoteVersionDiags := c . remoteBackendVersionCheck ( b , opReq . Workspace )
diags = diags . Append ( remoteVersionDiags )
c . showDiagnostics ( diags )
if diags . HasErrors ( ) {
return 1
}
2017-01-18 22:50:45 -06:00
// Get the context
2018-03-27 17:31:05 -05:00
ctx , state , ctxDiags := local . Context ( opReq )
2020-08-11 10:23:42 -05:00
diags = diags . Append ( ctxDiags )
if ctxDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
return 1
}
2016-05-04 12:06:16 -05:00
2020-08-11 10:23:42 -05:00
// Successfully creating the context can result in a lock, so ensure we release it
2018-03-20 11:44:12 -05:00
defer func ( ) {
2021-02-16 06:19:22 -06:00
diags := opReq . StateLocker . Unlock ( )
if diags . HasErrors ( ) {
c . showDiagnostics ( diags )
2018-03-20 11:44:12 -05:00
}
} ( )
2016-05-04 12:32:08 -05:00
// Perform the import. Note that as you can see it is possible for this
// API to import more than one resource at once. For now, we only allow
// one while we stabilize this feature.
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
newState , importDiags := ctx . Import ( & terraform . ImportOpts {
2016-05-04 12:32:08 -05:00
Targets : [ ] * terraform . ImportTarget {
& terraform . ImportTarget {
2020-02-12 13:45:41 -06:00
Addr : addr ,
ID : args [ 1 ] ,
2016-05-04 12:32:08 -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
diags = diags . Append ( importDiags )
if diags . 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 )
2016-05-04 12:06:16 -05:00
return 1
}
2016-05-04 12:32:08 -05:00
// Persist the final state
2016-05-04 12:06:16 -05:00
log . Printf ( "[INFO] Writing state output to: %s" , c . Meta . StateOutPath ( ) )
2017-01-18 22:50:45 -06:00
if err := state . WriteState ( newState ) ; err != nil {
c . Ui . Error ( fmt . Sprintf ( "Error writing state file: %s" , err ) )
return 1
}
if err := state . PersistState ( ) ; err != nil {
2016-05-04 12:06:16 -05:00
c . Ui . Error ( fmt . Sprintf ( "Error writing state file: %s" , err ) )
return 1
}
2017-05-16 20:26:20 -05:00
c . Ui . Output ( c . Colorize ( ) . Color ( "[reset][green]\n" + importCommandSuccessMsg ) )
2016-05-04 14:05:42 -05:00
2017-09-18 13:41:30 -05:00
if c . Meta . allowMissingConfig && rc == nil {
c . Ui . Output ( c . Colorize ( ) . Color ( "[reset][yellow]\n" + importCommandAllowMissingResourceMsg ) )
}
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 )
if diags . HasErrors ( ) {
return 1
}
2016-05-04 12:06:16 -05:00
return 0
}
func ( c * ImportCommand ) Help ( ) string {
helpText := `
2021-02-22 08:25:56 -06:00
Usage : terraform [ global options ] import [ options ] ADDR ID
2016-05-04 12:06:16 -05:00
Import existing infrastructure into your Terraform state .
This will find and import the specified resource into your Terraform
state , allowing existing infrastructure to come under Terraform
management without having to be initially created by Terraform .
The ADDR specified is the address to import the resource to . Please
see the documentation online for resource addresses . The ID is a
resource - specific ID to identify that resource being imported . Please
reference the documentation for the resource type you ' re importing to
determine the ID syntax to use . It typically matches directly to the ID
that the provider uses .
2017-09-18 13:41:30 -05:00
The current implementation of Terraform import can only import resources
into the state . It does not generate configuration . A future version of
Terraform will also generate configuration .
Because of this , prior to running terraform import it is necessary to write
a resource configuration block for the resource manually , to which the
imported object will be attached .
2016-05-04 12:06:16 -05:00
This command will not modify your infrastructure , but it will make
network requests to inspect parts of your infrastructure relevant to
the resource being imported .
Options :
2017-09-18 13:41:30 -05:00
- config = path Path to a directory of Terraform configuration files
to use to configure the provider . Defaults to pwd .
If no config files are present , they must be provided
via the input prompts or env vars .
2016-11-02 13:11:42 -05:00
2017-09-18 13:41:30 -05:00
- allow - missing - config Allow import when no resource configuration block exists .
2016-05-04 12:06:16 -05:00
2021-05-11 13:37:32 -05:00
- input = false Disable interactive input prompts .
2017-04-01 15:19:59 -05:00
2021-05-12 11:05:03 -05:00
- lock = false Don ' t hold a state lock during the operation . This is
dangerous if others might concurrently run commands
against the same workspace .
2017-04-01 15:19:59 -05:00
2017-09-18 13:41:30 -05:00
- lock - timeout = 0 s Duration to retry a state lock .
2016-05-04 12:06:16 -05:00
2017-09-18 13:41:30 -05:00
- no - color If specified , output won ' t contain any color .
2016-11-23 11:40:11 -06:00
2017-09-18 13:41:30 -05:00
- var ' foo = bar ' Set a variable in the Terraform configuration . This
flag can be set multiple times . This is only useful
with the "-config" flag .
- var - file = foo Set variables in the Terraform configuration from
a file . If "terraform.tfvars" or any ".auto.tfvars"
files are present , they will be automatically loaded .
2017-01-24 15:01:23 -06:00
2021-05-11 13:37:32 -05:00
- ignore - remote - version A rare option used for the remote backend only . See
the remote backend documentation for more information .
2017-01-24 15:01:23 -06:00
2021-03-24 18:17:03 -05:00
- state , state - out , and - backup are legacy options supported for the local
backend only . For more information , see the local backend ' s documentation .
2016-05-04 12:06:16 -05:00
`
return strings . TrimSpace ( helpText )
}
func ( c * ImportCommand ) Synopsis ( ) string {
2020-10-23 18:55:32 -05:00
return "Associate existing infrastructure with a Terraform resource"
2016-05-04 12:06:16 -05:00
}
2017-05-16 20:20:08 -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
const importCommandInvalidAddressReference = ` For information on valid syntax , see :
2021-01-19 15:53:40 -06:00
https : //www.terraform.io/docs/cli/state/resource-addressing.html`
2017-05-16 20:26:20 -05:00
2017-12-08 12:22:07 -06:00
const importCommandMissingResourceFmt = ` [ reset ] [ bold ] [ red ] Error : [ reset ] [ bold ] resource address % q does not exist in the configuration . [ reset ]
2017-05-16 20:26:20 -05:00
Before importing this resource , please create its configuration in % s . For example :
resource % q % q {
# ( resource arguments )
}
`
const importCommandSuccessMsg = ` Import successful !
The resources that were imported are shown above . These resources are now in
your Terraform state and will henceforth be managed by Terraform .
2017-09-18 13:41:30 -05:00
`
const importCommandAllowMissingResourceMsg = ` Import does not generate resource configuration , you must create a resource
configuration block that matches the current or desired state manually .
2017-05-16 20:26:20 -05:00
2017-09-18 13:41:30 -05:00
If there is no matching resource configuration block for the imported
resource , Terraform will delete the resource on the next "terraform apply" .
It is recommended that you run "terraform plan" to verify that the
configuration is correct and complete .
2017-05-16 20:26:20 -05:00
`