Skip to content

Commit

Permalink
Merge pull request #1258 from ish-hcc/tag_ibm
Browse files Browse the repository at this point in the history
IBM: Implement TagHandler
  • Loading branch information
powerkimhub authored Jul 27, 2024
2 parents 0634696 + 3d38547 commit 7036edf
Show file tree
Hide file tree
Showing 8 changed files with 967 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package ibmcloudvpc
import (
"context"
"errors"
"github.com/IBM/platform-services-go-sdk/globalsearchv2"
"time"

"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/globaltaggingv1"
vpc0230 "github.com/IBM/vpc-go-sdk/0.23.0/vpcv1"
Expand All @@ -12,7 +15,6 @@ import (
"github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/ibmcloud-vpc/utils/kubernetesserviceapiv1"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
icon "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/connect"
"time"
)

type IbmCloudDriver struct{}
Expand All @@ -38,6 +40,7 @@ func (IbmCloudDriver) GetDriverCapability() idrv.DriverCapabilityInfo {
drvCapabilityInfo.NLBHandler = true
drvCapabilityInfo.RegionZoneHandler = true
drvCapabilityInfo.PriceInfoHandler = true
drvCapabilityInfo.TagHandler = true

return drvCapabilityInfo
}
Expand Down Expand Up @@ -109,6 +112,14 @@ func (driver *IbmCloudDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (
if err != nil {
return nil, err
}
searchService, err := globalsearchv2.NewGlobalSearchV2(&globalsearchv2.GlobalSearchV2Options{
Authenticator: &core.IamAuthenticator{
ApiKey: connectionInfo.CredentialInfo.ApiKey,
},
})
if err != nil {
return nil, err
}

iConn := connect.IbmCloudConnection{
CredentialInfo: connectionInfo.CredentialInfo,
Expand All @@ -117,6 +128,7 @@ func (driver *IbmCloudDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (
VpcService0230: vpcService0230,
ClusterService: clusterService,
TaggingService: taggingService,
SearchService: searchService,
Ctx: ctx,
}
return &iConn, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connect
import (
"context"
"errors"
"github.com/IBM/platform-services-go-sdk/globalsearchv2"

"github.com/IBM/platform-services-go-sdk/globaltaggingv1"
vpcv0230 "github.com/IBM/vpc-go-sdk/0.23.0/vpcv1"
Expand All @@ -28,6 +29,7 @@ type IbmCloudConnection struct {
VpcService *vpcv1.VpcV1
ClusterService *kubernetesserviceapiv1.KubernetesServiceApiV1
TaggingService *globaltaggingv1.GlobalTaggingV1
SearchService *globalsearchv2.GlobalSearchV2
VpcService0230 *vpcv0230.VpcV1
Ctx context.Context
}
Expand Down Expand Up @@ -175,5 +177,15 @@ func (cloudConn *IbmCloudConnection) CreatePriceInfoHandler() (irs.PriceInfoHand
}

func (cloudConn *IbmCloudConnection) CreateTagHandler() (irs.TagHandler, error) {
return nil, errors.New("Ibm Driver: not implemented")
cblogger.Info("Ibm Cloud Driver: called CreateTagHandler()!")
TagHandler := ibmrs.IbmTagHandler{
CredentialInfo: cloudConn.CredentialInfo,
Region: cloudConn.Region,
VpcService: cloudConn.VpcService,
ClusterService: cloudConn.ClusterService,
Ctx: cloudConn.Ctx,
TaggingService: cloudConn.TaggingService,
SearchService: cloudConn.SearchService,
}
return &TagHandler, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"

cblog "github.com/cloud-barista/cb-log"
ibm "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/ibmcloud-vpc"
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"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"strconv"
"strings"
)

type Config struct {
Expand Down Expand Up @@ -256,7 +257,8 @@ func showTestHandlerInfo() {
cblogger.Info("10. RegionZoneHandler")
cblogger.Info("11. PriceInfoHandler")
cblogger.Info("12. ClusterHandler")
cblogger.Info("13. Exit")
cblogger.Info("13. TagHandler")
cblogger.Info("14. Exit")
cblogger.Info("==========================================================")
}

Expand Down Expand Up @@ -307,6 +309,8 @@ func getResourceHandler(resourceType string, config Config) (interface{}, error)
resourceHandler, err = ibmCon.CreatePriceInfoHandler()
case "cluster":
resourceHandler, err = ibmCon.CreateClusterHandler()
case "tag":
resourceHandler, err = ibmCon.CreateTagHandler()
}

return resourceHandler, nil
Expand Down Expand Up @@ -1716,6 +1720,259 @@ Loop:
}
}

func testTagHandlerListPrint() {
cblogger.Info("Test TagHandler")
cblogger.Info("0. Print Menu")
cblogger.Info("1. AddTag()")
cblogger.Info("2. ListTag()")
cblogger.Info("3. GetTag()")
cblogger.Info("4. RemoveTag()")
cblogger.Info("5. FindTag()")
cblogger.Info("6. Exit")
}

func inputToRSType(input string) irs.RSType {
switch input {
case "all":
return irs.ALL
case "image":
return irs.IMAGE
case "vpc":
return irs.VPC
case "subnet":
return irs.SUBNET
case "sg":
return irs.SG
case "keypair":
return irs.KEY
case "vm":
return irs.VM
case "nlb":
return irs.NLB
case "disk":
return irs.DISK
case "myimage":
return irs.MYIMAGE
case "cluster":
return irs.CLUSTER
case "nodegroup":
return irs.NODEGROUP
default:
return ""
}
}

func testTagHandler(config Config) {
resourceHandler, err := getResourceHandler("tag", config)
if err != nil {
cblogger.Error(err)
return
}
tagHandler := resourceHandler.(irs.TagHandler)

testTagHandlerListPrint()
Loop:
for {
var commandNum int
inputCnt, err := fmt.Scan(&commandNum)
if err != nil {
cblogger.Error(err)
}

if inputCnt == 1 {
switch commandNum {
case 0:
testTagHandlerListPrint()
case 1:
cblogger.Info("Start AddTag() ...")
fmt.Println("=== Enter resource type ===")
in := bufio.NewReader(os.Stdin)
resTypeStr, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resTypeStr = strings.TrimSpace(resTypeStr)
result := irs.RSTypeString(irs.RSType(resTypeStr))
if strings.Contains(result, "not supported") {
cblogger.Error(result)
break Loop
}
resType := inputToRSType(resTypeStr)

fmt.Println("=== Enter name of the resource ===")
in = bufio.NewReader(os.Stdin)
resName, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resName = strings.TrimSpace(resName)

fmt.Println("=== Enter tag's key ===")
in = bufio.NewReader(os.Stdin)
tagKey, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
tagKey = strings.TrimSpace(tagKey)

fmt.Println("=== Enter tag's value ===")
in = bufio.NewReader(os.Stdin)
tagValue, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
tagValue = strings.TrimSpace(tagValue)

if tagKeyValue, err := tagHandler.AddTag(resType, irs.IID{NameId: resName}, irs.KeyValue{Key: tagKey, Value: tagValue}); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagKeyValue)
}
cblogger.Info("Finish AddTag()")
case 2:
cblogger.Info("Start ListTag() ...")
fmt.Println("=== Enter resource type ===")
in := bufio.NewReader(os.Stdin)
resTypeStr, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resTypeStr = strings.TrimSpace(resTypeStr)
result := irs.RSTypeString(irs.RSType(resTypeStr))
if strings.Contains(result, "not supported") {
cblogger.Error(result)
break Loop
}
resType := inputToRSType(resTypeStr)

fmt.Println("=== Enter name of the resource ===")
in = bufio.NewReader(os.Stdin)
resName, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resName = strings.TrimSpace(resName)

if tagList, err := tagHandler.ListTag(resType, irs.IID{NameId: resName}); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagList)
}
cblogger.Info("Finish ListTag()")
case 3:
cblogger.Info("Start GetTag() ...")
fmt.Println("=== Enter resource type ===")
in := bufio.NewReader(os.Stdin)
resTypeStr, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resTypeStr = strings.TrimSpace(resTypeStr)
result := irs.RSTypeString(irs.RSType(resTypeStr))
if strings.Contains(result, "not supported") {
cblogger.Error(result)
break Loop
}
resType := inputToRSType(resTypeStr)

fmt.Println("=== Enter name of the resource ===")
in = bufio.NewReader(os.Stdin)
resName, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resName = strings.TrimSpace(resName)

fmt.Println("=== Enter tag's key ===")
in = bufio.NewReader(os.Stdin)
tagKey, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
tagKey = strings.TrimSpace(tagKey)

if tagKeyValue, err := tagHandler.GetTag(resType, irs.IID{NameId: resName}, tagKey); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagKeyValue)
}
cblogger.Info("Finish GetTag()")
case 4:
cblogger.Info("Start RemoveTag() ...")
fmt.Println("=== Enter resource type ===")
in := bufio.NewReader(os.Stdin)
resTypeStr, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resTypeStr = strings.TrimSpace(resTypeStr)
result := irs.RSTypeString(irs.RSType(resTypeStr))
if strings.Contains(result, "not supported") {
cblogger.Error(result)
break Loop
}
resType := inputToRSType(resTypeStr)

fmt.Println("=== Enter name of the resource ===")
in = bufio.NewReader(os.Stdin)
resName, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resName = strings.TrimSpace(resName)

fmt.Println("=== Enter tag's key ===")
in = bufio.NewReader(os.Stdin)
tagKey, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
tagKey = strings.TrimSpace(tagKey)

if tagKeyValue, err := tagHandler.RemoveTag(resType, irs.IID{NameId: resName}, tagKey); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagKeyValue)
}
cblogger.Info("Finish RemoveTag()")
case 5:
cblogger.Info("Start FindTag() ...")
fmt.Println("=== Enter resource type ===")
in := bufio.NewReader(os.Stdin)
resTypeStr, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
resTypeStr = strings.TrimSpace(resTypeStr)
result := irs.RSTypeString(irs.RSType(resTypeStr))
if strings.Contains(result, "not supported") {
cblogger.Error(result)
break Loop
}
resType := inputToRSType(resTypeStr)

fmt.Println("=== Enter keyword ===")
in = bufio.NewReader(os.Stdin)
keyword, err := in.ReadString('\n')
if err != nil {
cblogger.Error(err)
}
keyword = strings.TrimSpace(keyword)

if tagKeyValue, err := tagHandler.FindTag(resType, keyword); err != nil {
cblogger.Error(err)
} else {
spew.Dump(tagKeyValue)
}
cblogger.Info("Finish FindTag()")
case 6:
cblogger.Info("Exit")
break Loop
}
}
}
}

func main() {
showTestHandlerInfo()
config := readConfigFile()
Expand Down Expand Up @@ -1768,6 +2025,9 @@ Loop:
testClusterHandler(config)
showTestHandlerInfo()
case 13:
testTagHandler(config)
showTestHandlerInfo()
case 14:
cblogger.Info("Exit Test ResourceHandler Program")
break Loop
}
Expand Down
Loading

0 comments on commit 7036edf

Please sign in to comment.