@@ -287,9 +287,72 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
287287}
288288
289289// Update updates the resource and sets the updated Terraform state on success.
290- func (r * userResource ) Update (ctx context.Context , _ resource.UpdateRequest , resp * resource.UpdateResponse ) { // nolint:gocritic // function signature required by Terraform
291- // Update shouldn't be called
292- core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating user" , "User can't be updated" )
290+ func (r * userResource ) Update (ctx context.Context , req resource.UpdateRequest , resp * resource.UpdateResponse ) { // nolint:gocritic // function signature required by Terraform
291+ // Retrieve values from plan
292+ var model Model
293+ diags := req .Plan .Get (ctx , & model )
294+ resp .Diagnostics .Append (diags ... )
295+ if resp .Diagnostics .HasError () {
296+ return
297+ }
298+ projectId := model .ProjectId .ValueString ()
299+ instanceId := model .InstanceId .ValueString ()
300+ userId := model .UserId .ValueString ()
301+ ctx = tflog .SetField (ctx , "project_id" , projectId )
302+ ctx = tflog .SetField (ctx , "instance_id" , instanceId )
303+ ctx = tflog .SetField (ctx , "user_id" , userId )
304+
305+ // Retrieve values from state
306+ var stateModel Model
307+ diags = req .State .Get (ctx , & stateModel )
308+ resp .Diagnostics .Append (diags ... )
309+ if resp .Diagnostics .HasError () {
310+ return
311+ }
312+
313+ var roles []string
314+ if ! (model .Roles .IsNull () || model .Roles .IsUnknown ()) {
315+ diags = model .Roles .ElementsAs (ctx , & roles , false )
316+ resp .Diagnostics .Append (diags ... )
317+ if resp .Diagnostics .HasError () {
318+ return
319+ }
320+ }
321+
322+ // Generate API request body from model
323+ payload , err := toUpdatePayload (& model , roles )
324+ if err != nil {
325+ core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating user" , fmt .Sprintf ("Updating API payload: %v" , err ))
326+ return
327+ }
328+
329+ // Update existing instance
330+ err = r .client .UpdateUser (ctx , projectId , instanceId , userId ).UpdateUserPayload (* payload ).Execute ()
331+ if err != nil {
332+ core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating user" , err .Error ())
333+ return
334+ }
335+
336+ userResp , err := r .client .GetUser (ctx , projectId , instanceId , userId ).Execute ()
337+ if err != nil {
338+ core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating user" , fmt .Sprintf ("Calling API: %v" , err ))
339+ return
340+ }
341+
342+ // Map response body to schema
343+ err = mapFields (userResp , & stateModel )
344+ if err != nil {
345+ core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating user" , fmt .Sprintf ("Processing API payload: %v" , err ))
346+ return
347+ }
348+
349+ // Set state to fully populated data
350+ diags = resp .State .Set (ctx , stateModel )
351+ resp .Diagnostics .Append (diags ... )
352+ if resp .Diagnostics .HasError () {
353+ return
354+ }
355+ tflog .Info (ctx , "MongoDB Flex user updated" )
293356}
294357
295358// Delete deletes the resource and removes the Terraform state on success.
@@ -450,3 +513,17 @@ func toCreatePayload(model *Model, roles []string) (*mongodbflex.CreateUserPaylo
450513 Database : conversion .StringValueToPointer (model .Database ),
451514 }, nil
452515}
516+
517+ func toUpdatePayload (model * Model , roles []string ) (* mongodbflex.UpdateUserPayload , error ) {
518+ if model == nil {
519+ return nil , fmt .Errorf ("nil model" )
520+ }
521+ if roles == nil {
522+ return nil , fmt .Errorf ("nil roles" )
523+ }
524+
525+ return & mongodbflex.UpdateUserPayload {
526+ Roles : & roles ,
527+ Database : conversion .StringValueToPointer (model .Database ),
528+ }, nil
529+ }
0 commit comments