/
orchestration.go
646 lines (536 loc) · 21.6 KB
/
orchestration.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
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 mcis is to manage multi-cloud infra service
package mcis
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/cloud-barista/cb-tumblebug/src/core/common"
)
// Status for mcis automation
const (
// AutoStatusReady is const for "Ready" status.
AutoStatusReady string = "Ready"
// AutoStatusChecking is const for "Checking" status.
AutoStatusChecking string = "Checking"
// AutoStatusDetected is const for "Detected" status.
AutoStatusDetected string = "Detected"
// AutoStatusOperating is const for "Operating" status.
AutoStatusOperating string = "Operating"
// AutoStatusStabilizing is const for "Stabilizing" status.
AutoStatusStabilizing string = "Stabilizing"
// AutoStatusTimeout is const for "Timeout" status.
AutoStatusTimeout string = "Timeout"
// AutoStatusError is const for "Failed" status.
AutoStatusError string = "Failed"
// AutoStatusSuspended is const for "Suspended" status.
AutoStatusSuspended string = "Suspended"
)
// Action for mcis automation
const (
// AutoActionScaleOut is const for "ScaleOut" action.
AutoActionScaleOut string = "ScaleOut"
// AutoActionScaleIn is const for "ScaleIn" action.
AutoActionScaleIn string = "ScaleIn"
)
// AutoCondition is struct for MCIS auto-control condition.
type AutoCondition struct {
Metric string `json:"metric" example:"cpu"`
Operator string `json:"operator" example:">=" enums:"<,<=,>,>="`
Operand string `json:"operand" example:"80"`
EvaluationPeriod string `json:"evaluationPeriod" example:"10"`
EvaluationValue []string `json:"evaluationValue"`
//InitTime string `json:"initTime"` // to check start of duration
//Duration string `json:"duration"` // duration for checking
}
// AutoAction is struct for MCIS auto-control action.
type AutoAction struct {
ActionType string `json:"actionType" example:"ScaleOut" enums:"ScaleOut,ScaleIn"`
VmDynamicReq TbVmDynamicReq `json:"vmDynamicReq"`
// PostCommand is field for providing command to VMs after its creation. example:"wget https://raw.githubusercontent.com/cloud-barista/cb-tumblebug/main/scripts/setweb.sh -O ~/setweb.sh; chmod +x ~/setweb.sh; sudo ~/setweb.sh"
PostCommand McisCmdReq `json:"postCommand"`
PlacementAlgo string `json:"placementAlgo" example:"random"`
}
// Policy is struct for MCIS auto-control Policy request that includes AutoCondition, AutoAction, Status.
type Policy struct {
AutoCondition AutoCondition `json:"autoCondition"`
AutoAction AutoAction `json:"autoAction"`
Status string `json:"status"`
}
// McisPolicyInfo is struct for MCIS auto-control Policy object.
type McisPolicyInfo struct {
Name string `json:"Name"` //MCIS Name (for request)
Id string `json:"Id"` //MCIS Id (generated ID by the Name)
Policy []Policy `json:"policy"`
ActionLog string `json:"actionLog"`
Description string `json:"description" example:"Description"`
}
// McisPolicyReq is struct for MCIS auto-control Policy Request.
type McisPolicyReq struct {
Policy []Policy `json:"policy"`
Description string `json:"description" example:"Description"`
}
// OrchestrationController is responsible for executing MCIS automation policy.
// OrchestrationController will be periodically involked by a time.NewTicker in main.go.
func OrchestrationController() {
nsList, err := common.ListNsId()
if err != nil {
common.CBLog.Error(err)
err = fmt.Errorf("an error occurred while getting namespaces' list: " + err.Error())
return
}
for _, nsId := range nsList {
mcisPolicyList := ListMcisPolicyId(nsId)
for _, m := range mcisPolicyList {
fmt.Println("NS[" + nsId + "]" + "McisPolicy[" + m + "]")
}
for _, v := range mcisPolicyList {
key := common.GenMcisPolicyKey(nsId, v, "")
keyValue, err := common.CBStore.Get(key)
if err != nil {
common.CBLog.Error(err)
err = fmt.Errorf("In OrchestrationController(); CBStore.Get() returned an error.")
common.CBLog.Error(err)
// return nil, err
}
if keyValue == nil {
fmt.Println("keyValue is nil")
}
mcisPolicyTmp := McisPolicyInfo{}
json.Unmarshal([]byte(keyValue.Value), &mcisPolicyTmp)
/* FYI
const AutoStatusReady string = "Ready"
const AutoStatusChecking string = "Checking"
const AutoStatusHappened string = "Happened"
const AutoStatusOperating string = "Operating"
const AutoStatusTimeout string = "Timeout"
const AutoStatusError string = "Error"
const AutoStatusSuspend string = "Suspend"
*/
for policyIndex := range mcisPolicyTmp.Policy {
fmt.Println("\n[MCIS-Policy-StateMachine]")
common.PrintJsonPretty(mcisPolicyTmp.Policy[policyIndex])
switch {
case mcisPolicyTmp.Policy[policyIndex].Status == AutoStatusReady:
fmt.Println("- PolicyStatus[" + AutoStatusReady + "],[" + v + "]")
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusChecking
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
fmt.Println("[Check MCIS Policy] " + mcisPolicyTmp.Id)
check, err := CheckMcis(nsId, mcisPolicyTmp.Id)
fmt.Println("[Check existence of MCIS] " + mcisPolicyTmp.Id)
//keyValueMcis, _ := common.CBStore.Get(common.GenMcisKey(nsId, mcisPolicyTmp.Id, ""))
if !check || err != nil {
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
fmt.Println("[MCIS is not exist] " + mcisPolicyTmp.Id)
break
} else { // need to enhance : loop for each policies and realize metric
//Checking (measuring)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusChecking
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
fmt.Println("[MCIS is exist] " + mcisPolicyTmp.Id)
content, err := GetMonitoringData(nsId, mcisPolicyTmp.Id, mcisPolicyTmp.Policy[policyIndex].AutoCondition.Metric)
if err != nil {
common.CBLog.Error(err)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
break
}
//Statistic
sumMcis := 0.0
for _, monData := range content.McisMonitoring {
monDataValue, _ := strconv.ParseFloat(monData.Value, 64)
sumMcis += monDataValue
}
averMcis := (sumMcis / float64(len(content.McisMonitoring)))
fmt.Printf("[monData.Value] AverMcis: %f, SumMcis: %f \n", averMcis, sumMcis)
evaluationPeriod, _ := strconv.Atoi(mcisPolicyTmp.Policy[policyIndex].AutoCondition.EvaluationPeriod)
evaluationValue := mcisPolicyTmp.Policy[policyIndex].AutoCondition.EvaluationValue
evaluationValue = append([]string{fmt.Sprintf("%f", averMcis)}, evaluationValue...) // prepend current aver date
mcisPolicyTmp.Policy[policyIndex].AutoCondition.EvaluationValue = evaluationValue
sum := 0.0
aver := -0.1
// accumerate previous evaluation value
fmt.Printf("[Evaluation History]\n")
for evi, evv := range evaluationValue {
evvFloat, _ := strconv.ParseFloat(evv, 64)
sum += evvFloat
fmt.Printf("[%v] %f ", evi, evvFloat)
// break with outside evaluationValue
if evi >= evaluationPeriod-1 {
break
}
}
// average for evaluationPeriod (if data for the period is not enough, skip)
if evaluationPeriod != 0 && len(evaluationValue) >= evaluationPeriod {
aver = sum / float64(evaluationPeriod)
}
fmt.Printf("\n[Evaluation] Aver: %f, Period: %v \n", aver, evaluationPeriod)
//Detecting
operator := mcisPolicyTmp.Policy[policyIndex].AutoCondition.Operator
operand, _ := strconv.ParseFloat(mcisPolicyTmp.Policy[policyIndex].AutoCondition.Operand, 64)
if evaluationPeriod == 0 {
fmt.Println("[Checking] Not available evaluationPeriod ")
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
break
}
// not enough evaluationPeriod
if aver == -0.1 {
fmt.Println("[Checking] Not enough evaluationPeriod ")
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusReady
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
break
}
switch {
case operator == ">=":
if aver >= operand {
fmt.Printf("[Detected] Aver: %f >= Operand: %f \n", aver, operand)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusDetected
} else {
fmt.Printf("[Not Detected] Aver: %f >= Operand: %f \n", aver, operand)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusReady
}
case operator == ">":
if aver > operand {
fmt.Printf("[Detected] Aver: %f > Operand: %f \n", aver, operand)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusDetected
} else {
fmt.Printf("[Not Detected] Aver: %f > Operand: %f \n", aver, operand)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusReady
}
case operator == "<=":
if aver <= operand {
fmt.Printf("[Detected] Aver: %f <= Operand: %f \n", aver, operand)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusDetected
} else {
fmt.Printf("[Not Detected] Aver: %f <= Operand: %f \n", aver, operand)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusReady
}
case operator == "<":
if aver < operand {
fmt.Printf("[Detected] Aver: %f < Operand: %f \n", aver, operand)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusDetected
} else {
fmt.Printf("[Not Detected] Aver: %f < Operand: %f \n", aver, operand)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusReady
}
default:
fmt.Println("[Checking] Not available operator " + operator)
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
}
}
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
case mcisPolicyTmp.Policy[policyIndex].Status == AutoStatusChecking:
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
//mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusDetected
case mcisPolicyTmp.Policy[policyIndex].Status == AutoStatusDetected:
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusOperating
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
//Action
/*
// Actions for mcis automation
const AutoActionScaleOut string = "ScaleOut"
const AutoActionScaleIn string = "ScaleIn"
*/
autoAction := mcisPolicyTmp.Policy[policyIndex].AutoAction
fmt.Println("[autoAction] " + autoAction.ActionType)
switch {
case autoAction.ActionType == AutoActionScaleOut:
autoAction.VmDynamicReq.Label = labelAutoGen
// append UUID to given vm name to avoid duplicated vm ID.
autoAction.VmDynamicReq.Name = common.ToLower(autoAction.VmDynamicReq.Name) + "-" + common.GenUid()
//vmReqTmp := autoAction.Vm
// autoAction.VmDynamicReq.SubGroupSize = "1"
if autoAction.PlacementAlgo == "random" {
fmt.Println("[autoAction.PlacementAlgo] " + autoAction.PlacementAlgo)
// var vmTmpErr error
// existingVm, vmTmpErr := GetVmTemplate(nsId, mcisPolicyTmp.Id, autoAction.PlacementAlgo)
// if vmTmpErr != nil {
// mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
// UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
// }
autoAction.VmDynamicReq.CommonImage = "ubuntu18.04" // temporal default value. will be changed
autoAction.VmDynamicReq.CommonSpec = "aws-ap-northeast-2-t2-small" // temporal default value. will be changed
deploymentPlan := DeploymentPlan{}
deploymentPlan.Priority.Policy = append(deploymentPlan.Priority.Policy, PriorityCondition{Metric: "random"})
specList, err := RecommendVm(common.SystemCommonNs, deploymentPlan)
if err != nil {
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
}
if len(specList) != 0 {
recommendedSpec := specList[0].Id
autoAction.VmDynamicReq.CommonSpec = recommendedSpec
}
// autoAction.VmDynamicReq.Name = autoAction.VmDynamicReq.Name + "-random"
// autoAction.VmDynamicReq.Label = labelAutoGen
}
common.PrintJsonPretty(autoAction.VmDynamicReq)
fmt.Println("[Action] " + autoAction.ActionType)
// ScaleOut MCIS according to the VM requirement.
fmt.Println("[Generating VM]")
result, vmCreateErr := CreateMcisVmDynamic(nsId, mcisPolicyTmp.Id, &autoAction.VmDynamicReq)
if vmCreateErr != nil {
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
}
common.PrintJsonPretty(result)
nullMcisCmdReq := McisCmdReq{}
if autoAction.PostCommand != nullMcisCmdReq {
fmt.Println("[Post Command to VM] " + autoAction.PostCommand.Command)
_, cmdErr := RemoteCommandToMcis(nsId, mcisPolicyTmp.Id, common.ToLower(autoAction.VmDynamicReq.Name), &autoAction.PostCommand)
if cmdErr != nil {
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
}
}
case autoAction.ActionType == AutoActionScaleIn:
fmt.Println("[Action] " + autoAction.ActionType)
// ScaleIn MCIS.
fmt.Println("[Removing VM]")
vmList, vmListErr := GetVmListByLabel(nsId, mcisPolicyTmp.Id, labelAutoGen)
if vmListErr != nil {
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
}
if len(vmList) != 0 {
removeTargetVm := vmList[len(vmList)-1]
fmt.Println("[Removing VM ID] " + removeTargetVm)
delVmErr := DelMcisVm(nsId, mcisPolicyTmp.Id, removeTargetVm, "")
if delVmErr != nil {
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusError
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
}
}
default:
}
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusStabilizing
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
case mcisPolicyTmp.Policy[policyIndex].Status == AutoStatusStabilizing:
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
//initialize Evaluation history so that controller does not act too early.
//with this we can stablize MCIS by init previously measures.
//Will invoke [Checking] Not enough evaluationPeriod
mcisPolicyTmp.Policy[policyIndex].AutoCondition.EvaluationValue = nil
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusReady
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
case mcisPolicyTmp.Policy[policyIndex].Status == AutoStatusOperating:
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
//mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusReady
//UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
case mcisPolicyTmp.Policy[policyIndex].Status == AutoStatusTimeout:
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
case mcisPolicyTmp.Policy[policyIndex].Status == AutoStatusError:
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
mcisPolicyTmp.Policy[policyIndex].Status = AutoStatusReady
UpdateMcisPolicyInfo(nsId, mcisPolicyTmp)
case mcisPolicyTmp.Policy[policyIndex].Status == AutoStatusSuspended:
fmt.Println("- PolicyStatus[" + mcisPolicyTmp.Policy[policyIndex].Status + "],[" + v + "]")
default:
}
}
}
}
}
// UpdateMcisPolicyInfo updates McisPolicyInfo object in DB.
func UpdateMcisPolicyInfo(nsId string, mcisPolicyInfoData McisPolicyInfo) {
key := common.GenMcisPolicyKey(nsId, mcisPolicyInfoData.Id, "")
val, _ := json.Marshal(mcisPolicyInfoData)
err := common.CBStore.Put(key, string(val))
if err != nil && !strings.Contains(err.Error(), common.CbStoreKeyNotFoundErrorString) {
common.CBLog.Error(err)
}
}
// CreateMcisPolicy create McisPolicyInfo object in DB according to user's requirements.
func CreateMcisPolicy(nsId string, mcisId string, u *McisPolicyReq) (McisPolicyInfo, error) {
err := common.CheckString(nsId)
if err != nil {
temp := McisPolicyInfo{}
common.CBLog.Error(err)
return temp, err
}
err = common.CheckString(mcisId)
if err != nil {
temp := McisPolicyInfo{}
common.CBLog.Error(err)
return temp, err
}
check, _ := CheckMcisPolicy(nsId, mcisId)
//u.Status = AutoStatusReady
if check {
temp := McisPolicyInfo{}
err := fmt.Errorf("The MCIS Policy Obj " + mcisId + " already exists.")
return temp, err
}
for policyIndex := range u.Policy {
u.Policy[policyIndex].Status = AutoStatusReady
}
req := *u
obj := McisPolicyInfo{}
obj.Name = mcisId
obj.Id = mcisId
obj.Policy = req.Policy
obj.Description = req.Description
// cb-store
Key := common.GenMcisPolicyKey(nsId, obj.Id, "")
Val, _ := json.Marshal(obj)
err = common.CBStore.Put(Key, string(Val))
if err != nil {
common.CBLog.Error(err)
return obj, err
}
keyValue, err := common.CBStore.Get(Key)
if err != nil {
common.CBLog.Error(err)
err = fmt.Errorf("In CreateMcisPolicy(); CBStore.Get() returned an error.")
common.CBLog.Error(err)
// return nil, err
}
fmt.Println("<KEY>\n" + keyValue.Key + "\n<VAL>\n" + keyValue.Value)
return obj, nil
}
// GetMcisPolicyObject returns McisPolicyInfo object.
func GetMcisPolicyObject(nsId string, mcisId string) (McisPolicyInfo, error) {
fmt.Println("[GetMcisPolicyObject]" + mcisId)
err := common.CheckString(nsId)
if err != nil {
temp := McisPolicyInfo{}
common.CBLog.Error(err)
return temp, err
}
err = common.CheckString(mcisId)
if err != nil {
temp := McisPolicyInfo{}
common.CBLog.Error(err)
return temp, err
}
key := common.GenMcisPolicyKey(nsId, mcisId, "")
fmt.Println("Key: ", key)
keyValue, err := common.CBStore.Get(key)
if err != nil {
common.CBLog.Error(err)
return McisPolicyInfo{}, err
}
if keyValue == nil {
return McisPolicyInfo{}, err
}
fmt.Println("<KEY>\n" + keyValue.Key + "\n<VAL>\n" + keyValue.Value)
mcisPolicyTmp := McisPolicyInfo{}
json.Unmarshal([]byte(keyValue.Value), &mcisPolicyTmp)
return mcisPolicyTmp, nil
}
// GetAllMcisPolicyObject returns all McisPolicyInfo objects.
func GetAllMcisPolicyObject(nsId string) ([]McisPolicyInfo, error) {
err := common.CheckString(nsId)
if err != nil {
common.CBLog.Error(err)
return nil, err
}
Mcis := []McisPolicyInfo{}
mcisList := ListMcisPolicyId(nsId)
for _, v := range mcisList {
key := common.GenMcisPolicyKey(nsId, v, "")
keyValue, err := common.CBStore.Get(key)
if err != nil {
common.CBLog.Error(err)
err = fmt.Errorf("In GetAllMcisPolicyObject(); CBStore.Get() returned an error.")
common.CBLog.Error(err)
// return nil, err
}
if keyValue == nil {
return nil, fmt.Errorf("Cannot find " + key)
}
mcisTmp := McisPolicyInfo{}
json.Unmarshal([]byte(keyValue.Value), &mcisTmp)
Mcis = append(Mcis, mcisTmp)
}
return Mcis, nil
}
// ListMcisPolicyId returns a list of Ids for all McisPolicyInfo objects .
func ListMcisPolicyId(nsId string) []string {
err := common.CheckString(nsId)
if err != nil {
common.CBLog.Error(err)
return nil
}
key := "/ns/" + nsId + "/policy/mcis"
keyValue, err := common.CBStore.GetList(key, true)
if err != nil {
common.CBLog.Error(err)
err = fmt.Errorf("In ListMcisPolicyId(); CBStore.Get() returned an error.")
common.CBLog.Error(err)
// return nil, err
}
var mcisList []string
for _, v := range keyValue {
if !strings.Contains(v.Key, "vm") {
mcisList = append(mcisList, strings.TrimPrefix(v.Key, "/ns/"+nsId+"/policy/mcis/"))
}
}
return mcisList
}
// DelMcisPolicy deletes McisPolicyInfo object by mcisId.
func DelMcisPolicy(nsId string, mcisId string) error {
err := common.CheckString(nsId)
if err != nil {
common.CBLog.Error(err)
return err
}
err = common.CheckString(mcisId)
if err != nil {
common.CBLog.Error(err)
return err
}
check, _ := CheckMcisPolicy(nsId, mcisId)
if !check {
err := fmt.Errorf("The mcis Policy" + mcisId + " does not exist.")
return err
}
fmt.Println("[Delete MCIS Policy] " + mcisId)
key := common.GenMcisPolicyKey(nsId, mcisId, "")
fmt.Println(key)
// delete mcis Policy info
err = common.CBStore.Delete(key)
if err != nil {
common.CBLog.Error(err)
return err
}
return nil
}
// DelAllMcisPolicy deletes all McisPolicyInfo objects.
func DelAllMcisPolicy(nsId string) (string, error) {
err := common.CheckString(nsId)
if err != nil {
common.CBLog.Error(err)
return "", err
}
mcisList := ListMcisPolicyId(nsId)
if len(mcisList) == 0 {
return "No MCIS Policy to delete", nil
}
for _, v := range mcisList {
err := DelMcisPolicy(nsId, v)
if err != nil {
common.CBLog.Error(err)
return "", fmt.Errorf("Failed to delete All MCIS Policies")
}
}
return "All MCIS Policies has been deleted", nil
}