-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathldap_user_resource.go
More file actions
215 lines (189 loc) · 5.77 KB
/
ldap_user_resource.go
File metadata and controls
215 lines (189 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package provider
import (
"context"
"fmt"
dtrack "github.com/DependencyTrack/client-go"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var (
_ resource.Resource = &ldapUserResource{}
_ resource.ResourceWithConfigure = &ldapUserResource{}
_ resource.ResourceWithImportState = &ldapUserResource{}
)
type (
ldapUserResource struct {
client *dtrack.Client
semver *Semver
}
ldapUserResourceModel struct {
ID types.String `tfsdk:"id"`
Username types.String `tfsdk:"username"`
}
)
func NewLDAPUserResource() resource.Resource {
return &ldapUserResource{}
}
func (*ldapUserResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_ldap_user"
}
func (*ldapUserResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages an LDAP User.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Username of the User.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"username": schema.StringAttribute{
Description: "Username of the User.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
func (r *ldapUserResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan ldapUserResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
userReq := dtrack.LdapUser{
Username: plan.Username.ValueString(),
}
tflog.Debug(ctx, "Creating LDAP User", map[string]any{
"username": userReq.Username,
})
ldapUserRes, err := r.client.LDAP.CreateUser(ctx, userReq)
if err != nil {
resp.Diagnostics.AddError(
"Error creating LDAP user",
"Error from: "+err.Error(),
)
return
}
plan = ldapUserResourceModel{
ID: types.StringValue(ldapUserRes.Username),
Username: types.StringValue(ldapUserRes.Username),
}
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Created LDAP User", map[string]any{
"id": plan.ID.ValueString(),
"username": plan.Username.ValueString(),
})
}
func (r *ldapUserResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state ldapUserResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
username := state.ID.ValueString()
// Refresh.
user, err := FindPaged(
func(po dtrack.PageOptions) (dtrack.Page[dtrack.LdapUser], error) {
return r.client.LDAP.GetUsers(ctx, po)
},
func(user dtrack.LdapUser) bool {
return user.Username == username
},
)
if err != nil {
resp.Diagnostics.AddError(
"Unable to read LDAP user",
"Error for user: "+username+", in original error: "+err.Error(),
)
return
}
newState := ldapUserResourceModel{
ID: types.StringValue(user.Username),
Username: types.StringValue(user.Username),
}
// Update state.
diags = resp.State.Set(ctx, newState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Read LDAP User", map[string]any{
"id": state.ID.ValueString(),
"username": state.Username.ValueString(),
})
}
func (*ldapUserResource) Update(_ context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) {
// Resource has no update, since all attributes are RequireReplace.
}
func (r *ldapUserResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Load State.
var state ldapUserResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Map TF to SDK.
user := dtrack.LdapUser{
Username: state.Username.ValueString(),
}
// Execute.
tflog.Debug(ctx, "Deleting LDAP User", map[string]any{
"id": user.Username,
"username": state.Username.ValueString(),
})
err := r.client.LDAP.DeleteUser(ctx, user)
if err != nil {
resp.Diagnostics.AddError(
"Unable to delete LDAP user",
"Error for user: "+user.Username+", from original error: "+err.Error(),
)
return
}
tflog.Debug(ctx, "Deleted LDAP User", map[string]any{
"id": state.ID.ValueString(),
"username": state.Username.ValueString(),
})
}
func (*ldapUserResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
tflog.Debug(ctx, "Importing LDAP User", map[string]any{
"id": req.ID,
})
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Imported LDAP User", map[string]any{
"id": req.ID,
})
}
func (r *ldapUserResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
clientInfoData, ok := req.ProviderData.(clientInfo)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Configure Type",
fmt.Sprintf("Expected provider.clientInfo, got %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = clientInfoData.client
r.semver = clientInfoData.semver
}