Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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
6 changes: 5 additions & 1 deletion helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,12 @@ func appendImportBlockWithIdentity(config string, resourceName string, identityV

case string:
configBuilder += fmt.Sprintf(` %q = %q`+"\n", k, v)

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

panic(fmt.Sprintf("unexpected type %T for identity value %q", v, k))
}
}
Expand Down
Loading