Skip to content

Commit

Permalink
Add KT Cloud Tagging Features and Test Codes for them
Browse files Browse the repository at this point in the history
  • Loading branch information
innodreamer committed Aug 20, 2024
1 parent bc99094 commit e20dcdb
Show file tree
Hide file tree
Showing 7 changed files with 770 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
package connect

import (
"errors"
"fmt"

"github.com/sirupsen/logrus"

cblog "github.com/cloud-barista/cb-log"
Expand Down Expand Up @@ -121,6 +119,16 @@ func (cloudConn *KtCloudConnection) CreateRegionZoneHandler() (irs.RegionZoneHan
return &regionZoneHandler, nil
}

func (*KtCloudConnection) CreatePriceInfoHandler() (irs.PriceInfoHandler, error) {
return nil, fmt.Errorf("KT Cloud Driver does not support CreatePriceInfoHandler yet.")
}

func (cloudConn *KtCloudConnection) CreateTagHandler() (irs.TagHandler, error) {
cblogger.Info("KT Cloud Driver: called CreateTagHandler()!")
tagHandler := ktrs.KtCloudTagHandler{cloudConn.RegionInfo, cloudConn.Client}
return &tagHandler, nil
}

func (cloudConn *KtCloudConnection) IsConnected() (bool, error) {
cblogger.Info("KT Cloud Driver: called IsConnected()!")
if cloudConn == nil {
Expand All @@ -133,11 +141,3 @@ func (cloudConn *KtCloudConnection) Close() error {
cblogger.Info("KT Cloud Driver: called Close()!")
return nil
}

func (*KtCloudConnection) CreatePriceInfoHandler() (irs.PriceInfoHandler, error) {
return nil, errors.New("KT Cloud Driver: not implemented")
}

func (cloudConn *KtCloudConnection) CreateTagHandler() (irs.TagHandler, error) {
return nil, errors.New("KT Cloud Driver: not implemented")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// 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.07.

package main

import (
"os"
"errors"
"fmt"
// "bufio"
// "strings"
"github.com/davecgh/go-spew/spew"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"

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"
cblog "github.com/cloud-barista/cb-log"

ktdrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/ktcloud"
)

var cblogger *logrus.Logger

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

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

resourceHandler, err := getResourceHandler("Tag")
if err != nil {
cblogger.Error(err)
return
}
tagHandler := resourceHandler.(irs.TagHandler)
//config := readConfigFile()

for {
fmt.Println("\n============================================================================================")
fmt.Println("[ TagHandler Test ]")
cblogger.Info("1. ListTag()")
cblogger.Info("2. GetTag()")
cblogger.Info("3. AddTag()")
cblogger.Info("4. RemoveTag()")
cblogger.Info("5. FindTag()")
cblogger.Info("0. Exit")
fmt.Println("\n Select a number above!! : ")
fmt.Println("============================================================================================")

var commandNum int

// rsType := irs.RSType(irs.VM)
// rsType := irs.RSType(irs.MYIMAGE)
rsType := irs.RSType(irs.DISK)

rsIId := irs.IID{
NameId: "MyVM-1",

// SystemId: "92539c11-fe97-449f-a115-f301b3d6b7ae", // VM
// SystemId: "aef923e2-788d-42d2-8da6-d10b8c63abf9", // MyImage
SystemId: "8a5b97d6-e5f1-459e-bb11-9f568bdc37cb", // Disk
}

tagKV := irs.KeyValue{
Key: "MyImageNameTest",
Value: "KT-myImage-01",
}

tagKey := "DiskNameTest"

keyword := "Name"

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

if inputCnt == 1 {
switch commandNum {
case 0:
return
case 1:
cblogger.Info("Start ListTag() ...")
if tagList, err := tagHandler.ListTag(rsType, rsIId); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagList)
}
cblogger.Info("Finish ListTag()")
case 2:
cblogger.Info("Start GetTag() ...")
if tagKeyValue, err := tagHandler.GetTag(rsType, rsIId, tagKey); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagKeyValue)
}
cblogger.Info("Finish GetTag()")
case 3:
cblogger.Info("Start AddTag() ...")
if tagKeyValue, err := tagHandler.AddTag(rsType, rsIId, tagKV); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagKeyValue)
}
cblogger.Info("Finish AddTag()")
case 4:
cblogger.Info("Start RemoveTag() ...")
if tagKeyValue, err := tagHandler.RemoveTag(rsType, rsIId, tagKey); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagKeyValue)
}
cblogger.Info("Finish RemoveTag()")
case 5:
cblogger.Info("Start FindTag() ...")
if tagKeyValue, err := tagHandler.FindTag(rsType, keyword); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagKeyValue)
}
cblogger.Info("Finish FindTag()")
}
}
}
}

