Skip to content

Commit a29b570

Browse files
authored
Merge pull request #31874 from hashicorp/backport/jbardin/remove-planned-during-import/genuinely-shining-boar
Backport of RemovePlannedResourceInstanceObjects during import into v1.3
2 parents 8cc551a + 9136c72 commit a29b570

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

internal/terraform/context_import.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ func (c *Context) Import(config *configs.Config, prevRunState *states.State, opt
8282
return state, diags
8383
}
8484

85+
// Data sources which could not be read during the import plan will be
86+
// unknown. We need to strip those objects out so that the state can be
87+
// serialized.
88+
walker.State.RemovePlannedResourceInstanceObjects()
89+
8590
newState := walker.State.Close()
8691
return newState, diags
8792
}

internal/terraform/context_import_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,24 @@ func TestContextImport_providerConfigResources(t *testing.T) {
430430

431431
func TestContextImport_refresh(t *testing.T) {
432432
p := testProvider("aws")
433-
m := testModule(t, "import-provider")
433+
m := testModuleInline(t, map[string]string{
434+
"main.tf": `
435+
provider "aws" {
436+
foo = "bar"
437+
}
438+
439+
resource "aws_instance" "foo" {
440+
}
441+
442+
443+
// we are only importing aws_instance.foo, so these resources will be unknown
444+
resource "aws_instance" "bar" {
445+
}
446+
data "aws_data_source" "bar" {
447+
foo = aws_instance.bar.id
448+
}
449+
`})
450+
434451
ctx := testContext2(t, &ContextOpts{
435452
Providers: map[addrs.Provider]providers.Factory{
436453
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
@@ -448,6 +465,13 @@ func TestContextImport_refresh(t *testing.T) {
448465
},
449466
}
450467

468+
p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
469+
State: cty.ObjectVal(map[string]cty.Value{
470+
"id": cty.StringVal("id"),
471+
"foo": cty.UnknownVal(cty.String),
472+
}),
473+
}
474+
451475
p.ReadResourceFn = nil
452476

453477
p.ReadResourceResponse = &providers.ReadResourceResponse{
@@ -471,6 +495,10 @@ func TestContextImport_refresh(t *testing.T) {
471495
t.Fatalf("unexpected errors: %s", diags.Err())
472496
}
473497

498+
if d := state.ResourceInstance(mustResourceInstanceAddr("data.aws_data_source.bar")); d != nil {
499+
t.Errorf("data.aws_data_source.bar has a status of ObjectPlanned and should not be in the state\ngot:%#v\n", d.Current)
500+
}
501+
474502
actual := strings.TrimSpace(state.String())
475503
expected := strings.TrimSpace(testImportRefreshStr)
476504
if actual != expected {

0 commit comments

Comments
 (0)