This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
303 lines (224 loc) · 5.03 KB
/
project.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package entities
import (
"encoding/json"
"errors"
"io"
"net"
"time"
)
const DefaultProvider = "GLESYS"
const ErrorNoReservableProjects = "Could not find any available projects to reserve"
type Project struct {
ID string `json:"id"`
Provider string `json:"provider"`
ProviderID string `json:"providerID"` // Provider Project ID
ProviderToken string `json:"providerToken"`
Name string `json:"name"`
SSHKey *SSHKey `json:"sshKey"`
Nodes []Node `json:"nodes"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Context string `json:"context"`
Reserved bool `json:"reserved"`
Domain string `json:"domain"`
Tags []string `json:"tags"`
}
type Projects []Project
func(cfg *Project) WithProvider(provider string) *Project{
cfg.Provider = provider
return cfg
}
func(cfg *Project) WithProviderID(providerProjectId string) *Project{
cfg.ProviderID = providerProjectId
return cfg
}
func(cfg *Project) WithProviderToken(token string) *Project{
cfg.ProviderToken = token
return cfg
}
func(cfg *Project) WithName(name string) *Project{
cfg.Name = name
return cfg
}
func(lc *Project) ToJSON() ([]byte, error) {
return json.MarshalIndent(lc,""," ")
}
func(lc *Project) WriteJSON(w io.Writer) (error) {
enc := json.NewEncoder(w)
enc.SetIndent(""," ")
if err := enc.Encode(lc); err != nil {
return err
}
return nil
}
func (p *Project) Reserve() *Project{
p.Reserved = true
return p
}
func (p *Project) HasTag(tag string) bool {
for _,t := range p.Tags {
if(t == tag ) {
return true
}
}
return false
}
func (p *Project) Tag(tags ...string) *Project {
NewTags:
for _, newTag := range tags {
for _, tag := range p.Tags {
if(tag == newTag) {
continue NewTags
}
}
p.Tags = append(p.Tags, newTag)
}
return p
}
func(lc *Project) FindNodeByIPV4(ip net.IP) (*Node){
for mi := range lc.Nodes {
if(lc.Nodes[mi].IPV4.Equal(ip)){
return &lc.Nodes[mi]
}
}
return nil
}
func(lc *Project) FindNodeByID(id string) (*Node){
for mi := range lc.Nodes {
if(lc.Nodes[mi].ID == id){
return &lc.Nodes[mi]
}
}
return nil
}
func(lc *Project) FindNodeByName(name string) (*Node){
for mi := range lc.Nodes {
if(lc.Nodes[mi].Name == name){
return &lc.Nodes[mi]
}
}
return nil
}
func(lc *Project) FindClusterMasterNode(clusterName string) (*Node){
for mi := range lc.Nodes {
if(lc.Nodes[mi].ClusterName == clusterName && lc.Nodes[mi].IsMaster){
return &lc.Nodes[mi]
}
}
return nil
}
func (lc *Project) UpdateNode(node *Node) (*Project) {
node.Updated=time.Now()
for mi := range lc.Nodes {
if(lc.Nodes[mi].ID == node.ID){
lc.Nodes[mi] = *node
}
}
return lc
}
func (lc *Project) RemoveNode(node *Node) (*Project) {
for mi := range lc.Nodes {
if(lc.Nodes[mi].ID == node.ID){
lc.Nodes = removeNode(lc.Nodes, mi )
}
}
return lc
}
func (lc *Project) UpdateKey(key *SSHKey) (*Project) {
lc.SSHKey = key
return lc
}
func (lc *Project) RemoveKey(key *SSHKey) (*Project) {
lc.SSHKey = nil
return lc
}
func ParseConfig(jsonStr string) (*Project, error){
config := &Project{}
err := json.Unmarshal([]byte(jsonStr), config)
if(err!=nil){
return nil, err
}
return config, nil
}
func ParseProjects(jsonStr string) (Projects, error){
config := []Project{}
err := json.Unmarshal([]byte(jsonStr), &config)
if(err!=nil){
return nil, err
}
return config, nil
}
func removeKey(s []SSHKey, i int) []SSHKey {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func removeNode(s []Node, i int) []Node {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
type ProjectOption func(config *Project) *Project
func WithID(id string) ProjectOption {
return func(cfg *Project) *Project{
cfg.ID = id
return cfg
}
}
func WithProvider(provider string) ProjectOption {
return func(cfg *Project) *Project{
cfg.Provider = provider
return cfg
}
}
func WithProviderID(projectProviderId string) ProjectOption {
return func(cfg *Project) *Project{
cfg.ProviderID = projectProviderId
return cfg
}
}
func WithProviderToken(token string) ProjectOption {
return func(cfg *Project) *Project{
cfg.ProviderToken = token
return cfg
}
}
func WithName(name string) ProjectOption {
return func(cfg *Project) *Project{
cfg.Name = name
return cfg
}
}
func WithDomain(domain string) ProjectOption {
return func(cfg *Project) *Project{
cfg.Domain = domain
return cfg
}
}
func NewProject(opts ...ProjectOption) (*Project, error){
project := &Project{
Created: time.Now(),
Updated: time.Now(),
Nodes:[]Node{},
Provider:DefaultProvider, //TODO: Pluggable
}
for _, opt := range opts {
project = opt(project)
}
pKey,_, err := GetPublicKeys()
if (err != nil) {
return nil,err
}
project.SSHKey = pKey
if (err != nil) {
return nil,err
}
return project,nil
}
func(ps Projects) ReserveOne() (Projects, *Project, error){
for i, p := range ps {
if(p.Reserved == false && !p.HasTag("ertia-pool-master")){
ps[i].Reserved = true
return ps, &p, nil
}
}
return ps, nil, errors.New(ErrorNoReservableProjects)
}