-
Notifications
You must be signed in to change notification settings - Fork 44
/
VPCHandler.go
663 lines (575 loc) · 22 KB
/
VPCHandler.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
// Proof of Concepts 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
//
// This is a Cloud Driver Example for PoC Test.
//
// by ETRI 2022.08.
package resources
import (
// "errors"
"fmt"
"strings"
"time"
// "github.com/davecgh/go-spew/spew"
ktvpcsdk "github.com/cloud-barista/ktcloudvpc-sdk-go"
external "github.com/cloud-barista/ktcloudvpc-sdk-go/openstack/networking/v2/extensions/external"
networks "github.com/cloud-barista/ktcloudvpc-sdk-go/openstack/networking/v2/networks"
subnets "github.com/cloud-barista/ktcloudvpc-sdk-go/openstack/networking/v2/subnets"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
type KTVpcVPCHandler struct {
RegionInfo idrv.RegionInfo
NetworkClient *ktvpcsdk.ServiceClient
}
type NetworkWithExt struct {
networks.Network
external.NetworkExternalExt
}
func (vpcHandler *KTVpcVPCHandler) CreateVPC(vpcReqInfo irs.VPCReqInfo) (irs.VPCInfo, error) {
cblogger.Info("KT Cloud VPC Driver: called CreateVPC()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, vpcReqInfo.IId.NameId, "CreateVPC()")
if strings.EqualFold(vpcReqInfo.IId.NameId, "") {
newErr := fmt.Errorf("Invalid VPC NameId!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
// KT Cloud (D1) VPC API doc. : https://cloud.kt.com/docs/open-api-guide/d/computing/networking
// KT Cloud (D1) Tier API doc. : https://cloud.kt.com/docs/open-api-guide/d/computing/tier
start := call.Start()
listOpts := networks.ListOpts{}
firstPage, err := networks.List(vpcHandler.NetworkClient, listOpts).FirstPage() // Caution!! : First Page Only
if err != nil {
cblogger.Errorf("Failed to Create KT Cloud VPC : [%v]", err)
loggingError(callLogInfo, err)
return irs.VPCInfo{}, err
}
loggingInfo(callLogInfo, start)
vpcList, err := networks.ExtractVPCs(firstPage)
if err != nil {
newErr := fmt.Errorf("Failed to Get KT Cloud VPC Network list. [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
if len(vpcList) < 1 {
newErr := fmt.Errorf("Failed to Get Any VPC Network Info.")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
if strings.EqualFold(vpcList[0].ID, "") {
cblogger.Error("Failed to Create the Required VPC!!")
return irs.VPCInfo{}, nil
}
// Create the Requested Subnets
for _, subnetReqInfo := range vpcReqInfo.SubnetInfoList {
_, err := vpcHandler.AddSubnet(irs.IID{SystemId: vpcList[0].ID}, subnetReqInfo)
if err != nil {
newErr := fmt.Errorf("Failed to Create New Subnet : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr // Caution!!) D1 Platform Abnormal Error
}
}
vpcInfo, getErr := vpcHandler.GetVPC(irs.IID{SystemId: vpcList[0].ID})
if getErr != nil {
newErr := fmt.Errorf("Failed to Get the VPC Info : [%v]", getErr)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
} else {
vpcInfo.IId.NameId = vpcReqInfo.IId.NameId // Caution!! For IID2 NameID validation check for VPC
}
return vpcInfo, nil
}
func (vpcHandler *KTVpcVPCHandler) GetVPC(vpcIID irs.IID) (irs.VPCInfo, error) {
cblogger.Info("KT Cloud VPC Driver: called GetVPC()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, vpcIID.SystemId, "GetVPC()")
if strings.EqualFold(vpcIID.SystemId, "") {
newErr := fmt.Errorf("Invalid VPC SystemId!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
start := call.Start()
listOpts := networks.ListOpts{}
firstPage, err := networks.List(vpcHandler.NetworkClient, listOpts).FirstPage() // Caution!! : First Page Only
if err != nil {
cblogger.Errorf("Failed to Get VPC Network info from KT Cloud VPC : [%v]", err)
loggingError(callLogInfo, err)
return irs.VPCInfo{}, err
}
loggingInfo(callLogInfo, start)
vpcList, err := networks.ExtractVPCs(firstPage)
if err != nil {
newErr := fmt.Errorf("Failed to Get KT Cloud VPC Network list. [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
if len(vpcList) < 1 {
newErr := fmt.Errorf("Failed to Get Any VPC Network Info.")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
var vpcInfo *irs.VPCInfo
for _, vpc := range vpcList {
if strings.EqualFold(vpcIID.SystemId, vpc.ID) {
var mapErr error
vpcInfo, mapErr = vpcHandler.mappingVpcInfo(&vpc)
if mapErr != nil {
newErr := fmt.Errorf("Failed to Map the VPC Info : [%v]", mapErr)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
break
}
}
return *vpcInfo, nil
}
func (vpcHandler *KTVpcVPCHandler) ListVPC() ([]*irs.VPCInfo, error) {
cblogger.Info("KT Cloud VPC Driver: called ListVPC()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, "ListVPC()", "ListVPC()")
// KT Cloud (D1) VPC API guide : https://cloud.kt.com/docs/open-api-guide/d/computing/networking
// KT Cloud (D1) Tier API guide : https://cloud.kt.com/docs/open-api-guide/d/computing/tier
listOpts := networks.ListOpts{}
firstPage, err := networks.List(vpcHandler.NetworkClient, listOpts).FirstPage() // Caution!! : First Page Only
if err != nil {
cblogger.Errorf("Failed to Get VPC Network info from KT Cloud VPC : [%v]", err)
loggingError(callLogInfo, err)
return nil, err
}
vpcList, err := networks.ExtractVPCs(firstPage)
if err != nil {
newErr := fmt.Errorf("Failed to Get KT Cloud VPC Network list. [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return nil, newErr
}
if len(vpcList) < 1 {
newErr := fmt.Errorf("Failed to Get Any VPC Network Info.")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return nil, newErr
}
var vpcInfoList []*irs.VPCInfo
for _, vpc := range vpcList {
vpcInfo, err := vpcHandler.mappingVpcInfo(&vpc)
if err != nil {
newErr := fmt.Errorf("Failed to Map the VPC Info : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return nil, newErr
}
vpcInfoList = append(vpcInfoList, vpcInfo)
}
return vpcInfoList, nil
}
func (vpcHandler *KTVpcVPCHandler) DeleteVPC(vpcIID irs.IID) (bool, error) {
cblogger.Info("KT Cloud VPC Driver: called DeleteVPC()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, vpcIID.SystemId, "DeleteVPC()")
if strings.EqualFold(vpcIID.SystemId, "") {
newErr := fmt.Errorf("Invalid VPC SystemId!!")
cblogger.Error(newErr.Error())
return false, newErr
}
// Check whether the VPC exists.
vpcInfo, err := vpcHandler.GetVPC(vpcIID)
if err != nil {
cblogger.Errorf("Failed to Find the VPC with the SystemID. : [%s] : [%v]", vpcIID.SystemId, err)
loggingError(callLogInfo, err)
return false, err
}
// Delete the Subnets belonged in the VPC
for _, subnetInfo := range vpcInfo.SubnetInfoList {
if !strings.EqualFold(subnetInfo.IId.NameId, "Private") && !strings.EqualFold(subnetInfo.IId.NameId, "DMZ") && !strings.EqualFold(subnetInfo.IId.NameId, "external") && !strings.EqualFold(subnetInfo.IId.NameId, "NLB-SUBNET") {
_, err := vpcHandler.RemoveSubnet(irs.IID{SystemId: vpcIID.SystemId}, irs.IID{SystemId: subnetInfo.IId.SystemId})
if (err != nil) && !strings.Contains(err.Error(), ":true") { // Cauton!! : Abnormal Error when removing a subnet on D1 Platform
newErr := fmt.Errorf("Failed to Delete the Subnet : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return false, newErr
}
}
}
// Since KT Cloud VPC(D Platform) supports Single VPC
result, err := vpcHandler.GetVPC(vpcIID)
if err != nil {
cblogger.Errorf("Failed to Find the VPC with the SystemID. : [%s] : [%v]", vpcIID.SystemId, err)
loggingError(callLogInfo, err)
return false, err
} else {
cblogger.Infof("Succeeded in Deleting the VPC : " + result.IId.SystemId)
}
return true, nil
}
func (vpcHandler *KTVpcVPCHandler) GetSubnet(subnetIID irs.IID) (irs.SubnetInfo, error) {
cblogger.Info("KT Cloud VPC Driver: called GetSubnet()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, subnetIID.SystemId, "GetSubnet()")
if strings.EqualFold(subnetIID.SystemId, "") {
newErr := fmt.Errorf("Invalid Subnet SystemId!!")
cblogger.Error(newErr.Error())
return irs.SubnetInfo{}, newErr
}
subnet, err := subnets.Get(vpcHandler.NetworkClient, subnetIID.SystemId).Extract()
if err != nil {
cblogger.Errorf("Failed to Get Subnet with SystemId [%s] : %v", subnetIID.SystemId, err)
loggingError(callLogInfo, err)
return irs.SubnetInfo{}, nil
}
subnetInfo := vpcHandler.mappingSubnetInfo(*subnet)
return *subnetInfo, nil
}
func (vpcHandler *KTVpcVPCHandler) AddSubnet(vpcIID irs.IID, subnetReqInfo irs.SubnetInfo) (irs.VPCInfo, error) {
cblogger.Info("KT Cloud VPC driver: called AddSubnet()!!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, subnetReqInfo.IId.NameId, "AddSubnet()")
if subnetReqInfo.IId.NameId == "" {
newErr := fmt.Errorf("Invalid Sunbet NameId!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
// KT Cloud D1 platform API guide - Tier : https://cloud.kt.com/docs/open-api-guide/d/computing/tier
cidrBlock := strings.Split(subnetReqInfo.IPv4_CIDR, ".")
startIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "11"
endIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "140"
lbStartIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "141"
lbEndIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "200"
bmStartIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "201"
bmEndIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "250"
gatewayIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "1"
detailTierInfo := subnets.DetailInfo {
CIDR: subnetReqInfo.IPv4_CIDR,
StartIP: startIP,
EndIP: endIP,
LBStartIP: lbStartIP,
LBEndIP: lbEndIP,
BMStartIP: bmStartIP,
BMEndIP: bmEndIP,
Gateway: gatewayIP,
}
// Create Subnet
createOpts := subnets.CreateOpts{
Name: subnetReqInfo.IId.NameId, // Mandatory (Required)
Zone: vpcHandler.RegionInfo.Zone, // Mandatory (Required)
Type: "tier", // Mandatory (Required)
UserCustom: "y", // Mandatory (Required)
Detail: detailTierInfo,
}
// cblogger.Info("\n### Subnet createOpts : ")
// spew.Dump(createOpts)
// cblogger.Info("\n")
cblogger.Info("\n### Adding New Subnet Now!!")
start := call.Start()
_, err := subnets.Create(vpcHandler.NetworkClient, createOpts).Extract()
// subnet, err := subnets.Create(vpcHandler.NetworkClient, createOpts).Extract()
if err != nil {
if !strings.Contains(err.Error(), ":true") { // Cauton!! : Abnormal Error when creating a subnet on D1 Platform
newErr := fmt.Errorf("Failed to Add the Subnet on KTCoud : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
} else {
cblogger.Info("\n### Waiting for Adding the Subnet!!")
time.Sleep(time.Second * 15)
// cblogger.Infof("Succeeded in Adding the Subnet : [%s]", subnet.ID) // To prevent 'panic: runtime error', maded this line as a comment.
}
loggingInfo(callLogInfo, start)
// subnetInfo := vpcHandler.setterSubnet(*subnet)
// return *subnetInfo, nil
vpcInfo, err := vpcHandler.GetVPC(irs.IID{SystemId: vpcIID.SystemId})
if err != nil {
newErr := fmt.Errorf("Failed to Get the VPC Info!! : [%v] ", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
return vpcInfo, nil
}
func (vpcHandler *KTVpcVPCHandler) RemoveSubnet(vpcIID irs.IID, subnetIID irs.IID) (bool, error) {
cblogger.Info("KT Cloud VPC driver: called RemoveSubnet()!!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, subnetIID.SystemId, "RemoveSubnet()")
if strings.EqualFold(subnetIID.SystemId, "") {
newErr := fmt.Errorf("Invalid Subnet SystemId!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return false, newErr
}
err := subnets.Delete(vpcHandler.NetworkClient, subnetIID.SystemId).ExtractErr()
if err != nil {
newErr := fmt.Errorf("Failed to Remove the Subnet from KT Cloud : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return false, newErr
}
cblogger.Info("\n### Waiting for Deleting the Subnet!!")
time.Sleep(time.Second * 5)
return true, nil
}
func (vpcHandler *KTVpcVPCHandler) mappingVpcInfo(nvpc *networks.Network) (*irs.VPCInfo, error) {
cblogger.Info("KT Cloud VPC driver: called mappingVpcInfo()!!")
// cblogger.Info("\n### KTCloud VPC")
// spew.Dump(nvpc)
if strings.EqualFold(nvpc.ID, "") {
newErr := fmt.Errorf("Invalid VPC Info!!")
cblogger.Error(newErr.Error())
return nil, newErr
}
// Mapping VPC info.
vpcInfo := irs.VPCInfo {
IId: irs.IID{
NameId: nvpc.Name,
SystemId: nvpc.ID,
},
IPv4_CIDR: "172.25.0.0/12", // VPC CIDR of KT Cloud D1 Platform Default VPC
}
keyValueList := []irs.KeyValue{
{Key: "VpcID", Value: nvpc.ID},
{Key: "ZoneId", Value: nvpc.ZoneID},
// {Key: "CreatedTime", Value: nvpc.CreatedAt.String()},
}
vpcInfo.KeyValueList = keyValueList
// Get Subnet info list.
var subnetInfoList []irs.SubnetInfo
for _, subnet := range nvpc.Subnets {
// if !strings.EqualFold(subnet.Name, "Private_Sub") && !strings.EqualFold(subnet.Name, "DMZ_Sub") && !strings.EqualFold(subnet.Name, "external"){
// # When apply filtering
subnetInfo := vpcHandler.mappingSubnetInfo(subnet)
subnetInfoList = append(subnetInfoList, *subnetInfo)
// }
}
vpcInfo.SubnetInfoList = subnetInfoList
return &vpcInfo, nil
}
func (vpcHandler *KTVpcVPCHandler) mappingSubnetInfo(subnet subnets.Subnet) *irs.SubnetInfo {
cblogger.Info("KT Cloud VPC driver: called mappingSubnetInfo()!!")
// To remove "_Sub" in the subnet name (Note. "_Sub" is appended to a subnet name in the KT Cloud)
// Removing "_Sub" for CB-Spdier IID manager
subnetName := strings.Split(subnet.Name, "_Sub")
newName := subnetName[0]
subnetInfo := irs.SubnetInfo{
IId: irs.IID{
NameId: newName,
SystemId: subnet.OsNetworkID, // Caution!! Not 'ID' but 'OsNetworkID' to Create VM!!
},
Zone: subnet.ZoneID,
IPv4_CIDR: subnet.CIDR,
}
keyValueList := []irs.KeyValue{
{Key: "Type", Value: subnet.Type},
{Key: "StartIP", Value: subnet.StartIP},
{Key: "EndIP", Value: subnet.EndIP},
{Key: "Gateway", Value: subnet.Gateway},
{Key: "TierUUID", Value: subnet.ID}, // Tier 'ID' on KT Cloud D platform Consol
}
subnetInfo.KeyValueList = keyValueList
return &subnetInfo
}
func (vpcHandler *KTVpcVPCHandler) getKtCloudVpc(vpcId string) (*networks.Network, error) {
cblogger.Info("KT Cloud VPC Driver: called getKtCloudVpc()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, vpcId, "getKtCloudVpc()")
if strings.EqualFold(vpcId, "") {
newErr := fmt.Errorf("Invalid VPC ID!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return nil, newErr
}
start := call.Start()
listOpts := networks.ListOpts{}
firstPage, err := networks.List(vpcHandler.NetworkClient, listOpts).FirstPage() // Caution!! : First Page Only
if err != nil {
cblogger.Errorf("Failed to Get VPC Network info from KT Cloud VPC : [%v]", err)
loggingError(callLogInfo, err)
return nil, err
}
loggingInfo(callLogInfo, start)
vpcList, err := networks.ExtractVPCs(firstPage)
if err != nil {
newErr := fmt.Errorf("Failed to Get KT Cloud VPC Network list. [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return nil, newErr
}
if len(vpcList) < 1 {
newErr := fmt.Errorf("Failed to Get Any VPC Network Info.")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return nil, newErr
}
var ktVpc *networks.Network
for _, vpc := range vpcList {
if strings.EqualFold(vpcId, vpc.ID) {
ktVpc = &vpc
break
}
}
return ktVpc, nil
}
func (vpcHandler *KTVpcVPCHandler) getExtSubnetId(vpcIID irs.IID) (string, error) {
cblogger.Info("KT Cloud VPC Driver: called getExtSubnetId()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, vpcIID.SystemId, "getExtSubnetId()")
if strings.EqualFold(vpcIID.SystemId, "") {
newErr := fmt.Errorf("Invalid VPC SystemId!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
}
ktVpc, err := vpcHandler.getKtCloudVpc(vpcIID.SystemId)
if err != nil {
cblogger.Errorf("Failed to Get the VPC Info from KT Cloud. : [%v]", err)
loggingError(callLogInfo, err)
return "", err
}
var extSubnetId string
for _, subnet := range ktVpc.Subnets {
if strings.EqualFold(subnet.Name, "external"){
extSubnetId = subnet.ID
break
}
}
if strings.EqualFold(extSubnetId, "") {
newErr := fmt.Errorf("Failed to Find the External Subnet ID!!")
cblogger.Error(newErr.Error())
return "", newErr
}
return extSubnetId, nil
}
func (vpcHandler *KTVpcVPCHandler) getOsNetworkIdWithTierId(vpcId string, tierId string) (string, error) {
cblogger.Info("KT Cloud VPC Driver: called getOsNetworkIdWithTierId()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, tierId, "getOsNetworkIdWithTierId()")
if strings.EqualFold(vpcId, "") {
newErr := fmt.Errorf("Invalid VPC ID!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
}
if strings.EqualFold(tierId, "") {
newErr := fmt.Errorf("Invalid Subnet(Tier) ID!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
}
ktVpc, err := vpcHandler.getKtCloudVpc(vpcId)
if err != nil {
cblogger.Errorf("Failed to Get the VPC Info from KT Cloud. [%v]", err)
loggingError(callLogInfo, err)
return "", err
}
var osNetworkId string
for _, subnet := range ktVpc.Subnets {
if strings.EqualFold(subnet.ID, tierId){
osNetworkId = subnet.OsNetworkID
break
}
}
if strings.EqualFold(osNetworkId, "") {
newErr := fmt.Errorf("Failed to Find the OsNetwork ID!!")
cblogger.Error(newErr.Error())
return "", newErr
}
return osNetworkId, nil
}
func (vpcHandler *KTVpcVPCHandler) getOsNetworkIdWithTierName(tierName string) (string, error) {
cblogger.Info("KT Cloud VPC Driver: called getOsNetworkIdWithTierName()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, tierName, "getOsNetworkIdWithTierName()")
if strings.EqualFold(tierName, "") {
newErr := fmt.Errorf("Invalid Subnet(Tier) Name!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
}
cblogger.Infof("# Subnet(Tier) Name to Find OsNetwork ID : %s", tierName)
start := call.Start()
listOpts := networks.ListOpts{}
firstPage, err := networks.List(vpcHandler.NetworkClient, listOpts).FirstPage() // Caution!! : First Page Only
if err != nil {
cblogger.Errorf("Failed to Get VPC Network info from KT Cloud VPC : [%v]", err)
loggingError(callLogInfo, err)
return "", err
}
loggingInfo(callLogInfo, start)
vpcList, err := networks.ExtractVPCs(firstPage)
if err != nil {
newErr := fmt.Errorf("Failed to Get KT Cloud VPC Network list. [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
}
if len(vpcList) < 1 {
newErr := fmt.Errorf("Failed to Get Any VPC Network Info.")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
}
var osNetworkId string
for _, vpc := range vpcList {
for _, subnet := range vpc.Subnets {
// To remove "_Sub" in the subnet name (Note. "_Sub" is appended to a subnet name in the KT Cloud)
subnetName := strings.Split(subnet.Name, "_Sub") // Caution!!
if strings.EqualFold(subnetName[0], tierName){
osNetworkId = subnet.OsNetworkID
break
}
}
}
if strings.EqualFold(osNetworkId, "") {
newErr := fmt.Errorf("Failed to Find the OsNetwork ID!!")
cblogger.Error(newErr.Error())
return "", newErr
}
return osNetworkId, nil
}
// func (vpcHandler *KTVpcVPCHandler) getVpcIdWithName(vpcName string) (string, error) {
// cblogger.Info("KT Cloud VPC Driver: called getVpcIdWithName()!")
// callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, vpcName, "getVpcIdWithName()")
// if strings.EqualFold(vpcName, "") {
// newErr := fmt.Errorf("Invalid VPC ID!!")
// cblogger.Error(newErr.Error())
// loggingError(callLogInfo, newErr)
// return "", newErr
// }
// start := call.Start()
// listOpts := networks.ListOpts{}
// firstPage, err := networks.List(vpcHandler.NetworkClient, listOpts).FirstPage() // Caution!! : First Page Only
// if err != nil {
// cblogger.Errorf("Failed to Get VPC Network info from KT Cloud VPC : [%v]", err)
// loggingError(callLogInfo, err)
// return "", err
// }
// loggingInfo(callLogInfo, start)
// vpcList, err := networks.ExtractVPCs(firstPage)
// if err != nil {
// newErr := fmt.Errorf("Failed to Get KT Cloud VPC Network list. [%v]", err)
// cblogger.Error(newErr.Error())
// loggingError(callLogInfo, newErr)
// return "", newErr
// }
// if len(vpcList) < 1 {
// newErr := fmt.Errorf("Failed to Get Any VPC Network Info.")
// cblogger.Error(newErr.Error())
// loggingError(callLogInfo, newErr)
// return "", newErr
// }
// var vpcId string
// for _, vpc := range vpcList {
// if strings.EqualFold(vpc.Name, vpcName) {
// vpcId = vpc.ID
// break
// }
// }
// if strings.EqualFold(vpcId, "") {
// newErr := fmt.Errorf("Failed to Find the VPC ID!!")
// cblogger.Error(newErr.Error())
// return "", newErr
// }
// return vpcId, nil
// }