-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
namespace.go
67 lines (57 loc) · 2.67 KB
/
namespace.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
package models
import (
"time"
)
type Namespace struct {
Name string `json:"name" validate:"required,hostname_rfc1123,excludes=.,lowercase"`
Owner string `json:"owner"`
TenantID string `json:"tenant_id" bson:"tenant_id,omitempty"`
Members []Member `json:"members" bson:"members"`
Settings *NamespaceSettings `json:"settings"`
Devices int `json:"-" bson:"devices,omitempty"`
Sessions int `json:"-" bson:"sessions,omitempty"`
MaxDevices int `json:"max_devices" bson:"max_devices"`
DevicesCount int `json:"devices_count" bson:"devices_count,omitempty"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
Billing *Billing `json:"billing" bson:"billing,omitempty"`
}
// HasMaxDevices checks if the namespace has a maximum number of devices.
//
// Generally, a namespace has a MaxDevices value greater than 0 when the ShellHub is either in community version or
// the namespace does not have a billing plan enabled, because, in this case, we set this value to -1.
func (n *Namespace) HasMaxDevices() bool {
return n.MaxDevices > 0
}
// HasMaxDevicesReached checks if the namespace has reached the maximum number of devices.
func (n *Namespace) HasMaxDevicesReached() bool {
return uint64(n.DevicesCount) >= uint64(n.MaxDevices)
}
// HasLimitDevicesReached checks if the namespace limit was reached using the removed devices collection.
//
// This method is intended to be run only when the ShellHub instance is Cloud.
func (n *Namespace) HasLimitDevicesReached(removed int64) bool {
return int64(n.DevicesCount)+removed >= int64(n.MaxDevices)
}
// FindMember checks if a member with the specified ID exists in the namespace.
func (n *Namespace) FindMember(id string) (*Member, bool) {
for _, member := range n.Members {
if member.ID == id {
return &member, true
}
}
return nil, false
}
type NamespaceSettings struct {
SessionRecord bool `json:"session_record" bson:"session_record,omitempty"`
ConnectionAnnouncement string `json:"connection_announcement" bson:"connection_announcement"`
}
type Member struct {
ID string `json:"id,omitempty" bson:"id,omitempty"`
Username string `json:"username,omitempty" bson:"username,omitempty" validate:"username"`
Role string `json:"role" bson:"role" validate:"required,oneof=administrator operator observer"`
}
type NamespaceChanges struct {
Name string `bson:"name,omitempty"`
SessionRecord *bool `bson:"settings.session_record,omitempty"`
ConnectionAnnouncement *string `bson:"settings.connection_announcement,omitempty"`
}