-
Notifications
You must be signed in to change notification settings - Fork 44
/
CCMRest.go
2648 lines (2081 loc) · 72.1 KB
/
CCMRest.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Cloud Control Manager's Rest Runtime of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2020.04.
// by CB-Spider Team, 2019.10.
package restruntime
import (
cmrt "github.com/cloud-barista/cb-spider/api-runtime/common-runtime"
cres "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
// REST API (echo)
"net/http"
"net/url"
"github.com/labstack/echo/v4"
"strconv"
)
// define string of resource types
const (
rsImage string = "image"
rsVPC string = "vpc"
rsSubnet string = "subnet"
rsSG string = "sg"
rsKey string = "keypair"
rsVM string = "vm"
rsNLB string = "nlb"
rsDisk string = "disk"
)
//================ Image Handler
func CreateImage(c echo.Context) error {
cblog.Info("call CreateImage()")
var req struct {
ConnectionName string
ReqInfo struct {
Name string
}
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
reqInfo := cres.ImageReqInfo{
IId: cres.IID{req.ReqInfo.Name, ""},
}
// Call common-runtime API
result, err := cmrt.CreateImage(req.ConnectionName, rsImage, reqInfo)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
func ListImage(c echo.Context) error {
cblog.Info("call ListImage()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.ListImage(req.ConnectionName, rsImage)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
var jsonResult struct {
Result []*cres.ImageInfo `json:"image"`
}
jsonResult.Result = result
return c.JSON(http.StatusOK, &jsonResult)
}
func GetImage(c echo.Context) error {
cblog.Info("call GetImage()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
encodededImageName := c.Param("Name")
decodedImageName, err := url.QueryUnescape(encodededImageName)
if err != nil {
cblog.Fatal(err)
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
result, err := cmrt.GetImage(req.ConnectionName, rsImage, decodedImageName)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
func DeleteImage(c echo.Context) error {
cblog.Info("call DeleteImage()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, err := cmrt.DeleteImage(req.ConnectionName, rsImage, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
//================ VMSpec Handler
func ListVMSpec(c echo.Context) error {
cblog.Info("call ListVMSpec()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.ListVMSpec(req.ConnectionName)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
var jsonResult struct {
Result []*cres.VMSpecInfo `json:"vmspec"`
}
jsonResult.Result = result
return c.JSON(http.StatusOK, &jsonResult)
}
func GetVMSpec(c echo.Context) error {
cblog.Info("call GetVMSpec()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.GetVMSpec(req.ConnectionName, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
func ListOrgVMSpec(c echo.Context) error {
cblog.Info("call ListOrgVMSpec()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.ListOrgVMSpec(req.ConnectionName)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, result)
}
func GetOrgVMSpec(c echo.Context) error {
cblog.Info("call GetOrgVMSpec()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.GetOrgVMSpec(req.ConnectionName, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, result)
}
type vpcRegisterReq struct {
ConnectionName string
ReqInfo struct {
Name string
CSPId string
}
}
func RegisterVPC(c echo.Context) error {
cblog.Info("call RegisterVPC()")
req := vpcRegisterReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// create UserIID
userIId := cres.IID{req.ReqInfo.Name, req.ReqInfo.CSPId}
// Call common-runtime API
result, err := cmrt.RegisterVPC(req.ConnectionName, userIId)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func UnregisterVPC(c echo.Context) error {
cblog.Info("call UnregisterVPC()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, err := cmrt.UnregisterResource(req.ConnectionName, rsVPC, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
type vpcCreateReq struct {
ConnectionName string
ReqInfo struct {
Name string
IPv4_CIDR string
SubnetInfoList []struct {
Name string
IPv4_CIDR string
}
}
}
// createVPC godoc
// @Summary Create VPC
// @Description Create VPC
// @Tags [CCM] VPC management
// @Accept json
// @Produce json
// @Param vpcCreateReq body vpcCreateReq true "Request body to create VPC"
// @Success 200 {object} resources.VPCInfo
// @Failure 404 {object} SimpleMsg
// @Failure 500 {object} SimpleMsg
// @Router /vpc [post]
func CreateVPC(c echo.Context) error {
cblog.Info("call CreateVPC()")
req := vpcCreateReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Rest RegInfo => Driver ReqInfo
// (1) create SubnetInfo List
subnetInfoList := []cres.SubnetInfo{}
for _, info := range req.ReqInfo.SubnetInfoList {
subnetInfo := cres.SubnetInfo{IId: cres.IID{info.Name, ""}, IPv4_CIDR: info.IPv4_CIDR}
subnetInfoList = append(subnetInfoList, subnetInfo)
}
// (2) create VPCReqInfo with SubnetInfo List
reqInfo := cres.VPCReqInfo{
IId: cres.IID{req.ReqInfo.Name, ""},
IPv4_CIDR: req.ReqInfo.IPv4_CIDR,
SubnetInfoList: subnetInfoList,
}
// Call common-runtime API
result, err := cmrt.CreateVPC(req.ConnectionName, rsVPC, reqInfo)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
func ListVPC(c echo.Context) error {
cblog.Info("call ListVPC()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.ListVPC(req.ConnectionName, rsVPC)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
var jsonResult struct {
Result []*cres.VPCInfo `json:"vpc"`
}
jsonResult.Result = result
return c.JSON(http.StatusOK, &jsonResult)
}
// list all VPCs for management
// (1) get args from REST Call
// (2) get all VPC List by common-runtime API
// (3) return REST Json Format
func ListAllVPC(c echo.Context) error {
cblog.Info("call ListAllVPC()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
allResourceList, err := cmrt.ListAllResource(req.ConnectionName, rsVPC)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, &allResourceList)
}
func GetVPC(c echo.Context) error {
cblog.Info("call GetVPC()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.GetVPC(req.ConnectionName, rsVPC, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func DeleteVPC(c echo.Context) error {
cblog.Info("call DeleteVPC()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, _, err := cmrt.DeleteResource(req.ConnectionName, rsVPC, c.Param("Name"), c.QueryParam("force"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func DeleteCSPVPC(c echo.Context) error {
cblog.Info("call DeleteCSPVPC()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, _, err := cmrt.DeleteCSPResource(req.ConnectionName, rsVPC, c.Param("Id"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
// (1) get subnet info from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func AddSubnet(c echo.Context) error {
cblog.Info("call AddSubnet()")
var req struct {
ConnectionName string
ReqInfo struct {
Name string
IPv4_CIDR string
}
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Rest RegInfo => Driver ReqInfo
reqSubnetInfo := cres.SubnetInfo{IId: cres.IID{req.ReqInfo.Name, ""}, IPv4_CIDR: req.ReqInfo.IPv4_CIDR}
// Call common-runtime API
result, err := cmrt.AddSubnet(req.ConnectionName, rsSubnet, c.Param("VPCName"), reqSubnetInfo)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func RemoveSubnet(c echo.Context) error {
cblog.Info("call RemoveSubnet()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, err := cmrt.RemoveSubnet(req.ConnectionName, c.Param("VPCName"), c.Param("SubnetName"), c.QueryParam("force"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func RemoveCSPSubnet(c echo.Context) error {
cblog.Info("call RemoveCSPSubnet()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, err := cmrt.RemoveCSPSubnet(req.ConnectionName, c.Param("VPCName"), c.Param("Id"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
func GetSGOwnerVPC(c echo.Context) error {
cblog.Info("call GetSGOwnerVPC()")
var req struct {
ConnectionName string
ReqInfo struct {
CSPId string
}
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, err := cmrt.GetSGOwnerVPC(req.ConnectionName, req.ReqInfo.CSPId)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
type securityGroupRegisterReq struct {
ConnectionName string
ReqInfo struct {
VPCName string
Name string
CSPId string
}
}
func RegisterSecurity(c echo.Context) error {
cblog.Info("call RegisterSecurity()")
req := securityGroupRegisterReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// create UserIID
userIId := cres.IID{req.ReqInfo.Name, req.ReqInfo.CSPId}
// Call common-runtime API
result, err := cmrt.RegisterSecurity(req.ConnectionName, req.ReqInfo.VPCName, userIId)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func UnregisterSecurity(c echo.Context) error {
cblog.Info("call UnregisterSecurity()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, err := cmrt.UnregisterResource(req.ConnectionName, rsSG, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
type securityGroupCreateReq struct {
ConnectionName string
ReqInfo struct {
Name string
VPCName string
Direction string
SecurityRules *[]cres.SecurityRuleInfo
}
}
/* // createSecurity godoc
// @Summary Create Security Group
// @Description Create Security Group
// @Tags [CCM] Security Group management
// @Accept json
// @Produce json
// @Param securityGroupCreateReq body securityGroupCreateReq true "Request body to create Security Group"
// @Success 200 {object} resources.SecurityInfo
// @Failure 404 {object} SimpleMsg
// @Failure 500 {object} SimpleMsg
// @Router /securitygroup [post] */
func CreateSecurity(c echo.Context) error {
cblog.Info("call CreateSecurity()")
req := securityGroupCreateReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Rest RegInfo => Driver ReqInfo
reqInfo := cres.SecurityReqInfo{
//IId: cres.IID{req.ReqInfo.VPCName + cm.SG_DELIMITER + req.ReqInfo.Name, ""},
IId: cres.IID{req.ReqInfo.Name, req.ReqInfo.Name}, // for NCP: fixed NameID => SystemID, Driver: (1)search systemID with fixed NameID (2)replace fixed NameID into SysemID
VpcIID: cres.IID{req.ReqInfo.VPCName, ""},
// deprecated; Direction: req.ReqInfo.Direction,
SecurityRules: req.ReqInfo.SecurityRules,
}
// Call common-runtime API
result, err := cmrt.CreateSecurity(req.ConnectionName, rsSG, reqInfo)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
func ListSecurity(c echo.Context) error {
cblog.Info("call ListSecurity()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.ListSecurity(req.ConnectionName, rsSG)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
var jsonResult struct {
Result []*cres.SecurityInfo `json:"securitygroup"`
}
jsonResult.Result = result
return c.JSON(http.StatusOK, &jsonResult)
}
// list all SGs for management
// (1) get args from REST Call
// (2) get all SG List by common-runtime API
// (3) return REST Json Format
func ListAllSecurity(c echo.Context) error {
cblog.Info("call ListAllSecurity()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
allResourceList, err := cmrt.ListAllResource(req.ConnectionName, rsSG)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, &allResourceList)
}
func GetSecurity(c echo.Context) error {
cblog.Info("call GetSecurity()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// To support for Get-Query Param Type API
if req.ConnectionName == "" {
req.ConnectionName = c.QueryParam("ConnectionName")
}
// Call common-runtime API
result, err := cmrt.GetSecurity(req.ConnectionName, rsSG, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func DeleteSecurity(c echo.Context) error {
cblog.Info("call DeleteSecurity()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, _, err := cmrt.DeleteResource(req.ConnectionName, rsSG, c.Param("Name"), c.QueryParam("force"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func DeleteCSPSecurity(c echo.Context) error {
cblog.Info("call DeleteCSPSecurity()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, _, err := cmrt.DeleteCSPResource(req.ConnectionName, rsSG, c.Param("Id"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
type ruleControlReq struct {
ConnectionName string
ReqInfo struct {
RuleInfoList []struct {
Direction string
IPProtocol string
FromPort string
ToPort string
CIDR string
}
}
}
// (1) get rules info from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func AddRules(c echo.Context) error {
cblog.Info("call AddRules()")
req := ruleControlReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Rest RegInfo => Driver ReqInfo
// create RuleInfo List
reqRuleInfoList := []cres.SecurityRuleInfo{}
for _, info := range req.ReqInfo.RuleInfoList {
ruleInfo := cres.SecurityRuleInfo{Direction: info.Direction,
IPProtocol: info.IPProtocol, FromPort: info.FromPort, ToPort: info.ToPort, CIDR: info.CIDR}
reqRuleInfoList = append(reqRuleInfoList, ruleInfo)
}
// Call common-runtime API
result, err := cmrt.AddRules(req.ConnectionName, c.Param("SGName"), reqRuleInfoList)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
// (1) get rules info from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func RemoveRules(c echo.Context) error {
cblog.Info("call RemoveRules()")
req := ruleControlReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Rest RegInfo => Driver ReqInfo
// create RuleInfo List
reqRuleInfoList := []cres.SecurityRuleInfo{}
for _, info := range req.ReqInfo.RuleInfoList {
ruleInfo := cres.SecurityRuleInfo{Direction: info.Direction,
IPProtocol: info.IPProtocol, FromPort: info.FromPort, ToPort: info.ToPort, CIDR: info.CIDR}
reqRuleInfoList = append(reqRuleInfoList, ruleInfo)
}
// Call common-runtime API
// no force option
result, err := cmrt.RemoveRules(req.ConnectionName, c.Param("SGName"), reqRuleInfoList)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
type keyRegisterReq struct {
ConnectionName string
ReqInfo struct {
Name string
CSPId string
}
}
func RegisterKey(c echo.Context) error {
cblog.Info("call RegisterKey()")
req := keyRegisterReq{}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// create UserIID
userIId := cres.IID{req.ReqInfo.Name, req.ReqInfo.CSPId}
// Call common-runtime API
result, err := cmrt.RegisterKey(req.ConnectionName, userIId)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, result)
}
// (1) get args from REST Call
// (2) call common-runtime API
// (3) return REST Json Format
func UnregisterKey(c echo.Context) error {
cblog.Info("call UnregisterKey()")
var req struct {
ConnectionName string
}
if err := c.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
// Call common-runtime API
result, err := cmrt.UnregisterResource(req.ConnectionName, rsKey, c.Param("Name"))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
resultInfo := BooleanInfo{
Result: strconv.FormatBool(result),
}
return c.JSON(http.StatusOK, &resultInfo)
}
// type keyPairCreateReq struct {
// ConnectionName string
// ReqInfo struct {
// Name string
// }
// }
// JSONResult's data field will be overridden by the specific type
type JSONResult struct {
//Code int `json:"code" `
//Message string `json:"message"`