-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
deleteDevices.go
71 lines (63 loc) · 2.2 KB
/
deleteDevices.go
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
package service
import (
"context"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
pbIS "github.com/plgd-dev/hub/v2/identity-store/pb"
"github.com/plgd-dev/hub/v2/pkg/log"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/pkg/strings"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"google.golang.org/grpc/codes"
)
// Split array into two based on whether the array item is contained in the expected array or not
func partitionDeletedDevices(expected, actual []string) ([]string, []string) {
cache := make(map[string]struct{})
for _, v := range actual {
cache[v] = struct{}{}
}
contains := func(s string) bool {
_, ok := cache[s]
return ok
}
return strings.Split(expected, contains)
}
func (r *RequestHandler) DeleteDevices(ctx context.Context, req *pb.DeleteDevicesRequest) (*pb.DeleteDevicesResponse, error) {
// get unique non-empty ids
deviceIDs, _ := strings.Split(strings.Unique(req.DeviceIdFilter), func(s string) bool {
return s != ""
})
deleteAllOwned := len(deviceIDs) == 0
// ResourceAggregate
cmdRA := commands.DeleteDevicesRequest{DeviceIds: deviceIDs}
respRA, err := r.resourceAggregateClient.DeleteDevices(ctx, &cmdRA)
if err != nil {
return nil, log.LogAndReturnError(kitNetGrpc.ForwardErrorf(codes.Internal, "cannot delete devices from ResourceAggregate: %v", err))
}
if !deleteAllOwned {
_, notDeleted := partitionDeletedDevices(deviceIDs, respRA.GetDeviceIds())
if len(notDeleted) > 0 {
for _, deviceID := range notDeleted {
log.Debugf("failed to delete device('%v') in ResourceAggregate", deviceID)
}
}
}
// IdentityStore
cmdAS := pbIS.DeleteDevicesRequest{
DeviceIds: deviceIDs,
}
respIS, err := r.idClient.DeleteDevices(ctx, &cmdAS)
if err != nil {
return nil, log.LogAndReturnError(kitNetGrpc.ForwardErrorf(codes.Internal, "cannot delete devices from identity-store: %v", err))
}
if !deleteAllOwned {
_, notDeleted := partitionDeletedDevices(deviceIDs, respIS.GetDeviceIds())
if len(notDeleted) > 0 {
for _, deviceID := range notDeleted {
log.Debugf("failed to delete device('%v') from identity-store", deviceID)
}
}
}
return &pb.DeleteDevicesResponse{
DeviceIds: respIS.GetDeviceIds(),
}, nil
}