/
list_objects.go
72 lines (57 loc) · 1.88 KB
/
list_objects.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
package metathings_deviced_service
import (
"context"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
simple_storage "github.com/nayotta/metathings/pkg/deviced/simple_storage"
identityd_validator "github.com/nayotta/metathings/pkg/identityd2/validator"
pb "github.com/nayotta/metathings/proto/deviced"
)
func (self *MetathingsDevicedService) ValidateListObjects(ctx context.Context, in interface{}) error {
return self.validator.Validate(
identityd_validator.Providers{
func() object_getter {
req := in.(*pb.ListObjectsRequest)
return req
},
},
identityd_validator.Invokers{
ensure_get_object_device_id,
},
)
}
func (self *MetathingsDevicedService) AuthorizeListObjects(ctx context.Context, in interface{}) error {
return self.authorizer.Authorize(ctx, in.(*pb.ListObjectsRequest).GetObject().GetDevice().GetId().GetValue(), "deviced:list_objects")
}
func (self *MetathingsDevicedService) ListObjects(ctx context.Context, req *pb.ListObjectsRequest) (*pb.ListObjectsResponse, error) {
obj := req.GetObject()
obj_s := parse_object(obj)
opt := &simple_storage.ListObjectsOption{}
logger := self.get_logger()
if recur := req.GetRecursive(); recur != nil {
opt.Recursive = recur.GetValue()
}
if depth := req.GetDepth(); depth != nil {
opt.Depth = int(depth.GetValue())
}
if opt.Recursive && opt.Depth <= 0 {
opt.Depth = 16
}
objs_s, err := self.simple_storage.ListObjects(obj_s, opt)
if err != nil {
logger.WithError(err).Errorf("failed to list objects in storage")
return nil, status.Errorf(codes.Internal, err.Error())
}
logger = logger.WithFields(log.Fields{
"device": obj_s.Device,
"object": obj_s.FullName(),
"recursive": opt.Recursive,
"depth": opt.Depth,
})
res := &pb.ListObjectsResponse{
Objects: copy_objects(objs_s),
}
logger.Debugf("list objects")
return res, nil
}