Skip to content

Commit 9aafbf2

Browse files
committed
add resource identity for user resource
1 parent 497ed6e commit 9aafbf2

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

cloudfoundry/provider/resource_user.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -23,6 +24,7 @@ var (
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+
3945
func (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+
96112
func (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

176199
func (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

208244
func (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

307343
func (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
}

docs/resources/user.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,20 @@ Import is supported using the following syntax:
5454
# terraform import cloudfoundry_user.<resource_name> <user_guid>
5555
5656
terraform import cloudfoundry_user.my_user 283f59d2-d660-45fb-9d96-b3e1aa92cfc7
57+
58+
#terraform import using id attribute in import block
59+
60+
import {
61+
to = cloudfoundry_user.<resource_name>
62+
id = "<user_guid>"
63+
}
64+
65+
# this resource supports import using identity attribute from Terraform version 1.12 or higher
66+
67+
import {
68+
to = cloudfoundry_user.<resource_name>
69+
identity = {
70+
user_guid = "<user_guid>"
71+
}
72+
}
5773
```
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
# terraform import cloudfoundry_user.<resource_name> <user_guid>
22

3-
terraform import cloudfoundry_user.my_user 283f59d2-d660-45fb-9d96-b3e1aa92cfc7
3+
terraform import cloudfoundry_user.my_user 283f59d2-d660-45fb-9d96-b3e1aa92cfc7
4+
5+
#terraform import using id attribute in import block
6+
7+
import {
8+
to = cloudfoundry_user.<resource_name>
9+
id = "<user_guid>"
10+
}
11+
12+
# this resource supports import using identity attribute from Terraform version 1.12 or higher
13+
14+
import {
15+
to = cloudfoundry_user.<resource_name>
16+
identity = {
17+
user_guid = "<user_guid>"
18+
}
19+
}

0 commit comments

Comments
 (0)