Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemovePlannedResourceInstanceObjects during import #31871

Merged
merged 1 commit into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/terraform/context_import.go
Expand Up @@ -82,6 +82,11 @@ func (c *Context) Import(config *configs.Config, prevRunState *states.State, opt
return state, diags
}

// Data sources which could not be read during the import plan will be
// unknown. We need to strip those objects out so that the state can be
// serialized.
walker.State.RemovePlannedResourceInstanceObjects()

newState := walker.State.Close()
return newState, diags
}
30 changes: 29 additions & 1 deletion internal/terraform/context_import_test.go
Expand Up @@ -430,7 +430,24 @@ func TestContextImport_providerConfigResources(t *testing.T) {

func TestContextImport_refresh(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
m := testModuleInline(t, map[string]string{
"main.tf": `
provider "aws" {
foo = "bar"
}

resource "aws_instance" "foo" {
}


// we are only importing aws_instance.foo, so these resources will be unknown
resource "aws_instance" "bar" {
}
data "aws_data_source" "bar" {
foo = aws_instance.bar.id
}
`})

ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
Expand All @@ -448,6 +465,13 @@ func TestContextImport_refresh(t *testing.T) {
},
}

p.ReadDataSourceResponse = &providers.ReadDataSourceResponse{
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("id"),
"foo": cty.UnknownVal(cty.String),
}),
}

p.ReadResourceFn = nil

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

if d := state.ResourceInstance(mustResourceInstanceAddr("data.aws_data_source.bar")); d != nil {
t.Errorf("data.aws_data_source.bar has a status of ObjectPlanned and should not be in the state\ngot:%#v\n", d.Current)
}

actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testImportRefreshStr)
if actual != expected {
Expand Down