From 669f50646e50ee26a5372107b60a40689013b9b4 Mon Sep 17 00:00:00 2001 From: SungWoongz Date: Wed, 24 Jul 2024 14:18:51 +0900 Subject: [PATCH] =?UTF-8?q?ALL,=20=EB=A6=AC=EC=86=8C=EC=8A=A4=20=EB=B3=84?= =?UTF-8?q?=20find=20tag=20=EC=B6=94=EA=B0=80=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/resources/CommonAlibabaFunc.go | 65 +-- .../alibaba/resources/CommonHandler.go | 264 +++++++++++- .../drivers/alibaba/resources/TagHandler.go | 384 ++++++++++-------- 3 files changed, 492 insertions(+), 221 deletions(-) diff --git a/cloud-control-manager/cloud-driver/drivers/alibaba/resources/CommonAlibabaFunc.go b/cloud-control-manager/cloud-driver/drivers/alibaba/resources/CommonAlibabaFunc.go index 04ec05622..94ed1317f 100644 --- a/cloud-control-manager/cloud-driver/drivers/alibaba/resources/CommonAlibabaFunc.go +++ b/cloud-control-manager/cloud-driver/drivers/alibaba/resources/CommonAlibabaFunc.go @@ -286,25 +286,26 @@ func GetAlibabaApiVPCEndpoint(regionId string, productCode string) string { // Alibaba에서 사용되는 리소스별 api product type func GetAlibabaProductCode(resType irs.RSType) (string, error) { switch resType { - case irs.RSType("VM"): + case irs.RSType("VM"), irs.VM: return "ecs", nil - case irs.RSType("VPC"): + case irs.RSType("VPC"), irs.VPC: return "vpc", nil - case irs.RSType("SUBNET"): - return "ecs", nil - case irs.RSType("SG"): + case irs.RSType("SUBNET"), irs.SUBNET: + // return "ecs", nil + return "vpc", nil + case irs.RSType("SG"), irs.SG: return "ecs", nil - case irs.RSType("KEY"): + case irs.RSType("KEY"), irs.KEY: return "ecs", nil - case irs.RSType("NLB"): + case irs.RSType("NLB"), irs.NLB: return "slb", nil - case irs.RSType("DISK"): + case irs.RSType("DISK"), irs.DISK: return "ecs", nil - case irs.RSType("MYIMAGE"): + case irs.RSType("MYIMAGE"), irs.MYIMAGE: return "ecs", nil - case irs.RSType("CLUSTER"): + case irs.RSType("CLUSTER"), irs.CLUSTER: return "ack", nil - case irs.RSType("NODEGROUP"): + case irs.RSType("NODEGROUP"), irs.NODEGROUP: return "ack", nil default: //return "", nil @@ -315,24 +316,28 @@ func GetAlibabaProductCode(resType irs.RSType) (string, error) { // cb-spider의 resourceType 을 alibaba의 resourceType으로 func GetAlibabaResourceType(resType irs.RSType) (string, error) { switch resType { - case irs.RSType("VM"): + case irs.RSType("VM"), irs.VM: return "instance", nil - // case irs.RSType("VPC"): - // return "vpc", nil - // case irs.RSType("SUBNET"): - // return "ecs", nil - case irs.RSType("SG"): + case irs.RSType("VPC"), irs.VPC: + return "vpc", nil + case irs.RSType("SUBNET"), irs.SUBNET: + // return "ecs", nil + return "vpc", nil + case irs.RSType("SG"), irs.SG: return "securitygroup", nil - case irs.RSType("KEY"): + case irs.RSType("KEY"), irs.KEY: return "keypair", nil // case irs.RSType("NLB"): // return "slb", nil - case irs.RSType("DISK"): + case irs.RSType("DISK"), irs.DISK: return "disk", nil - case irs.RSType("MYIMAGE"): - return "snapshot", nil - case irs.RSType("CLUSTER"): + case irs.RSType("MYIMAGE"), irs.MYIMAGE: + // return "snapshot", nil + return "ecs", nil + case irs.RSType("CLUSTER"), irs.CLUSTER: return "CLUSTER", nil + case irs.RSType("ALL"), irs.ALL: + return "", nil // case irs.RSType("NODEGROUP"): // return "", nil default: @@ -348,27 +353,29 @@ func GetAlibabaResourceType(resType irs.RSType) (string, error) { return "", errors.New("not found ResourceType " + string(resType)) } -// resource Type별로 바로보는 api가 다름. ( ecs, bss, ... ) +// resource Type별로 바라보는 api가 다름. ( ecs, bss, ... ) func GetAliTargetApi(resType irs.RSType) (string, error) { switch resType { case irs.RSType("VM"): return "ecs", nil - // case irs.RSType("VPC"): - // return "vpc", nil - // case irs.RSType("SUBNET"): - // return "ecs", nil + case irs.RSType("VPC"): + return "vpc", nil + case irs.RSType("SUBNET"): + return "vpc", nil case irs.RSType("SG"): return "ecs", nil case irs.RSType("KEY"): return "ecs", nil - // case irs.RSType("NLB"): - // return "slb", nil + case irs.RSType("NLB"): + return "slb", nil case irs.RSType("DISK"): return "ecs", nil case irs.RSType("MYIMAGE"): return "ecs", nil case irs.RSType("CLUSTER"): return "cs", nil + case irs.RSType("ALL"): + return "all", nil // case irs.RSType("NODEGROUP"): // return "", nil default: diff --git a/cloud-control-manager/cloud-driver/drivers/alibaba/resources/CommonHandler.go b/cloud-control-manager/cloud-driver/drivers/alibaba/resources/CommonHandler.go index 6d627b90c..2eeb8d2a4 100644 --- a/cloud-control-manager/cloud-driver/drivers/alibaba/resources/CommonHandler.go +++ b/cloud-control-manager/cloud-driver/drivers/alibaba/resources/CommonHandler.go @@ -358,18 +358,20 @@ func DescribeImages(client *ecs.Client, regionInfo idrv.RegionInfo, imageIIDs [] request.RegionId = regionInfo.Region var imageIIDList []string - for _, imageIID := range imageIIDs { - imageIIDList = append(imageIIDList, imageIID.SystemId) - } + if imageIIDs != nil { + for _, imageIID := range imageIIDs { + imageIIDList = append(imageIIDList, imageIID.SystemId) + } - if len(imageIIDList) > 0 { - request.ImageId = strings.Join(imageIIDList, ",") + if len(imageIIDList) > 0 { + request.ImageId = strings.Join(imageIIDList, ",") + } } - // MyImage 여부 if isMyImage { request.ImageOwnerAlias = "self" } + //cblogger.Debug(request) result, err := client.DescribeImages(request) if err != nil { @@ -1033,8 +1035,11 @@ func DescribeDescribeVpcTags(client *vpc.Client, regionInfo idrv.RegionInfo, res // EcsRequest : Elastic Compute Service(ECS) func CallEcsRequest(resType irs.RSType, client *ecs.Client, regionInfo idrv.RegionInfo, apiName string, queryParams map[string]string) (*responses.CommonResponse, error) { regionID := regionInfo.Region - - apiProductCode, err := GetAlibabaProductCode(irs.RSType(resType)) + apiProductCode, err := GetAlibabaProductCode(resType) + if err != nil { + cblogger.Error(err.Error()) + return nil, err + } // call logger set callogger := call.GetLogger("HISCALL") @@ -1052,26 +1057,23 @@ func CallEcsRequest(resType irs.RSType, client *ecs.Client, regionInfo idrv.Regi request.Method = "POST" request.Scheme = "https" // https | http - //request.Domain = "ecs.cn-hongkong.aliyuncs.com" request.Domain = GetAlibabaApiEndPoint(regionID, apiProductCode) request.Version = "2014-05-26" request.ApiName = apiName - request.QueryParams["RegionId"] = regionID + request.QueryParams = queryParams - // Tag가 있으면 - if queryParams != nil { - request.QueryParams = queryParams - } + cblogger.Debug("API Request : ", request) callLogStart := call.Start() response, err := client.ProcessCommonRequest(request) - - callLogInfo.ElapsedTime = call.Elapsed(callLogStart) - callogger.Info(call.String(callLogInfo)) if err != nil { cblogger.Error(err.Error()) return nil, err } + + callLogInfo.ElapsedTime = call.Elapsed(callLogStart) + callogger.Info(call.String(callLogInfo)) + cblogger.Debug(response.GetHttpContentString()) return response, nil } @@ -1097,9 +1099,8 @@ func CallVpcRequest(resType irs.RSType, client *vpc.Client, regionInfo idrv.Regi request.Method = "POST" request.Scheme = "https" // https | http - //request.Domain = "ecs.cn-hongkong.aliyuncs.com" request.Domain = GetAlibabaApiEndPoint(regionID, apiProductCode) - request.Version = "2014-05-26" + request.Version = "2016-04-28" request.ApiName = apiName request.QueryParams["RegionId"] = regionID @@ -1259,3 +1260,228 @@ func aliRemoveCsTag(csClient *cs.Client, regionInfo idrv.RegionInfo, resType irs return true, nil } + +// ALibaba tag 검색 for ecs +// myimage는 aliMyImageTag() 사용 +func aliEcsTagList(Client *ecs.Client, regionInfo idrv.RegionInfo, alibabaResourceType string, resType irs.RSType, keyword string) ([]*irs.TagInfo, error) { + hiscallInfo := GetCallLogScheme(regionInfo, call.TAG, keyword, "FindTag()") + regionID := regionInfo.Region + var tagInfo []*irs.TagInfo + + queryParams := map[string]string{} + queryParams["RegionId"] = regionID + queryParams["ResourceType"] = alibabaResourceType //string(resType)//keypair + if keyword != "" { + queryParams["Tag.1.Key"] = keyword + cblogger.Info("쿼리", queryParams) + } + + start := call.Start() + response, err := CallEcsRequest(resType, Client, regionInfo, "DescribeResourceByTags", queryParams) + LoggingInfo(hiscallInfo, start) + if err != nil { + cblogger.Error(err.Error()) + LoggingError(hiscallInfo, err) + } + cblogger.Debug(response.GetHttpContentString()) + resResources := AliTagResourcesResponse{} + + tagResponseStr := response.GetHttpContentString() + err = json.Unmarshal([]byte(tagResponseStr), &resResources) + + if err != nil { + cblogger.Error(err.Error()) + return tagInfo, nil + } + + for _, aliTagResource := range resResources.AliTagResources.Resources { + + cblogger.Debug("aliTagResource ", aliTagResource) + aTagInfo, err := ExtractTagResourceInfo(&aliTagResource) + if err != nil { + cblogger.Error(err.Error()) + continue + } + cblogger.Info("aTagInfoaTagInfoaTagInfo", aTagInfo) + // api + aTagInfo.ResType = resType + + queryParams := map[string]string{ + "ResourceId": aTagInfo.ResIId.SystemId, + } + response, err := CallEcsRequest(resType, Client, regionInfo, "DescribeTags", queryParams) + if err != nil { + cblogger.Error(err.Error()) + continue + } + + var tagsResponse DescribeTagsResponse + err = json.Unmarshal([]byte(response.GetHttpContentString()), &tagsResponse) + if err != nil { + cblogger.Error("Failed to unmarshal response: ", err) + continue + } + + aTagInfo.TagList = []irs.KeyValue{} + for _, tag := range tagsResponse.Tags.Tag { + aTagInfo.TagList = append(aTagInfo.TagList, irs.KeyValue{ + Key: tag.TagKey, + Value: tag.TagValue, + }) + } + + cblogger.Debug("Updated tagInfo ", aTagInfo) + tagInfo = append(tagInfo, &aTagInfo) + } + return tagInfo, nil +} + +func aliMyImageTagList(Client *ecs.Client, regionInfo idrv.RegionInfo, keyword string) ([]*irs.TagInfo, error) { + var tagInfo []*irs.TagInfo + // TODO: keyword tag 검색 기능 + res, err := DescribeImages(Client, regionInfo, nil, true) + + cblogger.Info("resresresresres", err) + cblogger.Info("resresresresres", res) + // spew.Dump(res) + + return tagInfo, nil +} + +func aliVpcTagList(VpcClient *vpc.Client, regionInfo idrv.RegionInfo, alibabaResourceType string, resType irs.RSType, keyword string) ([]*irs.TagInfo, error) { + hiscallInfo := GetCallLogScheme(regionInfo, call.TAG, keyword, "FindTag()") + regionID := regionInfo.Region + var tagInfo []*irs.TagInfo + + queryParams := map[string]string{} + queryParams["RegionId"] = regionID + queryParams["ResourceType"] = alibabaResourceType //string(resType)//keypair + // queryParams["Tag.1.Key"] = keyword + // cblogger.Info("쿼리", queryParams) + + start := call.Start() + + response, err := CallVpcRequest(resType, VpcClient, regionInfo, "DescribeVpcs", queryParams) + LoggingInfo(hiscallInfo, start) + if err != nil { + cblogger.Error(err.Error()) + LoggingError(hiscallInfo, err) + } + cblogger.Debug(response.GetHttpContentString()) + resResources := DescribeVpcsResponse{} + + tagResponseStr := response.GetHttpContentString() + err = json.Unmarshal([]byte(tagResponseStr), &resResources) + + if err != nil { + cblogger.Error(err.Error()) + return tagInfo, nil + } + + for _, vpc := range resResources.Vpcs.Vpc { + for _, tag := range vpc.Tags.Tag { + aliTagResource := AliTagResource{ + ResourceType: "VPC", + ResourceId: vpc.VpcId, + TagKey: tag.Key, + TagValue: tag.Value, + } + aTagInfo, err := ExtractTagResourceInfo(&aliTagResource) + if err != nil { + cblogger.Error(err.Error()) + continue + } + tagInfo = append(tagInfo, &aTagInfo) + } + } + return tagInfo, nil +} + +func aliSubnetTagList(VpcClient *vpc.Client, regionInfo idrv.RegionInfo, alibabaResourceType string, resType irs.RSType, keyword string) ([]*irs.TagInfo, error) { + hiscallInfo := GetCallLogScheme(regionInfo, call.TAG, keyword, "FindTag()") + regionID := regionInfo.Region + var tagInfo []*irs.TagInfo + + queryParams := map[string]string{} + queryParams["RegionId"] = regionID + queryParams["ResourceType"] = alibabaResourceType //string(resType)//keypair + // queryParams["Tag.1.Key"] = keyword + // cblogger.Info("쿼리", queryParams) + + start := call.Start() + response, err := CallVpcRequest(resType, VpcClient, regionInfo, "DescribeVSwitches", queryParams) + LoggingInfo(hiscallInfo, start) + if err != nil { + cblogger.Error(err.Error()) + LoggingError(hiscallInfo, err) + } + cblogger.Debug(response.GetHttpContentString()) + resResources := DescribeVSwitchesResponse{} + + tagResponseStr := response.GetHttpContentString() + err = json.Unmarshal([]byte(tagResponseStr), &resResources) + + if err != nil { + cblogger.Error("Failed to unmarshal response: ", err) + } + + for _, vswitch := range resResources.VSwitches.VSwitch { + if len(vswitch.Tags.Tag) == 0 { + continue + } + for _, tag := range vswitch.Tags.Tag { + aliTagResource := AliTagResource{ + ResourceType: "VSwitch", + ResourceId: vswitch.VSwitchId, + TagKey: tag.Key, + TagValue: tag.Value, + } + cblogger.Debug("aliTagResourcealiTagResourcealiTagResourcealiTagResource", resType) + + vswitchTagInfo, err := ExtractTagResourceInfo(&aliTagResource) + cblogger.Debug("vswitchTagInfovswitchTagInfovswitchTagInfo", vswitchTagInfo) + if err != nil { + cblogger.Error(err.Error()) + continue + } + tagInfo = append(tagInfo, &vswitchTagInfo) + } + } + return tagInfo, nil +} + +func aliClusterTagList(CsClient *cs.Client, regionInfo idrv.RegionInfo, resType irs.RSType, keyword string) ([]*irs.TagInfo, error) { + hiscallInfo := GetCallLogScheme(regionInfo, call.TAG, keyword, "FindTag()") + var tagInfoList []*irs.TagInfo + + regionID := regionInfo.Region + clusters, err := aliDescribeClustersV1(CsClient, regionID) + if err != nil { + cblogger.Error(err) + LoggingError(hiscallInfo, err) + return nil, err + } + //cblogger.Debug("clusters ", clusters) + // 모든 cluster를 돌면서 Tag 찾기 + for _, cluster := range clusters { + cblogger.Debug("inCluster ") + for _, aliTag := range cluster.Tags { + //cblogger.Debug("aliTag ", aliTag) + //cblogger.Debug("keyword ", keyword) + //cblogger.Debug("aliTag.Key ", *(aliTag.Key)) + if *(aliTag.Key) == keyword { + var aTagInfo irs.TagInfo + aTagInfo.ResIId = irs.IID{SystemId: *cluster.ClusterId} + aTagInfo.ResType = resType + + tagList := []irs.KeyValue{} + tagList = append(tagList, irs.KeyValue{Key: "TagKey", Value: *aliTag.Key}) + tagList = append(tagList, irs.KeyValue{Key: "TagValue", Value: *aliTag.Value}) + aTagInfo.TagList = tagList + //cblogger.Debug("append Tag ", &tagInfo) + // tagInfo = &aTagInfo + } + } + } + return tagInfoList, err +} diff --git a/cloud-control-manager/cloud-driver/drivers/alibaba/resources/TagHandler.go b/cloud-control-manager/cloud-driver/drivers/alibaba/resources/TagHandler.go index 82bdcce4c..eb85b4463 100644 --- a/cloud-control-manager/cloud-driver/drivers/alibaba/resources/TagHandler.go +++ b/cloud-control-manager/cloud-driver/drivers/alibaba/resources/TagHandler.go @@ -6,6 +6,7 @@ import ( cs "github.com/alibabacloud-go/cs-20151215/v4/client" // cs : container service "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" // ecs : elastic compute service + "github.com/aliyun/alibaba-cloud-sdk-go/services/vpc" 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" @@ -13,9 +14,10 @@ import ( ) type AlibabaTagHandler struct { - Region idrv.RegionInfo - Client *ecs.Client - CsClient *cs.Client + Region idrv.RegionInfo + Client *ecs.Client + CsClient *cs.Client + VpcClient *vpc.Client } type AliTagResponse struct { @@ -50,6 +52,8 @@ type AliTagResourcesResponse struct { PageNumber int `json:"PageNumber" xml:"PageNumber"` TotalCount int `json:"TotalCount" xml:"TotalCount"` AliTagResources AliTagResources `json:"Resources" xml:"Resources"` + + // AliTagResources AliTagResources `json:"Resources" xml:"Resources"` } type AliResourceTypeCount struct { @@ -73,6 +77,88 @@ type DescribeTagsResponse struct { PageNumber int `json:"PageNumber" xml:"PageNumber"` Tags ecs.TagsInDescribeTags `json:"Tags" xml:"Tags"` } +type DescribeVpcsResponse struct { + TotalCount int `json:"TotalCount"` + PageSize int `json:"PageSize"` + RequestId string `json:"RequestId"` + PageNumber int `json:"PageNumber"` + Vpcs struct { + Vpc []struct { + Status string `json:"Status"` + IsDefault bool `json:"IsDefault"` + CenStatus string `json:"CenStatus"` + Description string `json:"Description"` + ResourceGroupId string `json:"ResourceGroupId"` + VSwitchIds struct { + VSwitchId []string `json:"VSwitchId"` + } `json:"VSwitchIds"` + SecondaryCidrBlocks struct { + SecondaryCidrBlock []string `json:"SecondaryCidrBlock"` + } `json:"SecondaryCidrBlocks"` + CidrBlock string `json:"CidrBlock"` + RouterTableIds struct { + RouterTableIds []string `json:"RouterTableIds"` + } `json:"RouterTableIds"` + UserCidrs struct { + UserCidr []string `json:"UserCidr"` + } `json:"UserCidrs"` + NetworkAclNum int `json:"NetworkAclNum"` + AdvancedResource bool `json:"AdvancedResource"` + VRouterId string `json:"VRouterId"` + NatGatewayIds struct { + NatGatewayIds []string `json:"NatGatewayIds"` + } `json:"NatGatewayIds"` + VpcId string `json:"VpcId"` + OwnerId int64 `json:"OwnerId"` + CreationTime string `json:"CreationTime"` + VpcName string `json:"VpcName"` + EnabledIpv6 bool `json:"EnabledIpv6"` + RegionId string `json:"RegionId"` + Ipv6CidrBlock string `json:"Ipv6CidrBlock"` + Tags struct { + Tag []struct { + Value string `json:"Value"` + Key string `json:"Key"` + } `json:"Tag"` + } `json:"Tags"` + } `json:"Vpc"` + } `json:"Vpcs"` +} +type DescribeVSwitchesResponse struct { + TotalCount int `json:"TotalCount"` + PageSize int `json:"PageSize"` + RequestId string `json:"RequestId"` + PageNumber int `json:"PageNumber"` + VSwitches struct { + VSwitch []struct { + Status string `json:"Status"` + IsDefault bool `json:"IsDefault"` + Description string `json:"Description"` + ResourceGroupId string `json:"ResourceGroupId"` + ZoneId string `json:"ZoneId"` + NetworkAclId string `json:"NetworkAclId"` + AvailableIpAddressCount int `json:"AvailableIpAddressCount"` + VSwitchId string `json:"VSwitchId"` + CidrBlock string `json:"CidrBlock"` + RouteTable struct { + RouteTableId string `json:"RouteTableId"` + RouteTableType string `json:"RouteTableType"` + } `json:"RouteTable"` + VpcId string `json:"VpcId"` + OwnerId int64 `json:"OwnerId"` + CreationTime string `json:"CreationTime"` + VSwitchName string `json:"VSwitchName"` + Ipv6CidrBlock string `json:"Ipv6CidrBlock"` + Tags struct { + Tag []struct { + Value string `json:"Value"` + Key string `json:"Key"` + } `json:"Tag"` + } `json:"Tags"` + ShareType string `json:"ShareType"` + } `json:"VSwitch"` + } `json:"VSwitches"` +} /* * ECS Instances 아래에 Tags and ResourceGroup이 있음. @@ -243,8 +329,8 @@ func (tagHandler *AlibabaTagHandler) ListTag(resType irs.RSType, resIID irs.IID) resTagResources := AliTagResourcesResponse{} cblogger.Debug("resTagResources ", resTagResources) - cblogger.Debug("resTagResources.AliTagResources ", resTagResources.AliTagResources) - cblogger.Debug("resTagResources.AliTagResources.Resources ", resTagResources.AliTagResources.Resources) + // cblogger.Debug("resTagResources.AliTagResources ", resTagResources.AliTagResources) + // cblogger.Debug("resTagResources.AliTagResources.Resources ", resTagResources.AliTagResources.Resources) for _, aliTagResource := range response.TagResource { cblogger.Debug("aliTagResource ", aliTagResource) @@ -335,8 +421,8 @@ func (tagHandler *AlibabaTagHandler) GetTag(resType irs.RSType, resIID irs.IID, resTagResources := AliTagResourcesResponse{} cblogger.Debug("resTagResources ", resTagResources) - cblogger.Debug("resTagResources.AliTagResources ", resTagResources.AliTagResources) - cblogger.Debug("resTagResources.AliTagResources.Resources ", resTagResources.AliTagResources.Resources) + // cblogger.Debug("resTagResources.AliTagResources ", resTagResources.AliTagResources) + // cblogger.Debug("resTagResources.AliTagResources.Resources ", resTagResources.AliTagResources.Resources) for _, aliTagResource := range response.TagResource { cblogger.Debug("aliTagResource ", aliTagResource) @@ -428,205 +514,158 @@ func (tagHandler *AlibabaTagHandler) RemoveTag(resType irs.RSType, resIID irs.II // if you want to find all tags, set keyword to "" or "*". // 해당 Resource Type에 tag가 있는 것들. ListTag는 resourceId가 있으나 당 function은 더 넒음 func (tagHandler *AlibabaTagHandler) FindTag(resType irs.RSType, keyword string) ([]*irs.TagInfo, error) { - var tagInfoList []*irs.TagInfo - //start := call.Start() - //regionID := tagHandler.Region.Region - //regionID = "ap-northeast-1" // for the test - - // alibabaResourceType, err := GetAlibabaResourceType(resType) - // if err != nil { - // return tagInfoList, err - // } - - // alibabaApiType, err := GetAliTargetApi(resType) - // if err != nil { - // return tagInfoList, err - // } - - // switch alibabaApiType { - // case "ecs": - - // queryParams := map[string]string{} - // queryParams["RegionId"] = regionID - // queryParams["ResourceType"] = alibabaResourceType //string(resType) - // queryParams["Tag.1.Key"] = keyword + var tagInfoList []*irs.TagInfo - // start := call.Start() - // response, err := CallEcsRequest(resType, tagHandler.Client, tagHandler.Region, "DescribeResourceByTags", queryParams) - // LoggingInfo(hiscallInfo, start) + regionInfo := tagHandler.Region + regionID := tagHandler.Region.Region - // if err != nil { - // cblogger.Error(err.Error()) - // LoggingError(hiscallInfo, err) - // } - // cblogger.Debug(response.GetHttpContentString()) + alibabaResourceType, err := GetAlibabaResourceType(resType) - // resTagResources := AliTagResourcesResponse{} + if err != nil { + return tagInfoList, err + } - // tagResponseStr := response.GetHttpContentString() - // err = json.Unmarshal([]byte(tagResponseStr), &resTagResources) - // if err != nil { - // cblogger.Error(err.Error()) - // return tagInfoList, nil - // } - // cblogger.Debug("resTagResources ", resTagResources) - // cblogger.Debug("resTagResources.AliTagResources ", resTagResources.AliTagResources) - // cblogger.Debug("resTagResources.AliTagResources.Resources ", resTagResources.AliTagResources.Resources) - - // for _, aliTagResource := range resTagResources.AliTagResources.Resources { - // cblogger.Debug("aliTagResource ", aliTagResource) - // aTagInfo, err := ExtractTagResourceInfo(&aliTagResource) - // if err != nil { - // cblogger.Error(err.Error()) - // continue - // } - - // aTagInfo.ResType = resType - - // cblogger.Debug("tagInfo ", aTagInfo) - // tagInfoList = append(tagInfoList, &aTagInfo) - // } - // case "cs": // cs : container service - // clusters, err := aliDescribeClustersV1(tagHandler.CsClient, regionID) - // if err != nil { - // cblogger.Error(err) - // LoggingError(hiscallInfo, err) - // return nil, err - // } + cblogger.Debug("resType : ", resType) + switch resType { - // //cblogger.Debug("clusters ", clusters) - // // 모든 cluster를 돌면서 Tag 찾기 - // for _, cluster := range clusters { - // cblogger.Debug("inCluster ") - // for _, aliTag := range cluster.Tags { - // //cblogger.Debug("aliTag ", aliTag) - // //cblogger.Debug("keyword ", keyword) - // //cblogger.Debug("aliTag.Key ", *(aliTag.Key)) - // if *(aliTag.Key) == keyword { - // var tagInfo irs.TagInfo - // tagInfo.ResIId = irs.IID{SystemId: *cluster.ClusterId} - // tagInfo.ResType = resType - - // tagList := []irs.KeyValue{} - // tagList = append(tagList, irs.KeyValue{Key: "TagKey", Value: *aliTag.Key}) - // tagList = append(tagList, irs.KeyValue{Key: "TagValue", Value: *aliTag.Value}) - // tagInfo.TagList = tagList - // //cblogger.Debug("append Tag ", &tagInfo) - // tagInfoList = append(tagInfoList, &tagInfo) - // } - // } - // } - // } + case "VM", irs.VM: + responseTagList, err := aliEcsTagList(tagHandler.Client, regionInfo, alibabaResourceType, resType, keyword) + if err != nil { + cblogger.Error(err) + } + cblogger.Debug("aliEcsTag response : ", responseTagList) - switch string(resType) { - case "ALL": - // for 모든 resource - default: + tagInfoList = append(tagInfoList, responseTagList...) - tagInfo, err := FindTag(tagHandler, resType, keyword) + case "KEY", irs.KEY: + responseTagList, err := aliEcsTagList(tagHandler.Client, regionInfo, alibabaResourceType, resType, keyword) if err != nil { - cblogger.Error(err.Error()) - return tagInfoList, err + cblogger.Error(err) } - tagInfoList = append(tagInfoList, tagInfo) - } - - return tagInfoList, nil -} + cblogger.Debug("aliEcsTag response : ", responseTagList) -// 1개의 resource Type에 대한 Tag 정보 -func FindTag(tagHandler *AlibabaTagHandler, resType irs.RSType, keyword string) (*irs.TagInfo, error) { - hiscallInfo := GetCallLogScheme(tagHandler.Region, call.TAG, keyword, "FindTag()") - var tagInfo *irs.TagInfo + tagInfoList = append(tagInfoList, responseTagList...) - regionID := tagHandler.Region.Region + case "SG", irs.SG: + responseTagList, err := aliEcsTagList(tagHandler.Client, regionInfo, alibabaResourceType, resType, keyword) + if err != nil { + cblogger.Error(err) + } + cblogger.Debug("aliEcsTag response : ", responseTagList) - alibabaResourceType, err := GetAlibabaResourceType(resType) - if err != nil { - return tagInfo, err - } + tagInfoList = append(tagInfoList, responseTagList...) - alibabaApiType, err := GetAliTargetApi(resType) - if err != nil { - return tagInfo, err - } + case "DISK", irs.DISK: + responseTagList, err := aliEcsTagList(tagHandler.Client, regionInfo, alibabaResourceType, resType, keyword) + if err != nil { + cblogger.Error(err) + } + cblogger.Debug("aliEcsTag response : ", responseTagList) - switch alibabaApiType { - case "ecs": + tagInfoList = append(tagInfoList, responseTagList...) - queryParams := map[string]string{} - queryParams["RegionId"] = regionID - queryParams["ResourceType"] = alibabaResourceType //string(resType) - queryParams["Tag.1.Key"] = keyword + case "MYIMAGE", irs.MYIMAGE: + responseTagList, err := aliMyImageTagList(tagHandler.Client, regionInfo, keyword) + if err != nil { + cblogger.Error(err) + } + cblogger.Debug("aliEcsTag response : ", responseTagList) - start := call.Start() - response, err := CallEcsRequest(resType, tagHandler.Client, tagHandler.Region, "DescribeResourceByTags", queryParams) - LoggingInfo(hiscallInfo, start) + tagInfoList = append(tagInfoList, responseTagList...) + case "VPC", irs.VPC: + responseTagList, err := aliVpcTagList(tagHandler.VpcClient, regionInfo, alibabaResourceType, resType, keyword) if err != nil { - cblogger.Error(err.Error()) - LoggingError(hiscallInfo, err) + cblogger.Error(err) } - cblogger.Debug(response.GetHttpContentString()) + cblogger.Debug("aliEcsTag response : ", responseTagList) - resTagResources := AliTagResourcesResponse{} + tagInfoList = append(tagInfoList, responseTagList...) - tagResponseStr := response.GetHttpContentString() - err = json.Unmarshal([]byte(tagResponseStr), &resTagResources) + case "SUBNET", irs.SUBNET: + responseTagList, err := aliSubnetTagList(tagHandler.VpcClient, regionInfo, alibabaResourceType, resType, keyword) if err != nil { - cblogger.Error(err.Error()) - return tagInfo, nil + cblogger.Error(err) } - cblogger.Debug("resTagResources ", resTagResources) - cblogger.Debug("resTagResources.AliTagResources ", resTagResources.AliTagResources) - cblogger.Debug("resTagResources.AliTagResources.Resources ", resTagResources.AliTagResources.Resources) - - for _, aliTagResource := range resTagResources.AliTagResources.Resources { - cblogger.Debug("aliTagResource ", aliTagResource) - aTagInfo, err := ExtractTagResourceInfo(&aliTagResource) - if err != nil { - cblogger.Error(err.Error()) - continue - } + cblogger.Debug("aliEcsTag response : ", responseTagList) - aTagInfo.ResType = resType + tagInfoList = append(tagInfoList, responseTagList...) - cblogger.Debug("tagInfo ", aTagInfo) - tagInfo = &aTagInfo - } - case "cs": // cs : container service - clusters, err := aliDescribeClustersV1(tagHandler.CsClient, regionID) + case "CLUSTER", irs.CLUSTER: // cs : container service + responseTagList, err := aliClusterTagList(tagHandler.CsClient, regionInfo, resType, keyword) if err != nil { cblogger.Error(err) - LoggingError(hiscallInfo, err) - return nil, err } + cblogger.Debug("aliEcsTag response : ", responseTagList) + + tagInfoList = append(tagInfoList, responseTagList...) + + case "ALL", irs.ALL: + // 모든 자원 유형을 포함하는 슬라이스를 선언 + allResourceTypes := []irs.RSType{irs.VM, irs.KEY, irs.SG, irs.DISK, irs.MYIMAGE, irs.VPC, irs.SUBNET, irs.CLUSTER} + + // 각 자원 유형별로 태그 정보 조회 + for _, resourceType := range allResourceTypes { + switch resourceType { + case irs.VM, irs.KEY, irs.SG, irs.DISK: + responseTagList, err := aliEcsTagList(tagHandler.Client, regionInfo, alibabaResourceType, resourceType, keyword) + if err != nil { + cblogger.Errorf("Error retrieving tags for %s: %v", resourceType, err) + } else { + tagInfoList = append(tagInfoList, responseTagList...) + } + + case irs.MYIMAGE: + responseTagList, err := aliMyImageTagList(tagHandler.Client, regionInfo, keyword) + if err != nil { + cblogger.Errorf("Error retrieving tags for MYIMAGE: %v", err) + } else { + tagInfoList = append(tagInfoList, responseTagList...) + } + + case irs.VPC: + responseTagList, err := aliVpcTagList(tagHandler.VpcClient, regionInfo, alibabaResourceType, resourceType, keyword) + if err != nil { + cblogger.Errorf("Error retrieving tags for VPC: %v", err) + } else { + tagInfoList = append(tagInfoList, responseTagList...) + } + + case irs.SUBNET: + responseTagList, err := aliSubnetTagList(tagHandler.VpcClient, regionInfo, alibabaResourceType, resourceType, keyword) + if err != nil { + cblogger.Errorf("Error retrieving tags for SUBNET: %v", err) + } else { + tagInfoList = append(tagInfoList, responseTagList...) + } - //cblogger.Debug("clusters ", clusters) - // 모든 cluster를 돌면서 Tag 찾기 - for _, cluster := range clusters { - cblogger.Debug("inCluster ") - for _, aliTag := range cluster.Tags { - //cblogger.Debug("aliTag ", aliTag) - //cblogger.Debug("keyword ", keyword) - //cblogger.Debug("aliTag.Key ", *(aliTag.Key)) - if *(aliTag.Key) == keyword { - var aTagInfo irs.TagInfo - aTagInfo.ResIId = irs.IID{SystemId: *cluster.ClusterId} - aTagInfo.ResType = resType - - tagList := []irs.KeyValue{} - tagList = append(tagList, irs.KeyValue{Key: "TagKey", Value: *aliTag.Key}) - tagList = append(tagList, irs.KeyValue{Key: "TagValue", Value: *aliTag.Value}) - aTagInfo.TagList = tagList - //cblogger.Debug("append Tag ", &tagInfo) - tagInfo = &aTagInfo + case irs.CLUSTER: + // CLUSTER 태그 정보 조회 로직 + clusters, err := aliDescribeClustersV1(tagHandler.CsClient, regionID) + if err != nil { + cblogger.Errorf("Error retrieving clusters: %v", err) + } else { + for _, cluster := range clusters { + for _, aliTag := range cluster.Tags { + if *(aliTag.Key) == keyword { + aTagInfo := irs.TagInfo{ + ResIId: irs.IID{SystemId: *cluster.ClusterId}, + ResType: resourceType, + TagList: []irs.KeyValue{ + {Key: "TagKey", Value: *aliTag.Key}, + {Key: "TagValue", Value: *aliTag.Value}, + }, + } + tagInfoList = append(tagInfoList, &aTagInfo) + } + } + } } } } } - return tagInfo, nil + return tagInfoList, nil } /* @@ -690,7 +729,6 @@ func ExtractTagResourceInfo(tagResource *AliTagResource) (irs.TagInfo, error) { //cblogger.Debug("tag ", aliTag) //cblogger.Debug("TagKey ", aliTag.TagKey) //cblogger.Debug("TagValue ", aliTag.TagValue) - tagInfo.ResType = irs.RSType(tagResource.ResourceType) tagInfo.ResIId = irs.IID{SystemId: tagResource.ResourceId}