@@ -387,27 +387,45 @@ func (s *service) UpdateDevice(ctx context.Context, req *requests.DeviceUpdate)
387387// mergeDevice merges an old device into a new device. It transfers all sessions from the old device to the new one and
388388// renames the new device to preserve the old device's identity. The old device is then deleted and the namespace's device count is decremented.
389389func (s * service ) mergeDevice (ctx context.Context , tenantID string , oldDevice * models.Device , newDevice * models.Device ) error {
390+ logFields := log.Fields {"tenant_id" : tenantID , "old_device_uid" : oldDevice .UID , "new_device_uid" : newDevice .UID }
391+
392+ log .WithFields (logFields ).Debug ("transferring tunnels from old device to new device" )
390393 if err := s .store .TunnelUpdateDeviceUID (ctx , tenantID , oldDevice .UID , newDevice .UID ); err != nil {
394+ log .WithError (err ).WithFields (logFields ).Error ("failed to transfer tunnels" )
395+
391396 return err
392397 }
393398
399+ log .WithFields (logFields ).Debug ("transferring sessions from old device to new device" )
394400 if err := s .store .SessionUpdateDeviceUID (ctx , models .UID (oldDevice .UID ), models .UID (newDevice .UID )); err != nil && ! errors .Is (err , store .ErrNoDocuments ) {
401+ log .WithError (err ).WithFields (logFields ).Error ("failed to transfer sessions" )
402+
395403 return err
396404 }
397405
406+ log .WithFields (logFields ).Debug ("updating new device name to preserve old device identity" )
398407 newDevice .Name = oldDevice .Name
399408 if err := s .store .DeviceUpdate (ctx , newDevice ); err != nil {
409+ log .WithError (err ).WithFields (logFields ).Error ("failed to update new device name" )
410+
400411 return err
401412 }
402413
414+ log .WithFields (logFields ).Debug ("mergeDevice: deleting old device" )
403415 if err := s .store .DeviceDelete (ctx , oldDevice ); err != nil {
416+ log .WithError (err ).WithFields (logFields ).Error ("failed to delete old device" )
417+
404418 return err
405419 }
406420
407- if err := s .store .NamespaceIncrementDeviceCount (ctx , tenantID , oldDevice .Status , - 1 ); err != nil { //nolint:revive
421+ if err := s .store .NamespaceIncrementDeviceCount (ctx , tenantID , oldDevice .Status , - 1 ); err != nil {
422+ log .WithError (err ).WithFields (logFields ).Error ("failed to decrement namespace device count" )
423+
408424 return err
409425 }
410426
427+ log .WithFields (logFields ).Info ("device merge operation completed successfully" )
428+
411429 return nil
412430}
413431
0 commit comments