@@ -3,7 +3,9 @@ package services
3
3
import (
4
4
"context"
5
5
6
+ "github.com/shellhub-io/shellhub/api/store"
6
7
"github.com/shellhub-io/shellhub/pkg/api/requests"
8
+ "github.com/shellhub-io/shellhub/pkg/envs"
7
9
"github.com/shellhub-io/shellhub/pkg/models"
8
10
)
9
11
@@ -22,11 +24,64 @@ type DeviceService interface {
22
24
}
23
25
24
26
func (s * service ) ListDevices (ctx context.Context , req * requests.DeviceList ) ([]models.Device , int , error ) {
25
- return nil , 0 , nil
27
+ // if req.DeviceStatus == models.DeviceStatusRemoved {
28
+ // // TODO: unique DeviceList
29
+ // removed, count, err := s.store.DeviceRemovedList(ctx, req.TenantID, req.Paginator, req.Filters, req.Sorter)
30
+ // if err != nil {
31
+ // return nil, 0, err
32
+ // }
33
+ //
34
+ // devices := make([]models.Device, 0, len(removed))
35
+ // for _, device := range removed {
36
+ // devices = append(devices, *device.Device)
37
+ // }
38
+ //
39
+ // return devices, count, nil
40
+ // }
41
+ //
42
+ // if req.TenantID != "" {
43
+ // ns, err := s.store.NamespaceGet(ctx, req.TenantID, s.store.Options().CountAcceptedDevices())
44
+ // if err != nil {
45
+ // return nil, 0, NewErrNamespaceNotFound(req.TenantID, err)
46
+ // }
47
+ //
48
+ // if ns.HasMaxDevices() {
49
+ // switch {
50
+ // case envs.IsCloud():
51
+ // removed, err := s.store.DeviceRemovedCount(ctx, ns.TenantID)
52
+ // if err != nil {
53
+ // return nil, 0, NewErrDeviceRemovedCount(err)
54
+ // }
55
+ //
56
+ // if ns.HasLimitDevicesReached(removed) {
57
+ // return s.store.DeviceList(ctx, req.DeviceStatus, req.Paginator, req.Filters, req.Sorter, store.DeviceAcceptableFromRemoved)
58
+ // }
59
+ // case envs.IsEnterprise():
60
+ // fallthrough
61
+ // case envs.IsCommunity():
62
+ // if ns.HasMaxDevicesReached() {
63
+ // return s.store.DeviceList(ctx, req.DeviceStatus, req.Paginator, req.Filters, req.Sorter, store.DeviceAcceptableAsFalse)
64
+ // }
65
+ // }
66
+ // }
67
+ // }
68
+
69
+ return s .store .DeviceList (
70
+ ctx ,
71
+ s .store .Options ().InNamespace (req .TenantID ),
72
+ s .store .Options ().Filter (req .Filters ),
73
+ s .store .Options ().Paginate (req .Paginator ),
74
+ s .store .Options ().Order (req .Sorter ),
75
+ )
26
76
}
27
77
28
78
func (s * service ) GetDevice (ctx context.Context , uid models.UID ) (* models.Device , error ) {
29
- return nil , nil
79
+ device , err := s .store .DeviceGet (ctx , store .DeviceIdentID , string (uid ))
80
+ if err != nil {
81
+ return nil , NewErrDeviceNotFound (uid , err )
82
+ }
83
+
84
+ return device , nil
30
85
}
31
86
32
87
// DeleteDevice deletes a device from a namespace.
@@ -38,7 +93,26 @@ func (s *service) GetDevice(ctx context.Context, uid models.UID) (*models.Device
38
93
// NewErrNamespaceNotFound(tenant, err), if the usage cannot be reported, ErrReport or if the store function that
39
94
// delete the device fails.
40
95
func (s * service ) DeleteDevice (ctx context.Context , uid models.UID , tenant string ) error {
41
- return nil
96
+ ns , err := s .store .NamespaceGet (ctx , store .NamespaceIdentID , tenant )
97
+ if err != nil {
98
+ return NewErrNamespaceNotFound (tenant , err )
99
+ }
100
+
101
+ device , err := s .store .DeviceGet (ctx , store .DeviceIdentID , string (uid ))
102
+ if err != nil {
103
+ return NewErrDeviceNotFound (uid , err )
104
+ }
105
+
106
+ // If the namespace has a limit of devices, we change the device's slot status to removed.
107
+ // This way, we can keep track of the number of devices that were removed from the namespace and void the device
108
+ // switching.
109
+ if envs .IsCloud () && envs .HasBilling () && ! ns .Billing .IsActive () {
110
+ if err := s .store .DeviceRemovedInsert (ctx , tenant , device ); err != nil {
111
+ return NewErrDeviceRemovedInsert (err )
112
+ }
113
+ }
114
+
115
+ return s .store .DeviceDelete (ctx , uid )
42
116
}
43
117
44
118
func (s * service ) RenameDevice (ctx context.Context , uid models.UID , name , tenant string ) error {
@@ -50,7 +124,12 @@ func (s *service) RenameDevice(ctx context.Context, uid models.UID, name, tenant
50
124
// It receives a context, used to "control" the request flow and, the namespace name from a models.Namespace and a
51
125
// device name from models.Device.
52
126
func (s * service ) LookupDevice (ctx context.Context , namespace , name string ) (* models.Device , error ) {
53
- return nil , nil
127
+ device , err := s .store .DeviceGet (ctx , store .DeviceIdentName , name )
128
+ if err != nil {
129
+ return nil , NewErrDeviceLookupNotFound (namespace , name , err )
130
+ }
131
+
132
+ return device , nil
54
133
}
55
134
56
135
func (s * service ) OfflineDevice (ctx context.Context , uid models.UID ) error {
0 commit comments