Skip to content

Commit 37a2c16

Browse files
committed
Fix identity values comparison when using ConfigFile
1 parent 3f8f2ca commit 37a2c16

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

helper/resource/importstate/import_block_in_config_file_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,31 @@ func TestImportBlock_InConfigFile(t *testing.T) {
4343
},
4444
})
4545
}
46+
47+
func TestImportBlock_WithResourceIdentity_InConfigFile(t *testing.T) {
48+
t.Parallel()
49+
50+
r.UnitTest(t, r.TestCase{
51+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
52+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
53+
},
54+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
55+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
56+
Resources: map[string]testprovider.Resource{
57+
"examplecloud_container": examplecloudResource(),
58+
},
59+
}),
60+
},
61+
Steps: []r.TestStep{
62+
{
63+
ConfigFile: config.StaticFile(`testdata/1/examplecloud_container.tf`),
64+
},
65+
{
66+
ResourceName: "examplecloud_container.test",
67+
ImportState: true,
68+
ImportStateKind: r.ImportBlockWithResourceIdentity,
69+
ConfigFile: config.StaticFile(`testdata/examplecloud_container_import_with_identity.tf`),
70+
},
71+
},
72+
})
73+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
resource "examplecloud_container" "test" {
5+
name = "somevalue"
6+
location = "westeurope"
7+
}
8+
9+
import {
10+
to = examplecloud_container.test
11+
identity = {
12+
id = "examplecloud_container.test"
13+
}
14+
}

helper/resource/testing_new_import_state.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,19 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
110110

111111
var priorIdentityValues map[string]any
112112

113+
if kind.plannable() && kind.resourceIdentity() {
114+
priorIdentityValues = identityValuesFromState(stateJSON, resourceName)
115+
if len(priorIdentityValues) == 0 {
116+
return fmt.Errorf("importing resource %s: expected prior state to have resource identity values, got none", resourceName)
117+
}
118+
}
119+
113120
// Append to previous step config unless using explicit inline Config, or ConfigFile, or ConfigDirectory
114121
if testStepConfig == nil && step.ConfigFile == nil && step.ConfigDirectory == nil {
115122
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
116123
importConfig := cfgRaw
117124

118125
if kind.plannable() && kind.resourceIdentity() {
119-
priorIdentityValues = identityValuesFromState(stateJSON, resourceName)
120-
if len(priorIdentityValues) == 0 {
121-
return fmt.Errorf("importing resource %s: expected prior state to have resource identity values, got none", resourceName)
122-
}
123126
importConfig = appendImportBlockWithIdentity(importConfig, resourceName, priorIdentityValues)
124127
} else if kind.plannable() {
125128
importConfig = appendImportBlock(importConfig, resourceName, importId)

0 commit comments

Comments
 (0)