diff --git a/cloud-control-manager/cloud-driver/drivers/ktcloud/connect/KtCloud_Connection.go b/cloud-control-manager/cloud-driver/drivers/ktcloud/connect/KtCloud_Connection.go index 701f30882..7cb5825e8 100644 --- a/cloud-control-manager/cloud-driver/drivers/ktcloud/connect/KtCloud_Connection.go +++ b/cloud-control-manager/cloud-driver/drivers/ktcloud/connect/KtCloud_Connection.go @@ -12,9 +12,7 @@ package connect import ( - "errors" "fmt" - "github.com/sirupsen/logrus" cblog "github.com/cloud-barista/cb-log" @@ -121,6 +119,16 @@ func (cloudConn *KtCloudConnection) CreateRegionZoneHandler() (irs.RegionZoneHan return ®ionZoneHandler, 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 { @@ -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") -} diff --git a/cloud-control-manager/cloud-driver/drivers/ktcloud/main/Test_TagHandler.go b/cloud-control-manager/cloud-driver/drivers/ktcloud/main/Test_TagHandler.go new file mode 100644 index 000000000..d0e1a6431 --- /dev/null +++ b/cloud-control-manager/cloud-driver/drivers/ktcloud/main/Test_TagHandler.go @@ -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 +} diff --git a/cloud-control-manager/cloud-driver/drivers/ktcloud/main/test_tag.sh b/cloud-control-manager/cloud-driver/drivers/ktcloud/main/test_tag.sh new file mode 100755 index 000000000..8e4f4a0c2 --- /dev/null +++ b/cloud-control-manager/cloud-driver/drivers/ktcloud/main/test_tag.sh @@ -0,0 +1,3 @@ +source ../../../../../setup.env + +go run Test_TagHandler.go diff --git a/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/DiskHandler.go b/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/DiskHandler.go index 93672a760..ec422486f 100644 --- a/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/DiskHandler.go +++ b/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/DiskHandler.go @@ -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 { @@ -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}, diff --git a/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/MyImageHandler.go b/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/MyImageHandler.go index 9c8f416c6..f9c1e6fad 100644 --- a/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/MyImageHandler.go +++ b/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/MyImageHandler.go @@ -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 } diff --git a/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/TagHandler.go b/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/TagHandler.go new file mode 100644 index 000000000..746e2d4bb --- /dev/null +++ b/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/TagHandler.go @@ -0,0 +1,429 @@ +// Cloud Driver Interface 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 +// +// KT Cloud Tag Handler +// +// by ETRI, 2024.08. + +package resources + +import ( + "fmt" + "strings" + "time" + + ktsdk "github.com/cloud-barista/ktcloud-sdk-go" + + 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 KtCloudTagHandler struct { + RegionInfo idrv.RegionInfo + Client *ktsdk.KtCloudClient +} + +// KT Cloud Resource Types for Tagging => 'userVm' : VM, 'Template' : MyImage, 'Volume' : Disk +const ( + VM string = "userVm" + MyImage string = "Template" + DISK string = "Volume" +) + +func (tagHandler *KtCloudTagHandler) AddTag(resType irs.RSType, resIID irs.IID, tag irs.KeyValue) (irs.KeyValue, error) { + cblogger.Info("KT Cloud driver: called AddTag()!!") + + if resType == "" { + newErr := fmt.Errorf("Invalid Resource Type!!") + cblogger.Error(newErr.Error()) + return irs.KeyValue{}, newErr + } + + if (resType != irs.VM) && (resType != irs.MYIMAGE) && (resType != irs.DISK) { + newErr := fmt.Errorf("Only 'VM', 'MyImage' and 'Disk' type are supported as a resource type to Add a Tag Info on KT Classic!!") + cblogger.Error(newErr.Error()) + return irs.KeyValue{}, newErr + } + + if strings.EqualFold(resIID.SystemId, "") { + newErr := fmt.Errorf("Invalid Resource SystemId!!") + cblogger.Error(newErr.Error()) + return irs.KeyValue{}, newErr + } + + tagList := []irs.KeyValue { + { + Key: tag.Key, + Value: tag.Value, + }, + } + _, err := tagHandler.createTagList(resType, &resIID.SystemId, tagList) + if err != nil { + newErr := fmt.Errorf("Failed to Add New Tag : [%v]", err) + cblogger.Error(newErr.Error()) + return irs.KeyValue{}, newErr + } + + time.Sleep(time.Second * 1) + tagKV, err := tagHandler.GetTag(resType, resIID, tag.Key) + if err != nil { + newErr := fmt.Errorf("Failed to Get the Tag Info : [%v]", err) + cblogger.Error(newErr.Error()) + return irs.KeyValue{}, newErr + } + return tagKV, nil +} + +func (tagHandler *KtCloudTagHandler) ListTag(resType irs.RSType, resIID irs.IID) ([]irs.KeyValue, error) { + cblogger.Info("KT Cloud driver: called ListTag()!!") + + if resType == "" { + newErr := fmt.Errorf("Invalid Resource Type!!") + cblogger.Error(newErr.Error()) + return nil, newErr + } + + if strings.EqualFold(resIID.SystemId, "") { + newErr := fmt.Errorf("Invalid Resource SystemId!!") + cblogger.Error(newErr.Error()) + return nil, newErr + } + + tagList, err := tagHandler.getTagListWithResId(resType, &resIID.SystemId) + if err != nil { + newErr := fmt.Errorf("Failed to Get the Tag List with the Resource ID : [%v]", err) + cblogger.Debug(newErr.Error()) + return nil, newErr + } + + var kvList []irs.KeyValue + if len(tagList) > 0 { + for _, curTag := range tagList { + kv := irs.KeyValue { + Key : curTag.Key, + Value: curTag.Value, + } + kvList = append(kvList, kv) + } + } + return kvList, nil +} + +func (tagHandler *KtCloudTagHandler) GetTag(resType irs.RSType, resIID irs.IID, key string) (irs.KeyValue, error) { + cblogger.Info("KT Cloud driver: called GetTag()!") + + if resType == "" { + newErr := fmt.Errorf("Invalid Resource Type!!") + cblogger.Error(newErr.Error()) + return irs.KeyValue{}, newErr + } + + if strings.EqualFold(resIID.SystemId, "") { + newErr := fmt.Errorf("Invalid Resource SystemId!!") + cblogger.Error(newErr.Error()) + return irs.KeyValue{}, newErr + } + + tagValue, err := tagHandler.getTagValue(resType, &resIID.SystemId, &key) + if err != nil { + newErr := fmt.Errorf("Failed to Get the Tag Value with the Key : [%s], [%v]", key, err) + cblogger.Error(newErr.Error()) + return irs.KeyValue{}, newErr + } + + tagKV := irs.KeyValue{ + Key: key, + Value: tagValue, + } + return tagKV, nil +} + +func (tagHandler *KtCloudTagHandler) RemoveTag(resType irs.RSType, resIID irs.IID, key string) (bool, error) { + cblogger.Info("KT Cloud driver: called RemoveTag()!") + + if resType == "" { + newErr := fmt.Errorf("Invalid Resource Type!!") + cblogger.Error(newErr.Error()) + return false, newErr + } + + if strings.EqualFold(resIID.SystemId, "") { + newErr := fmt.Errorf("Invalid Resource SystemId!!") + cblogger.Error(newErr.Error()) + return false, newErr + } + + if strings.EqualFold(key, "") { + newErr := fmt.Errorf("Invalid 'key' value!!") + cblogger.Error(newErr.Error()) + return false, newErr + } + + value, err := tagHandler.getTagValue(resType, &resIID.SystemId, &key) + if err != nil { + newErr := fmt.Errorf("Failed to Get the Tag Value with the Key [%v]", err) + cblogger.Error(newErr.Error()) + return false, newErr + } + + tagKV := ktsdk.TagArg{ + Key: key, + Value: value, + } + _, delErr := tagHandler.deleteTag(resType, &resIID.SystemId, tagKV) + if delErr != nil { + newErr := fmt.Errorf("Failed to Remove the Tag with the Key : [%v]", delErr) + cblogger.Error(newErr.Error()) + return false, newErr + } + + return true, nil +} + +func (tagHandler *KtCloudTagHandler) FindTag(resType irs.RSType, keyword string) ([]*irs.TagInfo, error) { + cblogger.Info("KT Cloud driver: called FindTag()!") + + if resType == "" { + newErr := fmt.Errorf("Invalid Resource Type!!") + cblogger.Error(newErr.Error()) + return nil, newErr + } + + if strings.EqualFold(keyword, "") { + newErr := fmt.Errorf("Invalid Keyword!!") + cblogger.Error(newErr.Error()) + return nil, newErr + } + + tagList, err := tagHandler.getTagList(resType) + if err != nil { + newErr := fmt.Errorf("Failed to Get Tag List : [%v]", err) + cblogger.Error(newErr.Error()) + return nil, newErr + } + + var tagInfoList []*irs.TagInfo + for _, curTag := range tagList { + if strings.Contains(curTag.Key, keyword) || strings.Contains(curTag.Value, keyword) { + var tagKVList []irs.KeyValue + tagKV := irs.KeyValue { + Key: curTag.Key, + Value: curTag.Value, + } + tagKVList = append(tagKVList, tagKV) + + tagInfo := &irs.TagInfo { + ResType : resType, + ResIId : irs.IID { + NameId : "", + SystemId: curTag.ResourceId, + }, + TagList : tagKVList, + // KeyValueList: , // reserved for optinal usage + } + tagInfoList = append(tagInfoList, tagInfo) + } + } + return tagInfoList, nil +} + +func (tagHandler *KtCloudTagHandler) createTagList(resType irs.RSType, resID *string, tagList []irs.KeyValue) (bool, error) { + cblogger.Info("KT Cloud driver: called createTagList()!") + + var rsType string + switch resType { + case irs.VM: + rsType = VM + case irs.MYIMAGE: + rsType = MyImage + case irs.DISK: + rsType = DISK + default: + newErr := fmt.Errorf("Invalid Resource Type. [%v] type is Not Supported on KT Cloud for Tagging!!", resType) + cblogger.Debug(newErr.Error()) + return false, newErr + } + + var tagKVs []ktsdk.TagArg + for _, curTag := range tagList { + tagKV := ktsdk.TagArg { + Key: curTag.Key, + Value: curTag.Value, + } + tagKVs = append(tagKVs, tagKV) + } + + tagReq := ktsdk.CreateTagsReqInfo { + ResourceType: rsType, + ResourceIds: []string{*resID, }, + Tags: tagKVs, + } + tagResult, err := tagHandler.Client.CreateTags(&tagReq) + if err != nil { + newErr := fmt.Errorf("Failed to Create the Tag List on the Resource : [%v]", err) + cblogger.Error(newErr.Error()) + return false, newErr + } + + cblogger.Info("### Waiting for Tags to be Created(300sec)!!\n") + waitJobErr := tagHandler.Client.WaitForAsyncJob(tagResult.Createtagsresponse.JobId, 300000000000) + if waitJobErr != nil { + cblogger.Errorf("Failed to Wait the Job : [%v]", waitJobErr) + return false, waitJobErr + } + + _, jobErr := tagHandler.Client.QueryAsyncJobResult(tagResult.Createtagsresponse.JobId) + if err != nil { + cblogger.Errorf("Failed to Find the Job: [%v]", jobErr) + return false, jobErr + } + + return true, nil +} + +func (tagHandler *KtCloudTagHandler) getTagList(resType irs.RSType) ([]ktsdk.Tag, error) { + cblogger.Info("KT Cloud driver: called getTagList()!") + + var rsType string + switch resType { + case irs.VM: + rsType = VM + case irs.MYIMAGE: + rsType = MyImage + case irs.DISK: + rsType = DISK + default: + newErr := fmt.Errorf("Invalid Resource Type. [%v] type is Not Supported on KT Cloud for Tagging!!", resType) + cblogger.Debug(newErr.Error()) + return nil, newErr + } + + tagReq := ktsdk.ListTagsReqInfo { + ResourceType: rsType, + } + result, err := tagHandler.Client.ListTags(&tagReq) + if err != nil { + newErr := fmt.Errorf("Failed to Get Tag List from KT Cloud : [%v]", err) + cblogger.Error(newErr.Error()) + return nil, newErr + } + if len(result.Listtagsresponse.Tag) < 1 { + newErr := fmt.Errorf("Failed to Find Any Tag with the Resource Type!!") + cblogger.Debug(newErr.Error()) + return nil, nil // Caution!! + } + return result.Listtagsresponse.Tag, nil +} + +func (tagHandler *KtCloudTagHandler) getTagListWithResId(resType irs.RSType, resourceID *string) ([]ktsdk.Tag, error) { + cblogger.Info("KT Cloud driver: called getTagListWithResId()!") + + var rsType string + switch resType { + case irs.VM: + rsType = VM + case irs.MYIMAGE: + rsType = MyImage + case irs.DISK: + rsType = DISK + default: + newErr := fmt.Errorf("Invalid Resource Type. [%v] type is Not Supported on KT Cloud for Tagging!!", resType) + cblogger.Debug(newErr.Error()) + return nil, newErr + } + + tagReq := ktsdk.ListTagsReqInfo { + ResourceType: rsType, + ResourceIds: *resourceID, + } + result, err := tagHandler.Client.ListTags(&tagReq) + if err != nil { + newErr := fmt.Errorf("Failed to Get Tag List from KT Cloud : [%v]", err) + cblogger.Error(newErr.Error()) + return nil, newErr + } + if len(result.Listtagsresponse.Tag) < 1 { + newErr := fmt.Errorf("Failed to Find Any Tag with the Resource ID!!") + cblogger.Debug(newErr.Error()) + return nil, nil // Caution!! + } + return result.Listtagsresponse.Tag, nil +} + +func (tagHandler *KtCloudTagHandler) getTagValue(resType irs.RSType, resourceID *string, key *string) (string, error) { + cblogger.Info("KT Cloud driver: called getTagValue()!") + + var rsType string + switch resType { + case irs.VM: + rsType = VM + case irs.MYIMAGE: + rsType = MyImage + case irs.DISK: + rsType = DISK + default: + newErr := fmt.Errorf("Invalid Resource Type. [%v] type is Not Supported on KT Cloud for Tagging!!", resType) + cblogger.Debug(newErr.Error()) + return "", newErr + } + + tagReq := ktsdk.ListTagsReqInfo { + ResourceType: rsType, + ResourceIds: *resourceID, + Key: *key, + } + result, err := tagHandler.Client.ListTags(&tagReq) + if err != nil { + newErr := fmt.Errorf("Failed to Get Tag List from KT Cloud : [%v]", err) + cblogger.Error(newErr.Error()) + return "", newErr + } + if len(result.Listtagsresponse.Tag) < 1 { + newErr := fmt.Errorf("Failed to Find Any Tag with the Resource ID and the Key!!") + cblogger.Debug(newErr.Error()) + return "", newErr + } + return result.Listtagsresponse.Tag[0].Value, nil +} + +func (tagHandler *KtCloudTagHandler) deleteTag(resType irs.RSType, resID *string, keyValue ktsdk.TagArg) (bool, error) { + cblogger.Info("KT Cloud driver: called deleteTag()!") + + var rsType string + switch resType { + case irs.VM: + rsType = VM + case irs.MYIMAGE: + rsType = MyImage + case irs.DISK: + rsType = DISK + default: + newErr := fmt.Errorf("Invalid Resource Type. [%v] type is Not Supported on KT Cloud for Tagging!!", resType) + cblogger.Debug(newErr.Error()) + return false, newErr + } + + var tagKVs []ktsdk.TagArg + tagKV := ktsdk.TagArg { + Key: keyValue.Key, + Value: keyValue.Value, + } + tagKVs = append(tagKVs, tagKV) + + tagReq := ktsdk.DeleteTagsReqInfo { + ResourceType: rsType, + ResourceIds: []string{*resID, }, + Tags: tagKVs, + } + _, err := tagHandler.Client.DeleteTags(&tagReq) + if err != nil { + newErr := fmt.Errorf("Failed to Delete the Tag with the Key on the Resource : [%v]", err) + cblogger.Error(newErr.Error()) + return false, newErr + } + + return true, nil +} diff --git a/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/VMHandler.go b/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/VMHandler.go index 71886af82..ebe4158fe 100644 --- a/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/VMHandler.go +++ b/cloud-control-manager/cloud-driver/drivers/ktcloud/resources/VMHandler.go @@ -229,7 +229,7 @@ func (vmHandler *KtCloudVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo, } } } - cblogger.Infof("init UserData : [%s]", *initUserData) + // cblogger.Infof("init UserData : [%s]", *initUserData) // # To Check if the Requested S/G exits var sgSystemIDs []string @@ -423,7 +423,7 @@ func (vmHandler *KtCloudVMHandler) StartVM(vmReqInfo irs.VMReqInfo) (irs.VMInfo, }, } - createTagsReq := ktsdk.CreateTags { + createTagsReq := ktsdk.CreateTagsReqInfo { ResourceIds: []string{newVM.Deployvirtualmachineresponse.ID, }, ResourceType: "userVm", Tags: vmTags, @@ -685,6 +685,30 @@ func (vmHandler *KtCloudVMHandler) mappingVMInfo(KtCloudInstance *ktsdk.Virtualm vmInfo.Region.Zone = KtCloudInstance.ZoneName } } + + // Get the Tag List of the VM + var kvList []irs.KeyValue + tagHandler := KtCloudTagHandler { + RegionInfo: vmHandler.RegionInfo, + Client: vmHandler.Client, + } + tagList, err := tagHandler.getTagListWithResId(irs.RSType(irs.VM), &KtCloudInstance.ID) + if err != nil { + newErr := fmt.Errorf("Failed to Get the Tag List with the VM SystemID : [%v]", err) + cblogger.Error(newErr.Error()) + return irs.VMInfo{}, newErr + } + if len(tagList) > 0 { + for _, curTag := range tagList { + kv := irs.KeyValue { + Key : curTag.Key, + Value: curTag.Value, + } + kvList = append(kvList, kv) + } + vmInfo.TagList = kvList + } + return vmInfo, nil } @@ -1526,7 +1550,7 @@ func (vmHandler *KtCloudVMHandler) disassociatePublicIp(publicIpId string) (irs. } func (vmHandler *KtCloudVMHandler) getVPCFromTags(instanceId string) (string, error) { - listTagsReq := ktsdk.ListTags { + listTagsReq := ktsdk.ListTagsReqInfo { Key: "vpcId", ResourceType: "userVm", ResourceIds: instanceId, @@ -1547,7 +1571,7 @@ func (vmHandler *KtCloudVMHandler) getVPCFromTags(instanceId string) (string, er } func (vmHandler *KtCloudVMHandler) getSubnetFromTags(instanceId string) (string, error) { - listTagsReq := ktsdk.ListTags { + listTagsReq := ktsdk.ListTagsReqInfo { Key: "subnetId", ResourceType: "userVm", ResourceIds: instanceId, @@ -1568,7 +1592,7 @@ func (vmHandler *KtCloudVMHandler) getSubnetFromTags(instanceId string) (string, } func (vmHandler *KtCloudVMHandler) getVMSpecFromTags(instanceId string) (string, error) { - listTagsReq := ktsdk.ListTags { + listTagsReq := ktsdk.ListTagsReqInfo { Key: "vmSpecId", ResourceType: "userVm", ResourceIds: instanceId, @@ -1588,7 +1612,7 @@ func (vmHandler *KtCloudVMHandler) getVMSpecFromTags(instanceId string) (string, } func (vmHandler *KtCloudVMHandler) getSGListFromTags(instanceId string) ([]irs.IID, error) { - listTagsReq := ktsdk.ListTags { + listTagsReq := ktsdk.ListTagsReqInfo { Key: "SecurityGroups", ResourceType: "userVm", ResourceIds: instanceId,