Skip to content

Commit 8c92208

Browse files
ansgarmaustinvalle
authored andcommitted
support importing by resource identity for sdkv2 resource
1 parent f4b542e commit 8c92208

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

internal/sdkv2provider/resource_user_identity.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package sdkv2
66

77
import (
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

Comments
 (0)