-
Notifications
You must be signed in to change notification settings - Fork 45
/
CommonHandler.go
767 lines (661 loc) · 22.9 KB
/
CommonHandler.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
// 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.
//
// program by ysjeon@mz.co.kr, 2019.07.
// modify by devunet@mz.co.kr, 2019.11.
package resources
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"sync"
"time"
cblog "github.com/cloud-barista/cb-log"
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"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
compute "google.golang.org/api/compute/v1"
container "google.golang.org/api/container/v1"
)
const (
CBVMUser = "cscservice"
//CBKeyPairPath = "/cloud-control-manager/cloud-driver/driver-libs/.ssh-gcp/"
// by powerkim, 2019.10.30
CBKeyPairPath = "/meta_db/.ssh-gcp/"
CBKeyPairProvider = "GCP"
)
const CBDefaultVNetName string = "cb-vnet" // CB Default Virtual Network Name
const CBDefaultSubnetName string = "cb-vnet" // CB Default Subnet Name
const KEY_VALUE_CONVERT_DEBUG_INFO bool = false
const OperationGlobal = 1
const OperationRegion = 2
const OperationZone = 3
type GcpCBNetworkInfo struct {
VpcName string
VpcId string
CidrBlock string
IsDefault bool
State string
SubnetName string
SubnetId string
}
var once sync.Once
var cblogger *logrus.Logger
var calllogger *logrus.Logger
func InitLog() {
once.Do(func() {
// cblog is a global variable.
cblogger = cblog.GetLogger("CB-SPIDER")
calllogger = call.GetLogger("HISCALL")
})
}
func LoggingError(hiscallInfo call.CLOUDLOGSCHEMA, err error) {
hiscallInfo.ErrorMSG = err.Error()
calllogger.Error(call.String(hiscallInfo))
}
func LoggingInfo(hiscallInfo call.CLOUDLOGSCHEMA, start time.Time) {
hiscallInfo.ElapsedTime = call.Elapsed(start)
calllogger.Info(call.String(hiscallInfo))
}
func GetCallLogScheme(region idrv.RegionInfo, resourceType call.RES_TYPE, resourceName string, apiName string) call.CLOUDLOGSCHEMA {
cblogger.Info(fmt.Sprintf("Call %s %s", call.GCP, apiName))
return call.CLOUDLOGSCHEMA{
CloudOS: call.GCP,
RegionZone: region.Region,
ResourceType: resourceType,
ResourceName: resourceName,
CloudOSAPI: apiName,
}
}
// VPC
func GetCBDefaultVNetName() string {
return CBDefaultVNetName
}
// Subnet
func GetCBDefaultSubnetName() string {
return CBDefaultSubnetName
}
// KeyValue gen func
func GetKeyValueList(i map[string]interface{}) []irs.KeyValue {
var keyValueList []irs.KeyValue
for k, v := range i {
//cblogger.Infof("K:[%s]====>", k)
_, ok := v.(string)
if !ok {
cblogger.Errorf("Key[%s]의 값은 변환 불가", k)
continue
}
//if strings.EqualFold(k, "users") {
// continue
//}
//cblogger.Infof("====>", v)
keyValueList = append(keyValueList, irs.KeyValue{k, v.(string)})
cblogger.Info("getKeyValueList : ", keyValueList)
}
return keyValueList
}
// Cloud Object를 CB-KeyValue 형식으로 변환이 필요할 경우 이용
func ConvertKeyValueList(v interface{}) ([]irs.KeyValue, error) {
//spew.Dump(v)
var keyValueList []irs.KeyValue
var i map[string]interface{}
jsonBytes, errJson := json.Marshal(v)
if errJson != nil {
cblogger.Error("KeyValue 변환 실패")
cblogger.Error(errJson)
return nil, errJson
}
json.Unmarshal(jsonBytes, &i)
for k, v := range i {
if KEY_VALUE_CONVERT_DEBUG_INFO {
cblogger.Debugf("K:[%s]====>", k)
}
/*
cblogger.Infof("v:[%s]====>", reflect.ValueOf(v))
vv := reflect.ValueOf(v)
cblogger.Infof("value ====>[%s]", vv.String())
s := fmt.Sprint(v)
cblogger.Infof("value2 ====>[%s]", s)
*/
//value := fmt.Sprint(v)
value, errString := ConvertToString(v)
if errString != nil {
//cblogger.Debugf("Key[%s]의 값은 변환 불가 - [%s]", k, errString) //요구에 의해서 Error에서 Warn으로 낮춤
continue
}
keyValueList = append(keyValueList, irs.KeyValue{k, value})
/*
_, ok := v.(string)
if !ok {
cblogger.Errorf("Key[%s]의 값은 변환 불가", k)
continue
}
keyValueList = append(keyValueList, irs.KeyValue{k, v.(string)})
*/
}
cblogger.Debug("getKeyValueList : ", keyValueList)
//keyValueList = append(keyValueList, irs.KeyValue{"test", typeToString([]float32{3.14, 1.53, 2.0000000000000})})
return keyValueList, nil
}
// CB-KeyValue 등을 위해 String 타입으로 변환
func ConvertToString(value interface{}) (string, error) {
if value == nil {
if KEY_VALUE_CONVERT_DEBUG_INFO {
cblogger.Debugf("Nil Value")
}
return "", errors.New("Nil. Value")
}
var result string
t := reflect.ValueOf(value)
if KEY_VALUE_CONVERT_DEBUG_INFO {
cblogger.Debug("==>ValueOf : ", t)
}
switch value.(type) {
case float32:
result = strconv.FormatFloat(t.Float(), 'f', -1, 32) // f, fmt, prec, bitSize
case float64:
result = strconv.FormatFloat(t.Float(), 'f', -1, 64) // f, fmt, prec, bitSize
//strconv.FormatFloat(instanceTypeInfo.MemorySize, 'f', 0, 64)
default:
if KEY_VALUE_CONVERT_DEBUG_INFO {
cblogger.Debug("--> default type:", reflect.ValueOf(value).Type())
}
result = fmt.Sprint(value)
}
return result, nil
}
// KeyPair 해시 생성 함수
func CreateHashString(credentialInfo idrv.CredentialInfo) (string, error) {
keyString := credentialInfo.ClientId + credentialInfo.ClientSecret + credentialInfo.TenantId + credentialInfo.SubscriptionId
hasher := md5.New()
_, err := io.WriteString(hasher, keyString)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}
// Public KeyPair 정보 가져오기
func GetPublicKey(credentialInfo idrv.CredentialInfo, keyPairName string) (string, error) {
keyPairPath := os.Getenv("CBSPIDER_ROOT") + CBKeyPairPath
hashString, err := CreateHashString(credentialInfo)
if err != nil {
return "", err
}
publicKeyPath := keyPairPath + hashString + "--" + keyPairName + ".pub"
publicKeyBytes, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
return "", err
}
return string(publicKeyBytes), nil
}
// InstanceGroup의 인스턴스 목록 return
func GetInstancesOfInstanceGroup(client *compute.Service, credential idrv.CredentialInfo, region idrv.RegionInfo, instanceGroup string) ([]string, error) {
projectID := credential.ProjectID
zone := region.Zone
rb := &compute.InstanceGroupsListInstancesRequest{
// TODO: Add desired fields of the request body.
}
instanceGroupsListInstances, err := client.InstanceGroups.ListInstances(projectID, zone, instanceGroup, rb).Do()
if err != nil {
cblogger.Error(err)
return nil, err
}
//cblogger.Info("instanceGroupsListInstances : ", instanceGroupsListInstances)
var instanceList []string
for _, instance := range instanceGroupsListInstances.Items {
instanceUrl := instance.Instance
urlArr := strings.Split(instanceUrl, "/")
instanceName := urlArr[len(urlArr)-1]
instanceList = append(instanceList, instanceName)
}
//cblogger.Info("instanceList : ", instanceList)
return instanceList, nil
}
// Instance 정보조회
func GetInstance(client *compute.Service, credential idrv.CredentialInfo, region idrv.RegionInfo, instance string) (*compute.Instance, error) {
projectID := credential.ProjectID
zone := region.Zone
instanceInfo, err := client.Instances.Get(projectID, zone, instance).Do()
if err != nil {
return nil, err
}
return instanceInfo, nil
}
// Operation 이 완료 될 때까지 기다림.
func WaitUntilComplete(client *compute.Service, project string, region string, resourceId string, isGlobalAction bool) error {
before_time := time.Now()
max_time := 300 //최대 300초간 체크
var opSatus *compute.Operation
var err error
for {
if isGlobalAction {
opSatus, err = client.GlobalOperations.Get(project, resourceId).Do()
} else {
opSatus, err = client.RegionOperations.Get(project, region, resourceId).Do()
}
if err != nil {
cblogger.Infof("WaitUntilComplete / [%s]", err)
return err
}
cblogger.Infof("==> 상태 : 진행율 : [%d] / [%s]", opSatus.Progress, opSatus.Status)
//PENDING, RUNNING, or DONE.
if (opSatus.Status == "RUNNING" || opSatus.Status == "DONE") && opSatus.Progress >= 100 {
//if opSatus.Status == "RUNNING" || opSatus.Status == "DONE" {
//if opSatus.Status == "DONE" {
cblogger.Info("Wait을 종료합니다.", resourceId, ":", opSatus.Status)
return nil
}
time.Sleep(time.Second * 1)
after_time := time.Now()
diff := after_time.Sub(before_time)
if int(diff.Seconds()) > max_time {
cblogger.Errorf("[%d]초 동안 리소스[%s]의 상태가 완료되지 않아서 Wait을 강제로 종료함.", max_time, resourceId)
return errors.New("장시간 요청 작업이 완료되지 않아서 Wait을 강제로 종료함.)")
}
}
return nil
}
func WaitOperationComplete(client *compute.Service, project string, region string, zone string, resourceId string, operationType int) error {
before_time := time.Now()
max_time := 300 //최대 300초간 체크
var opSatus *compute.Operation
var err error
for {
switch operationType {
case OperationGlobal:
opSatus, err = client.GlobalOperations.Get(project, resourceId).Do()
case OperationRegion:
opSatus, err = client.RegionOperations.Get(project, region, resourceId).Do()
case OperationZone:
opSatus, err = client.ZoneOperations.Get(project, zone, resourceId).Do()
}
if err != nil {
cblogger.Infof("WaitUntilOperationComplete / [%s]", err)
return err
}
cblogger.Infof("==> 상태 : 진행율 : [%d] / [%s]", opSatus.Progress, opSatus.Status)
//PENDING, RUNNING, or DONE.
if (opSatus.Status == "RUNNING" || opSatus.Status == "DONE") && opSatus.Progress >= 100 {
//if opSatus.Status == "RUNNING" || opSatus.Status == "DONE" {
//if opSatus.Status == "DONE" {
cblogger.Info("Wait을 종료합니다.", resourceId, ":", opSatus.Status)
return nil
}
time.Sleep(time.Second * 1)
after_time := time.Now()
diff := after_time.Sub(before_time)
if int(diff.Seconds()) > max_time {
cblogger.Errorf("[%d]초 동안 리소스[%s]의 상태가 완료되지 않아서 Wait을 강제로 종료함.", max_time, resourceId)
return errors.New("장시간 요청 작업이 완료되지 않아서 Wait을 강제로 종료함.)")
}
}
return nil
}
// Get 공통으로 사용
func GetDiskInfo(client *compute.Service, credential idrv.CredentialInfo, region idrv.RegionInfo, diskName string) (*compute.Disk, error) {
projectID := credential.ProjectID
zone := region.Zone
targetZone := region.TargetZone
// 대상 zone이 다른경우 targetZone을 사용
if targetZone != ""{
zone = targetZone
}
diskResp, err := client.Disks.Get(projectID, zone, diskName).Do()
if err != nil {
cblogger.Error(err)
return &compute.Disk{}, err
}
return diskResp, nil
}
func GetMachineImageInfo(client *compute.Service, projectId string, imageName string) (*compute.MachineImage, error) {
cblogger.Infof("projectId : [%s] / imageName : [%s]", projectId, imageName)
imageResp, err := client.MachineImages.Get(projectId, imageName).Do()
if err != nil {
cblogger.Error(err)
return &compute.MachineImage{}, err
}
if imageResp == nil {
return nil, errors.New("Not Found : [" + imageName + "] Image information not found")
}
// cblogger.Infof("result ", imageResp)
// spew.Dump(imageResp)
return imageResp, nil
}
// IID 에서 systemID로 image 조회. : systemID가 URL로 되어있어 필요한 값들을 추출하여 사용. projectId, imageName
func GetPublicImageInfo(client *compute.Service, imageIID irs.IID) (*compute.Image, error) {
projectId := ""
imageName := ""
arrLink := strings.Split(imageIID.SystemId, "/")
if len(arrLink) > 0 {
imageName = arrLink[len(arrLink)-1]
for pos, item := range arrLink {
if strings.EqualFold(item, "projects") {
projectId = arrLink[pos+1]
break
}
}
}
cblogger.Infof("projectId : [%s] / imageName : [%s]", projectId, imageName)
if projectId == "" {
return nil, errors.New("ProjectId information not found in URL.")
}
image, err := client.Images.Get(projectId, imageName).Do()
if err != nil {
cblogger.Error(err)
return nil, err
}
return image, nil
}
// IID 에서 systemID로 image 조회.
func FindImageByID(client *compute.Service, imageIID irs.IID) (*compute.Image, error) {
reqImageName := imageIID.SystemId
//https://cloud.google.com/compute/docs/images?hl=ko
arrImageProjectList := []string{
//"ubuntu-os-cloud",
"gce-uefi-images", // 보안 VM을 지원하는 이미지
//보안 VM을 지원하지 않는 이미지들
"centos-cloud",
"cos-cloud",
"coreos-cloud",
"debian-cloud",
"rhel-cloud",
"rhel-sap-cloud",
"suse-cloud",
"suse-sap-cloud",
"ubuntu-os-cloud",
"windows-cloud",
"windows-sql-cloud",
}
cnt := 0
nextPageToken := ""
var req *compute.ImagesListCall
var res *compute.ImageList
var err error
for _, projectId := range arrImageProjectList {
req = client.Images.List(projectId)
//req.Filter("name=" + reqImageName)
//req.Filter("SelfLink=" + reqImageName)
res, err = req.Do()
if err != nil {
cblogger.Errorf("[%s] 프로젝트 소유의 이미지 목록 조회 실패!", projectId)
cblogger.Error(err)
return nil, err
}
nextPageToken = res.NextPageToken
cblogger.Info("NestPageToken : ", nextPageToken)
for {
cblogger.Info("Loop?")
for _, item := range res.Items {
cnt++
if strings.EqualFold(reqImageName, item.SelfLink) {
cblogger.Info("found Image : ", item)
return item, nil
}
cblogger.Info("cnt : ", item)
}
}
}
return nil, errors.New("Not Found : [" + reqImageName + "] Image information not found")
}
// container 의 operation
func WaitContainerOperationComplete(client *container.Service, project string, region string, zone string, resourceId string, operationType int) error {
before_time := time.Now()
max_time := 300 //최대 300초간 체크
var opSatus *container.Operation
var err error
operationName := "projects/" + project + "/locations/" + zone + "/operations/" + resourceId
for {
opSatus, err = client.Projects.Locations.Operations.Get(operationName).Do()
if err != nil {
cblogger.Infof("WaitUntilOperationComplete / [%s]", err)
return err
}
cblogger.Infof("==> 상태 : 진행율 : [%d] / [%s]", opSatus.Progress, opSatus.Status)
//PENDING, RUNNING, or DONE.
// STATUS_UNSPECIFIED Not set.
// PENDING The operation has been created.
// RUNNING The operation is currently running.
// DONE The operation is done, either cancelled or completed.
// ABORTING The operation is aborting.
if opSatus.Status == "DONE" {
cblogger.Info("Wait을 종료합니다.", resourceId, ":", opSatus.Status)
return nil
}
time.Sleep(time.Second * 1)
after_time := time.Now()
diff := after_time.Sub(before_time)
if int(diff.Seconds()) > max_time {
cblogger.Errorf("[%d]초 동안 리소스[%s]의 상태가 완료되지 않아서 Wait을 강제로 종료함.", max_time, resourceId)
return errors.New("장시간 요청 작업이 완료되지 않아서 Wait을 강제로 종료함.)")
}
}
return nil
}
// 30초동안 Fail 이 떨어지지 않으면 성공
func WaitContainerOperationFail(client *container.Service, project string, region string, zone string, resourceId string, operationType int) error {
before_time := time.Now()
max_time := 30
var opSatus *container.Operation
var err error
operationName := "projects/" + project + "/locations/" + zone + "/operations/" + resourceId
for {
opSatus, err = client.Projects.Locations.Operations.Get(operationName).Do()
if err != nil {
cblogger.Infof("WaitContainerOperationFail / [%s]", err)
return err
}
spew.Dump(opSatus)
cblogger.Infof("==> 상태 : 진행율 : [%d] / [%s]", opSatus.Progress, opSatus.Status)
//PENDING, RUNNING, or DONE.
// STATUS_UNSPECIFIED Not set.
// PENDING The operation has been created.
// RUNNING The operation is currently running.
// DONE The operation is done, either cancelled or completed.
// ABORTING The operation is aborting.
if opSatus.Status == "ABORTING" {
cblogger.Info("Wait을 종료합니다.", resourceId, ":", opSatus.Status)
return nil
}
time.Sleep(time.Second * 5)
after_time := time.Now()
diff := after_time.Sub(before_time)
if int(diff.Seconds()) > max_time {
cblogger.Errorf("[%d]초 동안 리소스[%s]의 상태가 완료되지 않아서 Wait을 강제로 종료함.", max_time, resourceId)
return nil
}
}
return nil
}
// 20분
func WaitContainerOperationDone(client *container.Service, project string, region string, zone string, resourceId string, operationType int, maxTime int) error {
before_time := time.Now()
var opSatus *container.Operation
var err error
operationName := "projects/" + project + "/locations/" + zone + "/operations/" + resourceId
for {
opSatus, err = client.Projects.Locations.Operations.Get(operationName).Do()
if err != nil {
cblogger.Infof("WaitContainerOperationDone / [%s]", err)
return err
}
spew.Dump(opSatus)
cblogger.Infof("==> 상태 : 진행율 : [%d] / [%s]", opSatus.Progress, opSatus.Status)
//PENDING, RUNNING, or DONE.
// STATUS_UNSPECIFIED Not set.
// PENDING The operation has been created.
// RUNNING The operation is currently running.
// DONE The operation is done, either cancelled or completed.
// ABORTING The operation is aborting.
if opSatus.Status == "DONE" {
cblogger.Info("Wait을 종료합니다.", resourceId, ":", opSatus.Status)
return nil
}
time.Sleep(time.Second * 5)
after_time := time.Now()
diff := after_time.Sub(before_time)
if int(diff.Seconds()) > maxTime {
cblogger.Errorf("[%d]초 동안 리소스[%s]의 상태가 완료되지 않아서 Wait을 강제로 종료함.", maxTime, resourceId)
return nil
}
}
return nil
}
// 리전 목록 조회
func ListRegion(client *compute.Service, projectId string) (*compute.RegionList, error) {
if projectId == "" {
return nil, errors.New("ProjectId not found.")
}
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.GCP,
RegionZone: "",
ResourceType: call.REGIONZONE,
ResourceName: "",
CloudOSAPI: "List()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
resp, err := client.Regions.List(projectId).Do()
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Error(err)
return nil, err
}
return resp, nil
}
// region 조회
// GCP에서 region은 regionName과 regionUri로 구분 됨. regionName으로 찾는 function임.
func GetRegion(client *compute.Service, projectId string, regionName string) (*compute.Region, error) {
if projectId == "" {
return nil, errors.New("ProjectId not found.")
}
if regionName == "" {
return nil, errors.New("Region Name not found.")
}
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.GCP,
RegionZone: regionName,
ResourceType: call.REGIONZONE,
ResourceName: "",
CloudOSAPI: "Get()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
resp, err := client.Regions.Get(projectId, regionName).Do()
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Error(err)
return nil, err
}
return resp, nil
}
// region에 해당하는 zone 목록 조회
// filter조건으로 사용하는 region조건은 regionUrl로 넘겨야 함.
// filter조건 자체가 string이며 regionUrl에 특수문자가 있고 따옴표로 감싸야만 결과가 정상적으로 나옴
// region="xxx/xxx/xxx" 형태로 보내야하며
// ` ` 로 감싸야 함.
// filter := "region=https://www.googleapis.com/compute/v1/projects/xxx/regions/us-east1" -> error return.
// filter := `region="https://www.googleapis.com/compute/v1/projects/xxx/regions/us-east1"` -> 조회결과 옴
// filter := `region="us-east1"`// -> 조회결과 없음
func GetZoneListByRegion(client *compute.Service, projectId string, regionUrl string) (*compute.ZoneList, error) {
if projectId == "" {
return nil, errors.New("Project information not found")
}
if regionUrl == "" {
return nil, errors.New("Region information not found")
}
filter := `region="` + regionUrl + `"`
resp, err := client.Zones.List(projectId).Filter(filter).Do()
if err != nil {
cblogger.Error(err)
return nil, err
}
// spew.Dump(resp)
return resp, nil
}
// Available or Unavailable 로 return
// Status of the zone, either UP or DOWN. (지원하지 않는 경우 NotSupported)
func GetZoneStatus(status string) irs.ZoneStatus {
if status == "UP" {
return irs.ZoneAvailable
} else {
return irs.ZoneUnavailable
}
}
/*
### container operation ###
(*container.Operation)(0xc0003d6a00)({
ClusterConditions: ([]*container.StatusCondition) <nil>,
Detail: (string) "",
EndTime: (string) "",
Error: (*container.Status)(<nil>),
Location: (string) "",
Name: (string) (len=32) "operation-1670486783109-b7af5968",
NodepoolConditions: ([]*container.StatusCondition) <nil>,
OperationType: (string) (len=14) "CREATE_CLUSTER",
Progress: (*container.OperationProgress)(<nil>),
SelfLink: (string) (len=125) "https://container.googleapis.com/v1/projects/244703045150/zones/asia-northeast3-a/operations/operation-1670486783109-b7af5968",
StartTime: (string) (len=30) "2022-12-08T08:06:23.109241982Z",
Status: (string) (len=7) "RUNNING",
StatusMessage: (string) "",
TargetLink: (string) (len=97) "https://container.googleapis.com/v1/projects/244703045150/zones/asia-northeast3-a/clusters/pmks08",
Zone: (string) (len=17) "asia-northeast3-a",
ServerResponse: (googleapi.ServerResponse) {
HTTPStatusCode: (int) 200,
Header: (http.Header) (len=9) {
(string) (len=12) "Content-Type": ([]string) (len=1 cap=1) {
(string) (len=31) "application/json; charset=UTF-8"
},
(string) (len=4) "Vary": ([]string) (len=3 cap=4) {
(string) (len=6) "Origin",
(string) (len=8) "X-Origin",
(string) (len=7) "Referer"
},
(string) (len=4) "Date": ([]string) (len=1 cap=1) {
(string) (len=29) "Thu, 08 Dec 2022 08:06:23 GMT"
},
(string) (len=15) "X-Frame-Options": ([]string) (len=1 cap=1) {
(string) (len=10) "SAMEORIGIN"
},
(string) (len=22) "X-Content-Type-Options": ([]string) (len=1 cap=1) {
(string) (len=7) "nosniff"
},
(string) (len=7) "Alt-Svc": ([]string) (len=1 cap=1) {
(string) (len=162) "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\""
},
(string) (len=6) "Server": ([]string) (len=1 cap=1) {
(string) (len=3) "ESF"
},
(string) (len=13) "Cache-Control": ([]string) (len=1 cap=1) {
(string) (len=7) "private"
},
(string) (len=16) "X-Xss-Protection": ([]string) (len=1 cap=1) {
(string) (len=1) "0"
}
}
},
ForceSendFields: ([]string) <nil>,
NullFields: ([]string) <nil>
})
*/