Skip to content
36 changes: 36 additions & 0 deletions internal/sdkv2provider/resource_user_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package sdkv2

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -46,6 +47,41 @@ func resourceUserIdentity() *schema.Resource {
}
},
},

Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
if rd.Id() != "" {
return []*schema.ResourceData{rd}, nil // just return the resource data, since the string id is used
}

identity, err := rd.Identity()
if err != nil {
return nil, err
}

emailRaw, ok := identity.GetOk("email")
if !ok {
return nil, fmt.Errorf("error getting email from identity: %w", err)
}

email, ok := emailRaw.(string)
if !ok {
return nil, fmt.Errorf("error converting email to string")
}

if email == "" {
return nil, fmt.Errorf("email cannot be empty")
}

err = rd.Set("email", email)
rd.SetId(email) // TODO: document that this is still require with resource identity
if err != nil {
return nil, fmt.Errorf("error setting email: %w", err)
}

return []*schema.ResourceData{rd}, nil
},
},
}
}

Expand Down
10 changes: 6 additions & 4 deletions internal/sdkv2provider/resource_user_identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package sdkv2
import (
"testing"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
Expand All @@ -16,10 +15,8 @@ import (
func testAccResourceUserIdentity(t *testing.T) resource.TestCase {
return resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
// Latest alpha version that this JSON data is available in
// https://github.com/hashicorp/terraform/releases/tag/v1.12.0-alpha20250319
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(version.Must(version.NewVersion("1.12.0-alpha20250319"))),
tfversion.SkipBelow(tfversion.Version1_12_0),
},
Providers: testAccProviders,
Steps: []resource.TestStep{
Expand All @@ -31,6 +28,11 @@ func testAccResourceUserIdentity(t *testing.T) resource.TestCase {
}),
},
},
{
ResourceName: "corner_user_identity.foo",
ImportState: true,
ImportStateKind: resource.ImportBlockWithResourceIdentity,
},
},
}
}
Expand Down
Loading