1111package resources
1212
1313import (
14- // "errors"
1514 "errors"
1615 "fmt"
1716 // "io/ioutil"
1817 // "os"
1918 "strconv"
2019 "strings"
2120 "time"
22-
2321 // "github.com/davecgh/go-spew/spew"
2422
2523 "github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud"
@@ -31,18 +29,18 @@ import (
3129)
3230
3331const (
34- DefaultDiskSize string = "10"
32+ DefaultDiskSize string = "10"
3533)
3634
3735type NcpVpcDiskHandler struct {
38- RegionInfo idrv.RegionInfo
39- VMClient * vserver.APIClient
36+ RegionInfo idrv.RegionInfo
37+ VMClient * vserver.APIClient
4038}
4139
4240// Caution : Incase of NCP VPC, there must be a created VM to create a new disk volume.
4341func (diskHandler * NcpVpcDiskHandler ) CreateDisk (diskReqInfo irs.DiskInfo ) (irs.DiskInfo , error ) {
44- cblogger .Info ("NCP VPC Driver: called CreateDisk()" )
45- InitLog () // Caution!!
42+ cblogger .Info ("NCP VPC Driver: called CreateDisk()" )
43+ InitLog () // Caution!!
4644 callLogInfo := GetCallLogScheme (diskHandler .RegionInfo .Region , call .DISK , diskReqInfo .IId .NameId , "CreateDisk()" ) // HisCall logging
4745
4846 if strings .EqualFold (diskReqInfo .IId .NameId , "" ) {
@@ -51,7 +49,7 @@ func (diskHandler *NcpVpcDiskHandler) CreateDisk(diskReqInfo irs.DiskInfo) (irs.
5149 LoggingError (callLogInfo , newErr )
5250 return irs.DiskInfo {}, newErr
5351 }
54-
52+
5553 // To get created VM info.
5654 instanceList , err := diskHandler .GetNcpVMList ()
5755 if err != nil {
@@ -61,39 +59,47 @@ func (diskHandler *NcpVpcDiskHandler) CreateDisk(diskReqInfo irs.DiskInfo) (irs.
6159 return irs.DiskInfo {}, newErr
6260 }
6361
64- instanceNo := * instanceList [0 ].ServerInstanceNo // InstanceNo of any VM on the Zone
62+ instanceNo := * instanceList [0 ].ServerInstanceNo // InstanceNo of any VM on the Zone
6563 cblogger .Infof ("# instanceNo : [%v]" , instanceNo )
6664
67- reqDiskType := diskReqInfo .DiskType // Option : 'default', 'SSD' or 'HDD'
68- reqDiskSize := diskReqInfo .DiskSize // Range : 10~2000(GB)
65+ reqDiskType := diskReqInfo .DiskType // Option : 'default', 'SSD' or 'HDD'
66+ reqDiskSize := diskReqInfo .DiskSize // Range : 10~2000(GB)
6967
7068 if strings .EqualFold (reqDiskType , "" ) || strings .EqualFold (reqDiskType , "default" ) {
71- reqDiskType = "SSD" // In case, Volume Type is not specified.
72- }
69+ reqDiskType = "SSD" // In case, Volume Type is not specified.
70+ }
7371 if strings .EqualFold (reqDiskSize , "" ) || strings .EqualFold (reqDiskSize , "default" ) {
74- reqDiskSize = DefaultDiskSize // In case, Volume Size is not specified.
75- }
76-
72+ reqDiskSize = DefaultDiskSize // In case, Volume Size is not specified.
73+ }
74+
7775 // Covert String to Int32
7876 i , err := strconv .ParseInt (reqDiskSize , 10 , 32 )
7977 if err != nil {
8078 panic (err )
8179 }
8280 reqDiskSizeInt := int32 (i )
8381
84- if reqDiskSizeInt < 10 || reqDiskSizeInt > 2000 { // Range : 10~2000(GB)
82+ if reqDiskSizeInt < 10 || reqDiskSizeInt > 2000 { // Range : 10~2000(GB)
8583 newErr := fmt .Errorf ("Invalid Disk Size. Disk Size Must be between 10 and 2000(GB)." )
8684 cblogger .Error (newErr .Error ())
87- return irs.DiskInfo {}, newErr
85+ return irs.DiskInfo {}, newErr
86+ }
87+
88+ // For Zone-based control!!
89+ var reqZoneId string
90+ if strings .EqualFold (diskReqInfo .Zone , "" ) {
91+ reqZoneId = diskHandler .RegionInfo .Zone
92+ } else {
93+ reqZoneId = diskReqInfo .Zone
8894 }
8995
9096 storageReq := vserver.CreateBlockStorageInstanceRequest {
91- RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
92- BlockStorageName : ncloud .String (diskReqInfo .IId .NameId ),
93- BlockStorageSize : & reqDiskSizeInt , // *** Required (Not Optional)
97+ RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
98+ BlockStorageName : ncloud .String (diskReqInfo .IId .NameId ),
99+ BlockStorageSize : & reqDiskSizeInt , // *** Required (Not Optional)
94100 BlockStorageDiskDetailTypeCode : ncloud .String (reqDiskType ),
95- ServerInstanceNo : ncloud .String (instanceNo ), // *** Required (Not Optional)
96- ZoneCode : ncloud .String (diskHandler . RegionInfo . Zone ),
101+ ServerInstanceNo : ncloud .String (instanceNo ), // *** Required (Not Optional)
102+ ZoneCode : ncloud .String (reqZoneId ), // Apply Zone-based control!!
97103 }
98104
99105 callLogStart := call .Start ()
@@ -140,7 +146,7 @@ func (diskHandler *NcpVpcDiskHandler) CreateDisk(diskReqInfo irs.DiskInfo) (irs.
140146 // Caution!!
141147 // Incase of NCP VPC, there must be a created VM to create a new disk volume.
142148 // Therefore, the status of the new disk volume is 'Attached' after creation.
143- // ### Need to be 'Available' status after disk creation process like other CSP (with detachment).
149+ // ### Need to be 'Available' status after disk creation process like other CSP (with detachment).
144150 isDetached , err := diskHandler .DetachDisk (newDiskIID , irs.IID {SystemId : instanceNo })
145151 if err != nil {
146152 newErr := fmt .Errorf ("Failed to Detach the Disk Volume : [%v] " , err )
@@ -164,11 +170,11 @@ func (diskHandler *NcpVpcDiskHandler) CreateDisk(diskReqInfo irs.DiskInfo) (irs.
164170
165171func (diskHandler * NcpVpcDiskHandler ) ListDisk () ([]* irs.DiskInfo , error ) {
166172 cblogger .Info ("NCP VPC Driver: called ListDisk()" )
167- InitLog () // Caution!!
173+ InitLog () // Caution!!
168174 callLogInfo := GetCallLogScheme (diskHandler .RegionInfo .Region , call .DISK , "ListDisk()" , "ListDisk()" ) // HisCall logging
169175
170176 storageReq := vserver.GetBlockStorageInstanceListRequest {
171- RegionCode : ncloud .String (diskHandler .RegionInfo .Region ), // $$$ Caution!!
177+ RegionCode : ncloud .String (diskHandler .RegionInfo .Region ), // $$$ Caution!!
172178 }
173179
174180 callLogStart := call .Start ()
@@ -203,7 +209,7 @@ func (diskHandler *NcpVpcDiskHandler) ListDisk() ([]*irs.DiskInfo, error) {
203209
204210func (diskHandler * NcpVpcDiskHandler ) GetDisk (diskIID irs.IID ) (irs.DiskInfo , error ) {
205211 cblogger .Info ("NCP VPC Driver: called GetDisk()" )
206- InitLog () // Caution!!
212+ InitLog () // Caution!!
207213 callLogInfo := GetCallLogScheme (diskHandler .RegionInfo .Region , call .DISK , diskIID .SystemId , "GetDisk()" ) // HisCall logging
208214
209215 if strings .EqualFold (diskIID .SystemId , "" ) {
@@ -233,9 +239,9 @@ func (diskHandler *NcpVpcDiskHandler) GetDisk(diskIID irs.IID) (irs.DiskInfo, er
233239
234240func (diskHandler * NcpVpcDiskHandler ) ChangeDiskSize (diskIID irs.IID , size string ) (bool , error ) {
235241 cblogger .Info ("NCP VPC Driver: called ChangeDiskSize()" )
236- InitLog () // Caution!!
242+ InitLog () // Caution!!
237243 callLogInfo := GetCallLogScheme (diskHandler .RegionInfo .Region , call .DISK , diskIID .SystemId , "ChangeDiskSize()" ) // HisCall logging
238-
244+
239245 if strings .EqualFold (diskIID .SystemId , "" ) {
240246 newErr := fmt .Errorf ("Invalid Disk SystemId!!" )
241247 cblogger .Error (newErr .Error ())
@@ -245,10 +251,10 @@ func (diskHandler *NcpVpcDiskHandler) ChangeDiskSize(diskIID irs.IID, size strin
245251
246252 intSize , _ := strconv .Atoi (size )
247253 int32Size := int32 (intSize )
248- changeReq := vserver.ChangeBlockStorageVolumeSizeRequest {
249- RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
254+ changeReq := vserver.ChangeBlockStorageVolumeSizeRequest {
255+ RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
250256 BlockStorageInstanceNo : ncloud .String (diskIID .SystemId ),
251- BlockStorageSize : & int32Size ,
257+ BlockStorageSize : & int32Size ,
252258 }
253259
254260 callLogStart := call .Start ()
@@ -269,14 +275,14 @@ func (diskHandler *NcpVpcDiskHandler) ChangeDiskSize(diskIID irs.IID, size strin
269275 } else {
270276 cblogger .Info ("Succeeded in Changing the Block Storage Volume Size." )
271277 }
272- cblogger .Infof ("\n # Chaneged Size : [%s](GB)" , strconv .FormatInt (* result .BlockStorageInstanceList [0 ].BlockStorageSize / (1024 * 1024 * 1024 ), 10 ))
278+ cblogger .Infof ("\n # Chaneged Size : [%s](GB)" , strconv .FormatInt (* result .BlockStorageInstanceList [0 ].BlockStorageSize / (1024 * 1024 * 1024 ), 10 ))
273279
274280 return true , nil
275281}
276282
277283func (diskHandler * NcpVpcDiskHandler ) DeleteDisk (diskIID irs.IID ) (bool , error ) {
278284 cblogger .Info ("NCP VPC Driver: called DeleteDisk()" )
279- InitLog () // Caution!!
285+ InitLog () // Caution!!
280286 callLogInfo := GetCallLogScheme (diskHandler .RegionInfo .Region , call .DISK , diskIID .SystemId , "DeleteDisk()" ) // HisCall logging
281287
282288 if strings .EqualFold (diskIID .SystemId , "" ) {
@@ -301,9 +307,9 @@ func (diskHandler *NcpVpcDiskHandler) DeleteDisk(diskIID irs.IID) (bool, error)
301307 return false , newErr
302308 }
303309
304- storageNoList := []* string {ncloud .String (diskIID .SystemId )}
305- delReq := vserver.DeleteBlockStorageInstancesRequest {
306- RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
310+ storageNoList := []* string {ncloud .String (diskIID .SystemId ), }
311+ delReq := vserver.DeleteBlockStorageInstancesRequest {
312+ RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
307313 BlockStorageInstanceNoList : storageNoList ,
308314 }
309315
@@ -331,7 +337,7 @@ func (diskHandler *NcpVpcDiskHandler) DeleteDisk(diskIID irs.IID) (bool, error)
331337
332338func (diskHandler * NcpVpcDiskHandler ) AttachDisk (diskIID irs.IID , vmIID irs.IID ) (irs.DiskInfo , error ) {
333339 cblogger .Info ("NCP VPC Driver: called AttachDisk()" )
334- InitLog () // Caution!!
340+ InitLog () // Caution!!
335341 callLogInfo := GetCallLogScheme (diskHandler .RegionInfo .Region , call .DISK , diskIID .SystemId , "AttachDisk()" ) // HisCall logging
336342
337343 if strings .EqualFold (diskIID .SystemId , "" ) {
@@ -343,9 +349,8 @@ func (diskHandler *NcpVpcDiskHandler) AttachDisk(diskIID irs.IID, vmIID irs.IID)
343349 newErr := fmt .Errorf ("Invalid Disk SystemId!!" )
344350 cblogger .Error (newErr .Error ())
345351 LoggingError (callLogInfo , newErr )
346- return irs.DiskInfo {}, newErr
347- }
348-
352+ return irs.DiskInfo {}, newErr }
353+
349354 curStatus , err := diskHandler .GetDiskStatus (diskIID )
350355 if err != nil {
351356 newErr := fmt .Errorf ("Failed to Get the Disk Status : [%v] " , err )
@@ -359,9 +364,9 @@ func (diskHandler *NcpVpcDiskHandler) AttachDisk(diskIID irs.IID, vmIID irs.IID)
359364 }
360365
361366 attachReq := vserver.AttachBlockStorageInstanceRequest {
362- RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
363- ServerInstanceNo : ncloud .String (vmIID .SystemId ),
364- BlockStorageInstanceNo : ncloud .String (diskIID .SystemId ),
367+ RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
368+ ServerInstanceNo : ncloud .String (vmIID .SystemId ),
369+ BlockStorageInstanceNo : ncloud .String (diskIID .SystemId ),
365370 }
366371
367372 callLogStart := call .Start ()
@@ -405,7 +410,7 @@ func (diskHandler *NcpVpcDiskHandler) AttachDisk(diskIID irs.IID, vmIID irs.IID)
405410
406411func (diskHandler * NcpVpcDiskHandler ) DetachDisk (diskIID irs.IID , ownerVM irs.IID ) (bool , error ) {
407412 cblogger .Info ("NCP VPC Driver: called DetachDisk()" )
408- InitLog () // Caution!!
413+ InitLog () // Caution!!
409414 callLogInfo := GetCallLogScheme (diskHandler .RegionInfo .Region , call .DISK , diskIID .SystemId , "DetachDisk()" ) // HisCall logging
410415
411416 if strings .EqualFold (diskIID .SystemId , "" ) {
@@ -439,9 +444,9 @@ func (diskHandler *NcpVpcDiskHandler) DetachDisk(diskIID irs.IID, ownerVM irs.II
439444 return false , newErr
440445 }
441446
442- storageNoList := []* string {ncloud .String (diskIID .SystemId )}
447+ storageNoList := []* string {ncloud .String (diskIID .SystemId ), }
443448 detachReq := vserver.DetachBlockStorageInstancesRequest {
444- RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
449+ RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
445450 BlockStorageInstanceNoList : storageNoList ,
446451 }
447452
@@ -487,7 +492,7 @@ func (diskHandler *NcpVpcDiskHandler) GetNcpDiskInfo(diskIID irs.IID) (*vserver.
487492 }
488493
489494 storageReq := vserver.GetBlockStorageInstanceDetailRequest {
490- RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
495+ RegionCode : ncloud .String (diskHandler .RegionInfo .Region ),
491496 BlockStorageInstanceNo : ncloud .String (diskIID .SystemId ),
492497 }
493498
@@ -503,7 +508,7 @@ func (diskHandler *NcpVpcDiskHandler) GetNcpDiskInfo(diskIID irs.IID) (*vserver.
503508 return nil , newErr
504509 } else {
505510 cblogger .Info ("Succeeded in Getting NCP VPC Block Storage Info." )
506- }
511+ }
507512 return result .BlockStorageInstanceList [0 ], nil
508513}
509514
@@ -560,7 +565,7 @@ func (diskHandler *NcpVpcDiskHandler) WaitForDiskAttachment(diskIID irs.IID) (ir
560565 cblogger .Infof ("===> Disk Status : [%s]" , string (curStatus ))
561566
562567 switch string (curStatus ) {
563- case string (irs .DiskCreating ), string (irs .DiskAvailable ), string (irs .DiskDeleting ), string (irs .DiskError ), "Unknown" :
568+ case string (irs .DiskCreating ), string (irs .DiskAvailable ), string (irs .DiskDeleting ), string (irs .DiskError ), "Unknown" :
564569 curRetryCnt ++
565570 cblogger .Infof ("The Disk is still [%s], so wait for a second more during the Disk 'Attachment'." , string (curStatus ))
566571 time .Sleep (time .Second * 2 )
@@ -594,7 +599,7 @@ func (diskHandler *NcpVpcDiskHandler) WaitForDiskDetachment(diskIID irs.IID) (ir
594599 cblogger .Infof ("===> Disk Status : [%s]" , string (curStatus ))
595600
596601 switch string (curStatus ) {
597- case string (irs .DiskCreating ), string (irs .DiskAttached ), string (irs .DiskDeleting ), string (irs .DiskError ), "Detaching" , "Unknown" :
602+ case string (irs .DiskCreating ), string (irs .DiskAttached ), string (irs .DiskDeleting ), string (irs .DiskError ), "Detaching" , "Unknown" :
598603 curRetryCnt ++
599604 cblogger .Infof ("The Disk is still [%s], so wait for a second more during the Disk 'Detachment'." , string (curStatus ))
600605 time .Sleep (time .Second * 2 )
@@ -652,19 +657,20 @@ func (diskHandler *NcpVpcDiskHandler) MappingDiskInfo(storage vserver.BlockStora
652657
653658 diskInfo := irs.DiskInfo {
654659 IId : irs.IID {
655- NameId : ncloud .StringValue (storage .BlockStorageName ),
656- SystemId : ncloud .StringValue (storage .BlockStorageInstanceNo ),
660+ NameId : ncloud .StringValue (storage .BlockStorageName ),
661+ SystemId : ncloud .StringValue (storage .BlockStorageInstanceNo ),
657662 },
663+ Zone : ncloud .StringValue (storage .ZoneCode ),
658664 DiskSize : strconv .FormatInt ((* storage .BlockStorageSize )/ (1024 * 1024 * 1024 ), 10 ),
659- Status : ConvertDiskStatus (ncloud .StringValue (storage .BlockStorageInstanceStatusName )), // Not BlockStorageInstanceStatus.Code
665+ Status : ConvertDiskStatus (ncloud .StringValue (storage .BlockStorageInstanceStatusName )), // Not BlockStorageInstanceStatus.Code
660666 CreatedTime : convertedTime ,
661- DiskType : ncloud .StringValue (storage .BlockStorageDiskDetailType .Code ),
667+ DiskType : ncloud .StringValue (storage .BlockStorageDiskDetailType .Code ),
662668 }
663669
664670 if strings .EqualFold (ncloud .StringValue (storage .BlockStorageInstanceStatusName ), "attached" ) {
665671 vmHandler := NcpVpcVMHandler {
666- RegionInfo : diskHandler .RegionInfo ,
667- VMClient : diskHandler .VMClient ,
672+ RegionInfo : diskHandler .RegionInfo ,
673+ VMClient : diskHandler .VMClient ,
668674 }
669675
670676 vmInfo , err := vmHandler .GetNcpVMInfo (ncloud .StringValue (storage .ServerInstanceNo ))
@@ -675,19 +681,19 @@ func (diskHandler *NcpVpcDiskHandler) MappingDiskInfo(storage vserver.BlockStora
675681 }
676682
677683 diskInfo .OwnerVM = irs.IID {
678- NameId : ncloud .StringValue (vmInfo .ServerName ),
679- SystemId : ncloud .StringValue (storage .ServerInstanceNo ),
684+ NameId : ncloud .StringValue (vmInfo .ServerName ),
685+ SystemId : ncloud .StringValue (storage .ServerInstanceNo ),
680686 }
681687 }
682688
683689 keyValueList := []irs.KeyValue {
684- {Key : "DeviceName" , Value : ncloud .StringValue (storage .DeviceName )},
685- {Key : "ZoneCode" , Value : ncloud .StringValue (storage .ZoneCode )},
686- {Key : "BlockStorageType" , Value : ncloud .StringValue (storage .BlockStorageType .CodeName )},
687- {Key : "BlockStorageDiskType" , Value : ncloud .StringValue (storage .BlockStorageDiskType .CodeName )},
688- {Key : "MaxIOPS" , Value : strconv .FormatInt (int64 (* storage .MaxIopsThroughput ), 10 )},
689- {Key : "IsReturnProtection" , Value : strconv .FormatBool (* storage .IsReturnProtection )},
690- {Key : "IsEncryptedVolume" , Value : strconv .FormatBool (* storage .IsEncryptedVolume )},
690+ {Key : "DeviceName" , Value : ncloud .StringValue (storage .DeviceName )},
691+ // {Key: "ZoneCode", Value: ncloud.StringValue(storage.ZoneCode)},
692+ {Key : "BlockStorageType" , Value : ncloud .StringValue (storage .BlockStorageType .CodeName )},
693+ {Key : "BlockStorageDiskType" , Value : ncloud .StringValue (storage .BlockStorageDiskType .CodeName )},
694+ {Key : "MaxIOPS" , Value : strconv .FormatInt (int64 (* storage .MaxIopsThroughput ), 10 )},
695+ {Key : "IsReturnProtection" , Value : strconv .FormatBool (* storage .IsReturnProtection )},
696+ {Key : "IsEncryptedVolume" , Value : strconv .FormatBool (* storage .IsEncryptedVolume )},
691697 }
692698 diskInfo .KeyValueList = keyValueList
693699
@@ -696,7 +702,7 @@ func (diskHandler *NcpVpcDiskHandler) MappingDiskInfo(storage vserver.BlockStora
696702
697703func ConvertDiskStatus (diskStatus string ) irs.DiskStatus {
698704 cblogger .Info ("NCP VPC Cloud Driver: called ConvertDiskStatus()" )
699-
705+
700706 var resultStatus irs.DiskStatus
701707 switch strings .ToLower (diskStatus ) {
702708 case "creating" :
@@ -710,7 +716,7 @@ func ConvertDiskStatus(diskStatus string) irs.DiskStatus {
710716 case "error" :
711717 resultStatus = irs .DiskError
712718 case "detaching" :
713- resultStatus = "Detaching"
719+ resultStatus = "Detaching"
714720 default :
715721 resultStatus = "Unknown"
716722 }
@@ -724,7 +730,7 @@ func (diskHandler *NcpVpcDiskHandler) GetNcpVMList() ([]*vserver.ServerInstance,
724730 callLogInfo := GetCallLogScheme (diskHandler .RegionInfo .Region , call .DISK , "GetNcpVMList()" , "GetNcpVMList()" )
725731
726732 instanceReq := vserver.GetServerInstanceListRequest {
727- RegionCode : & diskHandler .RegionInfo .Region ,
733+ RegionCode : & diskHandler .RegionInfo .Region ,
728734 }
729735
730736 callLogStart := call .Start ()
@@ -762,7 +768,7 @@ func (diskHandler *NcpVpcDiskHandler) IsBasicBlockStorage(diskIID irs.IID) (bool
762768 return false , newErr
763769 }
764770
765- if strings .EqualFold (* ncpDiskInfo .BlockStorageType .Code , "BASIC" ) { // Ex) Basic, SVRBS, ...
771+ if strings .EqualFold (* ncpDiskInfo .BlockStorageType .Code , "BASIC" ) { // Ex) Basic, SVRBS, ...
766772 return true , nil
767773 } else {
768774 cblogger .Infof ("# BlockStorageType : [%s]" , * ncpDiskInfo .BlockStorageType .CodeName ) // Ex) Basic BS, Server BS, ...
0 commit comments