2016-05-10 05:32:31 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2016-05-10 18:25:54 +02:00
|
|
|
"fmt"
|
2016-05-10 05:32:31 +02:00
|
|
|
"log"
|
2018-10-16 03:15:08 +02:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
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
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
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
|
|
|
|
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"
|
2019-04-16 02:54:06 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2016-05-10 05:32:31 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2019-12-04 17:30:20 +01:00
|
|
|
// testStepImportState runs an import state test step
|
2016-05-10 05:32:31 +02:00
|
|
|
func testStepImportState(
|
|
|
|
opts terraform.ContextOpts,
|
|
|
|
state *terraform.State,
|
2018-12-07 03:14:31 +01:00
|
|
|
step TestStep) (*terraform.State, error) {
|
|
|
|
|
2016-05-10 05:32:31 +02:00
|
|
|
// Determine the ID to import
|
2017-09-07 04:42:16 +02:00
|
|
|
var importId string
|
|
|
|
switch {
|
|
|
|
case step.ImportStateIdFunc != nil:
|
|
|
|
var err error
|
|
|
|
importId, err = step.ImportStateIdFunc(state)
|
|
|
|
if err != nil {
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
case step.ImportStateId != "":
|
|
|
|
importId = step.ImportStateId
|
|
|
|
default:
|
2016-05-10 05:32:31 +02:00
|
|
|
resource, err := testResource(step, state)
|
|
|
|
if err != nil {
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
importId = resource.Primary.ID
|
|
|
|
}
|
2017-09-07 04:42:16 +02:00
|
|
|
|
2017-03-31 00:11:10 +02:00
|
|
|
importPrefix := step.ImportStateIdPrefix
|
|
|
|
if importPrefix != "" {
|
|
|
|
importId = fmt.Sprintf("%s%s", importPrefix, importId)
|
|
|
|
}
|
2016-05-10 05:32:31 +02:00
|
|
|
|
|
|
|
// Setup the context. We initialize with an empty state. We use the
|
|
|
|
// full config for provider configurations.
|
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
|
|
|
cfg, err := testConfig(opts, step)
|
2016-05-10 05:32:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
opts.Config = cfg
|
2018-10-16 03:15:08 +02:00
|
|
|
|
|
|
|
// import tests start with empty state
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
opts.State = states.NewState()
|
2018-10-16 03:15: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
|
|
|
ctx, stepDiags := terraform.NewContext(&opts)
|
|
|
|
if stepDiags.HasErrors() {
|
|
|
|
return state, stepDiags.Err()
|
2016-05-10 05:32:31 +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
|
|
|
// The test step provides the resource address as a string, so we need
|
|
|
|
// to parse it to get an addrs.AbsResourceAddress to pass in to the
|
|
|
|
// import method.
|
|
|
|
traversal, hclDiags := hclsyntax.ParseTraversalAbs([]byte(step.ResourceName), "", hcl.Pos{})
|
|
|
|
if hclDiags.HasErrors() {
|
|
|
|
return nil, hclDiags
|
|
|
|
}
|
|
|
|
importAddr, stepDiags := addrs.ParseAbsResourceInstance(traversal)
|
|
|
|
if stepDiags.HasErrors() {
|
|
|
|
return nil, stepDiags.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the import
|
2018-10-16 03:15:08 +02:00
|
|
|
importedState, stepDiags := ctx.Import(&terraform.ImportOpts{
|
2016-05-10 18:25:54 +02:00
|
|
|
// Set the module so that any provider config is loaded
|
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
|
|
|
Config: cfg,
|
2016-05-10 18:25:54 +02:00
|
|
|
|
2016-05-10 05:32:31 +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: importAddr,
|
2016-05-10 05:32:31 +02:00
|
|
|
ID: importId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
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 stepDiags.HasErrors() {
|
|
|
|
log.Printf("[ERROR] Test: ImportState failure: %s", stepDiags.Err())
|
|
|
|
return state, stepDiags.Err()
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-10 18:20:03 +01:00
|
|
|
newState, err := shimNewState(importedState, step.providers)
|
2018-12-04 21:42:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-10 05:32:31 +02:00
|
|
|
// Go through the new state and verify
|
|
|
|
if step.ImportStateCheck != nil {
|
2018-10-16 03:15:08 +02:00
|
|
|
var states []*terraform.InstanceState
|
2016-05-10 05:32:31 +02:00
|
|
|
for _, r := range newState.RootModule().Resources {
|
2018-10-16 03:15:08 +02:00
|
|
|
if r.Primary != nil {
|
2019-01-30 01:49:02 +01:00
|
|
|
is := r.Primary.DeepCopy()
|
|
|
|
is.Ephemeral.Type = r.Type // otherwise the check function cannot see the type
|
|
|
|
states = append(states, is)
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-30 01:49:02 +01:00
|
|
|
if err := step.ImportStateCheck(states); err != nil {
|
2016-05-10 05:32:31 +02:00
|
|
|
return state, err
|
2019-01-30 01:49:02 +01:00
|
|
|
}
|
2016-05-10 05:32:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 18:25:54 +02:00
|
|
|
// Verify that all the states match
|
|
|
|
if step.ImportStateVerify {
|
2018-10-16 03:15:08 +02:00
|
|
|
new := newState.RootModule().Resources
|
|
|
|
old := state.RootModule().Resources
|
|
|
|
for _, r := range new {
|
|
|
|
// Find the existing resource
|
|
|
|
var oldR *terraform.ResourceState
|
|
|
|
for _, r2 := range old {
|
|
|
|
if r2.Primary != nil && r2.Primary.ID == r.Primary.ID && r2.Type == r.Type {
|
|
|
|
oldR = r2
|
|
|
|
break
|
2016-05-10 18:25:54 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
|
|
|
if oldR == nil {
|
|
|
|
return state, fmt.Errorf(
|
|
|
|
"Failed state verification, resource with ID %s not found",
|
|
|
|
r.Primary.ID)
|
|
|
|
}
|
2016-05-10 18:25:54 +02:00
|
|
|
|
2019-04-16 02:54:06 +02:00
|
|
|
// We'll try our best to find the schema for this resource type
|
|
|
|
// so we can ignore Removed fields during validation. If we fail
|
|
|
|
// to find the schema then we won't ignore them and so the test
|
|
|
|
// will need to rely on explicit ImportStateVerifyIgnore, though
|
|
|
|
// this shouldn't happen in any reasonable case.
|
|
|
|
var rsrcSchema *schema.Resource
|
|
|
|
if providerAddr, diags := addrs.ParseAbsProviderConfigStr(r.Provider); !diags.HasErrors() {
|
Initial steps towards AbsProviderConfig/LocalProviderConfig separation (#23978)
* Introduce "Local" terminology for non-absolute provider config addresses
In a future change AbsProviderConfig and LocalProviderConfig are going to
become two entirely distinct types, rather than Abs embedding Local as
written here. This naming change is in preparation for that subsequent
work, which will also include introducing a new "ProviderConfig" type
that is an interface that AbsProviderConfig and LocalProviderConfig both
implement.
This is intended to be largely just a naming change to get started, so
we can deal with all of the messy renaming. However, this did also require
a slight change in modeling where the Resource.DefaultProviderConfig
method has become Resource.DefaultProvider returning a Provider address
directly, because this method doesn't have enough information to construct
a true and accurate LocalProviderConfig -- it would need to refer to the
configuration to know what this module is calling the provider it has
selected.
In order to leave a trail to follow for subsequent work, all of the
changes here are intended to ensure that remaining work will become
obvious via compile-time errors when all of the following changes happen:
- The concept of "legacy" provider addresses is removed from the addrs
package, including removing addrs.NewLegacyProvider and
addrs.Provider.LegacyString.
- addrs.AbsProviderConfig stops having addrs.LocalProviderConfig embedded
in it and has an addrs.Provider and a string alias directly instead.
- The provider-schema-handling parts of Terraform core are updated to
work with addrs.Provider to identify providers, rather than legacy
strings.
In particular, there are still several codepaths here making legacy
provider address assumptions (in order to limit the scope of this change)
but I've made sure each one is doing something that relies on at least
one of the above changes not having been made yet.
* addrs: ProviderConfig interface
In a (very) few special situations in the main "terraform" package we need
to make runtime decisions about whether a provider config is absolute
or local.
We currently do that by exploiting the fact that AbsProviderConfig has
LocalProviderConfig nested inside of it and so in the local case we can
just ignore the wrapping AbsProviderConfig and use the embedded value.
In a future change we'll be moving away from that embedding and making
these two types distinct in order to represent that mapping between them
requires consulting a lookup table in the configuration, and so here we
introduce a new interface type ProviderConfig that can represent either
AbsProviderConfig or LocalProviderConfig decided dynamically at runtime.
This also includes the Config.ResolveAbsProviderAddr method that will
eventually be responsible for that local-to-absolute translation, so
that callers with access to the configuration can normalize to an
addrs.AbsProviderConfig given a non-nil addrs.ProviderConfig. That's
currently unused because existing callers are still relying on the
simplistic structural transform, but we'll switch them over in a later
commit.
* rename LocalType to LocalName
Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
2020-01-31 14:23:07 +01:00
|
|
|
providerType := providerAddr.ProviderConfig.LocalName
|
2019-04-16 02:54:06 +02:00
|
|
|
if provider, ok := step.providers[providerType]; ok {
|
|
|
|
if provider, ok := provider.(*schema.Provider); ok {
|
|
|
|
rsrcSchema = provider.ResourcesMap[r.Type]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 21:42:33 +01:00
|
|
|
// don't add empty flatmapped containers, so we can more easily
|
|
|
|
// compare the attributes
|
|
|
|
skipEmpty := func(k, v string) bool {
|
|
|
|
if strings.HasSuffix(k, ".#") || strings.HasSuffix(k, ".%") {
|
|
|
|
if v == "0" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// Compare their attributes
|
|
|
|
actual := make(map[string]string)
|
|
|
|
for k, v := range r.Primary.Attributes {
|
2018-12-04 21:42:33 +01:00
|
|
|
if skipEmpty(k, v) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
actual[k] = v
|
|
|
|
}
|
2018-12-04 21:42:33 +01:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
expected := make(map[string]string)
|
|
|
|
for k, v := range oldR.Primary.Attributes {
|
2018-12-04 21:42:33 +01:00
|
|
|
if skipEmpty(k, v) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
expected[k] = v
|
|
|
|
}
|
2016-05-13 20:27:23 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// Remove fields we're ignoring
|
|
|
|
for _, v := range step.ImportStateVerifyIgnore {
|
2019-04-16 02:54:06 +02:00
|
|
|
for k := range actual {
|
2018-10-16 03:15:08 +02:00
|
|
|
if strings.HasPrefix(k, v) {
|
|
|
|
delete(actual, k)
|
2016-05-13 20:27:23 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2019-04-16 02:54:06 +02:00
|
|
|
for k := range expected {
|
2018-10-16 03:15:08 +02:00
|
|
|
if strings.HasPrefix(k, v) {
|
|
|
|
delete(expected, k)
|
2016-05-13 20:27:23 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2016-05-13 20:27:23 +02:00
|
|
|
|
2019-04-16 02:54:06 +02:00
|
|
|
// Also remove any attributes that are marked as "Removed" in the
|
|
|
|
// schema, if we have a schema to check that against.
|
|
|
|
if rsrcSchema != nil {
|
|
|
|
for k := range actual {
|
|
|
|
for _, schema := range rsrcSchema.SchemasForFlatmapPath(k) {
|
|
|
|
if schema.Removed != "" {
|
|
|
|
delete(actual, k)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k := range expected {
|
|
|
|
for _, schema := range rsrcSchema.SchemasForFlatmapPath(k) {
|
|
|
|
if schema.Removed != "" {
|
|
|
|
delete(expected, k)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
// Determine only the different attributes
|
|
|
|
for k, v := range expected {
|
|
|
|
if av, ok := actual[k]; ok && v == av {
|
|
|
|
delete(expected, k)
|
|
|
|
delete(actual, k)
|
2016-05-10 18:25:54 +02: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
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
|
|
|
|
spewConf := spew.NewDefaultConfig()
|
|
|
|
spewConf.SortKeys = true
|
|
|
|
return state, fmt.Errorf(
|
|
|
|
"ImportStateVerify attributes not equivalent. Difference is shown below. Top is actual, bottom is expected."+
|
|
|
|
"\n\n%s\n\n%s",
|
|
|
|
spewConf.Sdump(actual), spewConf.Sdump(expected))
|
2016-05-10 18:25:54 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2016-05-10 18:25:54 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 05:32:31 +02:00
|
|
|
// Return the old state (non-imported) so we don't change anything.
|
|
|
|
return state, nil
|
|
|
|
}
|