Skip to content

Commit

Permalink
IBM: Add cluster support to TagHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Jul 26, 2024
1 parent 64f68aa commit 3d38547
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (cloudConn *IbmCloudConnection) CreateTagHandler() (irs.TagHandler, error)
CredentialInfo: cloudConn.CredentialInfo,
Region: cloudConn.Region,
VpcService: cloudConn.VpcService,
ClusterService: cloudConn.ClusterService,
Ctx: cloudConn.Ctx,
TaggingService: cloudConn.TaggingService,
SearchService: cloudConn.SearchService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,42 @@ func (ic *IbmClusterHandler) ListCluster() ([]*irs.ClusterInfo, error) {
return ret, nil
}

func (ic *IbmClusterHandler) getRawCluster(clusterIID irs.IID) (kubernetesserviceapiv1.GetClusterDetailResponse, error) {
rawCluster := kubernetesserviceapiv1.GetClusterDetailResponse{}

if clusterIID.NameId == "" && clusterIID.SystemId == "" {
return rawCluster, errors.New("Failed to Get Cluster. err = invalid IID")
}

resourceGroupId, getResourceGroupIdErr := ic.getDefaultResourceGroupId()
if getResourceGroupIdErr != nil {
return rawCluster, errors.New(fmt.Sprintf("Failed to Get Cluster. err = %s", getResourceGroupIdErr))
}

var cluster string
if clusterIID.SystemId != "" {
cluster = clusterIID.SystemId
} else {
cluster = clusterIID.NameId
}
rawClusters, _, getClustersErr := ic.ClusterService.VpcGetClusterWithContext(ic.Ctx, &kubernetesserviceapiv1.VpcGetClusterOptions{
Cluster: core.StringPtr(cluster),
XAuthResourceGroup: core.StringPtr(resourceGroupId),
ShowResources: core.StringPtr("true"),
})
if getClustersErr != nil {
return rawCluster, errors.New(fmt.Sprintf("Failed to Get Cluster. err = %s", getClustersErr))
}

for _, rCluster := range *rawClusters {
if rCluster.Id == clusterIID.SystemId || rCluster.Name == clusterIID.NameId {
return rCluster, nil
}
}

return rawCluster, errors.New("Failed to Get Cluster. err = cluster not found")
}

func (ic *IbmClusterHandler) GetCluster(clusterIID irs.IID) (irs.ClusterInfo, error) {
hiscallInfo := GetCallLogScheme(ic.Region, call.CLUSTER, clusterIID.NameId, "GetCluster()")
start := call.Start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
"github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/ibmcloud-vpc/utils/kubernetesserviceapiv1"
"strings"

"github.com/IBM/platform-services-go-sdk/globalsearchv2"
Expand All @@ -18,6 +19,7 @@ type IbmTagHandler struct {
Region idrv.RegionInfo
CredentialInfo idrv.CredentialInfo
VpcService *vpcv1.VpcV1
ClusterService *kubernetesserviceapiv1.KubernetesServiceApiV1
Ctx context.Context
TaggingService *globaltaggingv1.GlobalTaggingV1
SearchService *globalsearchv2.GlobalSearchV2
Expand Down Expand Up @@ -73,7 +75,7 @@ func rsTypeToIBMType(resType irs.RSType) string {
case irs.MYIMAGE:
return "snapshot"
case irs.CLUSTER:
return ""
return "k8-cluster"
case irs.NODEGROUP:
return "instance-group"
default:
Expand All @@ -82,8 +84,6 @@ func rsTypeToIBMType(resType irs.RSType) string {
}

func ibmTypeToRSType(ibmType string) (irs.RSType, error) {
fmt.Println(ibmType)

switch ibmType {
case "image":
return irs.IMAGE, nil
Expand All @@ -103,8 +103,8 @@ func ibmTypeToRSType(ibmType string) (irs.RSType, error) {
return irs.DISK, nil
case "snapshot":
return irs.MYIMAGE, nil
//case "???":
// return irs.CLUSTER
case "k8-cluster":
return irs.CLUSTER, nil
case "instance-group":
return irs.NODEGROUP, nil
default:
Expand Down Expand Up @@ -303,6 +303,22 @@ func handleTagAddOrRemove(tagHandler *IbmTagHandler, resType irs.RSType, resIID
}

err2 = attachOrDetachTag(tagHandler.TaggingService, tag, *rawNLB.CRN, action)
case irs.CLUSTER:
clusterHandler := &IbmClusterHandler{
CredentialInfo: tagHandler.CredentialInfo,
Region: tagHandler.Region,
Ctx: tagHandler.Ctx,
VpcService: tagHandler.VpcService,
ClusterService: tagHandler.ClusterService,
TaggingService: tagHandler.TaggingService,
}
rawCluster, err := clusterHandler.getRawCluster(resIID)
if err != nil {
err2 = errors.New(fmt.Sprintf("Failed to add tag. err = %s", err))
break
}

err2 = attachOrDetachTag(tagHandler.TaggingService, tag, rawCluster.Crn, action)
default:
return errors.New("invalid resource type")
}
Expand Down Expand Up @@ -346,9 +362,9 @@ func (tagHandler *IbmTagHandler) ListTag(resType irs.RSType, resIID irs.IID) ([]
var query string

if resIID.NameId != "" {
query = fmt.Sprintf("type:%s AND name:%s", resType, resIID.NameId)
query = fmt.Sprintf("type:%s AND name:%s", ibmType, resIID.NameId)
} else {
query = fmt.Sprintf("type:%s AND id:%s", resType, resIID.SystemId)
query = fmt.Sprintf("type:%s AND id:%s", ibmType, resIID.SystemId)
}

searchOptions.SetQuery(query)
Expand Down Expand Up @@ -514,9 +530,37 @@ func (tagHandler *IbmTagHandler) FindTag(resType irs.RSType, keyword string) ([]
continue
}

name, ok := item.GetProperty("name").(string)
if !ok {
cblogger.Error("name is not a string")
continue
}
resourceId, ok := item.GetProperty("resource_id").(string)
if !ok {
cblogger.Error("resource_id is not a string")
continue
}

if rsType == irs.CLUSTER {
clusterHandler := &IbmClusterHandler{
CredentialInfo: tagHandler.CredentialInfo,
Region: tagHandler.Region,
Ctx: tagHandler.Ctx,
VpcService: tagHandler.VpcService,
ClusterService: tagHandler.ClusterService,
TaggingService: tagHandler.TaggingService,
}
rawCluster, err := clusterHandler.getRawCluster(irs.IID{NameId: name})
if err != nil {
cblogger.Error(err)
continue
}
resourceId = rawCluster.Id
}

tagInfo = append(tagInfo, &irs.TagInfo{
ResType: rsType,
ResIId: irs.IID{NameId: (item.GetProperty("name")).(string), SystemId: (item.GetProperty("resource_id").(string))},
ResIId: irs.IID{NameId: name, SystemId: resourceId},
TagList: tagKeyValue,
KeyValueList: []irs.KeyValue{}, // reserved for optional usage
})
Expand Down

0 comments on commit 3d38547

Please sign in to comment.