@@ -9,17 +9,20 @@ import (
99 "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1010 "github.com/hashicorp/terraform-plugin-framework/path"
1111 "github.com/hashicorp/terraform-plugin-framework/resource"
12+ "github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
1213 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
1314 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1415 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1516 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
17+ "github.com/hashicorp/terraform-plugin-framework/types"
1618 "github.com/hashicorp/terraform-plugin-log/tflog"
1719)
1820
1921var (
2022 _ resource.Resource = & UserResource {}
2123 _ resource.ResourceWithConfigure = & UserResource {}
2224 _ resource.ResourceWithImportState = & UserResource {}
25+ _ resource.ResourceWithIdentity = & UserResource {}
2326)
2427
2528// Instantiates a user resource.
@@ -32,6 +35,10 @@ type UserCFResource struct {
3235 cfClient * cfv3client.Client
3336}
3437
38+ type userCfResourceIdentityModel struct {
39+ UserGUID types.String `tfsdk:"user_guid"`
40+ }
41+
3542func (r * UserCFResource ) Metadata (ctx context.Context , req resource.MetadataRequest , resp * resource.MetadataResponse ) {
3643 resp .TypeName = req .ProviderTypeName + "_user_cf"
3744}
@@ -94,6 +101,16 @@ func (r *UserCFResource) Schema(ctx context.Context, req resource.SchemaRequest,
94101 }
95102}
96103
104+ func (rs * UserCFResource ) IdentitySchema (_ context.Context , _ resource.IdentitySchemaRequest , resp * resource.IdentitySchemaResponse ) {
105+ resp .IdentitySchema = identityschema.Schema {
106+ Attributes : map [string ]identityschema.Attribute {
107+ "user_guid" : identityschema.StringAttribute {
108+ RequiredForImport : true ,
109+ },
110+ },
111+ }
112+ }
113+
97114func (r * UserCFResource ) Configure (ctx context.Context , req resource.ConfigureRequest , resp * resource.ConfigureResponse ) {
98115 if req .ProviderData == nil {
99116 return
@@ -155,6 +172,13 @@ func (r *UserCFResource) Create(ctx context.Context, req resource.CreateRequest,
155172
156173 tflog .Trace (ctx , "created a cf user resource" )
157174 resp .Diagnostics .Append (resp .State .Set (ctx , & plan )... )
175+
176+ identity := userCfResourceIdentityModel {
177+ UserGUID : types .StringValue (plan .Id .ValueString ()),
178+ }
179+
180+ diags = resp .Identity .Set (ctx , identity )
181+ resp .Diagnostics .Append (diags ... )
158182}
159183
160184func (rs * UserCFResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
@@ -177,6 +201,19 @@ func (rs *UserCFResource) Read(ctx context.Context, req resource.ReadRequest, re
177201
178202 tflog .Trace (ctx , "read a cf user resource" )
179203 resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
204+
205+ var identity userCfResourceIdentityModel
206+
207+ diags = req .Identity .Get (ctx , & identity )
208+ if diags .HasError () {
209+ identity = userCfResourceIdentityModel {
210+ UserGUID : types .StringValue (data .Id .ValueString ()),
211+ }
212+
213+ diags = resp .Identity .Set (ctx , identity )
214+ resp .Diagnostics .Append (diags ... )
215+ }
216+
180217}
181218
182219func (rs * UserCFResource ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
@@ -237,5 +274,9 @@ func (rs *UserCFResource) Delete(ctx context.Context, req resource.DeleteRequest
237274}
238275
239276func (rs * UserCFResource ) ImportState (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
240- resource .ImportStatePassthroughID (ctx , path .Root ("id" ), req , resp )
277+ if req .ID != "" {
278+ resource .ImportStatePassthroughID (ctx , path .Root ("id" ), req , resp )
279+ return
280+ }
281+ resource .ImportStatePassthroughWithIdentity (ctx , path .Root ("id" ), path .Root ("user_guid" ), req , resp )
241282}
0 commit comments