-
Notifications
You must be signed in to change notification settings - Fork 51
/
namespace.go
412 lines (342 loc) · 9.36 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
Copyright 2019 The Cloud-Barista Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package common is to include common methods for managing multi-cloud infra
package common
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/cloud-barista/cb-tumblebug/src/core/common/label"
"github.com/cloud-barista/cb-tumblebug/src/core/model"
"github.com/cloud-barista/cb-tumblebug/src/kvstore/kvstore"
"github.com/cloud-barista/cb-tumblebug/src/kvstore/kvutil"
)
func NsValidation() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
nsId := c.Param("nsId")
if nsId == "" {
return next(c)
}
err := CheckString(nsId)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, "The first character of name must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.")
}
check, err := CheckNs(nsId)
if !check || err != nil {
return echo.NewHTTPError(http.StatusNotFound, "Not valid namespace")
}
return next(c)
}
}
}
func CreateNs(u *model.NsReq) (model.NsInfo, error) {
err := CheckString(u.Name)
if err != nil {
temp := model.NsInfo{}
log.Error().Err(err).Msg("")
return temp, err
}
check, err := CheckNs(u.Name)
if check {
temp := model.NsInfo{}
err := fmt.Errorf("CreateNs(); The namespace " + u.Name + " already exists.")
return temp, err
}
if err != nil {
temp := model.NsInfo{}
log.Error().Err(err).Msg("")
return temp, err
}
content := model.NsInfo{}
content.Id = u.Name
content.Name = u.Name
content.Description = u.Description
key := "/ns/" + content.Id
Val, _ := json.Marshal(content)
err = kvstore.Put(key, string(Val))
if err != nil {
log.Error().Err(err).Msg("")
return content, err
}
// Store label info using CreateOrUpdateLabel
labels := map[string]string{
model.LabelManager: model.StrManager,
model.LabelNamespace: content.Id,
model.LabelLabelType: model.StrNamespace,
model.LabelId: content.Id,
model.LabelName: content.Name,
model.LabelUid: content.Uid,
model.LabelDescription: content.Description,
}
err = label.CreateOrUpdateLabel(model.StrNamespace, content.Uid, key, labels)
if err != nil {
log.Error().Err(err).Msg("")
return content, err
}
return content, nil
}
// UpdateNs is func to update namespace info
func UpdateNs(id string, u *model.NsReq) (model.NsInfo, error) {
res := model.NsInfo{}
emptyInfo := model.NsInfo{}
err := CheckString(id)
if err != nil {
log.Error().Err(err).Msg("")
return emptyInfo, err
}
check, err := CheckNs(id)
if !check {
errString := "The namespace " + id + " does not exist."
err := fmt.Errorf(errString)
return emptyInfo, err
}
if err != nil {
log.Error().Err(err).Msg("")
return emptyInfo, err
}
key := "/ns/" + id
keyValue, err := kvstore.GetKv(key)
if err != nil {
log.Error().Err(err).Msg("")
return emptyInfo, err
}
err = json.Unmarshal([]byte(keyValue.Value), &res)
if err != nil {
log.Error().Err(err).Msg("")
return emptyInfo, err
}
res.Id = id
res.Name = u.Name
res.Description = u.Description
Key := "/ns/" + id
//mapA := map[string]string{"name": content.Name, "description": content.Description}
Val, err := json.Marshal(res)
if err != nil {
log.Error().Err(err).Msg("")
return emptyInfo, err
}
err = kvstore.Put(Key, string(Val))
if err != nil {
log.Error().Err(err).Msg("")
return emptyInfo, err
}
keyValue, err = kvstore.GetKv(key)
if err != nil {
log.Error().Err(err).Msg("")
return emptyInfo, err
}
err = json.Unmarshal([]byte(keyValue.Value), &res)
if err != nil {
log.Error().Err(err).Msg("")
return emptyInfo, err
}
return res, nil
}
func GetNs(id string) (model.NsInfo, error) {
res := model.NsInfo{}
err := CheckString(id)
if err != nil {
temp := model.NsInfo{}
log.Error().Err(err).Msg("")
return temp, err
}
check, err := CheckNs(id)
if !check {
errString := "The namespace " + id + " does not exist."
//mapA := map[string]string{"message": errString}
//mapB, _ := json.Marshal(mapA)
err := fmt.Errorf(errString)
return res, err
}
if err != nil {
temp := model.NsInfo{}
log.Error().Err(err).Msg("")
return temp, err
}
log.Debug().Msg("[Get namespace] " + id)
key := "/ns/" + id
log.Debug().Msg(key)
keyValue, err := kvstore.GetKv(key)
if err != nil {
log.Error().Err(err).Msg("")
return res, err
}
err = json.Unmarshal([]byte(keyValue.Value), &res)
if err != nil {
log.Error().Err(err).Msg("")
return res, err
}
return res, nil
}
func ListNs() ([]model.NsInfo, error) {
log.Debug().Msg("[List namespace]")
key := "/ns"
log.Debug().Msg(key)
keyValue, err := kvstore.GetKvList(key)
keyValue = kvutil.FilterKvListBy(keyValue, key, 1)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
if keyValue != nil {
res := []model.NsInfo{}
for _, v := range keyValue {
tempObj := model.NsInfo{}
err = json.Unmarshal([]byte(v.Value), &tempObj)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
res = append(res, tempObj)
}
return res, nil
//return true, nil
}
return nil, nil // When err == nil && keyValue == nil
}
func AppendIfMissing(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}
func ListNsId() ([]string, error) {
key := "/ns"
var nsList []string
// Implementation Option 1
// keyValue, _ := kvstore.GetKvList(key)
// r, _ := regexp.Compile("/ns/[a-z]([-a-z0-9]*[a-z0-9])?$")
// for _, v := range keyValue {
// if v.Key == "" {
// continue
// }
// filtered := r.FindString(v.Key)
// if filtered != v.Key {
// continue
// } else {
// trimmedString := strings.TrimPrefix(v.Key, "/ns/")
// nsList = AppendIfMissing(nsList, trimmedString)
// }
// }
// EOF of Implementation Option 1
// Implementation Option 2
keyValue, err := kvstore.GetKvList(key)
keyValue = kvutil.FilterKvListBy(keyValue, key, 1)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
if keyValue != nil {
for _, v := range keyValue {
trimmedString := strings.TrimPrefix(v.Key, "/ns/")
nsList = append(nsList, trimmedString)
}
}
// EOF of Implementation Option 2
return nsList, nil
}
func DelNs(id string) error {
err := CheckString(id)
if err != nil {
log.Error().Err(err).Msg("")
return err
}
ns, err := GetNs(id)
if err != nil {
log.Error().Err(err).Msg("")
return err
}
log.Debug().Msg("[Delete ns] " + id)
key := "/ns/" + id
mciList := GetChildIdList(key + "/mci")
imageList := GetChildIdList(key + "/resources/image")
vNetList := GetChildIdList(key + "/resources/vNet")
//subnetList := GetChildIdList(key + "/resources/subnet")
//publicIpList := GetChildIdList(key + "/resources/publicIp")
securityGroupList := GetChildIdList(key + "/resources/securityGroup")
specList := GetChildIdList(key + "/resources/spec")
sshKeyList := GetChildIdList(key + "/resources/sshKey")
//vNicList := GetChildIdList(key + "/resources/vNic")
if len(mciList)+
len(imageList)+
len(vNetList)+
//len(subnetList)
len(securityGroupList)+
len(specList)+
len(sshKeyList) > 0 {
errString := "Cannot delete NS " + id + ", which is not empty. There exists at least one MCI or one of resources."
errString += " \n len(mciList): " + strconv.Itoa(len(mciList))
errString += " \n len(imageList): " + strconv.Itoa(len(imageList))
errString += " \n len(vNetList): " + strconv.Itoa(len(vNetList))
//errString += " \n len(publicIpList): " + strconv.Itoa(len(publicIpList))
errString += " \n len(securityGroupList): " + strconv.Itoa(len(securityGroupList))
errString += " \n len(specList): " + strconv.Itoa(len(specList))
errString += " \n len(sshKeyList): " + strconv.Itoa(len(sshKeyList))
//errString += " \n len(subnetList): " + strconv.Itoa(len(subnetList))
//errString += " \n len(vNicList): " + strconv.Itoa(len(vNicList))
err := fmt.Errorf(errString)
log.Error().Err(err).Msg("")
return err
}
// delete ns info
err = kvstore.Delete(key)
if err != nil {
log.Error().Err(err).Msg("")
return err
}
err = label.DeleteLabelObject(model.StrNamespace, ns.Uid)
if err != nil {
log.Error().Err(err).Msg("")
}
return nil
}
func DelAllNs() error {
nsIdList, err := ListNsId()
if err != nil {
return err
}
if len(nsIdList) == 0 {
return nil
}
for _, v := range nsIdList {
err := DelNs(v)
if err != nil {
return err
}
}
return nil
}
func CheckNs(id string) (bool, error) {
if id == "" {
err := fmt.Errorf("CheckNs failed; nsId given is null.")
return false, err
}
err := CheckString(id)
if err != nil {
log.Error().Err(err).Msg("")
return false, err
}
key := "/ns/" + id
keyValue, _ := kvstore.GetKv(key)
if keyValue != (kvstore.KeyValue{}) {
return true, nil
}
return false, nil
}