Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions helper/resource/importstate/import_block_with_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand Down Expand Up @@ -376,3 +379,50 @@ func TestImportBlock_WithID_WithBlankComputedAttribute_GeneratesCorrectPlan(t *t
},
})
}

func TestImportBlock_WithID_WithExternalProvider(t *testing.T) {
t.Parallel()

config := `
resource "random_string" "mystery_message" {
length = 31
}
`

configWithImportBlock := config + `
import {
to = random_string.mystery_message
id = "It was a dark and stormy night."
}
`

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
},
ExternalProviders: map[string]r.ExternalProvider{
"random": {
Source: "hashicorp/random",
},
},
Steps: []r.TestStep{
{
Config: config,
},
{
ImportState: true,
ImportStateKind: r.ImportBlockWithID,
Config: configWithImportBlock,
Copy link
Member

@austinvalle austinvalle Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm my understanding, provider developers wouldn't need to use a config in this scenario to do this (if the ID needing to be explicitly defined), they could just use ImportStateId:

{
	ImportState:     true,
	ImportStateKind: r.ImportBlockWithID,
	ImportStateId:   "It was a dark and stormy night.",
	ResourceName:    "random_string.mystery_message",
	ImportPlanChecks: r.ImportPlanChecks{
		PreApply: []plancheck.PlanCheck{
			plancheck.ExpectKnownValue(
				"random_string.mystery_message",
				tfjsonpath.New("result"),
				knownvalue.StringExact("It was a dark and stormy night.")),
		},
	},
},

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ImportStateId

Nice catch. We could come up with a different example here so that it better models a situation that calls for an inline config.

ResourceName: "random_string.mystery_message",
ImportPlanChecks: r.ImportPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectKnownValue(
"random_string.mystery_message",
tfjsonpath.New("result"),
knownvalue.StringExact("It was a dark and stormy night.")),
},
},
},
},
})
}
11 changes: 4 additions & 7 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,10 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest

logging.HelperResourceTrace(ctx, fmt.Sprintf("Using import identifier: %s", importId))

// Append to previous step config unless using ConfigFile or ConfigDirectory
if testStepConfig == nil || step.Config != "" {
importConfig := step.Config
if importConfig == "" {
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
importConfig = cfgRaw
}
// Append to previous step config unless using explicit inline Config, or ConfigFile, or ConfigDirectory
if testStepConfig == nil && step.ConfigFile == nil && step.ConfigDirectory == nil {
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
importConfig := cfgRaw

if kind.plannable() {
importConfig = appendImportBlock(importConfig, resourceName, importId)
Expand Down