@@ -6,6 +6,7 @@ package sdkv2
66
77import (
88 "context"
9+ "fmt"
910
1011 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1112 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -46,6 +47,41 @@ func resourceUserIdentity() *schema.Resource {
4647 }
4748 },
4849 },
50+
51+ Importer : & schema.ResourceImporter {
52+ StateContext : func (ctx context.Context , rd * schema.ResourceData , i interface {}) ([]* schema.ResourceData , error ) {
53+ if rd .Id () != "" {
54+ return []* schema.ResourceData {rd }, nil // just return the resource data, since the string id is used
55+ }
56+
57+ identity , err := rd .Identity ()
58+ if err != nil {
59+ return nil , err
60+ }
61+
62+ emailRaw , ok := identity .GetOk ("email" )
63+ if ! ok {
64+ return nil , fmt .Errorf ("error getting email from identity: %w" , err )
65+ }
66+
67+ email , ok := emailRaw .(string )
68+ if ! ok {
69+ return nil , fmt .Errorf ("error converting email to string" )
70+ }
71+
72+ if email == "" {
73+ return nil , fmt .Errorf ("email cannot be empty" )
74+ }
75+
76+ err = rd .Set ("email" , email )
77+ rd .SetId (email ) // TODO: document that this is still require with resource identity
78+ if err != nil {
79+ return nil , fmt .Errorf ("error setting email: %w" , err )
80+ }
81+
82+ return []* schema.ResourceData {rd }, nil
83+ },
84+ },
4985 }
5086}
5187
0 commit comments