From 64429a4ecdeb2ed3a9afb8156e022dd81de94163 Mon Sep 17 00:00:00 2001 From: Seokho Son Date: Mon, 19 Aug 2024 16:08:48 +0900 Subject: [PATCH] Simplify ID system using Gen UID and no Transform --- docker-compose.yaml | 2 +- src/api/rest/server/mcir/common.go | 48 ++--- src/api/rest/server/server.go | 2 +- src/core/mci/k8scluster.go | 8 +- src/core/mci/provisioning.go | 6 +- src/core/mci/snapshot.go | 2 +- src/core/mcir/common.go | 322 +++++++++++++++-------------- src/core/mcir/datadisk.go | 4 +- src/core/mcir/securitygroup.go | 4 +- src/core/mcir/sshkey.go | 3 +- src/core/mcir/subnet.go | 2 +- src/core/mcir/vnet.go | 5 +- 12 files changed, 207 insertions(+), 201 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 19122498..7464aadd 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -133,7 +133,7 @@ services: # - API_PASSWORD= - SPIDER_LOG_LEVEL=error - SPIDER_HISCALL_LOG_LEVEL=error - - ID_TRANSFORM_MODE=ON + - ID_TRANSFORM_MODE=OFF healthcheck: # for CB-Spider test: [ "CMD", "curl", "-f", "http://localhost:1024/spider/readyz" ] interval: 1m diff --git a/src/api/rest/server/mcir/common.go b/src/api/rest/server/mcir/common.go index cb766e50..3624f6bc 100644 --- a/src/api/rest/server/mcir/common.go +++ b/src/api/rest/server/mcir/common.go @@ -73,31 +73,33 @@ func RestDelResource(c echo.Context) error { return common.EndRequestWithLog(c, reqID, err, content) } +// Todo: need to reimplment the following invalid function + // RestDelChildResource is a common function to handle 'DelChildResource' REST API requests. // Dummy functions for Swagger exist in [mcir/*.go] -func RestDelChildResource(c echo.Context) error { - reqID, idErr := common.StartRequestWithLog(c) - if idErr != nil { - return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()}) - } - nsId := c.Param("nsId") - - childResourceType := strings.Split(c.Path(), "/")[7] - // c.Path(): /tumblebug/ns/:nsId/resources/vNet/:vNetId/subnet/:subnetId - - parentResourceId := c.Param("parentResourceId") - childResourceId := c.Param("childResourceId") - parentResourceId = strings.ReplaceAll(parentResourceId, " ", "+") - parentResourceId = strings.ReplaceAll(parentResourceId, "%2B", "+") - childResourceId = strings.ReplaceAll(childResourceId, " ", "+") - childResourceId = strings.ReplaceAll(childResourceId, "%2B", "+") - - forceFlag := c.QueryParam("force") - - err := mcir.DelChildResource(nsId, childResourceType, parentResourceId, childResourceId, forceFlag) - content := map[string]string{"message": "The " + childResourceType + " " + childResourceId + " has been deleted"} - return common.EndRequestWithLog(c, reqID, err, content) -} +// func RestDelChildResource(c echo.Context) error { +// reqID, idErr := common.StartRequestWithLog(c) +// if idErr != nil { +// return c.JSON(http.StatusBadRequest, map[string]string{"message": idErr.Error()}) +// } +// nsId := c.Param("nsId") + +// childResourceType := strings.Split(c.Path(), "/")[7] +// // c.Path(): /tumblebug/ns/:nsId/resources/vNet/:vNetId/subnet/:subnetId + +// parentResourceId := c.Param("parentResourceId") +// childResourceId := c.Param("childResourceId") +// parentResourceId = strings.ReplaceAll(parentResourceId, " ", "+") +// parentResourceId = strings.ReplaceAll(parentResourceId, "%2B", "+") +// childResourceId = strings.ReplaceAll(childResourceId, " ", "+") +// childResourceId = strings.ReplaceAll(childResourceId, "%2B", "+") + +// forceFlag := c.QueryParam("force") + +// err := mcir.DelChildResource(nsId, childResourceType, parentResourceId, childResourceId, forceFlag) +// content := map[string]string{"message": "The " + childResourceType + " " + childResourceId + " has been deleted"} +// return common.EndRequestWithLog(c, reqID, err, content) +// } // RestGetAllResources is a common function to handle 'GetAllResources' REST API requests. // Dummy functions for Swagger exist in [mcir/*.go] diff --git a/src/api/rest/server/server.go b/src/api/rest/server/server.go index 06e43938..eb53cd65 100644 --- a/src/api/rest/server/server.go +++ b/src/api/rest/server/server.go @@ -450,7 +450,7 @@ func RunServer(port string) { // g.GET("/:nsId/resources/vNet/:vNetId/subnet/:subnetId", rest_mcir.RestGetSubnet) // g.GET("/:nsId/resources/vNet/:vNetId/subnet", rest_mcir.RestGetAllSubnet) // g.PUT("/:nsId/resources/vNet/:vNetId/subnet/:subnetId", rest_mcir.RestPutSubnet) - g.DELETE("/:nsId/resources/vNet/:parentResourceId/subnet/:childResourceId", rest_mcir.RestDelChildResource) + // g.DELETE("/:nsId/resources/vNet/:parentResourceId/subnet/:childResourceId", rest_mcir.RestDelChildResource) // g.DELETE("/:nsId/resources/vNet/:vNetId/subnet", rest_mcir.RestDelAllSubnet) /* diff --git a/src/core/mci/k8scluster.go b/src/core/mci/k8scluster.go index 265cff54..7ccf5754 100644 --- a/src/core/mci/k8scluster.go +++ b/src/core/mci/k8scluster.go @@ -495,8 +495,6 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus * Build RequestBody for SpiderClusterReq{} */ - spName := fmt.Sprintf("%s-%s", nsId, u.Id) - // Validate err = validateAtCreateK8sCluster(u) if err != nil { @@ -605,7 +603,7 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus } spNodeGroupList = append(spNodeGroupList, SpiderNodeGroupReqInfo{ - Name: v.Name, + Name: common.GenUid(), ImageName: spImgName, VMSpecName: spSpecName, RootDiskType: v.RootDiskType, @@ -622,7 +620,7 @@ func CreateK8sCluster(nsId string, u *TbK8sClusterReq, option string) (TbK8sClus NameSpace: "", // should be empty string from Tumblebug ConnectionName: u.ConnectionName, ReqInfo: SpiderClusterReqInfo{ - Name: spName, + Name: common.GenUid(), Version: spVersion, VPCName: spVPCName, SubnetNames: spSubnetNames, @@ -1210,7 +1208,7 @@ func GetK8sCluster(nsId string, k8sClusterId string) (TbK8sClusterInfo, error) { client := resty.New() client.SetTimeout(10 * time.Minute) - url := common.SpiderRestUrl + "/cluster/" + nsId + "-" + k8sClusterId + url := common.SpiderRestUrl + "/cluster/" + storedTbK8sCInfo.CspK8sClusterName method := "GET" // Create Request body for GetK8sCluster of CB-Spider diff --git a/src/core/mci/provisioning.go b/src/core/mci/provisioning.go index 203e80f0..dec3e1be 100644 --- a/src/core/mci/provisioning.go +++ b/src/core/mci/provisioning.go @@ -1657,8 +1657,7 @@ func CreateVm(nsId string, mciId string, vmInfoData *TbVmInfo, option string) er requestBody.ConnectionName = vmInfoData.ConnectionName //generate VM ID(Name) to request to CSP(Spider) - //combination of nsId, mcidId, and vmName reqested from user - requestBody.ReqInfo.Name = fmt.Sprintf("%s-%s-%s", nsId, mciId, vmInfoData.Name) + requestBody.ReqInfo.Name = common.GenUid() customImageFlag := false @@ -1771,6 +1770,9 @@ func CreateVm(nsId string, mciId string, vmInfoData *TbVmInfo, option string) er } } + log.Info().Msg("VM request body to CB-Spider") + common.PrintJsonPretty(requestBody) + // Randomly sleep within 20 Secs to avoid rateLimit from CSP common.RandomSleep(0, 20) client := resty.New() diff --git a/src/core/mci/snapshot.go b/src/core/mci/snapshot.go index 2420c277..b921b510 100644 --- a/src/core/mci/snapshot.go +++ b/src/core/mci/snapshot.go @@ -45,7 +45,7 @@ func CreateVmSnapshot(nsId string, mciId string, vmId string, snapshotName strin json.Unmarshal([]byte(keyValue.Value), &vm) if snapshotName == "" { - snapshotName = fmt.Sprintf("%s-%s", vm.Name, common.GenerateNewRandomString(5)) + snapshotName = common.GenUid() } requestBody := mcir.SpiderMyImageReq{ diff --git a/src/core/mcir/common.go b/src/core/mcir/common.go index 44aea639..f5f2d7c3 100644 --- a/src/core/mcir/common.go +++ b/src/core/mcir/common.go @@ -354,166 +354,168 @@ func DelResource(nsId string, resourceType string, resourceId string, forceFlag return nil } -// DelChildResource deletes the TB MCIR object -func DelChildResource(nsId string, resourceType string, parentResourceId string, resourceId string, forceFlag string) error { - - var parentResourceType string - switch resourceType { - case common.StrSubnet: - parentResourceType = common.StrVNet - default: - err := fmt.Errorf("Not valid child resource type.") - return err - } - - err := common.CheckString(nsId) - if err != nil { - log.Error().Err(err).Msg("") - return err - } - - err = common.CheckString(parentResourceId) - if err != nil { - log.Error().Err(err).Msg("") - return err - } - - err = common.CheckString(resourceId) - if err != nil { - log.Error().Err(err).Msg("") - return err - } - - check, err := CheckResource(nsId, parentResourceType, parentResourceId) - - if !check { - errString := "The " + parentResourceType + " " + parentResourceId + " does not exist." - err := fmt.Errorf(errString) - return err - } - - if err != nil { - log.Error().Err(err).Msg("") - return err - } - - check, err = CheckChildResource(nsId, resourceType, parentResourceId, resourceId) - - if !check { - errString := "The " + resourceType + " " + resourceId + " does not exist." - err := fmt.Errorf(errString) - return err - } - - if err != nil { - log.Error().Err(err).Msg("") - return err - } - - parentResourceKey := common.GenResourceKey(nsId, parentResourceType, parentResourceId) - log.Debug().Msg("parentResourceKey: " + parentResourceKey) - - childResourceKey := common.GenChildResourceKey(nsId, resourceType, parentResourceId, resourceId) - log.Debug().Msg("childResourceKey: " + childResourceKey) - - parentKeyValue, _ := kvstore.GetKv(parentResourceKey) - - //cspType := common.GetResourcesCspType(nsId, resourceType, resourceId) - - var url string - - // Create Req body - type JsonTemplate struct { - ConnectionName string - } - requestBody := JsonTemplate{} - - switch resourceType { - case common.StrSubnet: - temp := TbVNetInfo{} - err = json.Unmarshal([]byte(parentKeyValue.Value), &temp) - if err != nil { - log.Error().Err(err).Msg("") - return err - } - requestBody.ConnectionName = temp.ConnectionName - url = fmt.Sprintf("%s/vpc/%s/subnet/%s", common.SpiderRestUrl, temp.Name, resourceId) - default: - err := fmt.Errorf("invalid resourceType") - return err - } - - if forceFlag == "true" { - url += "?force=true" - } - var callResult interface{} - client := resty.New() - method := "DELETE" - //client.SetTimeout(60 * time.Second) - - err = common.ExecuteHttpRequest( - client, - method, - url, - nil, - common.SetUseBody(requestBody), - &requestBody, - &callResult, - common.VeryShortDuration, - ) - - if err != nil { - log.Error().Err(err).Msg("") - return err - } - - err = kvstore.Delete(childResourceKey) - if err != nil { - log.Error().Err(err).Msg("") - return err - } - - // Delete the child element in parent resources' array - switch resourceType { - case common.StrSubnet: - oldVNet := TbVNetInfo{} - err = json.Unmarshal([]byte(parentKeyValue.Value), &oldVNet) - if err != nil { - log.Error().Err(err).Msg("") - return err - } - - newVNet := TbVNetInfo{} - newVNet = oldVNet - - var subnetIndex int - subnetIndex = -1 - for i, v := range newVNet.SubnetInfoList { - if v.Name == resourceId { - subnetIndex = i - break - } - } - - if subnetIndex != -1 { - DelEleInSlice(&newVNet.SubnetInfoList, subnetIndex) - } else { - err := fmt.Errorf("Failed to find and delete subnet %s in vNet %s.", resourceId, parentResourceId) - log.Error().Err(err).Msg("") - } - - Val, _ := json.Marshal(newVNet) - err = kvstore.Put(parentResourceKey, string(Val)) - if err != nil { - log.Error().Err(err).Msg("") - return err - } - // default: - } - - return nil - -} +// Todo: need to reimplment the following invalid function + +// // DelChildResource deletes the TB MCIR object +// func DelChildResource(nsId string, resourceType string, parentResourceId string, resourceId string, forceFlag string) error { + +// var parentResourceType string +// switch resourceType { +// case common.StrSubnet: +// parentResourceType = common.StrVNet +// default: +// err := fmt.Errorf("Not valid child resource type.") +// return err +// } + +// err := common.CheckString(nsId) +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } + +// err = common.CheckString(parentResourceId) +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } + +// err = common.CheckString(resourceId) +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } + +// check, err := CheckResource(nsId, parentResourceType, parentResourceId) + +// if !check { +// errString := "The " + parentResourceType + " " + parentResourceId + " does not exist." +// err := fmt.Errorf(errString) +// return err +// } + +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } + +// check, err = CheckChildResource(nsId, resourceType, parentResourceId, resourceId) + +// if !check { +// errString := "The " + resourceType + " " + resourceId + " does not exist." +// err := fmt.Errorf(errString) +// return err +// } + +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } + +// parentResourceKey := common.GenResourceKey(nsId, parentResourceType, parentResourceId) +// log.Debug().Msg("parentResourceKey: " + parentResourceKey) + +// childResourceKey := common.GenChildResourceKey(nsId, resourceType, parentResourceId, resourceId) +// log.Debug().Msg("childResourceKey: " + childResourceKey) + +// parentKeyValue, _ := kvstore.GetKv(parentResourceKey) + +// //cspType := common.GetResourcesCspType(nsId, resourceType, resourceId) + +// var url string + +// // Create Req body +// type JsonTemplate struct { +// ConnectionName string +// } +// requestBody := JsonTemplate{} + +// switch resourceType { +// case common.StrSubnet: +// vnet := TbVNetInfo{} +// err = json.Unmarshal([]byte(parentKeyValue.Value), &vnet) +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } +// requestBody.ConnectionName = vnet.ConnectionName +// url = fmt.Sprintf("%s/vpc/%s/subnet/%s", common.SpiderRestUrl, vnet.CspVNetName, vnet.SubnetInfoList) +// default: +// err := fmt.Errorf("invalid resourceType") +// return err +// } + +// if forceFlag == "true" { +// url += "?force=true" +// } +// var callResult interface{} +// client := resty.New() +// method := "DELETE" +// //client.SetTimeout(60 * time.Second) + +// err = common.ExecuteHttpRequest( +// client, +// method, +// url, +// nil, +// common.SetUseBody(requestBody), +// &requestBody, +// &callResult, +// common.VeryShortDuration, +// ) + +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } + +// err = kvstore.Delete(childResourceKey) +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } + +// // Delete the child element in parent resources' array +// switch resourceType { +// case common.StrSubnet: +// oldVNet := TbVNetInfo{} +// err = json.Unmarshal([]byte(parentKeyValue.Value), &oldVNet) +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } + +// newVNet := TbVNetInfo{} +// newVNet = oldVNet + +// var subnetIndex int +// subnetIndex = -1 +// for i, v := range newVNet.SubnetInfoList { +// if v.Name == resourceId { +// subnetIndex = i +// break +// } +// } + +// if subnetIndex != -1 { +// DelEleInSlice(&newVNet.SubnetInfoList, subnetIndex) +// } else { +// err := fmt.Errorf("Failed to find and delete subnet %s in vNet %s.", resourceId, parentResourceId) +// log.Error().Err(err).Msg("") +// } + +// Val, _ := json.Marshal(newVNet) +// err = kvstore.Put(parentResourceKey, string(Val)) +// if err != nil { +// log.Error().Err(err).Msg("") +// return err +// } +// // default: +// } + +// return nil + +// } // DelEleInSlice delete an element from slice by index // - arr: the reference of slice diff --git a/src/core/mcir/datadisk.go b/src/core/mcir/datadisk.go index 77801e31..6b03eb8f 100644 --- a/src/core/mcir/datadisk.go +++ b/src/core/mcir/datadisk.go @@ -187,7 +187,7 @@ func CreateDataDisk(nsId string, u *TbDataDiskReq, option string) (TbDataDiskInf requestBody := SpiderDiskReqInfoWrapper{ ConnectionName: u.ConnectionName, ReqInfo: SpiderDiskInfo{ - Name: fmt.Sprintf("%s-%s", nsId, u.Name), + Name: common.GenUid(), CSPid: u.CspDataDiskId, // for option=register DiskType: u.DiskType, DiskSize: u.DiskSize, @@ -350,7 +350,7 @@ func UpsizeDataDisk(nsId string, resourceId string, u *TbDataDiskUpsizeReq) (TbD var resp *resty.Response // var err error - url := fmt.Sprintf("%s/disk/%s/size", common.SpiderRestUrl, fmt.Sprintf("%s-%s", nsId, resourceId)) + url := fmt.Sprintf("%s/disk/%s/size", common.SpiderRestUrl, dataDisk.CspDataDiskName) resp, err = req.Put(url) if err != nil { diff --git a/src/core/mcir/securitygroup.go b/src/core/mcir/securitygroup.go index 109e3032..a1965f74 100644 --- a/src/core/mcir/securitygroup.go +++ b/src/core/mcir/securitygroup.go @@ -236,7 +236,7 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq, option string) (TbS requestBody := SpiderSecurityReqInfoWrapper{} requestBody.ConnectionName = u.ConnectionName - requestBody.ReqInfo.Name = fmt.Sprintf("%s-%s", nsId, u.Name) + requestBody.ReqInfo.Name = common.GenUid() requestBody.ReqInfo.VPCName = vNetInfo.CspVNetName requestBody.ReqInfo.CSPId = u.CspSecurityGroupId @@ -305,7 +305,7 @@ func CreateSecurityGroup(nsId string, u *TbSecurityGroupReq, option string) (TbS content.Id = u.Name content.Name = u.Name content.ConnectionName = u.ConnectionName - content.VNetId = tempSpiderSecurityInfo.VpcIID.NameId + content.VNetId = u.VNetId content.CspSecurityGroupId = tempSpiderSecurityInfo.IId.SystemId content.CspSecurityGroupName = tempSpiderSecurityInfo.IId.NameId content.Description = u.Description diff --git a/src/core/mcir/sshkey.go b/src/core/mcir/sshkey.go index f8dc3c54..ef6027f5 100644 --- a/src/core/mcir/sshkey.go +++ b/src/core/mcir/sshkey.go @@ -153,7 +153,7 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err requestBody := SpiderKeyPairReqInfoWrapper{} requestBody.ConnectionName = u.ConnectionName - requestBody.ReqInfo.Name = fmt.Sprintf("%s-%s", nsId, u.Name) + requestBody.ReqInfo.Name = common.GenUid() requestBody.ReqInfo.CSPId = u.CspSshKeyId var tempSpiderKeyPairInfo *SpiderKeyPairInfo @@ -203,7 +203,6 @@ func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, err content.Id = u.Name content.Name = u.Name content.ConnectionName = u.ConnectionName - fmt.Printf("tempSpiderKeyPairInfo.IId.SystemId: %s \n", tempSpiderKeyPairInfo.IId.SystemId) content.CspSshKeyId = tempSpiderKeyPairInfo.IId.SystemId content.CspSshKeyName = tempSpiderKeyPairInfo.IId.NameId content.Fingerprint = tempSpiderKeyPairInfo.Fingerprint diff --git a/src/core/mcir/subnet.go b/src/core/mcir/subnet.go index deb3c678..da5b22c2 100644 --- a/src/core/mcir/subnet.go +++ b/src/core/mcir/subnet.go @@ -98,7 +98,7 @@ func CreateSubnet(nsId string, vNetId string, req TbSubnetReq, objectOnly bool) if objectOnly == false { // then, call CB-Spider CreateSubnet API requestBody := SpiderSubnetReqInfoWrapper{} requestBody.ConnectionName = oldVNet.ConnectionName - requestBody.ReqInfo.Name = req.Name + requestBody.ReqInfo.Name = common.GenUid() requestBody.ReqInfo.IPv4_CIDR = req.IPv4_CIDR url := fmt.Sprintf("%s/vpc/%s/subnet", common.SpiderRestUrl, vNetId) diff --git a/src/core/mcir/vnet.go b/src/core/mcir/vnet.go index af79255d..0da1b3d6 100644 --- a/src/core/mcir/vnet.go +++ b/src/core/mcir/vnet.go @@ -188,7 +188,7 @@ func CreateVNet(nsId string, u *TbVNetReq, option string) (TbVNetInfo, error) { requestBody := SpiderVPCReqInfoWrapper{} requestBody.ConnectionName = u.ConnectionName - requestBody.ReqInfo.Name = fmt.Sprintf("%s-%s", nsId, u.Name) + requestBody.ReqInfo.Name = common.GenUid() requestBody.ReqInfo.IPv4_CIDR = u.CidrBlock requestBody.ReqInfo.CSPId = u.CspVNetId @@ -204,6 +204,9 @@ func CreateVNet(nsId string, u *TbVNetReq, option string) (TbVNetInfo, error) { if err != nil { log.Error().Err(err).Msg("") } + //spiderSubnetInfo.Name = common.GenUid() + //TODO: need to use GenUid() after enable CB-TB Subnet opject and its ID (for now, pass the given subnet name) + spiderSubnetInfo.Name = v.Name requestBody.ReqInfo.SubnetInfoList = append(requestBody.ReqInfo.SubnetInfoList, spiderSubnetInfo) }