/
cell_presence.go
153 lines (121 loc) · 3.29 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package models
import "strings"
type CellSet map[string]*CellPresence
func NewCellSet() CellSet {
return make(CellSet)
}
func NewCellSetFromList(cells []*CellPresence) CellSet {
cellSet := NewCellSet()
for _, v := range cells {
cellSet.Add(v)
}
return cellSet
}
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
}
func (set CellSet) CellIDs() []string {
keys := make([]string, 0, len(set))
for k := range set {
keys = append(keys, k)
}
return keys
}
func NewCellCapacity(memoryMB, diskMB, containers int32) 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
}
func NewCellPresence(
cellID, repAddress, repUrl, zone string,
capacity CellCapacity,
rootFSProviders, preloadedRootFSes, placementTags, optionalPlacementTags []string,
) CellPresence {
var providers []*Provider
var pProviders []string
pProviders = append(pProviders, preloadedRootFSes...)
providers = append(providers, &Provider{PreloadedRootFSScheme, pProviders})
providers = append(providers, &Provider{PreloadedOCIRootFSScheme, pProviders})
for _, prov := range rootFSProviders {
providers = append(providers, &Provider{prov, []string{}})
}
return CellPresence{
CellId: cellID,
RepAddress: repAddress,
RepUrl: repUrl,
Zone: zone,
Capacity: &capacity,
RootfsProviders: providers,
PlacementTags: placementTags,
OptionalPlacementTags: optionalPlacementTags,
}
}
func (c CellPresence) Validate() error {
var validationError ValidationError
if c.CellId == "" {
validationError = validationError.Append(ErrInvalidField{"cell_id"})
}
if c.RepAddress == "" {
validationError = validationError.Append(ErrInvalidField{"rep_address"})
}
if c.RepUrl != "" && !strings.HasPrefix(c.RepUrl, "http://") && !strings.HasPrefix(c.RepUrl, "https://") {
validationError = validationError.Append(ErrInvalidField{"rep_url"})
}
if err := c.Capacity.Validate(); err != nil {
validationError = validationError.Append(err)
}
if !validationError.Empty() {
return validationError
}
return nil
}
const (
EventTypeCellDisappeared = "cell_disappeared"
)
type CellEvent interface {
EventType() string
CellIDs() []string
}
type CellDisappearedEvent struct {
IDs []string
}
func NewCellDisappearedEvent(ids []string) CellDisappearedEvent {
return CellDisappearedEvent{ids}
}
func (CellDisappearedEvent) EventType() string {
return EventTypeCellDisappeared
}
func (e CellDisappearedEvent) CellIDs() []string {
return e.IDs
}
func (c *CellPresence) Copy() *CellPresence {
newCellPresense := *c
return &newCellPresense
}