func testErr() error {

return errors.New("")
}

func main() {
cblogger.Info("NCP Resource Test")
handleTag()
}

//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,
},
}

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()
case "Tag":
resourceHandler, err = cloudConnection.CreateTagHandler()
}

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"`
}

func readConfigFile() Config {
// # Set Environment Value of Project Root Path
// goPath := os.Getenv("GOPATH")
// rootPath := goPath + "/src/github.com/cloud-barista/ncp/ncp/main"
// cblogger.Debugf("Test Config file : [%]", rootPath+"/config/config.yaml")
rootPath := os.Getenv("CBSPIDER_ROOT")
configPath := rootPath + "/cloud-control-manager/cloud-driver/drivers/ktcloud/main/config/config.yaml"
cblogger.Debugf("Test Config file : [%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("ConfigFile Loaded ...")

// 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_TagHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,9 @@ func (diskHandler *KtCloudDiskHandler) mappingDiskInfo(volume *ktsdk.Volume) (ir
// cblogger.Info("\n\n### volume : ")
// spew.Dump(volume)
// cblogger.Info("\n")
cblogger.Infof("# Given Volume State on KT Cloud : %s", volume.State) // Not Correct!!

cblogger.Infof("# Given Volume State on KT Cloud : %s", volume.State) // Not Correct!!
// cblogger.Infof("# volume.ID : %s", volume.ID)

volumeIID := irs.IID{SystemId: volume.ID}
curStatus, err := diskHandler.getDiskStatus(volumeIID)
if err != nil {
Expand Down Expand Up @@ -628,11 +629,34 @@ func (diskHandler *KtCloudDiskHandler) mappingDiskInfo(volume *ktsdk.Volume) (ir
}
}

// Get the Tag List of the Disk
var kvList []irs.KeyValue
tagHandler := KtCloudTagHandler {
RegionInfo: diskHandler.RegionInfo,
Client: diskHandler.Client,
}
tagList, err := tagHandler.getTagListWithResId(irs.RSType(irs.DISK), &volume.ID)
if err != nil {
newErr := fmt.Errorf("Failed to Get the Tag List with the Disk ID : [%v]", err)
cblogger.Error(newErr.Error())
return irs.DiskInfo{}, newErr
}
if len(tagList) > 0 {
for _, curTag := range tagList {
kv := irs.KeyValue {
Key : curTag.Key,
Value: curTag.Value,
}
kvList = append(kvList, kv)
}
diskInfo.TagList = kvList
}

var iops string
if !strings.EqualFold(strconv.FormatInt(volume.MaxIOPS, 10), "0") {
iops = strconv.FormatInt(volume.MaxIOPS, 10)
}

keyValueList := []irs.KeyValue{
{Key: "Type", Value: volume.Type},
{Key: "MaxIOPS", Value: iops},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,30 @@ func (myImageHandler *KtCloudMyImageHandler) mappingMyImageInfo(myImage *ktsdk.T
{Key: "Region", Value: myImageHandler.RegionInfo.Region},
}
myImageInfo.KeyValueList = keyValueList

// Get the Tag List of the MyImage
var kvList []irs.KeyValue
tagHandler := KtCloudTagHandler {
RegionInfo: myImageHandler.RegionInfo,
Client: myImageHandler.Client,
}
tagList, err := tagHandler.getTagListWithResId(irs.RSType(irs.MYIMAGE), &myImage.ID)
if err != nil {
newErr := fmt.Errorf("Failed to Get the Tag List with the MyImage ID : [%v]", err)
cblogger.Error(newErr.Error())
return nil, newErr
}
if len(tagList) > 0 {
for _, curTag := range tagList {
kv := irs.KeyValue {
Key : curTag.Key,
Value: curTag.Value,
}
kvList = append(kvList, kv)
}
myImageInfo.TagList = kvList
}

return myImageInfo, nil
}

Expand Down
Loading

0 comments on commit e20dcdb

Please sign in to comment.