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
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20250507-174510.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'ImportState: Fixes a bug where an identity attribute set to `null` would panic when testing import via identity.'
time: 2025-05-07T17:45:10.221508-04:00
custom:
Issue: "499"
117 changes: 117 additions & 0 deletions helper/resource/importstate/examplecloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,120 @@ func examplecloudResourceWithEveryIdentitySchemaType() testprovider.Resource {
},
}
}

func examplecloudResourceWithNullIdentityAttr() testprovider.Resource {
return testprovider.Resource{
CreateResponse: &resource.CreateResponse{
NewState: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "westeurope"),
"name": tftypes.NewValue(tftypes.String, "somevalue"),
},
),
NewIdentity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"value_we_dont_always_need": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"value_we_dont_always_need": tftypes.NewValue(tftypes.String, nil),
},
)),
},
ReadResponse: &resource.ReadResponse{
NewState: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "westeurope"),
"name": tftypes.NewValue(tftypes.String, "somevalue"),
},
),
NewIdentity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"value_we_dont_always_need": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"value_we_dont_always_need": tftypes.NewValue(tftypes.String, nil),
},
)),
},
ImportStateResponse: &resource.ImportStateResponse{
State: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "westeurope"),
"name": tftypes.NewValue(tftypes.String, "somevalue"),
},
),
Identity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"value_we_dont_always_need": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"value_we_dont_always_need": tftypes.NewValue(tftypes.String, nil),
},
)),
},
SchemaResponse: &resource.SchemaResponse{
Schema: &tfprotov6.Schema{
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
ComputedStringAttribute("id"),
RequiredStringAttribute("location"),
RequiredStringAttribute("name"),
},
},
},
},
IdentitySchemaResponse: &resource.IdentitySchemaResponse{
Schema: &tfprotov6.ResourceIdentitySchema{
Version: 1,
IdentityAttributes: []*tfprotov6.ResourceIdentitySchemaAttribute{
{
Name: "id",
Type: tftypes.String,
RequiredForImport: true,
},
{
Name: "value_we_dont_always_need",
Type: tftypes.String,
OptionalForImport: true,
},
},
},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -47,6 +49,43 @@ func TestImportBlock_WithResourceIdentity(t *testing.T) {
})
}

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

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_12_0), // ImportBlockWithResourceIdentity requires Terraform 1.12.0 or later
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Resources: map[string]testprovider.Resource{
"examplecloud_container": examplecloudResourceWithNullIdentityAttr(),
},
}),
},
Steps: []r.TestStep{
{
Config: `
resource "examplecloud_container" "test" {
location = "westeurope"
name = "somevalue"
}`,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectIdentity("examplecloud_container.test", map[string]knownvalue.Check{
"id": knownvalue.StringExact("westeurope/somevalue"),
"value_we_dont_always_need": knownvalue.Null(), // This value will not be brought over to import config
}),
},
},
{
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithResourceIdentity,
},
},
})
}

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

Expand Down
5 changes: 5 additions & 0 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ func appendImportBlockWithIdentity(config teststep.Config, resourceName string,
resourceName))

for k, v := range identityValues {
// It's valid for identity attributes to be null, we can just omit it from config
if v == nil {
continue
}

switch v := v.(type) {
case bool:
configBuilder.WriteString(fmt.Sprintf(` %q = %t`+"\n", k, v))
Expand Down
Loading