@@ -11,6 +11,7 @@ import (
1111 "github.com/cloudfoundry/terraform-provider-cloudfoundry/cloudfoundry/provider/managers"
1212 "github.com/hashicorp/terraform-plugin-framework/path"
1313 "github.com/hashicorp/terraform-plugin-framework/resource"
14+ "github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
1415 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
1516 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1617 "github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
2324 _ resource.Resource = & UserResource {}
2425 _ resource.ResourceWithConfigure = & UserResource {}
2526 _ resource.ResourceWithImportState = & UserResource {}
27+ _ resource.ResourceWithIdentity = & UserResource {}
2628)
2729
2830// Instantiates a user resource.
@@ -36,6 +38,10 @@ type UserResource struct {
3638 uaaClient * uaa.API
3739}
3840
41+ type userResourceIdentityModel struct {
42+ UserGUID types.String `tfsdk:"user_guid"`
43+ }
44+
3945func (r * UserResource ) Metadata (ctx context.Context , req resource.MetadataRequest , resp * resource.MetadataResponse ) {
4046 resp .TypeName = req .ProviderTypeName + "_user"
4147}
@@ -93,6 +99,16 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
9399 }
94100}
95101
102+ func (rs * UserResource ) IdentitySchema (_ context.Context , _ resource.IdentitySchemaRequest , resp * resource.IdentitySchemaResponse ) {
103+ resp .IdentitySchema = identityschema.Schema {
104+ Attributes : map [string ]identityschema.Attribute {
105+ "user_guid" : identityschema.StringAttribute {
106+ RequiredForImport : true ,
107+ },
108+ },
109+ }
110+ }
111+
96112func (r * UserResource ) Configure (ctx context.Context , req resource.ConfigureRequest , resp * resource.ConfigureResponse ) {
97113 if req .ProviderData == nil {
98114 return
@@ -171,6 +187,13 @@ func (r *UserResource) Create(ctx context.Context, req resource.CreateRequest, r
171187
172188 tflog .Trace (ctx , "created a user resource" )
173189 resp .Diagnostics .Append (resp .State .Set (ctx , & plan )... )
190+
191+ identity := userResourceIdentityModel {
192+ UserGUID : types .StringValue (plan .Id .ValueString ()),
193+ }
194+
195+ diags = resp .Identity .Set (ctx , identity )
196+ resp .Diagnostics .Append (diags ... )
174197}
175198
176199func (rs * UserResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
@@ -203,6 +226,19 @@ func (rs *UserResource) Read(ctx context.Context, req resource.ReadRequest, resp
203226
204227 tflog .Trace (ctx , "read a user resource" )
205228 resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
229+
230+ var identity userResourceIdentityModel
231+
232+ diags = req .Identity .Get (ctx , & identity )
233+ if diags .HasError () {
234+ identity = userResourceIdentityModel {
235+ UserGUID : types .StringValue (data .Id .ValueString ()),
236+ }
237+
238+ diags = resp .Identity .Set (ctx , identity )
239+ resp .Diagnostics .Append (diags ... )
240+ }
241+
206242}
207243
208244func (rs * UserResource ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) {
@@ -305,5 +341,9 @@ func (rs *UserResource) Delete(ctx context.Context, req resource.DeleteRequest,
305341}
306342
307343func (rs * UserResource ) ImportState (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
308- resource .ImportStatePassthroughID (ctx , path .Root ("id" ), req , resp )
344+ if req .ID != "" {
345+ resource .ImportStatePassthroughID (ctx , path .Root ("id" ), req , resp )
346+ return
347+ }
348+ resource .ImportStatePassthroughWithIdentity (ctx , path .Root ("id" ), path .Root ("user_guid" ), req , resp )
309349}
0 commit comments