Skip to content

Commit 0c5f188

Browse files
committed
add resource identity for resource user_cf
1 parent 9aafbf2 commit 0c5f188

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

cloudfoundry/provider/resource_user_cf.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

1921
var (
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+
3542
func (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+
97114
func (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

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

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

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

docs/resources/user_cf.md

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

3-
terraform import cloudfoundry_user_cf.my_user 283f59d2-d660-45fb-9d96-b3e1aa92cfc7
3+
terraform import cloudfoundry_user_cf.my_user 283f59d2-d660-45fb-9d96-b3e1aa92cfc7
4+
5+
#terraform import using id attribute in import block
6+
7+
import {
8+
to = cloudfoundry_user_cf.<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_cf.<resource_name>
16+
identity = {
17+
user_guid = "<user_guid>"
18+
}
19+
}

0 commit comments

Comments
 (0)