-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
deleteDevices.go
80 lines (71 loc) · 2.65 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
72
73
74
75
76
77
78
79
80
package service
import (
"context"
pbCA "github.com/plgd-dev/hub/v2/certificate-authority/pb"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
pbIS "github.com/plgd-dev/hub/v2/identity-store/pb"
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, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot delete devices from ResourceAggregate: %v", err)
}
if !deleteAllOwned {
_, notDeleted := partitionDeletedDevices(deviceIDs, respRA.GetDeviceIds())
if len(notDeleted) > 0 {
for _, deviceID := range notDeleted {
r.logger.Debugf("failed to delete device('%v') in ResourceAggregate", deviceID)
}
}
}
// CertificateAuthority
respCA, err := r.certificateAuthorityClient.DeleteSigningRecords(ctx, &pbCA.DeleteSigningRecordsRequest{
DeviceIdFilter: deviceIDs,
})
if err != nil {
return nil, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot delete certificates linked to devices %v from CertificateAuthority: %v", deviceIDs, err)
}
r.logger.Debugf("certificate records(num: %v) linked to %v devices has been deleted", respCA.GetCount(), deviceIDs)
// IdentityStore
cmdAS := pbIS.DeleteDevicesRequest{
DeviceIds: deviceIDs,
}
respIS, err := r.idClient.DeleteDevices(ctx, &cmdAS)
if err != nil {
return nil, kitNetGrpc.ForwardErrorf(codes.InvalidArgument, "cannot delete devices from identity-store: %v", err)
}
if !deleteAllOwned {
_, notDeleted := partitionDeletedDevices(deviceIDs, respIS.GetDeviceIds())
if len(notDeleted) > 0 {
for _, deviceID := range notDeleted {
r.logger.Debugf("failed to delete device('%v') from identity-store", deviceID)
}
}
}
return &pb.DeleteDevicesResponse{
DeviceIds: respIS.GetDeviceIds(),
}, nil
}