Skip to content

Commit

Permalink
Add KT Cloud Classic(G1/G2 platform) DiskHandler features
Browse files Browse the repository at this point in the history
  • Loading branch information
innodreamer committed Jan 16, 2024
1 parent 19ce70a commit 64cbc4d
Show file tree
Hide file tree
Showing 4 changed files with 939 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func (cloudConn *KtCloudConnection) CreateNLBHandler() (irs.NLBHandler, error) {

func (cloudConn *KtCloudConnection) CreateDiskHandler() (irs.DiskHandler, error) {
cblogger.Info("KT Cloud Driver: called CreateDiskHandler()!")
return nil, fmt.Errorf("KT Cloud Driver does not support CreateDiskHandler yet.")
diskHandler := ktrs.KtCloudDiskHandler{RegionInfo: cloudConn.RegionInfo, Client: cloudConn.Client}
return &diskHandler, nil
}

func (cloudConn *KtCloudConnection) CreateClusterHandler() (irs.ClusterHandler, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
// 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 Tester Example.
//
// by ETRI, 2024.01.

package main

import (
"errors"
"fmt"
"os"

"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"

cblog "github.com/cloud-barista/cb-log"
ktdrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/ktcloud"
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"
)

var cblogger *logrus.Logger

func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("KT Cloud Resource Test")
cblog.SetLevel("info")
}

func handleDisk() {
cblogger.Debug("Start Disk Resource Test")

resourceHandler, err := getResourceHandler("Disk")
if err != nil {
cblogger.Error(err)
return
}
diskHandler := resourceHandler.(irs.DiskHandler)
//config := readConfigFile()

for {
fmt.Println("\n============================================================================================")
fmt.Println("[ DiskHandler Test ]")
cblogger.Info("1. ListDisk()")
cblogger.Info("2. GetDisk()")
cblogger.Info("3. CreateDisk()")
cblogger.Info("4. DeleteDisk()")
cblogger.Info("5. ChangeDiskSize()")
cblogger.Info("6. AttachDisk()")
cblogger.Info("7. DetachDisk()")
cblogger.Info("0. Exit")
fmt.Println("\n Select a number above!! : ")
fmt.Println("============================================================================================")

var commandNum int

diskIId := irs.IID{
NameId: "...",
SystemId: "5fb57e6c-a8ed-4e66-8549-f9d804e72f95",
}

createReqInfo := irs.DiskInfo{
IId: irs.IID{
NameId: "kt-disk-1",
},
// DiskType: "default",
// DiskType: "HDD",
DiskType: "SSD",
// DiskSize: "default",
DiskSize: "100",
}

vmIId := irs.IID{ // To attach disk
NameId: "kt-vm-01",
SystemId: "0e0f6583-64f9-4d27-8e1d-36d4174d4a40",
}

newDiskSize := "200"

inputCnt, err := fmt.Scan(&commandNum)
if err != nil {
panic(err)
}

if inputCnt == 1 {
switch commandNum {
case 0:
return
case 1:
cblogger.Info("Start ListDisk() ...")
if listResult, err := diskHandler.ListDisk(); err != nil {
cblogger.Error(err)
} else {
spew.Dump(listResult)
cblogger.Info("# 출력 결과 수 : ", len(listResult))
}
cblogger.Info("Finish ListDisk()")
case 2:
cblogger.Info("Start GetDisk() ...")
if diskInfo, err := diskHandler.GetDisk(diskIId); err != nil {
cblogger.Error(err)
} else {
spew.Dump(diskInfo)
}
cblogger.Info("Finish GetDisk()")
case 3:
cblogger.Info("Start CreateDisk() ...")
if diskInfo, err := diskHandler.CreateDisk(createReqInfo); err != nil {
cblogger.Error(err)
} else {
spew.Dump(diskInfo)
}
cblogger.Info("Finish CreateDisk()")
case 4:
cblogger.Info("Start DeleteDisk() ...")
if delResult, err := diskHandler.DeleteDisk(diskIId); err != nil {
cblogger.Error(err)
} else {
spew.Dump(delResult)
}
cblogger.Info("Finish DeleteDisk()")
case 5:
cblogger.Info("Start ChangeDiskSize() ...")
if diskInfo, err := diskHandler.ChangeDiskSize(diskIId, newDiskSize); err != nil {
cblogger.Error(err)
} else {
spew.Dump(diskInfo)
}
cblogger.Info("Finish ChangeDiskSize()")
case 6:
cblogger.Info("Start AttachDisk() ...")
if diskInfo, err := diskHandler.AttachDisk(diskIId, vmIId); err != nil {
cblogger.Error(err)
} else {
spew.Dump(diskInfo)
}
cblogger.Info("Finish AttachDisk()")
case 7:
cblogger.Info("Start DetachDisk() ...")
if result, err := diskHandler.DetachDisk(diskIId, vmIId); err != nil {
cblogger.Error(err)
} else {
spew.Dump(result)
}
cblogger.Info("Finish DetachDisk()")
}
}
}
}

func testErr() error {
return errors.New("")
}

func main() {
cblogger.Info("KT Cloud Resource Test")
handleDisk()
}

// handlerType : resources폴더의 xxxHandler.go에서 Handler이전까지의 문자열
// (예) ImageHandler.go -> "Image"
func getResourceHandler(handlerType string) (interface{}, error) {
var cloudDriver idrv.CloudDriver
cloudDriver = new(ktdrv.KtCloudDriver)

config := readConfigFile()
connectionInfo := idrv.ConnectionInfo{
CredentialInfo: idrv.CredentialInfo{
ClientId: config.KtCloud.KtCloudAccessKeyID,
ClientSecret: config.KtCloud.KtCloudSecretKey,
},
RegionInfo: idrv.RegionInfo{
Region: config.KtCloud.Region,
Zone: config.KtCloud.Zone,
},
}

// NOTE Just for test
//cblogger.Info(config.KtCloud.KtCloudAccessKeyID)
//cblogger.Info(config.KtCloud.KtCloudSecretKey)

cloudConnection, errCon := cloudDriver.ConnectCloud(connectionInfo)
if errCon != nil {
return nil, errCon
}

var resourceHandler interface{}
var err error

switch handlerType {
case "Image":
resourceHandler, err = cloudConnection.CreateImageHandler()
case "Security":
resourceHandler, err = cloudConnection.CreateSecurityHandler()
case "VNetwork":
resourceHandler, err = cloudConnection.CreateVPCHandler()
case "VM":
resourceHandler, err = cloudConnection.CreateVMHandler()
case "VMSpec":
resourceHandler, err = cloudConnection.CreateVMSpecHandler()
case "VPC":
resourceHandler, err = cloudConnection.CreateVPCHandler()
case "Disk":
resourceHandler, err = cloudConnection.CreateDiskHandler()
}

if err != nil {
return nil, err
}
return resourceHandler, nil
}

type Config struct {
KtCloud struct {
KtCloudAccessKeyID string `yaml:"ktcloud_access_key_id"`
KtCloudSecretKey string `yaml:"ktcloud_secret_key"`
Region string `yaml:"region"`
Zone string `yaml:"zone"`

ImageID string `yaml:"image_id"`

VmID string `yaml:"ktcloud_instance_id"`
BaseName string `yaml:"base_name"`
InstanceType string `yaml:"instance_type"`
KeyName string `yaml:"key_name"`
MinCount int64 `yaml:"min_count"`
MaxCount int64 `yaml:"max_count"`

SubnetID string `yaml:"subnet_id"`
SecurityGroupID string `yaml:"security_group_id"`

PublicIP string `yaml:"public_ip"`
} `yaml:"ktcloud"`
}

// 환경변수 CBSPIDER_PATH 설정 후 해당 폴더 하위에 /config/config.yaml 파일 생성해야 함.
func readConfigFile() Config {
rootPath := os.Getenv("CBSPIDER_ROOT")
configPath := rootPath + "/cloud-control-manager/cloud-driver/drivers/ktcloud/main/config/config.yaml"
cblogger.Debugf("Test Data 설정파일 : [%s]", configPath)

data, err := os.ReadFile(configPath)
if err != nil {
panic(err)
}

var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
panic(err)
}

cblogger.Info("Loaded ConfigFile...")
//spew.Dump(config)
//cblogger.Info(config)

// NOTE Just for test
//cblogger.Info(config.KtCloud.KtCloudAccessKeyID)
//cblogger.Info(config.KtCloud.KtCloudSecretKey)

// NOTE Just for test
cblogger.Debug(config.KtCloud.KtCloudAccessKeyID, " ", config.KtCloud.Region)

return config
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source ../../../../../setup.env

go run Test_DiskHandler.go
Loading

0 comments on commit 64cbc4d

Please sign in to comment.