Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/routes/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ func (h *Handler) GetDeviceList(c gateway.Context) error {
return err
}

res, count, err := h.service.ListDevices(c.Ctx(), req)
var tenant string
if c.Tenant() != nil {
tenant = c.Tenant().ID
}

res, count, err := h.service.ListDevices(c.Ctx(), tenant, req)
c.Response().Header().Set("X-Total-Count", strconv.Itoa(count))

if err != nil {
Expand Down
53 changes: 27 additions & 26 deletions api/services/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
const StatusAccepted = "accepted"

type DeviceService interface {
ListDevices(ctx context.Context, req *requests.DeviceList) ([]models.Device, int, error)
ListDevices(ctx context.Context, tenant string, req *requests.DeviceList) ([]models.Device, int, error)
GetDevice(ctx context.Context, uid models.UID) (*models.Device, error)

// ResolveDevice attempts to resolve a device by searching for either its UID or hostname. When both are provided,
Expand All @@ -34,10 +34,13 @@ type DeviceService interface {
UpdateDevice(ctx context.Context, req *requests.DeviceUpdate) error
}

func (s *service) ListDevices(ctx context.Context, req *requests.DeviceList) ([]models.Device, int, error) {
func (s *service) ListDevices(ctx context.Context, tenant string, req *requests.DeviceList) ([]models.Device, int, error) {
if tenant == "" {
return s.store.DeviceList(ctx, req.DeviceStatus, req.Paginator, req.Filters, req.Sorter, false)
}

if req.DeviceStatus == models.DeviceStatusRemoved {
// TODO: unique DeviceList
removed, count, err := s.store.DeviceRemovedList(ctx, req.TenantID, req.Paginator, req.Filters, req.Sorter)
removed, count, err := s.store.DeviceRemovedList(ctx, tenant, req.Paginator, req.Filters, req.Sorter)
if err != nil {
return nil, 0, err
}
Expand All @@ -50,34 +53,32 @@ func (s *service) ListDevices(ctx context.Context, req *requests.DeviceList) ([]
return devices, count, nil
}

if req.TenantID != "" {
ns, err := s.store.NamespaceGet(ctx, req.TenantID)
if err != nil {
return nil, 0, NewErrNamespaceNotFound(req.TenantID, err)
}
ns, err := s.store.NamespaceGet(ctx, tenant)
if err != nil {
return nil, 0, NewErrNamespaceNotFound(tenant, err)
}

if ns.HasMaxDevices() {
switch {
case envs.IsCloud():
removed, err := s.store.DeviceRemovedCount(ctx, ns.TenantID)
if err != nil {
return nil, 0, NewErrDeviceRemovedCount(err)
}
var limitReached bool

if ns.HasLimitDevicesReached(removed) {
return s.store.DeviceList(ctx, req.DeviceStatus, req.Paginator, req.Filters, req.Sorter, store.DeviceAcceptableFromRemoved)
}
case envs.IsEnterprise():
fallthrough
case envs.IsCommunity():
if ns.HasMaxDevicesReached() {
return s.store.DeviceList(ctx, req.DeviceStatus, req.Paginator, req.Filters, req.Sorter, store.DeviceAcceptableAsFalse)
}
if ns.HasMaxDevices() {
switch {
case envs.IsCloud():
removed, err := s.store.DeviceRemovedCount(ctx, ns.TenantID)
if err != nil {
return nil, 0, NewErrDeviceRemovedCount(err)
}

if ns.HasLimitDevicesReached(removed) {
limitReached = true
}
case envs.IsEnterprise() || envs.IsCommunity():
if ns.HasMaxDevicesReached() {
limitReached = true
}
}
}

return s.store.DeviceList(ctx, req.DeviceStatus, req.Paginator, req.Filters, req.Sorter, store.DeviceAcceptableIfNotAccepted)
return s.store.DeviceList(ctx, req.DeviceStatus, req.Paginator, req.Filters, req.Sorter, limitReached)
}

func (s *service) GetDevice(ctx context.Context, uid models.UID) (*models.Device, error) {
Expand Down
22 changes: 11 additions & 11 deletions api/services/mocks/services.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 1 addition & 13 deletions api/store/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ import (
"github.com/shellhub-io/shellhub/pkg/models"
)

type DeviceAcceptable uint

const (
// DeviceAcceptableIfNotAccepted is used to indicate the all devices not accepted will be defined as "acceptabled".
DeviceAcceptableIfNotAccepted DeviceAcceptable = iota + 1
// DeviceAcceptableFromRemoved is used to indicate that the namepsace's device maxium number of devices has been
// reached and should set the "acceptable" value to true for devices that were recently removed.
DeviceAcceptableFromRemoved
// DeviceAcceptableAsFalse set acceptable to false to all returned devices.
DeviceAcceptableAsFalse
)

type DeviceResolver uint

const (
Expand All @@ -28,7 +16,7 @@ const (
)

type DeviceStore interface {
DeviceList(ctx context.Context, status models.DeviceStatus, pagination query.Paginator, filters query.Filters, sorter query.Sorter, acceptable DeviceAcceptable) ([]models.Device, int, error)
DeviceList(ctx context.Context, status models.DeviceStatus, pagination query.Paginator, filters query.Filters, sorter query.Sorter, full bool) ([]models.Device, int, error)

// DeviceResolve fetches a device using a specific resolver within a given tenant ID.
//
Expand Down
22 changes: 11 additions & 11 deletions api/store/mocks/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 23 additions & 45 deletions api/store/mongo/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

// DeviceList returns a list of devices based on the given filters, pagination and sorting.
func (s *Store) DeviceList(ctx context.Context, status models.DeviceStatus, paginator query.Paginator, filters query.Filters, sorter query.Sorter, acceptable store.DeviceAcceptable) ([]models.Device, int, error) {
func (s *Store) DeviceList(ctx context.Context, status models.DeviceStatus, paginator query.Paginator, filters query.Filters, sorter query.Sorter, full bool) ([]models.Device, int, error) {
query := []bson.M{
{
"$match": bson.M{
Expand Down Expand Up @@ -63,58 +63,36 @@ func (s *Store) DeviceList(ctx context.Context, status models.DeviceStatus, pagi
}}, query...)
}

// When the listing mode is [store.DeviceListModeMaxDeviceReached], we should evaluate the `removed_devices`
// collection to check its `accetable` status.
switch acceptable {
case store.DeviceAcceptableFromRemoved:
query = append(query, []bson.M{
{
"$lookup": bson.M{
"from": "removed_devices",
"localField": "uid",
"foreignField": "device.uid",
"as": "removed",
},
},
{
"$addFields": bson.M{
"acceptable": bson.M{
"$cond": bson.M{
"if": bson.M{
"$and": bson.A{
bson.M{"$ne": bson.A{"$status", models.DeviceStatusAccepted}},
bson.M{"$anyElementTrue": []interface{}{"$removed"}},
},
},
"then": true,
"else": false,
},
},
},
},
{
"$unset": "removed",
},
}...)
case store.DeviceAcceptableAsFalse:
query = append(query, bson.M{
"$addFields": bson.M{
"acceptable": false,
query = append(query, []bson.M{
{
"$lookup": bson.M{
"from": "removed_devices",
"localField": "uid",
"foreignField": "device.uid",
"as": "removed",
},
})
case store.DeviceAcceptableIfNotAccepted:
query = append(query, bson.M{
},
{
"$addFields": bson.M{
"acceptable": bson.M{
"$cond": bson.M{
"if": bson.M{"$ne": bson.A{"$status", models.DeviceStatusAccepted}},
"then": true,
"if": bson.M{"$ne": bson.A{"$status", models.DeviceStatusAccepted}},
"then": bson.M{
"$cond": bson.M{
"if": bson.M{"$anyElementTrue": []any{"$removed"}},
"then": true,
"else": bson.M{"$not": full},
},
},
"else": false,
},
},
},
})
}
},
{
"$unset": "removed",
},
}...)

queryMatch, err := queries.FromFilters(&filters)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions api/store/mongo/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,5 +414,9 @@ func (s *Store) NamespaceIncrementDeviceCount(ctx context.Context, tenantID stri
return store.ErrNoDocuments
}

if err := s.cache.Delete(ctx, strings.Join([]string{"namespace", tenantID}, "/")); err != nil {
log.Error(err)
}

return nil
}
1 change: 0 additions & 1 deletion pkg/api/requests/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

type DeviceList struct {
TenantID string `header:"X-Tenant-ID"`
DeviceStatus models.DeviceStatus `query:"status"` // TODO: validate
query.Paginator
query.Sorter
Expand Down