Skip to content

Commit 5c118ec

Browse files
heiytorgustavosbarreto
authored andcommitted
feat(api): add tunnel device UID update support during device merging
- Implement TunnelUpdateDeviceUID method in store interface - Update mergeDevice to transfer tunnels along with sessions - Add tunnel UID updates when merging devices with same MAC address - Include TunnelStore interface in main Store composition - Add corresponding test mocks for tunnel device UID updates - Remove TODO comment as tunnel updates are now implemented
1 parent 9cc569e commit 5c118ec

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

api/services/device.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ func (s *service) UpdateDevice(ctx context.Context, req *requests.DeviceUpdate)
374374
// mergeDevice merges an old device into a new device. It transfers all sessions from the old device to the new one and
375375
// 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.
376376
func (s *service) mergeDevice(ctx context.Context, tenantID string, oldDevice *models.Device, newDevice *models.Device) error {
377-
// TODO: update tunnels as well?
377+
if err := s.store.TunnelUpdateDeviceUID(ctx, tenantID, oldDevice.UID, newDevice.UID); err != nil {
378+
return err
379+
}
378380

379381
if err := s.store.SessionUpdateDeviceUID(ctx, models.UID(oldDevice.UID), models.UID(newDevice.UID)); err != nil && !errors.Is(err, store.ErrNoDocuments) {
380382
return err

api/services/device_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,10 @@ func TestUpdateDeviceStatus(t *testing.T) {
17141714
).
17151715
Once()
17161716
// Merge operations
1717+
storeMock.
1718+
On("TunnelUpdateDeviceUID", ctx, "00000000-0000-0000-0000-000000000000", "old-device", "new-device").
1719+
Return(nil).
1720+
Once()
17171721
storeMock.
17181722
On("SessionUpdateDeviceUID", ctx, models.UID("old-device"), models.UID("new-device")).
17191723
Return(nil).

api/store/mocks/store.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/store/mongo/tunnel.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package mongo
2+
3+
import (
4+
"context"
5+
6+
"go.mongodb.org/mongo-driver/bson"
7+
)
8+
9+
func (s *Store) TunnelUpdateDeviceUID(ctx context.Context, tenantID, oldUID, newUID string) error {
10+
_, err := s.db.Collection("tunnels").UpdateMany(ctx, bson.M{"namespace": tenantID, "device": oldUID}, bson.M{"$set": bson.M{"device": newUID}})
11+
if err != nil {
12+
return FromMongoError(err)
13+
}
14+
15+
return nil
16+
}

api/store/store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Store interface {
1515
APIKeyStore
1616
TransactionStore
1717
SystemStore
18+
TunnelStore
1819

1920
Options() QueryOptions
2021
}

api/store/tunnel.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package store
2+
3+
import "context"
4+
5+
type TunnelStore interface {
6+
// TunnelUpdateDeviceUID changes all tunnels from oldUID to newUID within the specified tenantID.
7+
TunnelUpdateDeviceUID(ctx context.Context, tenantID, oldUID, newUID string) error
8+
}

0 commit comments

Comments
 (0)