2016-04-26 19:47:56 +02:00
package terraform
import (
2016-05-10 18:15:19 +02:00
"fmt"
2016-04-26 19:47:56 +02:00
"strings"
"testing"
2018-05-05 04:24:06 +02:00
"github.com/hashicorp/terraform/addrs"
2018-07-05 19:33:29 +02:00
"github.com/hashicorp/terraform/configs/configschema"
2018-08-17 21:32:35 +02:00
"github.com/hashicorp/terraform/providers"
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"
2018-05-05 04:24:06 +02:00
"github.com/zclconf/go-cty/cty"
2016-04-26 19:47:56 +02:00
)
2016-04-28 19:25:58 +02:00
func TestContextImport_basic ( t * testing . T ) {
2016-04-26 19:47:56 +02:00
p := testProvider ( "aws" )
2017-04-22 02:19:37 +02:00
m := testModule ( t , "import-provider" )
2016-04-26 19:47:56 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-04-26 19:47:56 +02:00
} )
2016-04-28 19:25:58 +02:00
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-04-28 11:46:46 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2018-12-04 21:39:46 +01:00
ID : "bar" ,
2016-04-28 11:46:46 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-04-26 19:47:56 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
2016-04-28 11:46:46 +02:00
expected := strings . TrimSpace ( testImportStr )
2016-04-26 19:47:56 +02:00
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-04-28 11:46:46 +02:00
2016-08-20 01:14:33 +02:00
func TestContextImport_countIndex ( t * testing . T ) {
p := testProvider ( "aws" )
2017-04-22 02:19:37 +02:00
m := testModule ( t , "import-provider" )
2016-08-20 01:14:33 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-08-20 01:14:33 +02:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-08-20 01:14:33 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . IntKey ( 0 ) ,
) ,
2018-12-04 21:39:46 +01:00
ID : "bar" ,
2016-08-20 01:14:33 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-08-20 01:14:33 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
expected := strings . TrimSpace ( testImportCountIndexStr )
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-05-01 00:53:48 +02:00
func TestContextImport_collision ( t * testing . T ) {
p := testProvider ( "aws" )
2017-04-22 02:19:37 +02:00
m := testModule ( t , "import-provider" )
2016-05-01 00:53:48 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-05-01 00:53:48 +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
State : states . BuildState ( func ( s * states . SyncState ) {
s . SetResourceInstanceCurrent (
addrs . Resource {
Mode : addrs . ManagedResourceMode ,
Type : "aws_instance" ,
Name : "foo" ,
} . Instance ( addrs . NoKey ) . Absolute ( addrs . RootModuleInstance ) ,
& states . ResourceInstanceObjectSrc {
AttrsFlat : map [ string ] string {
"id" : "bar" ,
2016-05-01 00:53:48 +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
Status : states . ObjectReady ,
2016-05-01 00:53:48 +02:00
} ,
2020-02-13 21:32:58 +01:00
addrs . AbsProviderConfig {
2020-03-31 20:03:33 +02:00
Provider : addrs . NewDefaultProvider ( "aws" ) ,
2020-03-11 02:21:19 +01:00
Module : addrs . RootModule ,
2020-02-13 21:32:58 +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
)
} ) ,
2016-05-01 00:53:48 +02:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-05-01 00:53:48 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2018-12-04 21:39:46 +01:00
ID : "bar" ,
2016-05-01 00:53:48 +02:00
} ,
} ,
} )
2018-05-29 21:17:59 +02:00
if ! diags . HasErrors ( ) {
t . Fatalf ( "succeeded; want an error indicating that the resource already exists in state" )
2016-05-01 00:53:48 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
2018-09-21 00:22:42 +02:00
expected := ` aws_instance . foo :
ID = bar
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ] `
2018-09-21 00:22:42 +02:00
2016-05-01 00:53:48 +02:00
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-04-30 23:55:26 +02:00
func TestContextImport_missingType ( t * testing . T ) {
p := testProvider ( "aws" )
2017-04-22 02:19:37 +02:00
m := testModule ( t , "import-provider" )
2018-09-21 00:22:42 +02:00
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
} ,
}
2016-04-30 23:55:26 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-04-30 23:55:26 +02:00
} )
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-04-30 23:55:26 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2018-12-04 21:39:46 +01:00
ID : "bar" ,
2016-04-30 23:55:26 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if ! diags . HasErrors ( ) {
2016-04-30 23:55:26 +02:00
t . Fatal ( "should error" )
}
2016-04-30 23:56:22 +02:00
actual := strings . TrimSpace ( state . String ( ) )
2016-05-01 00:53:48 +02:00
expected := "<no state>"
2016-04-30 23:56:22 +02:00
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
2016-04-30 23:55:26 +02:00
}
2016-05-10 18:15:19 +02:00
func TestContextImport_moduleProvider ( t * testing . T ) {
2018-05-29 22:14:20 +02:00
p := testProvider ( "aws" )
2018-05-29 21:50:31 +02:00
2016-05-10 18:15:19 +02:00
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
configured := false
p . ConfigureFn = func ( c * ResourceConfig ) error {
configured = true
if v , ok := c . Get ( "foo" ) ; ! ok || v . ( string ) != "bar" {
return fmt . Errorf ( "bad" )
}
return nil
}
2018-05-29 22:14:20 +02:00
m := testModule ( t , "import-provider" )
ctx := testContext2 ( t , & ContextOpts {
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2018-05-29 22:14:20 +02:00
} )
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-05-10 18:15:19 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2018-12-04 21:39:46 +01:00
ID : "bar" ,
2016-05-10 18:15:19 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-05-10 18:15:19 +02:00
}
if ! configured {
t . Fatal ( "didn't configure provider" )
}
actual := strings . TrimSpace ( state . String ( ) )
2016-11-02 18:29:58 +01:00
expected := strings . TrimSpace ( testImportStr )
if actual != expected {
2018-09-21 00:22:42 +02:00
t . Fatalf ( "expected:\n%s\n\ngot:\n%s" , expected , actual )
2016-11-02 18:29:58 +01:00
}
}
2017-10-18 01:02:21 +02:00
// Importing into a module requires a provider config in that module.
func TestContextImport_providerModule ( t * testing . T ) {
2017-01-25 00:36:45 +01:00
p := testProvider ( "aws" )
2020-03-20 13:15:29 +01:00
m := testModule ( t , "import-module" )
2017-01-25 00:36:45 +01:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2017-01-25 00:36:45 +01:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
configured := false
p . ConfigureFn = func ( c * ResourceConfig ) error {
configured = true
if v , ok := c . Get ( "foo" ) ; ! ok || v . ( string ) != "bar" {
return fmt . Errorf ( "bad" )
}
return nil
}
2018-05-29 21:02:34 +02:00
_ , diags := ctx . Import ( & ImportOpts {
2017-01-25 00:36:45 +01:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . Child ( "child" , addrs . NoKey ) . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2018-12-04 21:39:46 +01:00
ID : "bar" ,
2017-01-25 00:36:45 +01:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2017-01-25 00:36:45 +01:00
}
if ! configured {
t . Fatal ( "didn't configure provider" )
}
}
2016-11-02 18:29:58 +01:00
// Test that import will interpolate provider configuration and use
// that configuration for import.
terraform: Relax provider config ref constraints
When configuring providers, it is normally valid to refer to any value
which is known at apply time. This can include resource instance
attributes, variables, locals, and so on.
The import command has a simpler graph evaluation, which means that
many of these values are unknown. We previously prevented this from
happening by restricting provider configuration references to input
variables (#22862), but this was more restrictive than is necessary.
This commit changes how we verify provider configuration for import.
We no longer inspect the configuration references during graph building,
because this is too early to determine if these values will become known
or not.
Instead, when the provider is configured during evaluation, we
check if the configuration value is wholly known. If not, we fail with a
diagnostic error.
Includes a test case which verifies that providers can now be configured
using locals as well as vars, and an updated test case which verifies
that providers cannot be configured with references to resources.
2020-06-27 00:15:00 +02:00
func TestContextImport_providerConfig ( t * testing . T ) {
testCases := map [ string ] struct {
module string
value string
} {
"variables" : {
module : "import-provider-vars" ,
value : "bar" ,
2020-03-31 20:03:33 +02:00
} ,
terraform: Relax provider config ref constraints
When configuring providers, it is normally valid to refer to any value
which is known at apply time. This can include resource instance
attributes, variables, locals, and so on.
The import command has a simpler graph evaluation, which means that
many of these values are unknown. We previously prevented this from
happening by restricting provider configuration references to input
variables (#22862), but this was more restrictive than is necessary.
This commit changes how we verify provider configuration for import.
We no longer inspect the configuration references during graph building,
because this is too early to determine if these values will become known
or not.
Instead, when the provider is configured during evaluation, we
check if the configuration value is wholly known. If not, we fail with a
diagnostic error.
Includes a test case which verifies that providers can now be configured
using locals as well as vars, and an updated test case which verifies
that providers cannot be configured with references to resources.
2020-06-27 00:15:00 +02:00
"locals" : {
module : "import-provider-locals" ,
value : "baz-bar" ,
2016-11-02 18:29:58 +01:00
} ,
}
terraform: Relax provider config ref constraints
When configuring providers, it is normally valid to refer to any value
which is known at apply time. This can include resource instance
attributes, variables, locals, and so on.
The import command has a simpler graph evaluation, which means that
many of these values are unknown. We previously prevented this from
happening by restricting provider configuration references to input
variables (#22862), but this was more restrictive than is necessary.
This commit changes how we verify provider configuration for import.
We no longer inspect the configuration references during graph building,
because this is too early to determine if these values will become known
or not.
Instead, when the provider is configured during evaluation, we
check if the configuration value is wholly known. If not, we fail with a
diagnostic error.
Includes a test case which verifies that providers can now be configured
using locals as well as vars, and an updated test case which verifies
that providers cannot be configured with references to resources.
2020-06-27 00:15:00 +02:00
for name , test := range testCases {
t . Run ( name , func ( t * testing . T ) {
p := testProvider ( "aws" )
m := testModule ( t , test . module )
ctx := testContext2 ( t , & ContextOpts {
Config : m ,
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
Variables : InputValues {
"foo" : & InputValue {
Value : cty . StringVal ( "bar" ) ,
SourceType : ValueFromCaller ,
} ,
} ,
} )
2016-11-02 18:29:58 +01:00
terraform: Relax provider config ref constraints
When configuring providers, it is normally valid to refer to any value
which is known at apply time. This can include resource instance
attributes, variables, locals, and so on.
The import command has a simpler graph evaluation, which means that
many of these values are unknown. We previously prevented this from
happening by restricting provider configuration references to input
variables (#22862), but this was more restrictive than is necessary.
This commit changes how we verify provider configuration for import.
We no longer inspect the configuration references during graph building,
because this is too early to determine if these values will become known
or not.
Instead, when the provider is configured during evaluation, we
check if the configuration value is wholly known. If not, we fail with a
diagnostic error.
Includes a test case which verifies that providers can now be configured
using locals as well as vars, and an updated test case which verifies
that providers cannot be configured with references to resources.
2020-06-27 00:15:00 +02:00
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
state , diags := ctx . Import ( & ImportOpts {
Targets : [ ] * ImportTarget {
& ImportTarget {
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
ID : "bar" ,
} ,
} ,
} )
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
}
if ! p . ConfigureCalled {
t . Fatal ( "didn't configure provider" )
}
if foo := p . ConfigureRequest . Config . GetAttr ( "foo" ) . AsString ( ) ; foo != test . value {
t . Fatalf ( "bad value %#v; want %#v" , foo , test . value )
}
actual := strings . TrimSpace ( state . String ( ) )
expected := strings . TrimSpace ( testImportStr )
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
} )
2016-05-10 18:15:19 +02:00
}
}
2016-11-02 19:08:16 +01:00
// Test that provider configs can't reference resources.
terraform: Relax provider config ref constraints
When configuring providers, it is normally valid to refer to any value
which is known at apply time. This can include resource instance
attributes, variables, locals, and so on.
The import command has a simpler graph evaluation, which means that
many of these values are unknown. We previously prevented this from
happening by restricting provider configuration references to input
variables (#22862), but this was more restrictive than is necessary.
This commit changes how we verify provider configuration for import.
We no longer inspect the configuration references during graph building,
because this is too early to determine if these values will become known
or not.
Instead, when the provider is configured during evaluation, we
check if the configuration value is wholly known. If not, we fail with a
diagnostic error.
Includes a test case which verifies that providers can now be configured
using locals as well as vars, and an updated test case which verifies
that providers cannot be configured with references to resources.
2020-06-27 00:15:00 +02:00
func TestContextImport_providerConfigResources ( t * testing . T ) {
2016-11-02 19:08:16 +01:00
p := testProvider ( "aws" )
terraform: Relax provider config ref constraints
When configuring providers, it is normally valid to refer to any value
which is known at apply time. This can include resource instance
attributes, variables, locals, and so on.
The import command has a simpler graph evaluation, which means that
many of these values are unknown. We previously prevented this from
happening by restricting provider configuration references to input
variables (#22862), but this was more restrictive than is necessary.
This commit changes how we verify provider configuration for import.
We no longer inspect the configuration references during graph building,
because this is too early to determine if these values will become known
or not.
Instead, when the provider is configured during evaluation, we
check if the configuration value is wholly known. If not, we fail with a
diagnostic error.
Includes a test case which verifies that providers can now be configured
using locals as well as vars, and an updated test case which verifies
that providers cannot be configured with references to resources.
2020-06-27 00:15:00 +02:00
pTest := testProvider ( "test" )
m := testModule ( t , "import-provider-resources" )
2016-11-02 19:08:16 +01:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
terraform: Relax provider config ref constraints
When configuring providers, it is normally valid to refer to any value
which is known at apply time. This can include resource instance
attributes, variables, locals, and so on.
The import command has a simpler graph evaluation, which means that
many of these values are unknown. We previously prevented this from
happening by restricting provider configuration references to input
variables (#22862), but this was more restrictive than is necessary.
This commit changes how we verify provider configuration for import.
We no longer inspect the configuration references during graph building,
because this is too early to determine if these values will become known
or not.
Instead, when the provider is configured during evaluation, we
check if the configuration value is wholly known. If not, we fail with a
diagnostic error.
Includes a test case which verifies that providers can now be configured
using locals as well as vars, and an updated test case which verifies
that providers cannot be configured with references to resources.
2020-06-27 00:15:00 +02:00
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
addrs . NewDefaultProvider ( "test" ) : testProviderFuncFixed ( pTest ) ,
2020-03-31 20:03:33 +02:00
} ,
2016-11-02 19:08:16 +01:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-05-29 21:02:34 +02:00
_ , diags := ctx . Import ( & ImportOpts {
2016-11-02 19:08:16 +01:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2020-03-20 13:15:29 +01:00
ID : "bar" ,
2016-11-02 19:08:16 +01:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if ! diags . HasErrors ( ) {
2016-11-02 19:08:16 +01:00
t . Fatal ( "should error" )
}
terraform: Relax provider config ref constraints
When configuring providers, it is normally valid to refer to any value
which is known at apply time. This can include resource instance
attributes, variables, locals, and so on.
The import command has a simpler graph evaluation, which means that
many of these values are unknown. We previously prevented this from
happening by restricting provider configuration references to input
variables (#22862), but this was more restrictive than is necessary.
This commit changes how we verify provider configuration for import.
We no longer inspect the configuration references during graph building,
because this is too early to determine if these values will become known
or not.
Instead, when the provider is configured during evaluation, we
check if the configuration value is wholly known. If not, we fail with a
diagnostic error.
Includes a test case which verifies that providers can now be configured
using locals as well as vars, and an updated test case which verifies
that providers cannot be configured with references to resources.
2020-06-27 00:15:00 +02:00
if got , want := diags . Err ( ) . Error ( ) , ` The configuration for provider["registry.terraform.io/hashicorp/aws"] depends on values that cannot be determined until apply. ` ; ! strings . Contains ( got , want ) {
t . Errorf ( "wrong error\n got: %s\nwant: %s" , got , want )
}
2016-11-02 19:08:16 +01:00
}
2016-04-28 19:28:23 +02:00
func TestContextImport_refresh ( t * testing . T ) {
p := testProvider ( "aws" )
2017-04-22 02:19:37 +02:00
m := testModule ( t , "import-provider" )
2016-04-28 19:28:23 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-04-28 19:28:23 +02:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-09-05 23:35:30 +02:00
p . ReadResourceFn = nil
2018-09-21 00:22:42 +02:00
2018-09-05 23:35:30 +02:00
p . ReadResourceResponse = providers . ReadResourceResponse {
NewState : cty . ObjectVal ( map [ string ] cty . Value {
"id" : cty . StringVal ( "foo" ) ,
"foo" : cty . StringVal ( "bar" ) ,
} ) ,
2016-04-28 19:28:23 +02:00
}
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-04-28 19:28:23 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2020-03-20 13:15:29 +01:00
ID : "bar" ,
2016-04-28 19:28:23 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-04-28 19:28:23 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
expected := strings . TrimSpace ( testImportRefreshStr )
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-05-09 22:39:34 +02:00
func TestContextImport_refreshNil ( t * testing . T ) {
p := testProvider ( "aws" )
2017-04-22 02:19:37 +02:00
m := testModule ( t , "import-provider" )
2016-05-09 22:39:34 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-05-09 22:39:34 +02:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-09-05 23:35:30 +02:00
p . ReadResourceFn = func ( req providers . ReadResourceRequest ) providers . ReadResourceResponse {
return providers . ReadResourceResponse {
NewState : cty . NullVal ( cty . DynamicPseudoType ) ,
}
2016-05-09 22:39:34 +02:00
}
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-05-09 22:39:34 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2020-03-20 13:15:29 +01:00
ID : "bar" ,
2016-05-09 22:39:34 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if ! diags . HasErrors ( ) {
2016-05-09 22:39:34 +02:00
t . Fatal ( "should error" )
}
actual := strings . TrimSpace ( state . String ( ) )
expected := "<no state>"
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-04-30 07:09:34 +02:00
func TestContextImport_module ( t * testing . T ) {
p := testProvider ( "aws" )
2020-03-20 13:15:29 +01:00
m := testModule ( t , "import-module" )
2016-04-30 07:09:34 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-04-30 07:09:34 +02:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-04-30 07:09:34 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2020-06-10 23:02:41 +02:00
Addr : addrs . RootModuleInstance . Child ( "child" , addrs . IntKey ( 0 ) ) . ResourceInstance (
2018-05-05 04:24:06 +02:00
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2020-03-20 13:15:29 +01:00
ID : "bar" ,
2016-04-30 07:09:34 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-04-30 07:09:34 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
2016-04-30 08:28:58 +02:00
expected := strings . TrimSpace ( testImportModuleStr )
2016-04-30 07:09:34 +02:00
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-04-30 08:59:11 +02:00
func TestContextImport_moduleDepth2 ( t * testing . T ) {
p := testProvider ( "aws" )
2020-03-20 13:15:29 +01:00
m := testModule ( t , "import-module" )
2016-04-30 08:59:11 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-04-30 08:59:11 +02:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-04-30 08:59:11 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2020-06-10 23:02:41 +02:00
Addr : addrs . RootModuleInstance . Child ( "child" , addrs . IntKey ( 0 ) ) . Child ( "nested" , addrs . NoKey ) . ResourceInstance (
2018-05-05 04:24:06 +02:00
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2020-03-20 13:15:29 +01:00
ID : "baz" ,
2016-04-30 08:59:11 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-04-30 08:59:11 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
expected := strings . TrimSpace ( testImportModuleDepth2Str )
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-05-01 00:01:31 +02:00
func TestContextImport_moduleDiff ( t * testing . T ) {
p := testProvider ( "aws" )
2020-03-20 13:15:29 +01:00
m := testModule ( t , "import-module" )
2016-05-01 00:01:31 +02:00
ctx := testContext2 ( t , & ContextOpts {
2018-05-05 04:24:06 +02:00
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2016-05-01 00:01:31 +02:00
} )
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
}
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-05-01 00:01:31 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2020-06-10 23:02:41 +02:00
Addr : addrs . RootModuleInstance . Child ( "child" , addrs . IntKey ( 0 ) ) . ResourceInstance (
2018-05-05 04:24:06 +02:00
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2020-03-20 13:15:29 +01:00
ID : "baz" ,
2016-05-01 00:00:18 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-05-01 00:00:18 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
2020-03-20 13:15:29 +01:00
expected := strings . TrimSpace ( testImportModuleStr )
2016-05-01 00:00:18 +02:00
if actual != expected {
2018-09-21 00:22:42 +02:00
t . Fatalf ( "\nexpected: %q\ngot: %q\n" , expected , actual )
2016-05-01 00:00:18 +02:00
}
}
2016-05-01 00:04:40 +02:00
func TestContextImport_multiState ( t * testing . T ) {
p := testProvider ( "aws" )
2017-04-22 02:19:37 +02:00
m := testModule ( t , "import-provider" )
2018-09-21 00:22:42 +02:00
p . GetSchemaReturn = & ProviderSchema {
Provider : & configschema . Block {
Attributes : map [ string ] * configschema . Attribute {
"foo" : { Type : cty . String , Optional : true } ,
2017-04-22 02:40:46 +02:00
} ,
2018-09-21 00:22:42 +02:00
} ,
ResourceTypes : map [ string ] * configschema . Block {
"aws_instance" : {
Attributes : map [ string ] * configschema . Attribute {
"id" : { Type : cty . String , Computed : true } ,
} ,
} ,
"aws_instance_thing" : {
Attributes : map [ string ] * configschema . Attribute {
"id" : { Type : cty . String , Computed : true } ,
} ,
} ,
} ,
}
2016-05-01 00:04:40 +02:00
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
& InstanceState {
ID : "bar" ,
Ephemeral : EphemeralState { Type : "aws_instance_thing" } ,
} ,
}
2018-09-21 00:22:42 +02:00
ctx := testContext2 ( t , & ContextOpts {
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2018-09-21 00:22:42 +02:00
} )
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-05-01 00:04:40 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2020-03-20 13:15:29 +01:00
ID : "bar" ,
2016-05-01 00:04:40 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-05-01 00:04:40 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
expected := strings . TrimSpace ( testImportMultiStr )
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-05-01 00:29:03 +02:00
func TestContextImport_multiStateSame ( t * testing . T ) {
p := testProvider ( "aws" )
2017-04-22 02:19:37 +02:00
m := testModule ( t , "import-provider" )
2018-09-21 00:22:42 +02:00
p . GetSchemaReturn = & ProviderSchema {
Provider : & configschema . Block {
Attributes : map [ string ] * configschema . Attribute {
"foo" : { Type : cty . String , Optional : true } ,
2017-04-22 02:40:46 +02:00
} ,
2018-09-21 00:22:42 +02:00
} ,
ResourceTypes : map [ string ] * configschema . Block {
"aws_instance" : {
Attributes : map [ string ] * configschema . Attribute {
"id" : { Type : cty . String , Computed : true } ,
} ,
} ,
"aws_instance_thing" : {
Attributes : map [ string ] * configschema . Attribute {
"id" : { Type : cty . String , Computed : true } ,
} ,
} ,
} ,
}
2016-05-01 00:29:03 +02:00
p . ImportStateReturn = [ ] * InstanceState {
& InstanceState {
ID : "foo" ,
Ephemeral : EphemeralState { Type : "aws_instance" } ,
} ,
& InstanceState {
ID : "bar" ,
Ephemeral : EphemeralState { Type : "aws_instance_thing" } ,
} ,
& InstanceState {
ID : "qux" ,
Ephemeral : EphemeralState { Type : "aws_instance_thing" } ,
} ,
}
2018-09-21 00:22:42 +02:00
ctx := testContext2 ( t , & ContextOpts {
Config : m ,
2020-03-31 20:03:33 +02:00
Providers : map [ addrs . Provider ] providers . Factory {
addrs . NewDefaultProvider ( "aws" ) : testProviderFuncFixed ( p ) ,
} ,
2018-09-21 00:22:42 +02:00
} )
2018-05-29 21:02:34 +02:00
state , diags := ctx . Import ( & ImportOpts {
2016-05-01 00:29:03 +02:00
Targets : [ ] * ImportTarget {
& ImportTarget {
2018-05-05 04:24:06 +02:00
Addr : addrs . RootModuleInstance . ResourceInstance (
addrs . ManagedResourceMode , "aws_instance" , "foo" , addrs . NoKey ,
) ,
2020-03-20 13:15:29 +01:00
ID : "bar" ,
2016-05-01 00:29:03 +02:00
} ,
} ,
} )
2018-05-29 21:02:34 +02:00
if diags . HasErrors ( ) {
t . Fatalf ( "unexpected errors: %s" , diags . Err ( ) )
2016-05-01 00:29:03 +02:00
}
actual := strings . TrimSpace ( state . String ( ) )
expected := strings . TrimSpace ( testImportMultiSameStr )
if actual != expected {
t . Fatalf ( "bad: \n%s" , actual )
}
}
2016-04-28 19:25:58 +02:00
const testImportStr = `
aws_instance . foo :
ID = foo
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-04-28 19:25:58 +02:00
`
2016-04-28 19:28:23 +02:00
2016-08-20 01:14:33 +02:00
const testImportCountIndexStr = `
aws_instance . foo .0 :
ID = foo
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-08-20 01:14:33 +02:00
`
2016-04-30 08:28:58 +02:00
const testImportModuleStr = `
< no state >
2020-06-10 23:02:41 +02:00
module . child [ 0 ] :
2016-04-30 08:28:58 +02:00
aws_instance . foo :
ID = foo
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-04-30 08:28:58 +02:00
`
2016-04-30 08:59:11 +02:00
const testImportModuleDepth2Str = `
< no state >
2020-06-10 23:02:41 +02:00
module . child [ 0 ] . nested :
2016-05-01 00:01:31 +02:00
aws_instance . foo :
ID = foo
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-05-01 00:01:31 +02:00
`
2016-05-01 00:00:18 +02:00
const testImportModuleExistingStr = `
2018-09-21 00:22:42 +02:00
< no state >
2016-05-01 00:00:18 +02:00
module . foo :
aws_instance . bar :
ID = bar
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-05-01 00:00:18 +02:00
aws_instance . foo :
ID = foo
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-05-01 00:00:18 +02:00
`
2016-05-01 00:04:40 +02:00
const testImportMultiStr = `
aws_instance . foo :
ID = foo
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-05-01 00:04:40 +02:00
aws_instance_thing . foo :
ID = bar
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-05-01 00:04:40 +02:00
`
2016-05-01 00:29:03 +02:00
const testImportMultiSameStr = `
aws_instance . foo :
ID = foo
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-05-01 00:29:03 +02:00
aws_instance_thing . foo :
ID = bar
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-05-01 00:29:03 +02:00
aws_instance_thing . foo - 1 :
ID = qux
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-05-01 00:29:03 +02:00
`
2016-04-28 19:28:23 +02:00
const testImportRefreshStr = `
aws_instance . foo :
ID = foo
2020-03-31 20:03:33 +02:00
provider = provider [ "registry.terraform.io/hashicorp/aws" ]
2016-04-28 19:28:23 +02:00
foo = bar
`