forked from HuaweiTech/lattice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell_presence.go
98 lines (77 loc) · 2.06 KB
/
cell_presence.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package models
type CellSet map[string]CellPresence
func (set CellSet) Add(cell CellPresence) {
set[cell.CellID] = cell
}
func (set CellSet) Each(predicate func(cell CellPresence)) {
for _, cell := range set {
predicate(cell)
}
}
func (set CellSet) HasCellID(cellID string) bool {
_, ok := set[cellID]
return ok
}
type CellCapacity struct {
MemoryMB int `json:"memory_mb"`
DiskMB int `json:"disk_mb"`
Containers int `json:"containers"`
}
func NewCellCapacity(memoryMB, diskMB, containers int) CellCapacity {
return CellCapacity{
MemoryMB: memoryMB,
DiskMB: diskMB,
Containers: containers,
}
}
func (cap CellCapacity) Validate() error {
var validationError ValidationError
if cap.MemoryMB <= 0 {
validationError = validationError.Append(ErrInvalidField{"memory_mb"})
}
if cap.DiskMB < 0 {
validationError = validationError.Append(ErrInvalidField{"disk_mb"})
}
if cap.Containers <= 0 {
validationError = validationError.Append(ErrInvalidField{"containers"})
}
if !validationError.Empty() {
return validationError
}
return nil
}
type CellPresence struct {
CellID string `json:"cell_id"`
Stack string `json:"stack"`
RepAddress string `json:"rep_address"`
Zone string `json:"zone"`
Capacity CellCapacity `json:"capacity"`
}
func NewCellPresence(cellID, stack, repAddress, zone string, capacity CellCapacity) CellPresence {
return CellPresence{
CellID: cellID,
Stack: stack,
RepAddress: repAddress,
Zone: zone,
Capacity: capacity,
}
}
func (c CellPresence) Validate() error {
var validationError ValidationError
if c.CellID == "" {
validationError = validationError.Append(ErrInvalidField{"cell_id"})
}
if c.Stack == "" {
validationError = validationError.Append(ErrInvalidField{"stack"})
}
if c.RepAddress == "" {
validationError = validationError.Append(ErrInvalidField{"rep_address"})
}
if err := c.Capacity.Validate(); err != nil {
validationError = validationError.Append(err)
}
if !validationError.Empty() {
return validationError
}
return nil
}