mirror of https://github.com/hashicorp/terraform
parent
e3d2b7de8b
commit
7494492fd5
@ -1,6 +0,0 @@
|
||||
resource "aws_instance" "web" {}
|
||||
|
||||
import {
|
||||
to = aws_instance.web
|
||||
id = "test"
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
import-in-child-module/child/main.tf:3,1-7: Invalid import configuration; An import block was detected in "module.child". Import blocks are only allowed in the root module.
|
||||
@ -1,10 +0,0 @@
|
||||
resource "aws_instance" "web" {}
|
||||
|
||||
import {
|
||||
to = aws_instance.web
|
||||
id = "test"
|
||||
}
|
||||
|
||||
module "child" {
|
||||
source = "./child"
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
provider "random" {
|
||||
alias = "thisone"
|
||||
}
|
||||
|
||||
import {
|
||||
to = random_string.test1
|
||||
provider = localname
|
||||
id = "importlocalname"
|
||||
}
|
||||
|
||||
import {
|
||||
to = random_string.test2
|
||||
provider = random.thisone
|
||||
id = "importaliased"
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
module "child" {
|
||||
source = "./child"
|
||||
}
|
||||
@ -0,0 +1,118 @@
|
||||
package terraform
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
||||
"github.com/hashicorp/terraform/internal/addrs"
|
||||
"github.com/hashicorp/terraform/internal/plans"
|
||||
"github.com/hashicorp/terraform/internal/providers"
|
||||
"github.com/hashicorp/terraform/internal/states"
|
||||
"github.com/hashicorp/terraform/internal/tfdiags"
|
||||
)
|
||||
|
||||
// other import tests can be found in context_apply2_test.go
|
||||
func TestContextApply_import_in_module(t *testing.T) {
|
||||
m := testModule(t, "import-in-module")
|
||||
|
||||
p := testProvider("aws")
|
||||
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
||||
ImportedResources: []providers.ImportedResource{
|
||||
{
|
||||
TypeName: "aws_instance",
|
||||
State: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("importable"),
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
p.ImportResourceStateFn = func(req providers.ImportResourceStateRequest) providers.ImportResourceStateResponse {
|
||||
return providers.ImportResourceStateResponse{
|
||||
ImportedResources: []providers.ImportedResource{
|
||||
{
|
||||
TypeName: "aws_instance",
|
||||
State: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("importable"),
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
||||
return providers.PlanResourceChangeResponse{
|
||||
PlannedState: req.ProposedNewState,
|
||||
}
|
||||
}
|
||||
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Providers: map[addrs.Provider]providers.Factory{
|
||||
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
plan, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
|
||||
Mode: plans.NormalMode,
|
||||
})
|
||||
tfdiags.AssertNoErrors(t, diags)
|
||||
|
||||
state, diags := ctx.Apply(plan, m, nil)
|
||||
tfdiags.AssertNoErrors(t, diags)
|
||||
|
||||
rs := state.ResourceInstance(mustResourceInstanceAddr("module.child.aws_instance.bar"))
|
||||
if rs == nil {
|
||||
t.Fatal("imported resource not found in module")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextApply_import_in_nested_module(t *testing.T) { // more nested than the test above. nesteder.
|
||||
m := testModule(t, "import-in-nested-module")
|
||||
|
||||
p := testProvider("aws")
|
||||
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
|
||||
ImportedResources: []providers.ImportedResource{
|
||||
{
|
||||
TypeName: "aws_instance",
|
||||
State: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("importable"),
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
p.ImportResourceStateFn = func(req providers.ImportResourceStateRequest) providers.ImportResourceStateResponse {
|
||||
return providers.ImportResourceStateResponse{
|
||||
ImportedResources: []providers.ImportedResource{
|
||||
{
|
||||
TypeName: "aws_instance",
|
||||
State: cty.ObjectVal(map[string]cty.Value{
|
||||
"id": cty.StringVal("importable"),
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
||||
return providers.PlanResourceChangeResponse{
|
||||
PlannedState: req.ProposedNewState,
|
||||
}
|
||||
}
|
||||
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Providers: map[addrs.Provider]providers.Factory{
|
||||
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
plan, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
|
||||
Mode: plans.NormalMode,
|
||||
})
|
||||
tfdiags.AssertNoErrors(t, diags)
|
||||
|
||||
state, diags := ctx.Apply(plan, m, nil)
|
||||
tfdiags.AssertNoErrors(t, diags)
|
||||
|
||||
rs := state.ResourceInstance(mustResourceInstanceAddr("module.child.module.kinder.aws_instance.bar"))
|
||||
if rs == nil {
|
||||
t.Fatal("imported resource not found in module")
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
import {
|
||||
to = aws_instance.bar
|
||||
id = "importable"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {}
|
||||
@ -0,0 +1,3 @@
|
||||
module "child" {
|
||||
source = "./child"
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
import {
|
||||
to = aws_instance.bar
|
||||
id = "importable"
|
||||
}
|
||||
|
||||
resource "aws_instance" "bar" {}
|
||||
@ -0,0 +1,3 @@
|
||||
module "kinder" {
|
||||
source = "./kinder"
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
module "child" {
|
||||
source = "./child"
|
||||
}
|
||||
Loading…
Reference in new issue