2016-05-04 19:06:16 +02: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 19:33:53 +02:00
"errors"
2016-05-04 19:06:16 +02:00
"fmt"
"log"
2016-11-02 18:30:28 +01:00
"os"
2016-05-04 19:06:16 +02:00
"strings"
2016-05-04 19:32:08 +02:00
2017-12-08 19:22:07 +01:00
"github.com/hashicorp/hcl2/hcl"
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
"github.com/hashicorp/hcl2/hcl/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-07 01:41:48 +01:00
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
"github.com/hashicorp/terraform/addrs"
2017-01-19 05:50:45 +01:00
"github.com/hashicorp/terraform/backend"
2018-03-28 00:31:05 +02:00
"github.com/hashicorp/terraform/configs"
2016-05-04 19:32:08 +02:00
"github.com/hashicorp/terraform/terraform"
2017-12-08 19:22:07 +01:00
"github.com/hashicorp/terraform/tfdiags"
2016-05-04 19:06:16 +02: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 19:11:42 +01: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
2017-03-08 05:09:48 +01:00
args , err = c . Meta . process ( args , true )
if err != nil {
return 1
}
2016-05-04 19:06:16 +02:00
cmdFlags := c . Meta . flagSet ( "import" )
cmdFlags . IntVar ( & c . Meta . parallelism , "parallelism" , 0 , "parallelism" )
2016-11-02 19:08:16 +01:00
cmdFlags . StringVar ( & c . Meta . statePath , "state" , DefaultStateFilename , "path" )
2016-05-04 19:06:16 +02:00
cmdFlags . StringVar ( & c . Meta . stateOutPath , "state-out" , "" , "path" )
cmdFlags . StringVar ( & c . Meta . backupPath , "backup" , "" , "path" )
2016-11-02 19:11:42 +01:00
cmdFlags . StringVar ( & configPath , "config" , pwd , "path" )
2016-11-23 10:44:52 +01:00
cmdFlags . StringVar ( & c . Meta . provider , "provider" , "" , "provider" )
2017-04-01 22:19:59 +02:00
cmdFlags . BoolVar ( & c . Meta . stateLock , "lock" , true , "lock state" )
cmdFlags . DurationVar ( & c . Meta . stateLockTimeout , "lock-timeout" , 0 , "lock timeout" )
2017-09-18 20:41:30 +02:00
cmdFlags . BoolVar ( & c . Meta . allowMissingConfig , "allow-missing-config" , false , "allow missing config" )
2016-05-04 19:06:16 +02: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 19:33:53 +02:00
var diags tfdiags . Diagnostics
// Parse the provided resource address.
2018-10-10 02:47:53 +02: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 19:33:53 +02:00
diags = diags . Append ( travDiags )
if travDiags . HasErrors ( ) {
2018-10-10 02:47:53 +02: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 19:33:53 +02:00
c . showDiagnostics ( diags )
c . Ui . Info ( importCommandInvalidAddressReference )
2017-05-17 03:20:08 +02: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 19:33:53 +02:00
addr , addrDiags := addrs . ParseAbsResourceInstance ( traversal )
diags = diags . Append ( addrDiags )
if addrDiags . HasErrors ( ) {
2018-10-10 02:47:53 +02: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 19:33:53 +02:00
c . showDiagnostics ( diags )
c . Ui . Info ( importCommandInvalidAddressReference )
2017-05-17 03:20:08 +02: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 19:33:53 +02: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-17 03:20:08 +02:00
return 1
}
2018-03-28 00:31:05 +02: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 19:22:07 +01:00
2018-03-28 00:31:05 +02: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-19 05:50:45 +01:00
}
2017-05-17 03:26:20 +02: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 19:33:53 +02:00
targetConfig := config . DescendentForInstance ( addr . Module )
2018-03-28 00:31:05 +02: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 19:33:53 +02:00
modulePath := addr . Module . String ( )
2017-12-08 19:22:07 +01: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-17 03:26:20 +02:00
return 1
}
2018-03-28 00:31:05 +02: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 19:33:53 +02:00
var rc * configs . Resource
resourceRelAddr := addr . Resource . Resource
2017-05-17 03:26:20 +02: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 19:33:53 +02:00
if resourceRelAddr . Type == thisRc . Type && resourceRelAddr . Name == thisRc . Name {
2017-05-17 03:26:20 +02:00
rc = thisRc
break
}
}
2017-09-18 20:41:30 +02: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 19:33:53 +02:00
modulePath := addr . Module . String ( )
2017-05-17 03:26:20 +02:00
if modulePath == "" {
modulePath = "the root module"
}
2017-12-08 19:22:07 +01: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-17 03:26:20 +02: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 19:33:53 +02:00
addr , modulePath , resourceRelAddr . Type , resourceRelAddr . Name ,
2017-05-17 03:26:20 +02:00
) )
return 1
2017-05-01 23:47:53 +02:00
}
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
// Also parse the user-provided provider address, if any.
var providerAddr addrs . AbsProviderConfig
if c . Meta . provider != "" {
traversal , travDiags := hclsyntax . ParseTraversalAbs ( [ ] byte ( c . Meta . provider ) , ` -provider=... ` , hcl . Pos { Line : 1 , Column : 1 } )
diags = diags . Append ( travDiags )
if travDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
c . Ui . Info ( importCommandInvalidAddressReference )
return 1
}
relAddr , addrDiags := addrs . ParseProviderConfigCompact ( traversal )
diags = diags . Append ( addrDiags )
if addrDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
return 1
}
providerAddr = relAddr . Absolute ( addrs . RootModuleInstance )
} else {
// Use a default address inferred from the resource type.
2018-05-08 20:19:02 +02:00
// We assume the same module as the resource address here, which
// may get resolved to an inherited provider when we construct the
// import graph inside ctx.Import, called below.
providerAddr = resourceRelAddr . DefaultProviderConfig ( ) . Absolute ( addr . Module )
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
}
2017-06-27 00:28:45 +02: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-19 05:50:45 +01:00
// Load the backend
2018-03-28 00:31:05 +02:00
b , backendDiags := c . Backend ( & BackendOpts {
Config : config . Module . Backend ,
2017-05-01 23:47:53 +02:00
} )
2018-03-28 00:31:05 +02:00
diags = diags . Append ( backendDiags )
if backendDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
2017-01-19 05:50:45 +01:00
return 1
}
2017-08-09 00:41:00 +02: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-19 05:50:45 +01: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 23:24:45 +02:00
opReq := c . Operation ( b )
2018-03-28 00:31:05 +02:00
opReq . ConfigDir = configPath
opReq . ConfigLoader , err = c . initConfigLoader ( )
if err != nil {
diags = diags . Append ( err )
c . showDiagnostics ( diags )
return 1
}
2018-10-15 01:54:23 +02:00
{
var moreDiags tfdiags . Diagnostics
opReq . Variables , moreDiags = c . collectVariableValues ( )
diags = diags . Append ( moreDiags )
if moreDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
return 1
}
}
2017-01-19 05:50:45 +01:00
// Get the context
2018-03-28 00:31:05 +02:00
ctx , state , ctxDiags := local . Context ( opReq )
diags = diags . Append ( ctxDiags )
if ctxDiags . HasErrors ( ) {
c . showDiagnostics ( diags )
2016-05-04 19:06:16 +02:00
return 1
}
2018-03-20 17:44:12 +01:00
defer func ( ) {
err := opReq . StateLocker . Unlock ( nil )
if err != nil {
c . Ui . Error ( err . Error ( ) )
}
} ( )
2016-05-04 19:32:08 +02: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 19:33:53 +02:00
newState , importDiags := ctx . Import ( & terraform . ImportOpts {
2016-05-04 19:32:08 +02:00
Targets : [ ] * terraform . ImportTarget {
& terraform . ImportTarget {
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
Addr : addr ,
ID : args [ 1 ] ,
ProviderAddr : providerAddr ,
2016-05-04 19:32:08 +02:00
} ,
} ,
} )
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
diags = diags . Append ( 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-07 01:41:48 +01:00
c . showDiagnostics ( diags )
2016-05-04 19:06:16 +02:00
return 1
}
2016-05-04 19:32:08 +02:00
// Persist the final state
2016-05-04 19:06:16 +02:00
log . Printf ( "[INFO] Writing state output to: %s" , c . Meta . StateOutPath ( ) )
2017-01-19 05:50:45 +01: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 19:06:16 +02:00
c . Ui . Error ( fmt . Sprintf ( "Error writing state file: %s" , err ) )
return 1
}
2017-05-17 03:26:20 +02:00
c . Ui . Output ( c . Colorize ( ) . Color ( "[reset][green]\n" + importCommandSuccessMsg ) )
2016-05-04 21:05:42 +02:00
2017-09-18 20:41:30 +02: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-07 01:41:48 +01:00
c . showDiagnostics ( diags )
if diags . HasErrors ( ) {
return 1
}
2016-05-04 19:06:16 +02:00
return 0
}
func ( c * ImportCommand ) Help ( ) string {
helpText := `
Usage : terraform import [ options ] ADDR ID
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 20:41:30 +02: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 19:06:16 +02: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 20:41:30 +02:00
- 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 .
2016-05-04 19:06:16 +02:00
2017-09-18 20:41:30 +02: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 19:11:42 +01:00
2017-09-18 20:41:30 +02:00
- allow - missing - config Allow import when no resource configuration block exists .
2016-05-04 19:06:16 +02:00
2017-09-18 20:41:30 +02:00
- input = true Ask for input for variables if not directly set .
2017-04-01 22:19:59 +02:00
2017-09-18 20:41:30 +02:00
- lock = true Lock the state file when locking is supported .
2017-04-01 22:19:59 +02:00
2017-09-18 20:41:30 +02:00
- lock - timeout = 0 s Duration to retry a state lock .
2016-05-04 19:06:16 +02:00
2017-09-18 20:41:30 +02:00
- no - color If specified , output won ' t contain any color .
2016-11-23 18:40:11 +01:00
2017-09-18 20:41:30 +02:00
- provider = provider Specific provider to use for import . This is used for
specifying aliases , such as "aws.eu" . Defaults to the
normal provider prefix of the resource being imported .
2016-05-04 19:06:16 +02:00
2017-09-18 20:41:30 +02:00
- state = PATH Path to the source state file . Defaults to the configured
backend , or "terraform.tfstate"
2016-05-04 19:06:16 +02:00
2017-09-18 20:41:30 +02:00
- state - out = PATH Path to the destination state file to write to . If this
isn ' t specified , the source state file will be used . This
can be a new or existing path .
2017-01-24 22:01:23 +01:00
2017-09-18 20:41:30 +02: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 22:01:23 +01:00
2016-05-04 19:06:16 +02:00
`
return strings . TrimSpace ( helpText )
}
func ( c * ImportCommand ) Synopsis ( ) string {
return "Import existing infrastructure into Terraform"
}
2017-05-17 03:20:08 +02:00
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
const importCommandInvalidAddressReference = ` For information on valid syntax , see :
https : //www.terraform.io/docs/internals/resource-addressing.html`
2017-05-17 03:26:20 +02:00
2017-12-08 19:22:07 +01:00
const importCommandMissingResourceFmt = ` [ reset ] [ bold ] [ red ] Error : [ reset ] [ bold ] resource address % q does not exist in the configuration . [ reset ]
2017-05-17 03:26:20 +02: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 20:41:30 +02: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-17 03:26:20 +02:00
2017-09-18 20:41:30 +02: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-17 03:26:20 +02:00
`