Skip to content

Commit

Permalink
TagHandler in Development
Browse files Browse the repository at this point in the history
  • Loading branch information
dev4unet committed Jul 10, 2024
1 parent 42f63de commit 785a614
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 10 deletions.
3 changes: 3 additions & 0 deletions cloud-control-manager/cloud-driver/drivers/aws/AwsDriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (AwsDriver) GetDriverCapability() idrv.DriverCapabilityInfo {
drvCapabilityInfo.NLBHandler = true
drvCapabilityInfo.RegionZoneHandler = true
drvCapabilityInfo.PriceInfoHandler = true
drvCapabilityInfo.TagHandler = true

return drvCapabilityInfo
}
Expand Down Expand Up @@ -254,6 +255,8 @@ func (driver *AwsDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (icon.

// Connection for AnyCall
AnyCallClient: vmClient,

TagClient: vmClient,
}

return &iConn, nil // return type: (icon.CloudConnection, error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
package connect

import (
"errors"

cblog "github.com/cloud-barista/cb-log"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"

Expand Down Expand Up @@ -63,6 +61,7 @@ type AwsCloudConnection struct {
AutoScalingClient *autoscaling.AutoScaling

AnyCallClient *ec2.EC2
TagClient *ec2.EC2
}

var cblogger *logrus.Logger
Expand Down Expand Up @@ -111,6 +110,11 @@ func (cloudConn *AwsCloudConnection) CreateSecurityHandler() (irs.SecurityHandle
return &handler, nil
}

func (cloudConn *AwsCloudConnection) CreateTagHandler() (irs.TagHandler, error) {
handler := ars.AwsTagHandler{cloudConn.Region, cloudConn.VMClient}
return &handler, nil
}

/*
func (cloudConn *AwsCloudConnection) CreateVNicHandler() (irs.VNicHandler, error) {
cblogger.Info("Start")
Expand Down Expand Up @@ -182,7 +186,3 @@ func (cloudConn *AwsCloudConnection) CreatePriceInfoHandler() (irs.PriceInfoHand
handler := ars.AwsPriceInfoHandler{cloudConn.Region, cloudConn.PriceInfoClient}
return &handler, nil
}

func (cloudConn *AwsCloudConnection) CreateTagHandler() (irs.TagHandler, error) {
return nil, errors.New("AWS Driver: not implemented")
}
104 changes: 100 additions & 4 deletions cloud-control-manager/cloud-driver/drivers/aws/main/Test_Resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,100 @@ func handlePriceInfo() {
}
}

// Test AMI
func handleTag() {
cblogger.Debug("Start TagHandler Resource Test")

ResourceHandler, err := getResourceHandler("Tag")
if err != nil {
panic(err)
}
handler := ResourceHandler.(irs.TagHandler)

var reqType irs.RSType = irs.VM
reqIID := irs.IID{SystemId: "i-02ac1c4ff1d40815c"}
reqTag := irs.KeyValue{Key: "tag3", Value: "태그3"}
reqKey := "Name"

for {
fmt.Println("TagHandler Management")
fmt.Println("0. Quit")
fmt.Println("1. Tag List")
fmt.Println("2. Tag Add")
fmt.Println("3. Tag Get")
fmt.Println("4. Tag Delete")
fmt.Println("5. Tag Find")
fmt.Println("6. Tag Create")

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

if inputCnt == 1 {
switch commandNum {
case 0:
return

case 1:
cblogger.Infof("조회 요청 태그 타입 : [%s]", reqType)
if reqType == irs.VM {
cblogger.Debug("VM 요청됨")
}

result, err := handler.ListTag(reqType, reqIID)
if err != nil {
cblogger.Info(" Tag 목록 조회 실패 : ", err)
} else {
cblogger.Info("Tag 목록 조회 결과")
cblogger.Debug(result)
cblogger.Infof("로그 레벨 : [%s]", cblog.GetLevel())
//spew.Dump(result)
cblogger.Info("출력 결과 수 : ", len(result))

//조회및 삭제 테스트를 위해 리스트의 첫번째 정보의 ID를 요청ID로 자동 갱신함.
if result != nil {
//tagReqInfo.IId = result[0].IId // 조회 및 삭제를 위해 생성된 ID로 변경
}
}

case 2:
cblogger.Infof("[%s] Tag 추가 테스트", reqIID.SystemId)
result, err := handler.AddTag(reqType, reqIID, reqTag)
if err != nil {
cblogger.Infof(reqIID.SystemId, " Tag 생성 실패 : ", err)
} else {
cblogger.Info("Tag 생성 결과 : ", result)
spew.Dump(result)
}

case 3:
cblogger.Infof("[%s] Tag 조회 테스트 - Key[%s]", reqIID.SystemId, reqKey)
result, err := handler.GetTag(reqType, reqIID, reqKey)
if err != nil {
cblogger.Infof("[%s] Tag 조회 실패 : [%v]", reqKey, err)
} else {
cblogger.Infof("[%s] Tag 조회 결과 : [%s]", reqKey, result)
spew.Dump(result)
}

case 4:
cblogger.Infof("[%s] Tag 삭제 테스트 - Key[%s]", reqIID.SystemId, reqKey)
result, err := handler.RemoveTag(reqType, reqIID, reqKey)
if err != nil {
cblogger.Infof("[%s] Tag 삭제 실패 : [%v]", reqKey, err)
} else {
cblogger.Infof("[%s] Tag 삭제 결과 : [%v]", reqKey, result)
}

case 7:

}
}
}
}

// handlerType : resources폴더의 xxxHandler.go에서 Handler이전까지의 문자열
// (예) ImageHandler.go -> "Image"
func getResourceHandler(handlerType string) (interface{}, error) {
Expand Down Expand Up @@ -1826,6 +1920,8 @@ func getResourceHandler(handlerType string) (interface{}, error) {
resourceHandler, err = cloudConnection.CreateRegionZoneHandler()
case "PriceInfo":
resourceHandler, err = cloudConnection.CreatePriceInfoHandler()
case "Tag":
resourceHandler, err = cloudConnection.CreateTagHandler()
}

if err != nil {
Expand Down Expand Up @@ -1933,7 +2029,7 @@ func readConfigFile() Config {
//cblogger.Infof("최종 환경 설정파일 경로 : [%s]", rootPath+"/config/config.yaml")
//data, err := ioutil.ReadFile(rootPath + "/config/config.yaml")
//data, err = ioutil.ReadFile("/Users/mzc01-swy/projects/feature_aws_filter_swy_240130/cloud-control-manager/cloud-driver/drivers/aws/main/Sample/config/config.yaml")

data, err := ioutil.ReadFile(confPath)
if err != nil {
panic(err)
Expand All @@ -1951,8 +2047,7 @@ func readConfigFile() Config {
}

func main() {
cblogger.Info("AWS Resource Test")
// handleVPC()
//handleVPC()
// handleKeyPair()
// handlePublicIP() // PublicIP 생성 후 conf
// handleSecurity()
Expand All @@ -1963,5 +2058,6 @@ func main() {
// handleNLB()
// handleCluster()
//handleRegionZone()
handlePriceInfo()
//handlePriceInfo()
handleTag()
}
133 changes: 133 additions & 0 deletions cloud-control-manager/cloud-driver/drivers/aws/resources/TagHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// 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
//
// This is Resouces interfaces of Cloud Driver.
//
// by [email protected]

// https://github.com/cloud-barista/cb-spider/wiki/Tag-and-Cloud-Driver-API
package resources

import (
//"errors"
//"reflect"
//"strconv"

"errors"

"github.com/aws/aws-sdk-go/aws"
//"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
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 AwsTagHandler struct {
Region idrv.RegionInfo
Client *ec2.EC2
}

func (tagHandler *AwsTagHandler) AddTag(resType irs.RSType, resIID irs.IID, tag irs.KeyValue) (irs.KeyValue, error) {
cblogger.Debugf("Req resTyp:[%s] / resIID:[%s] / Tag Key:[%s] / Tag Value:[%s]", resType, resIID, tag.Key, tag.Value)

if resIID.SystemId == "" {
msg := "tag will not be add because resIID.SystemId is not provided"
cblogger.Error(msg)
return irs.KeyValue{}, errors.New(msg)
}

hiscallInfo := GetCallLogScheme(tagHandler.Region, call.TAG, resIID.SystemId, "DescribeTags()")
start := call.Start()

// 리소스에 신규 태그 추가
result, errtag := tagHandler.Client.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{&resIID.SystemId},
Tags: []*ec2.Tag{
{
Key: aws.String(tag.Key),
Value: aws.String(tag.Value),
},
},
})

if errtag != nil {
cblogger.Errorf("Failed to set Name Tag for [%s] VM", resIID.NameId)
cblogger.Error(errtag)
return irs.KeyValue{}, errtag
}
LoggingInfo(hiscallInfo, start)

if cblogger.Level.String() == "debug" {
cblogger.Info(result)
}

return tag, nil
}

func (tagHandler *AwsTagHandler) ListTag(resType irs.RSType, resIID irs.IID) ([]irs.KeyValue, error) {
cblogger.Debugf("Req resTyp:[%s] / resIID:[%s]", resType, resIID)

input := &ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: []*string{
aws.String(resIID.SystemId),
},
},
},
}
if cblogger.Level.String() == "debug" {
cblogger.Debug(input)
}

hiscallInfo := GetCallLogScheme(tagHandler.Region, call.TAG, resIID.SystemId, "DescribeTags()")
start := call.Start()

result, errtag := tagHandler.Client.DescribeTags(input)
if errtag != nil {
cblogger.Errorf("Failed to set Name Tag for [%s] VM", resIID.NameId)
cblogger.Error(errtag)
LoggingError(hiscallInfo, errtag)
return nil, errtag
}
LoggingInfo(hiscallInfo, start)

if cblogger.Level.String() == "debug" {
cblogger.Info(result)
}

return nil, nil
}

// describetags
func (tagHandler *AwsTagHandler) GetTag(resType irs.RSType, resIID irs.IID, key string) (irs.KeyValue, error) {
cblogger.Info("resTyp : ", resType)
cblogger.Info("resIID : ", resIID)
cblogger.Info("key : ", key)

return irs.KeyValue{}, nil
}

// deletetags
func (tagHandler *AwsTagHandler) RemoveTag(resType irs.RSType, resIID irs.IID, key string) (bool, error) {
cblogger.Info("resTyp : ", resType)
cblogger.Info("resIID : ", resIID)
cblogger.Info("key : ", key)

return false, nil
}

// Find tags by tag key or value
// resType: ALL | VPC, SUBNET, etc.,.
// keyword: The keyword to search for in the tag key or value.
// if you want to find all tags, set keyword to "" or "*".
func (tagHandler *AwsTagHandler) FindTag(resType irs.RSType, keyword string) ([]*irs.TagInfo, error) {
cblogger.Info("resTyp : ", resType)
cblogger.Info("keyword : ", keyword)
return nil, nil
}

0 comments on commit 785a614

Please sign in to comment.