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"
|
2016-05-10 18:25:54 +02:00
|
|
|
"reflect"
|
2016-05-13 20:27:23 +02:00
|
|
|
"strings"
|
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
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
|
|
"github.com/hashicorp/hcl2/hcl/hclsyntax"
|
|
|
|
|
2016-05-10 18:25:54 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2016-05-10 05:32:31 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testStepImportState runs an imort state test step
|
|
|
|
func testStepImportState(
|
|
|
|
opts terraform.ContextOpts,
|
|
|
|
state *terraform.State,
|
|
|
|
step TestStep) (*terraform.State, error) {
|
|
|
|
// 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
|
2016-05-10 05:32:31 +02:00
|
|
|
opts.State = terraform.NewState()
|
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
|
|
|
|
newState, 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
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the new state and verify
|
|
|
|
if step.ImportStateCheck != nil {
|
|
|
|
var states []*terraform.InstanceState
|
|
|
|
for _, r := range newState.RootModule().Resources {
|
|
|
|
if r.Primary != nil {
|
|
|
|
states = append(states, r.Primary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := step.ImportStateCheck(states); err != nil {
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 18:25:54 +02:00
|
|
|
// Verify that all the states match
|
|
|
|
if step.ImportStateVerify {
|
|
|
|
new := newState.RootModule().Resources
|
|
|
|
old := state.RootModule().Resources
|
|
|
|
for _, r := range new {
|
|
|
|
// Find the existing resource
|
|
|
|
var oldR *terraform.ResourceState
|
|
|
|
for _, r2 := range old {
|
2016-08-22 03:24:34 +02:00
|
|
|
if r2.Primary != nil && r2.Primary.ID == r.Primary.ID && r2.Type == r.Type {
|
2016-05-10 18:25:54 +02:00
|
|
|
oldR = r2
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if oldR == nil {
|
|
|
|
return state, fmt.Errorf(
|
|
|
|
"Failed state verification, resource with ID %s not found",
|
|
|
|
r.Primary.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare their attributes
|
2016-07-15 21:15:47 +02:00
|
|
|
actual := make(map[string]string)
|
|
|
|
for k, v := range r.Primary.Attributes {
|
|
|
|
actual[k] = v
|
|
|
|
}
|
|
|
|
expected := make(map[string]string)
|
|
|
|
for k, v := range oldR.Primary.Attributes {
|
|
|
|
expected[k] = v
|
|
|
|
}
|
2016-05-13 20:27:23 +02:00
|
|
|
|
|
|
|
// Remove fields we're ignoring
|
|
|
|
for _, v := range step.ImportStateVerifyIgnore {
|
|
|
|
for k, _ := range actual {
|
|
|
|
if strings.HasPrefix(k, v) {
|
|
|
|
delete(actual, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, _ := range expected {
|
|
|
|
if strings.HasPrefix(k, v) {
|
|
|
|
delete(expected, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 18:25:54 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spewConf := spew.NewDefaultConfig()
|
|
|
|
spewConf.SortKeys = true
|
|
|
|
return state, fmt.Errorf(
|
2016-05-13 20:27:23 +02:00
|
|
|
"ImportStateVerify attributes not equivalent. Difference is shown below. Top is actual, bottom is expected."+
|
2016-05-10 18:25:54 +02:00
|
|
|
"\n\n%s\n\n%s",
|
|
|
|
spewConf.Sdump(actual), spewConf.Sdump(expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 05:32:31 +02:00
|
|
|
// Return the old state (non-imported) so we don't change anything.
|
|
|
|
return state, nil
|
|
|
|
}
|