diff --git a/cmd/cloud-server/logics/cvm/assign.go b/cmd/cloud-server/logics/cvm/assign.go index 3d66d8ddbf..ef64a4c0bd 100644 --- a/cmd/cloud-server/logics/cvm/assign.go +++ b/cmd/cloud-server/logics/cvm/assign.go @@ -27,7 +27,10 @@ import ( "hcm/cmd/cloud-server/logics/eip" logicsni "hcm/cmd/cloud-server/logics/network-interface" "hcm/pkg/api/core" + corecvm "hcm/pkg/api/core/cloud/cvm" dataproto "hcm/pkg/api/data-service/cloud" + protocvm "hcm/pkg/api/hc-service/cvm" + "hcm/pkg/client" dataservice "hcm/pkg/client/data-service" "hcm/pkg/criteria/constant" "hcm/pkg/criteria/enumor" @@ -35,49 +38,79 @@ import ( "hcm/pkg/kit" "hcm/pkg/logs" "hcm/pkg/runtime/filter" + "hcm/pkg/thirdparty/esb" + "hcm/pkg/thirdparty/esb/cmdb" + "hcm/pkg/tools/converter" + "hcm/pkg/tools/maps" "hcm/pkg/tools/slice" ) // Assign 分配主机及关联资源到业务下 -func Assign(kt *kit.Kit, cli *dataservice.Client, ids []string, bizID int64) error { - - if len(ids) == 0 { - return fmt.Errorf("ids is required") +func Assign(kt *kit.Kit, cli *dataservice.Client, cvms []AssignedCvmInfo) error { + if len(cvms) == 0 { + return fmt.Errorf("cvms is required") } + ids := make([]string, 0, len(cvms)) + bizIDCvmIDsMap := make(map[int64][]string) + for _, cvmInfo := range cvms { + ids = append(ids, cvmInfo.CvmID) + bizIDCvmIDsMap[cvmInfo.BkBizID] = append(bizIDCvmIDsMap[cvmInfo.BkBizID], cvmInfo.CvmID) + } // 校验主机信息 if err := ValidateBeforeAssign(kt, cli, ids); err != nil { return err } // 获取主机关联资源 - eipIDs, diskIDs, niIDs, err := GetCvmRelResIDs(kt, cli, ids) + cvmIDEipIDsMap, cvmIDDiskIDsMap, cvmIDNiIDsMap, err := GetCvmRelResIDs(kt, cli, ids) if err != nil { return err } - // 校验主机关联资源信息 - if err := ValidateCvmRelResBeforeAssign(kt, cli, bizID, eipIDs, diskIDs, niIDs); err != nil { - return err - } + for bizID, cvmIDs := range bizIDCvmIDsMap { + eipIDs := make([]string, 0) + diskIDs := make([]string, 0) + niIDs := make([]string, 0) + for _, cvmID := range cvmIDs { + eipIDs = append(eipIDs, cvmIDEipIDsMap[cvmID]...) + diskIDs = append(diskIDs, cvmIDDiskIDsMap[cvmID]...) + niIDs = append(niIDs, cvmIDNiIDsMap[cvmID]...) + } - // create assign audit - audit := logicaudit.NewAudit(cli) - if err := audit.ResBizAssignAudit(kt, enumor.CvmAuditResType, ids, bizID); err != nil { - logs.Errorf("create assign cvm audit failed, err: %v, rid: %s", err, kt.Rid) - return err - } + // 校验主机关联资源信息 + if err := ValidateCvmRelResBeforeAssign(kt, cli, bizID, eipIDs, diskIDs, niIDs); err != nil { + return err + } - // 分配主机关联资源 - if err := AssignCvmRelRes(kt, cli, eipIDs, diskIDs, niIDs, bizID); err != nil { - return err + // create assign audit + audit := logicaudit.NewAudit(cli) + if err := audit.ResBizAssignAudit(kt, enumor.CvmAuditResType, cvmIDs, bizID); err != nil { + logs.Errorf("create assign cvm audit failed, err: %v, rid: %s", err, kt.Rid) + return err + } + + // 分配主机关联资源 + if err := AssignCvmRelRes(kt, cli, eipIDs, diskIDs, niIDs, bizID); err != nil { + return err + } } // 分配主机 - update := &dataproto.CvmCommonInfoBatchUpdateReq{IDs: ids, BkBizID: bizID} - if err := cli.Global.Cvm.BatchUpdateCvmCommonInfo(kt, update); err != nil { - logs.Errorf("batch update cvm common info failed, err: %v, req: %v, rid: %s", err, update, kt.Rid) - return err + for _, batch := range slice.Split(cvms, constant.BatchOperationMaxLimit) { + updateCvms := make([]dataproto.CvmCommonInfoBatchUpdateData, 0, len(batch)) + for _, cvmInfo := range batch { + updateCvms = append(updateCvms, dataproto.CvmCommonInfoBatchUpdateData{ + ID: cvmInfo.CvmID, + BkBizID: cvmInfo.BkBizID, + BkCloudID: &cvmInfo.BkCloudID, + }) + } + update := &dataproto.CvmCommonInfoBatchUpdateReq{Cvms: updateCvms} + if err := cli.Global.Cvm.BatchUpdateCvmCommonInfo(kt, update); err != nil { + logs.Errorf("batch update cvm common info failed, err: %v, req: %v, rid: %s", err, update, kt.Rid) + return err + } } return nil @@ -109,50 +142,74 @@ func AssignCvmRelRes(kt *kit.Kit, cli *dataservice.Client, eipIDs []string, } // GetCvmRelResIDs 获取主机关联资源ID列表 -func GetCvmRelResIDs(kt *kit.Kit, cli *dataservice.Client, ids []string) ( - eipIDs []string, diskIDs []string, niIDs []string, err error) { +func GetCvmRelResIDs(kt *kit.Kit, cli *dataservice.Client, ids []string) (cvmIDEipIDsMap map[string][]string, + cvmIDDiskIDsMap map[string][]string, cvmIDNiIDsMap map[string][]string, err error) { + + cvmIDEipIDsMap = make(map[string][]string) + for { + listRelReq := &core.ListReq{ + Filter: tools.ContainersExpression("cvm_id", ids), + Page: core.NewDefaultBasePage(), + } + eipResp, err := cli.Global.ListEipCvmRel(kt, listRelReq) + if err != nil { + logs.Errorf("list eip cvm rel failed, err: %v, rid: %s", err, kt.Rid) + return nil, nil, nil, err + } + for _, detail := range eipResp.Details { + cvmIDEipIDsMap[detail.CvmID] = append(cvmIDEipIDsMap[detail.CvmID], detail.EipID) + } - listRelReq := &core.ListReq{ - Filter: tools.ContainersExpression("cvm_id", ids), - Page: core.NewDefaultBasePage(), - } - diskResp, err := cli.Global.ListDiskCvmRel(kt, listRelReq) - if err != nil { - logs.Errorf("list disk cvm rel failed, err: %v, rid: %s", err, kt.Rid) - return nil, nil, nil, err - } + if len(eipResp.Details) < int(core.DefaultMaxPageLimit) { + break + } - listRelReq = &core.ListReq{ - Filter: tools.ContainersExpression("cvm_id", ids), - Page: core.NewDefaultBasePage(), - } - eipResp, err := cli.Global.ListEipCvmRel(kt, listRelReq) - if err != nil { - logs.Errorf("list eip cvm rel failed, err: %v, rid: %s", err, kt.Rid) - return nil, nil, nil, err + listRelReq.Page.Start += uint32(core.DefaultMaxPageLimit) } - listRelReq = &core.ListReq{ - Filter: tools.ContainersExpression("cvm_id", ids), - Page: core.NewDefaultBasePage(), - } - niResp, err := cli.Global.NetworkInterfaceCvmRel.ListNetworkCvmRels(kt, listRelReq) - if err != nil { - logs.Errorf("list network_interface cvm rel failed, err: %v, rid: %s", err, kt.Rid) - return nil, nil, nil, err + cvmIDDiskIDsMap = make(map[string][]string) + for { + listRelReq := &core.ListReq{ + Filter: tools.ContainersExpression("cvm_id", ids), + Page: core.NewDefaultBasePage(), + } + diskResp, err := cli.Global.ListDiskCvmRel(kt, listRelReq) + if err != nil { + logs.Errorf("list disk cvm rel failed, err: %v, rid: %s", err, kt.Rid) + return nil, nil, nil, err + } + for _, detail := range diskResp.Details { + cvmIDDiskIDsMap[detail.CvmID] = append(cvmIDDiskIDsMap[detail.CvmID], detail.DiskID) + } + + if len(diskResp.Details) < int(core.DefaultMaxPageLimit) { + break + } + + listRelReq.Page.Start += uint32(core.DefaultMaxPageLimit) } - eipIDs = slice.Map(eipResp.Details, func(rel *dataproto.EipCvmRelResult) string { - return rel.EipID - }) + cvmIDNiIDsMap = make(map[string][]string) + for { + listRelReq := &core.ListReq{ + Filter: tools.ContainersExpression("cvm_id", ids), + Page: core.NewDefaultBasePage(), + } + niResp, err := cli.Global.NetworkInterfaceCvmRel.ListNetworkCvmRels(kt, listRelReq) + if err != nil { + logs.Errorf("list network_interface cvm rel failed, err: %v, rid: %s", err, kt.Rid) + return nil, nil, nil, err + } + for _, detail := range niResp.Details { + cvmIDNiIDsMap[detail.CvmID] = append(cvmIDNiIDsMap[detail.CvmID], detail.NetworkInterfaceID) + } - diskIDs = slice.Map(diskResp.Details, func(rel *dataproto.DiskCvmRelResult) string { - return rel.DiskID - }) + if len(niResp.Details) < int(core.DefaultMaxPageLimit) { + break + } - niIDs = slice.Map(niResp.Details, func(rel *dataproto.NetworkInterfaceCvmRelResult) string { - return rel.NetworkInterfaceID - }) + listRelReq.Page.Start += uint32(core.DefaultMaxPageLimit) + } return } @@ -185,7 +242,7 @@ func ValidateCvmRelResBeforeAssign(kt *kit.Kit, cli *dataservice.Client, targetB // ValidateBeforeAssign 分配主机前校验主机信息 func ValidateBeforeAssign(kt *kit.Kit, cli *dataservice.Client, ids []string) error { listReq := &core.ListReq{ - Fields: []string{"id", "bk_biz_id", "bk_cloud_id"}, + Fields: []string{"id", "bk_biz_id"}, Filter: &filter.Expression{ Op: filter.And, Rules: []filter.RuleFactory{ @@ -202,26 +259,389 @@ func ValidateBeforeAssign(kt *kit.Kit, cli *dataservice.Client, ids []string) er accountIDMap := make(map[string]struct{}, 0) assignedIDs := make([]string, 0) - unBindCloudIDs := make([]string, 0) for _, one := range result.Details { accountIDMap[one.AccountID] = struct{}{} if one.BkBizID != constant.UnassignedBiz { assignedIDs = append(assignedIDs, one.ID) } - - if one.BkCloudID == constant.UnbindBkCloudID { - unBindCloudIDs = append(unBindCloudIDs, one.ID) - } } if len(assignedIDs) != 0 { return fmt.Errorf("cvm(ids=%v) already assigned", assignedIDs) } - if len(unBindCloudIDs) != 0 { - return fmt.Errorf("cvm(ids=%v) not bind cloud area", unBindCloudIDs) + return nil +} + +// AssignPreview 分配主机预览 +func AssignPreview(kt *kit.Kit, cli *client.ClientSet, ids []string) (map[string][]PreviewCvmMatchResult, error) { + // 1.查询cvm信息(云实例id、云厂商、内网IP、mac地址、账号所属业务) + cvmInfos, err := getAssignedCvmInfo(kt, cli, ids) + if err != nil { + logs.Errorf("get assigned cvm info failed, err: %v, ids: %v, rid: %s", err, ids, kt.Rid) + return nil, err } - return nil + // 2.获取可能匹配的cc主机 + fields := []string{"bk_host_id", "bk_cloud_id", "bk_cloud_inst_id", "bk_cloud_vendor", "bk_host_innerip", "bk_mac"} + ccHosts, ccBizHostIDsMap, err := GetAssignedHostInfoFromCC(kt, cvmInfos, fields) + if err != nil { + logs.Errorf("get assign host from cc failed, err: %v, rid: %s", err, kt.Rid) + return nil, err + } + + // 3.根据匹配规则,返回匹配cc主机结果 + return matchAssignedCvm(cvmInfos, ccHosts, ccBizHostIDsMap) +} + +func getAssignedCvmInfo(kt *kit.Kit, cli *client.ClientSet, ids []string) ([]PreviewAssignedCvmInfo, error) { + // 1.查询cvm信息 + accountIDs := make([]string, 0) + cvmMap := make(map[string]corecvm.BaseCvm) + vendorCvmMap := make(map[enumor.Vendor]map[string]corecvm.BaseCvm) + for _, batch := range slice.Split(ids, int(core.DefaultMaxPageLimit)) { + listCvmReq := &core.ListReq{ + Filter: tools.ContainersExpression("id", batch), + Page: core.NewDefaultBasePage(), + } + cvmList, err := cli.DataService().Global.Cvm.ListCvm(kt, listCvmReq) + if err != nil { + logs.Errorf("list cvm failed, err: %v, ids: %v, rid: %s", err, batch, kt.Rid) + return nil, err + } + for _, detail := range cvmList.Details { + accountIDs = append(accountIDs, detail.AccountID) + cvmMap[detail.ID] = detail + if _, ok := vendorCvmMap[detail.Vendor]; !ok { + vendorCvmMap[detail.Vendor] = make(map[string]corecvm.BaseCvm) + } + vendorCvmMap[detail.Vendor][detail.ID] = detail + } + } + + // 2.查询cvm所属账号对应的业务id + accountIDs = slice.Unique(accountIDs) + accountBizIDMap := make(map[string][]int64) + for _, batch := range slice.Split(accountIDs, int(core.DefaultMaxPageLimit)) { + accountReq := &dataproto.AccountListReq{ + Filter: tools.ContainersExpression("id", batch), + Page: core.NewDefaultBasePage(), + } + resp, err := cli.DataService().Global.Account.List(kt.Ctx, kt.Header(), accountReq) + if err != nil { + logs.Errorf("list account failed, err: %v, account ids: %v, rid: %s", err, batch, kt.Rid) + return nil, err + } + if resp == nil || len(resp.Details) == 0 { + return nil, fmt.Errorf("not found account by ids(%v)", batch) + } + for _, detail := range resp.Details { + accountBizIDMap[detail.ID] = detail.BkBizIDs + } + } + + // 3.查询cvm对应的mac地址 + cvmIPMacAddrMap, err := getAssignedCvmMacAddr(kt, cli, vendorCvmMap) + if err != nil { + logs.Errorf("get assigned cvm mac addr failed, err: %v, ids: %v, rid: %s", err, ids, kt.Rid) + return nil, err + } + + // 4.组合结果数据 + infos := make([]PreviewAssignedCvmInfo, 0, len(ids)) + for _, id := range ids { + cvmInfo, ok := cvmMap[id] + if !ok { + logs.Errorf("not found cvm info by id(%s), rid: %s", id, kt.Rid) + return nil, fmt.Errorf("not found cvm info by id(%s)", id) + } + bizIDs, ok := accountBizIDMap[cvmInfo.AccountID] + if !ok { + logs.Errorf("not found biz ids by account id(%s), cvm id(%s), rid: %s", cvmInfo.AccountID, id, kt.Rid) + return nil, fmt.Errorf("not found biz ids by account id(%s), cvm id(%s)", cvmInfo.AccountID, id) + } + var innerIPv4, macAddr string + if len(cvmInfo.PrivateIPv4Addresses) != 0 { + innerIPv4 = cvmInfo.PrivateIPv4Addresses[0] + if ipMacAddrMap, ok := cvmIPMacAddrMap[id]; ok { + macAddr = ipMacAddrMap[innerIPv4] + } + } + info := PreviewAssignedCvmInfo{CvmID: id, AccountBizIDs: bizIDs, Vendor: cvmInfo.Vendor, + CloudID: cvmInfo.CloudID, InnerIPv4: innerIPv4, MacAddr: macAddr} + infos = append(infos, info) + } + return infos, nil +} + +func getAssignedCvmMacAddr(kt *kit.Kit, cli *client.ClientSet, + vendorCvmMap map[enumor.Vendor]map[string]corecvm.BaseCvm) (map[string]map[string]string, error) { + + cvmIPv4MacAddrMap := make(map[string]map[string]string) + for vendor, cvmMap := range vendorCvmMap { + var err error + var subCvmIPv4MacAddrMap map[string]map[string]string + + switch vendor { + case enumor.TCloud, enumor.Aws: + subCvmIPv4MacAddrMap, err = getAssignedCvmMacAddrFromCloud(kt, cli, vendor, cvmMap) + if err != nil { + logs.Errorf("get assigned cvm mac addr from cloud failed, err: %v, cvmMap: %v, rid: %s", err, cvmMap, + kt.Rid) + return nil, err + } + + case enumor.HuaWei, enumor.Azure: + cvmIDs := converter.MapKeyToStringSlice(cvmMap) + subCvmIPv4MacAddrMap, err = getAssignedCvmMacAddrFromDB(kt, cli, vendor, cvmIDs) + if err != nil { + logs.Errorf("get assigned cvm mac addr from db failed, err: %v, ids: %v, rid: %s", err, cvmIDs, kt.Rid) + return nil, err + } + + case enumor.Gcp: + // todo 暂不能通过接口获取mac地址 + + default: + return nil, fmt.Errorf("no support vendor: %s", vendor) + } + + cvmIPv4MacAddrMap = maps.MapAppend(cvmIPv4MacAddrMap, subCvmIPv4MacAddrMap) + } + + return cvmIPv4MacAddrMap, nil +} + +func getAssignedCvmMacAddrFromCloud(kt *kit.Kit, cli *client.ClientSet, vendor enumor.Vendor, + cvmMap map[string]corecvm.BaseCvm) (map[string]map[string]string, error) { + + accountIDRegionCvmIDsMap := make(map[string]map[string][]string) + for _, cvmInfo := range cvmMap { + if _, ok := accountIDRegionCvmIDsMap[cvmInfo.AccountID]; !ok { + accountIDRegionCvmIDsMap[cvmInfo.AccountID] = make(map[string][]string) + } + accountIDRegionCvmIDsMap[cvmInfo.AccountID][cvmInfo.Region] = + append(accountIDRegionCvmIDsMap[cvmInfo.AccountID][cvmInfo.Region], cvmInfo.ID) + } + + cvmIPv4MacAddrMap := make(map[string]map[string]string) + for accountID, regionCvmIDsMap := range accountIDRegionCvmIDsMap { + for region, cvmIDs := range regionCvmIDsMap { + for _, batch := range slice.Split(cvmIDs, 50) { + req := &protocvm.ListCvmNetworkInterfaceReq{ + AccountID: accountID, + Region: region, + CvmIDs: batch, + } + var err error + result := new(map[string]*protocvm.ListCvmNetworkInterfaceRespItem) + switch vendor { + case enumor.TCloud: + result, err = cli.HCService().TCloud.Cvm.ListCvmNetworkInterface(kt, req) + if err != nil { + logs.Errorf("list cvm network interface failed, err: %v, vendor: %s, req: %v, rid: %s", err, + vendor, req, kt.Rid) + return nil, err + } + case enumor.Aws: + result, err = cli.HCService().Aws.Cvm.ListCvmNetworkInterface(kt, req) + if err != nil { + logs.Errorf("list cvm network interface failed, err: %v, vendor: %s, req: %v, rid: %s", err, + vendor, req, kt.Rid) + return nil, err + } + default: + return nil, fmt.Errorf("no support vendor: %s", vendor) + } + + for cvmID, item := range *result { + if _, ok := cvmIPv4MacAddrMap[cvmID]; !ok { + cvmIPv4MacAddrMap[cvmID] = make(map[string]string) + } + for macAddress, innerIPv4s := range item.MacAddressToPrivateIpAddresses { + for _, innerIPv4 := range innerIPv4s { + cvmIPv4MacAddrMap[cvmID][innerIPv4] = macAddress + } + } + } + } + } + } + + return cvmIPv4MacAddrMap, nil +} + +func getAssignedCvmMacAddrFromDB(kt *kit.Kit, cli *client.ClientSet, vendor enumor.Vendor, ids []string) ( + map[string]map[string]string, error) { + + cvmIPv4MacAddrMap := make(map[string]map[string]string) + for _, batch := range slice.Split(ids, int(core.DefaultMaxPageLimit)) { + reqData := &dataproto.NetworkInterfaceCvmRelWithListReq{CvmIDs: batch} + switch vendor { + case enumor.HuaWei: + resp, err := cli.DataService().HuaWei.ListNetworkCvmRelWithExt(kt.Ctx, kt.Header(), reqData) + if err != nil { + logs.Errorf("list network interface cvm rel failed, err: %v, ids: %v, rid: %s", err, batch, kt.Rid) + return nil, err + } + for _, detail := range resp { + if detail.Extension == nil { + continue + } + if _, ok := cvmIPv4MacAddrMap[detail.CvmID]; !ok { + cvmIPv4MacAddrMap[detail.CvmID] = make(map[string]string) + } + for _, innerIPv4 := range detail.BaseNetworkInterface.PrivateIPv4 { + cvmIPv4MacAddrMap[detail.CvmID][innerIPv4] = *detail.Extension.MacAddr + } + } + + case enumor.Azure: + resp, err := cli.DataService().Azure.ListNetworkCvmRelWithExt(kt.Ctx, kt.Header(), reqData) + if err != nil { + logs.Errorf("list network interface cvm rel failed, err: %v, ids: %v, rid: %s", err, batch, kt.Rid) + return nil, err + } + for _, detail := range resp { + if detail.Extension == nil { + continue + } + if _, ok := cvmIPv4MacAddrMap[detail.CvmID]; !ok { + cvmIPv4MacAddrMap[detail.CvmID] = make(map[string]string) + } + for _, innerIPv4 := range detail.BaseNetworkInterface.PrivateIPv4 { + cvmIPv4MacAddrMap[detail.CvmID][innerIPv4] = *detail.Extension.MacAddress + } + } + + default: + return nil, fmt.Errorf("no support vendor: %s", vendor) + } + } + + return cvmIPv4MacAddrMap, nil +} + +// GetAssignedHostInfoFromCC get assigned host from cc +func GetAssignedHostInfoFromCC(kt *kit.Kit, cvmInfos []PreviewAssignedCvmInfo, fields []string) (map[int64]cmdb.Host, + map[int64][]int64, error) { + + innerIPv4s := make([]string, 0) + macAddrs := make([]string, 0) + cloudIDs := make([]string, 0) + for _, info := range cvmInfos { + if info.InnerIPv4 != "" { + innerIPv4s = append(innerIPv4s, info.InnerIPv4) + } + if info.MacAddr != "" { + macAddrs = append(macAddrs, info.MacAddr) + } + if info.CloudID != "" { + cloudIDs = append(cloudIDs, info.CloudID) + } + } + rules := make([]cmdb.Rule, 0) + if len(innerIPv4s) != 0 { + rule := &cmdb.CombinedRule{ + Condition: "AND", + Rules: []cmdb.Rule{ + &cmdb.AtomRule{Field: "bk_addressing", Operator: cmdb.OperatorEqual, Value: cmdb.StaticAddressing}, + &cmdb.AtomRule{Field: "bk_host_innerip", Operator: cmdb.OperatorIn, Value: innerIPv4s}, + }, + } + rules = append(rules, rule) + } + if len(macAddrs) != 0 { + rules = append(rules, &cmdb.AtomRule{Field: "bk_mac", Operator: cmdb.OperatorIn, Value: macAddrs}) + } + if len(cloudIDs) != 0 { + rule := &cmdb.AtomRule{Field: "bk_cloud_inst_id", Operator: cmdb.OperatorIn, Value: cloudIDs} + rules = append(rules, rule) + } + fields = append(fields, "bk_cloud_id") + listParams := &cmdb.ListHostWithoutBizParams{ + Fields: fields, + Page: cmdb.BasePage{Sort: "bk_host_id", Start: 0, Limit: int64(core.DefaultMaxPageLimit)}, + HostPropertyFilter: &cmdb.QueryFilter{Rule: &cmdb.CombinedRule{Condition: "OR", Rules: rules}}, + } + hostIDs := make([]int64, 0) + hostMap := make(map[int64]cmdb.Host, 0) + for { + hostRes, err := esb.EsbClient().Cmdb().ListHostWithoutBiz(kt, listParams) + if err != nil { + logs.Errorf("list host from cc failed, err: %v, rid: %s", err, kt.Rid) + return nil, nil, err + } + for _, host := range hostRes.Info { + if host.BkCloudID == 0 { // todo 暂不支持管控区域为0的机器 + continue + } + hostIDs = append(hostIDs, host.BkHostID) + hostMap[host.BkHostID] = host + } + if len(hostRes.Info) < int(core.DefaultMaxPageLimit) { + break + } + listParams.Page.Start += int64(core.DefaultMaxPageLimit) + } + + bizHostIDsMap := make(map[int64][]int64, 0) + for _, batch := range slice.Split(hostIDs, int(core.DefaultMaxPageLimit)) { + param := cmdb.HostModuleRelationParams{HostID: batch} + relationRes, err := esb.EsbClient().Cmdb().FindHostBizRelations(kt, ¶m) + if err != nil { + logs.Errorf("find cmdb topo relation failed, err: %v, param: %+v, rid: %s", err, param, kt.Rid) + return nil, nil, err + } + for _, relation := range *relationRes { + if _, ok := bizHostIDsMap[relation.BizID]; !ok { + bizHostIDsMap[relation.BizID] = make([]int64, 0) + } + bizHostIDsMap[relation.BizID] = append(bizHostIDsMap[relation.BizID], relation.HostID) + } + } + + return hostMap, bizHostIDsMap, nil +} + +func matchAssignedCvm(cvmInfos []PreviewAssignedCvmInfo, ccHosts map[int64]cmdb.Host, + ccBizHostIDsMap map[int64][]int64) ( + map[string][]PreviewCvmMatchResult, error) { + + result := make(map[string][]PreviewCvmMatchResult, len(cvmInfos)) + + for _, cvmInfo := range cvmInfos { + for _, bizID := range cvmInfo.AccountBizIDs { + hostIDs, ok := ccBizHostIDsMap[bizID] + if !ok || len(hostIDs) == 0 { + continue + } + + for _, hostID := range hostIDs { + ccHost, ok := ccHosts[hostID] + if !ok { + continue + } + + ccHostVendor := cmdb.CmdbHcmVendorMap[ccHost.BkCloudVendor] + if cvmInfo.Vendor == ccHostVendor && cvmInfo.CloudID == ccHost.BkCloudInstID { + result[cvmInfo.CvmID] = append(result[cvmInfo.CvmID], PreviewCvmMatchResult{ + BkBizID: bizID, + BkCloudID: ccHost.BkCloudID, + }) + continue + } + + if cvmInfo.InnerIPv4 == ccHost.BkHostInnerIP && cvmInfo.MacAddr == ccHost.BkMac { + result[cvmInfo.CvmID] = append(result[cvmInfo.CvmID], PreviewCvmMatchResult{ + BkBizID: bizID, + BkCloudID: ccHost.BkCloudID, + }) + } + } + } + } + + return result, nil } diff --git a/cmd/cloud-server/logics/cvm/cvm.go b/cmd/cloud-server/logics/cvm/cvm.go index a75730aa34..2f8c19c16a 100644 --- a/cmd/cloud-server/logics/cvm/cvm.go +++ b/cmd/cloud-server/logics/cvm/cvm.go @@ -64,3 +64,26 @@ func NewCvm(client *client.ClientSet, audit audit.Interface, eip eip.Interface, esbClient: esbClient, } } + +// AssignedCvmInfo assigned cvm info +type AssignedCvmInfo struct { + CvmID string `json:"cvm_id"` + BkBizID int64 `json:"bk_biz_id"` + BkCloudID int64 `json:"bk_cloud_id"` +} + +// PreviewAssignedCvmInfo preview assigned cvm info +type PreviewAssignedCvmInfo struct { + CvmID string + AccountBizIDs []int64 + Vendor enumor.Vendor + CloudID string + InnerIPv4 string + MacAddr string +} + +// PreviewCvmMatchResult preview cvm match result +type PreviewCvmMatchResult struct { + BkBizID int64 + BkCloudID int64 +} diff --git a/cmd/cloud-server/service/application/handlers/cvm/aws/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/cvm/aws/create_itsm_ticket.go index 957008b118..006dc86542 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/aws/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/cvm/aws/create_itsm_ticket.go @@ -22,6 +22,8 @@ package aws import ( "fmt" "strings" + + "hcm/pkg/tools/converter" ) type formItem struct { @@ -158,7 +160,7 @@ func (a *ApplicationOfCreateAwsCvm) renderNetwork() ([]formItem, error) { } // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(vpcInfo.BkCloudID) + bkCloudAreaName, err := a.GetCloudAreaName(converter.PtrToVal(req.BkCloudID)) if err != nil { return formItems, err } diff --git a/cmd/cloud-server/service/application/handlers/cvm/aws/deliver.go b/cmd/cloud-server/service/application/handlers/cvm/aws/deliver.go index 4da7457f76..45eb90602b 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/aws/deliver.go +++ b/cmd/cloud-server/service/application/handlers/cvm/aws/deliver.go @@ -23,8 +23,6 @@ import ( "fmt" actioncvm "hcm/cmd/task-server/logics/action/cvm" - protocloud "hcm/pkg/api/data-service/cloud" - protodisk "hcm/pkg/api/data-service/cloud/disk" ts "hcm/pkg/api/task-server" "hcm/pkg/async/action" "hcm/pkg/criteria/constant" @@ -36,7 +34,8 @@ import ( func (a *ApplicationOfCreateAwsCvm) Deliver() (enumor.ApplicationStatus, map[string]interface{}, error) { req := a.toHcProtoAwsBatchCreateReq(false) - tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, a.req.BkBizID, constant.BatchCreateCvmFromCloudMaxLimit, + opt := &actioncvm.AssignCvmOption{BizID: a.req.BkBizID, BkCloudID: a.req.BkCloudID} + tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.BatchCreateCvmFromCloudMaxLimit, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = count return ts.CustomFlowTask{ @@ -62,60 +61,3 @@ func (a *ApplicationOfCreateAwsCvm) Deliver() (enumor.ApplicationStatus, map[str return enumor.Delivering, deliverDetail, nil } - -func (a *ApplicationOfCreateAwsCvm) assignToBiz(cloudCvmIDs []string) ([]string, error) { - req := a.req - // 云ID查询主机 - cvmInfo, err := a.ListCvm(a.Vendor(), req.AccountID, cloudCvmIDs) - if err != nil { - return []string{}, err - } - cvmIDs := make([]string, 0, len(cvmInfo)) - for _, cvm := range cvmInfo { - cvmIDs = append(cvmIDs, cvm.ID) - } - - // 主机分配给业务 - err = a.Client.DataService().Global.Cvm.BatchUpdateCvmCommonInfo( - a.Cts.Kit, - &protocloud.CvmCommonInfoBatchUpdateReq{IDs: cvmIDs, BkBizID: req.BkBizID}, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.CvmAuditResType, cvmIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver cvm audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - - // 主机关联资源分配给业务,目前只有硬盘是一同创建出来的 - diskIDs, err := a.ListDiskIDByCvm(cvmIDs) - if err != nil { - return cvmIDs, err - } - if len(diskIDs) > 0 { - // 硬盘分配给业务 - _, err = a.Client.DataService().Global.BatchUpdateDisk( - a.Cts.Kit, - &protodisk.DiskBatchUpdateReq{ - IDs: diskIDs, - BkBizID: uint64(req.BkBizID), - }, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.DiskAuditResType, diskIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver disk audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - } - - return cvmIDs, nil -} diff --git a/cmd/cloud-server/service/application/handlers/cvm/azure/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/cvm/azure/create_itsm_ticket.go index ce3ac95c2e..568041dd4b 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/azure/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/cvm/azure/create_itsm_ticket.go @@ -22,6 +22,8 @@ package azure import ( "fmt" "strings" + + "hcm/pkg/tools/converter" ) type formItem struct { @@ -160,7 +162,7 @@ func (a *ApplicationOfCreateAzureCvm) renderNetwork() ([]formItem, error) { } // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(vpcInfo.BkCloudID) + bkCloudAreaName, err := a.GetCloudAreaName(converter.PtrToVal(req.BkCloudID)) if err != nil { return formItems, err } diff --git a/cmd/cloud-server/service/application/handlers/cvm/azure/deliver.go b/cmd/cloud-server/service/application/handlers/cvm/azure/deliver.go index 93ad4e958d..14d6446014 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/azure/deliver.go +++ b/cmd/cloud-server/service/application/handlers/cvm/azure/deliver.go @@ -23,9 +23,6 @@ import ( "fmt" actioncvm "hcm/cmd/task-server/logics/action/cvm" - protocloud "hcm/pkg/api/data-service/cloud" - protodisk "hcm/pkg/api/data-service/cloud/disk" - protoni "hcm/pkg/api/data-service/cloud/network-interface" ts "hcm/pkg/api/task-server" "hcm/pkg/async/action" "hcm/pkg/criteria/enumor" @@ -36,7 +33,8 @@ import ( func (a *ApplicationOfCreateAzureCvm) Deliver() (enumor.ApplicationStatus, map[string]interface{}, error) { req := a.toHcProtoAzureBatchCreateReq() - tasks := actioncvm.BuildCreateCvmTasks(a.req.RequiredCount, a.req.BkBizID, 1, + opt := &actioncvm.AssignCvmOption{BizID: a.req.BkBizID, BkCloudID: a.req.BkCloudID} + tasks := actioncvm.BuildCreateCvmTasks(a.req.RequiredCount, 1, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { return ts.CustomFlowTask{ ActionID: actionID, @@ -62,84 +60,3 @@ func (a *ApplicationOfCreateAzureCvm) Deliver() (enumor.ApplicationStatus, map[s return enumor.Delivering, deliverDetail, nil } - -func (a *ApplicationOfCreateAzureCvm) assignToBiz(cloudCvmIDs []string) ([]string, error) { - req := a.req - // 云ID查询主机 - cvmInfo, err := a.ListCvm(a.Vendor(), req.AccountID, cloudCvmIDs) - if err != nil { - return []string{}, err - } - cvmIDs := make([]string, 0, len(cvmInfo)) - for _, cvm := range cvmInfo { - cvmIDs = append(cvmIDs, cvm.ID) - } - - // 主机分配给业务 - err = a.Client.DataService().Global.Cvm.BatchUpdateCvmCommonInfo( - a.Cts.Kit, - &protocloud.CvmCommonInfoBatchUpdateReq{IDs: cvmIDs, BkBizID: req.BkBizID}, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.CvmAuditResType, cvmIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver cvm audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - - // 主机关联资源硬盘分配给业务 - diskIDs, err := a.ListDiskIDByCvm(cvmIDs) - if err != nil { - return cvmIDs, err - } - if len(diskIDs) > 0 { - _, err = a.Client.DataService().Global.BatchUpdateDisk( - a.Cts.Kit, - &protodisk.DiskBatchUpdateReq{ - IDs: diskIDs, - BkBizID: uint64(req.BkBizID), - }, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.DiskAuditResType, diskIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver disk audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - } - - // 主机关联资源网络接口分配给业务 - niIDs, err := a.ListNIIDByCvm(cvmIDs) - if err != nil { - return cvmIDs, err - } - if len(niIDs) > 0 { - err = a.Client.DataService().Global.NetworkInterface.BatchUpdateNICommonInfo( - a.Cts.Kit, - &protoni.NetworkInterfaceCommonInfoBatchUpdateReq{ - IDs: niIDs, - BkBizID: int64(req.BkBizID), - }, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.NetworkInterfaceAuditResType, niIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver ni audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - } - - return cvmIDs, nil -} diff --git a/cmd/cloud-server/service/application/handlers/cvm/gcp/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/cvm/gcp/create_itsm_ticket.go index 23a81480f5..d41c721a6b 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/gcp/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/cvm/gcp/create_itsm_ticket.go @@ -24,6 +24,7 @@ import ( "strings" typecvm "hcm/pkg/adaptor/types/cvm" + "hcm/pkg/tools/converter" ) type formItem struct { @@ -160,7 +161,7 @@ func (a *ApplicationOfCreateGcpCvm) renderNetwork() ([]formItem, error) { } // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(vpcInfo.BkCloudID) + bkCloudAreaName, err := a.GetCloudAreaName(converter.PtrToVal(req.BkCloudID)) if err != nil { return formItems, err } diff --git a/cmd/cloud-server/service/application/handlers/cvm/gcp/deliver.go b/cmd/cloud-server/service/application/handlers/cvm/gcp/deliver.go index 045c2dcced..1c6194ea50 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/gcp/deliver.go +++ b/cmd/cloud-server/service/application/handlers/cvm/gcp/deliver.go @@ -23,9 +23,6 @@ import ( "fmt" actioncvm "hcm/cmd/task-server/logics/action/cvm" - protocloud "hcm/pkg/api/data-service/cloud" - protodisk "hcm/pkg/api/data-service/cloud/disk" - protoni "hcm/pkg/api/data-service/cloud/network-interface" ts "hcm/pkg/api/task-server" "hcm/pkg/async/action" "hcm/pkg/criteria/constant" @@ -37,7 +34,8 @@ import ( func (a *ApplicationOfCreateGcpCvm) Deliver() (enumor.ApplicationStatus, map[string]interface{}, error) { req := a.toHcProtoGcpBatchCreateReq() - tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, a.req.BkBizID, constant.BatchCreateCvmFromCloudMaxLimit, + opt := &actioncvm.AssignCvmOption{BizID: a.req.BkBizID, BkCloudID: a.req.BkCloudID} + tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.BatchCreateCvmFromCloudMaxLimit, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = count return ts.CustomFlowTask{ @@ -63,80 +61,3 @@ func (a *ApplicationOfCreateGcpCvm) Deliver() (enumor.ApplicationStatus, map[str return enumor.Delivering, deliverDetail, nil } - -func (a *ApplicationOfCreateGcpCvm) assignToBiz(cloudCvmIDs []string) ([]string, error) { - req := a.req - // 云ID查询主机 - cvmInfo, err := a.ListCvm(a.Vendor(), req.AccountID, cloudCvmIDs) - if err != nil { - return []string{}, err - } - cvmIDs := make([]string, 0, len(cvmInfo)) - for _, cvm := range cvmInfo { - cvmIDs = append(cvmIDs, cvm.ID) - } - - // 主机分配给业务 - err = a.Client.DataService().Global.Cvm.BatchUpdateCvmCommonInfo(a.Cts.Kit, - &protocloud.CvmCommonInfoBatchUpdateReq{IDs: cvmIDs, BkBizID: req.BkBizID}, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.CvmAuditResType, cvmIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver cvm audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - - // 主机关联资源硬盘分配给业务 - diskIDs, err := a.ListDiskIDByCvm(cvmIDs) - if err != nil { - return cvmIDs, err - } - if len(diskIDs) > 0 { - _, err = a.Client.DataService().Global.BatchUpdateDisk( - a.Cts.Kit, - &protodisk.DiskBatchUpdateReq{ - IDs: diskIDs, - BkBizID: uint64(req.BkBizID), - }, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.DiskAuditResType, diskIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver disk audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - } - - // 主机关联资源网络接口分配给业务 - niIDs, err := a.ListNIIDByCvm(cvmIDs) - if err != nil { - return cvmIDs, err - } - if len(niIDs) > 0 { - err = a.Client.DataService().Global.NetworkInterface.BatchUpdateNICommonInfo( - a.Cts.Kit, - &protoni.NetworkInterfaceCommonInfoBatchUpdateReq{IDs: niIDs, BkBizID: int64(req.BkBizID)}, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.NetworkInterfaceAuditResType, niIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver ni audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - } - - return cvmIDs, nil -} diff --git a/cmd/cloud-server/service/application/handlers/cvm/huawei/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/cvm/huawei/create_itsm_ticket.go index cfaf2a62eb..056cdb5e61 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/huawei/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/cvm/huawei/create_itsm_ticket.go @@ -23,6 +23,8 @@ import ( "fmt" "strconv" "strings" + + "hcm/pkg/tools/converter" ) type formItem struct { @@ -165,7 +167,7 @@ func (a *ApplicationOfCreateHuaWeiCvm) renderNetwork() ([]formItem, error) { } // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(vpcInfo.BkCloudID) + bkCloudAreaName, err := a.GetCloudAreaName(converter.PtrToVal(req.BkCloudID)) if err != nil { return formItems, err } diff --git a/cmd/cloud-server/service/application/handlers/cvm/huawei/deliver.go b/cmd/cloud-server/service/application/handlers/cvm/huawei/deliver.go index a29e3913bc..a875c22498 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/huawei/deliver.go +++ b/cmd/cloud-server/service/application/handlers/cvm/huawei/deliver.go @@ -23,9 +23,6 @@ import ( "fmt" actioncvm "hcm/cmd/task-server/logics/action/cvm" - protocloud "hcm/pkg/api/data-service/cloud" - protodisk "hcm/pkg/api/data-service/cloud/disk" - protoni "hcm/pkg/api/data-service/cloud/network-interface" ts "hcm/pkg/api/task-server" "hcm/pkg/async/action" "hcm/pkg/criteria/constant" @@ -37,8 +34,8 @@ import ( func (a *ApplicationOfCreateHuaWeiCvm) Deliver() (enumor.ApplicationStatus, map[string]interface{}, error) { req := a.toHcProtoHuaWeiBatchCreateReq(false) - tasks := actioncvm.BuildCreateCvmTasks(int64(req.RequiredCount), a.req.BkBizID, - constant.BatchCreateCvmFromCloudMaxLimit, + opt := &actioncvm.AssignCvmOption{BizID: a.req.BkBizID, BkCloudID: a.req.BkCloudID} + tasks := actioncvm.BuildCreateCvmTasks(int64(req.RequiredCount), constant.BatchCreateCvmFromCloudMaxLimit, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = int32(count) return ts.CustomFlowTask{ @@ -64,84 +61,3 @@ func (a *ApplicationOfCreateHuaWeiCvm) Deliver() (enumor.ApplicationStatus, map[ return enumor.Delivering, deliverDetail, nil } - -func (a *ApplicationOfCreateHuaWeiCvm) assignToBiz(cloudCvmIDs []string) ([]string, error) { - req := a.req - // 云ID查询主机 - cvmInfo, err := a.ListCvm(a.Vendor(), req.AccountID, cloudCvmIDs) - if err != nil { - return []string{}, err - } - cvmIDs := make([]string, 0, len(cvmInfo)) - for _, cvm := range cvmInfo { - cvmIDs = append(cvmIDs, cvm.ID) - } - - // 主机分配给业务 - err = a.Client.DataService().Global.Cvm.BatchUpdateCvmCommonInfo( - a.Cts.Kit, - &protocloud.CvmCommonInfoBatchUpdateReq{IDs: cvmIDs, BkBizID: req.BkBizID}, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.CvmAuditResType, cvmIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver cvm audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - - // 主机关联资源硬盘分配给业务 - diskIDs, err := a.ListDiskIDByCvm(cvmIDs) - if err != nil { - return cvmIDs, err - } - if len(diskIDs) > 0 { - _, err = a.Client.DataService().Global.BatchUpdateDisk( - a.Cts.Kit, - &protodisk.DiskBatchUpdateReq{ - IDs: diskIDs, - BkBizID: uint64(req.BkBizID), - }, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.DiskAuditResType, diskIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver disk audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - } - - // 主机关联资源网络接口分配给业务 - niIDs, err := a.ListNIIDByCvm(cvmIDs) - if err != nil { - return cvmIDs, err - } - if len(niIDs) > 0 { - err = a.Client.DataService().Global.NetworkInterface.BatchUpdateNICommonInfo( - a.Cts.Kit, - &protoni.NetworkInterfaceCommonInfoBatchUpdateReq{ - IDs: niIDs, - BkBizID: int64(req.BkBizID), - }, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.NetworkInterfaceAuditResType, niIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver ni audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - } - - return cvmIDs, nil -} diff --git a/cmd/cloud-server/service/application/handlers/cvm/tcloud/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/cvm/tcloud/create_itsm_ticket.go index 46ade14ab9..9ddbf08ef7 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/tcloud/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/cvm/tcloud/create_itsm_ticket.go @@ -178,7 +178,7 @@ func (a *ApplicationOfCreateTCloudCvm) renderNetwork() ([]formItem, error) { } // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(vpcInfo.BkCloudID) + bkCloudAreaName, err := a.GetCloudAreaName(cvt.PtrToVal(req.BkCloudID)) if err != nil { return formItems, err } diff --git a/cmd/cloud-server/service/application/handlers/cvm/tcloud/deliver.go b/cmd/cloud-server/service/application/handlers/cvm/tcloud/deliver.go index 7384a711d7..3f3a92811f 100644 --- a/cmd/cloud-server/service/application/handlers/cvm/tcloud/deliver.go +++ b/cmd/cloud-server/service/application/handlers/cvm/tcloud/deliver.go @@ -23,8 +23,6 @@ import ( "fmt" actioncvm "hcm/cmd/task-server/logics/action/cvm" - protocloud "hcm/pkg/api/data-service/cloud" - protodisk "hcm/pkg/api/data-service/cloud/disk" ts "hcm/pkg/api/task-server" "hcm/pkg/async/action" "hcm/pkg/criteria/constant" @@ -35,8 +33,8 @@ import ( // Deliver 执行资源交付 func (a *ApplicationOfCreateTCloudCvm) Deliver() (enumor.ApplicationStatus, map[string]interface{}, error) { req := a.toHcProtoTCloudBatchCreateReq(false) - - tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, a.req.BkBizID, constant.BatchCreateCvmFromCloudMaxLimit, + opt := &actioncvm.AssignCvmOption{BizID: a.req.BkBizID, BkCloudID: a.req.BkCloudID} + tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.BatchCreateCvmFromCloudMaxLimit, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = count return ts.CustomFlowTask{ @@ -63,59 +61,3 @@ func (a *ApplicationOfCreateTCloudCvm) Deliver() (enumor.ApplicationStatus, map[ return enumor.Delivering, deliverDetail, nil } - -func (a *ApplicationOfCreateTCloudCvm) assignToBiz(cloudCvmIDs []string) ([]string, error) { - req := a.req - // 云ID查询主机 - cvmInfo, err := a.ListCvm(a.Vendor(), req.AccountID, cloudCvmIDs) - if err != nil { - return []string{}, err - } - cvmIDs := make([]string, 0, len(cvmInfo)) - for _, cvm := range cvmInfo { - cvmIDs = append(cvmIDs, cvm.ID) - } - - // 主机交付给业务 - err = a.Client.DataService().Global.Cvm.BatchUpdateCvmCommonInfo( - a.Cts.Kit, - &protocloud.CvmCommonInfoBatchUpdateReq{IDs: cvmIDs, BkBizID: req.BkBizID}, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.CvmAuditResType, cvmIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver cvm audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - - // 主机关联资源分配给业务,目前只有硬盘是一同创建出来的 - diskIDs, err := a.ListDiskIDByCvm(cvmIDs) - if err != nil { - return cvmIDs, err - } - if len(diskIDs) > 0 { - // 硬盘分配给业务 - _, err = a.Client.DataService().Global.BatchUpdateDisk( - a.Cts.Kit, - &protodisk.DiskBatchUpdateReq{ - IDs: diskIDs, - BkBizID: uint64(req.BkBizID), - }, - ) - if err != nil { - return cvmIDs, err - } - - // create deliver audit - err = a.Audit.ResDeliverAudit(a.Cts.Kit, enumor.DiskAuditResType, diskIDs, int64(req.BkBizID)) - if err != nil { - logs.Errorf("create deliver disk audit failed, err: %v, rid: %s", err, a.Cts.Kit) - return nil, err - } - } - return cvmIDs, nil -} diff --git a/cmd/cloud-server/service/application/handlers/vpc/aws/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/vpc/aws/create_itsm_ticket.go index fe6d680461..199264123b 100644 --- a/cmd/cloud-server/service/application/handlers/vpc/aws/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/vpc/aws/create_itsm_ticket.go @@ -108,13 +108,6 @@ func (a *ApplicationOfCreateAwsVpc) renderVpc() ([]formItem, error) { // IPv4 CIDR formItems = append(formItems, formItem{Label: "IPv4 CIDR", Value: req.IPv4Cidr}) - // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(req.BkCloudID) - if err != nil { - return formItems, err - } - formItems = append(formItems, formItem{Label: "所属的蓝鲸云区域", Value: bkCloudAreaName}) - // 租期 instanceTenancyNameMap := map[string]string{"default": "默认", "dedicated": "专用"} formItems = append(formItems, formItem{ diff --git a/cmd/cloud-server/service/application/handlers/vpc/azure/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/vpc/azure/create_itsm_ticket.go index 14730d40cc..6e5bef7867 100644 --- a/cmd/cloud-server/service/application/handlers/vpc/azure/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/vpc/azure/create_itsm_ticket.go @@ -118,13 +118,6 @@ func (a *ApplicationOfCreateAzureVpc) renderVpc() ([]formItem, error) { // IPv4 CIDR formItems = append(formItems, formItem{Label: "IPv4 CIDR", Value: req.IPv4Cidr}) - // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(req.BkCloudID) - if err != nil { - return formItems, err - } - formItems = append(formItems, formItem{Label: "所属的蓝鲸云区域", Value: bkCloudAreaName}) - return formItems, nil } diff --git a/cmd/cloud-server/service/application/handlers/vpc/gcp/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/vpc/gcp/create_itsm_ticket.go index a744da240c..96311e8820 100644 --- a/cmd/cloud-server/service/application/handlers/vpc/gcp/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/vpc/gcp/create_itsm_ticket.go @@ -112,13 +112,6 @@ func (a *ApplicationOfCreateGcpVpc) renderVpc() ([]formItem, error) { // 名称 formItems = append(formItems, formItem{Label: "名称", Value: req.Name}) - // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(req.BkCloudID) - if err != nil { - return formItems, err - } - formItems = append(formItems, formItem{Label: "所属的蓝鲸云区域", Value: bkCloudAreaName}) - // 动态路由模式 RoutingModeNameMap := map[string]string{"REGIONAL": "区域", "GLOBAL": "全局"} formItems = append(formItems, formItem{Label: "动态路由模式", Value: RoutingModeNameMap[req.RoutingMode]}) diff --git a/cmd/cloud-server/service/application/handlers/vpc/huawei/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/vpc/huawei/create_itsm_ticket.go index 9700fee55b..016f53720b 100644 --- a/cmd/cloud-server/service/application/handlers/vpc/huawei/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/vpc/huawei/create_itsm_ticket.go @@ -115,13 +115,6 @@ func (a *ApplicationOfCreateHuaWeiVpc) renderVpc() ([]formItem, error) { // IPv4 CIDR formItems = append(formItems, formItem{Label: "IPv4 CIDR", Value: req.IPv4Cidr}) - // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(req.BkCloudID) - if err != nil { - return formItems, err - } - formItems = append(formItems, formItem{Label: "所属的蓝鲸云区域", Value: bkCloudAreaName}) - return formItems, nil } diff --git a/cmd/cloud-server/service/application/handlers/vpc/tcloud/create_itsm_ticket.go b/cmd/cloud-server/service/application/handlers/vpc/tcloud/create_itsm_ticket.go index 97d38277d2..027d8ca3cb 100644 --- a/cmd/cloud-server/service/application/handlers/vpc/tcloud/create_itsm_ticket.go +++ b/cmd/cloud-server/service/application/handlers/vpc/tcloud/create_itsm_ticket.go @@ -115,13 +115,6 @@ func (a *ApplicationOfCreateTCloudVpc) renderVpc() ([]formItem, error) { // IPv4 CIDR formItems = append(formItems, formItem{Label: "IPv4 CIDR", Value: req.IPv4Cidr}) - // 所属的蓝鲸云区域 - bkCloudAreaName, err := a.GetCloudAreaName(req.BkCloudID) - if err != nil { - return formItems, err - } - formItems = append(formItems, formItem{Label: "所属的蓝鲸云区域", Value: bkCloudAreaName}) - return formItems, nil } diff --git a/cmd/cloud-server/service/common/conv.go b/cmd/cloud-server/service/common/conv.go index 438ed3871e..150e982c4c 100644 --- a/cmd/cloud-server/service/common/conv.go +++ b/cmd/cloud-server/service/common/conv.go @@ -368,7 +368,6 @@ func ConvHuaWeiVpcCreateReq(req *csvpc.HuaWeiVpcCreateReq) *hcprotovpc.VpcCreate Name: req.Name, Category: enumor.BizVpcCategory, Memo: req.Memo, - BkCloudID: req.BkCloudID, BkBizID: req.BkBizID, }, Extension: &hcprotovpc.HuaWeiVpcCreateExt{ @@ -402,7 +401,6 @@ func ConvGcpVpcCreateReq(req *csvpc.GcpVpcCreateReq) *hcprotovpc.VpcCreateReq[hc Name: req.Name, Category: enumor.BizVpcCategory, Memo: req.Memo, - BkCloudID: req.BkCloudID, BkBizID: req.BkBizID, }, Extension: &hcprotovpc.GcpVpcCreateExt{ @@ -436,7 +434,6 @@ func ConvAzureVpcCreateReq(req *csvpc.AzureVpcCreateReq) *hcprotovpc.VpcCreateRe Name: req.Name, Category: enumor.BizVpcCategory, Memo: req.Memo, - BkCloudID: req.BkCloudID, BkBizID: req.BkBizID, }, Extension: &hcprotovpc.AzureVpcCreateExt{ @@ -469,7 +466,6 @@ func ConvAwsVpcCreateReq(req *csvpc.AwsVpcCreateReq) *hcprotovpc.VpcCreateReq[hc Name: req.Name, Category: enumor.BizVpcCategory, Memo: req.Memo, - BkCloudID: req.BkCloudID, BkBizID: req.BkBizID, }, Extension: &hcprotovpc.AwsVpcCreateExt{ @@ -489,7 +485,6 @@ func ConvTCloudVpcCreateReq(req *csvpc.TCloudVpcCreateReq) *hcprotovpc.VpcCreate Name: req.Name, Category: enumor.BizVpcCategory, Memo: req.Memo, - BkCloudID: req.BkCloudID, BkBizID: req.BkBizID, }, Extension: &hcprotovpc.TCloudVpcCreateExt{ diff --git a/cmd/cloud-server/service/cvm/assign.go b/cmd/cloud-server/service/cvm/assign.go index 6c72ddc4a8..294fae149e 100644 --- a/cmd/cloud-server/service/cvm/assign.go +++ b/cmd/cloud-server/service/cvm/assign.go @@ -20,13 +20,21 @@ package cvm import ( + "fmt" + "strings" + "hcm/cmd/cloud-server/logics/cvm" proto "hcm/pkg/api/cloud-server/cvm" + "hcm/pkg/api/core" dataproto "hcm/pkg/api/data-service/cloud" "hcm/pkg/criteria/enumor" "hcm/pkg/criteria/errf" + "hcm/pkg/dal/dao/tools" "hcm/pkg/iam/meta" + "hcm/pkg/logs" "hcm/pkg/rest" + "hcm/pkg/tools/converter" + "hcm/pkg/tools/hooks/handler" ) // AssignCvmToBiz assign cvm to biz. @@ -40,9 +48,21 @@ func (svc *cvmSvc) AssignCvmToBiz(cts *rest.Contexts) (interface{}, error) { } // 权限校验 + cvmIDs := make([]string, 0, len(req.Cvms)) + cvmIDBizIDMap := make(map[string]int64, len(req.Cvms)) + infos := make([]cvm.AssignedCvmInfo, 0, len(req.Cvms)) + for _, cvmInfo := range req.Cvms { + cvmIDs = append(cvmIDs, cvmInfo.CvmID) + cvmIDBizIDMap[cvmInfo.CvmID] = cvmInfo.BkBizID + infos = append(infos, cvm.AssignedCvmInfo{ + CvmID: cvmInfo.CvmID, + BkBizID: cvmInfo.BkBizID, + BkCloudID: converter.PtrToVal(cvmInfo.BkCloudID), + }) + } basicInfoReq := dataproto.ListResourceBasicInfoReq{ ResourceType: enumor.CvmCloudResType, - IDs: req.CvmIDs, + IDs: cvmIDs, } basicInfoMap, err := svc.client.DataService().Global.Cloud.ListResBasicInfo(cts.Kit, basicInfoReq) if err != nil { @@ -52,12 +72,135 @@ func (svc *cvmSvc) AssignCvmToBiz(cts *rest.Contexts) (interface{}, error) { authRes := make([]meta.ResourceAttribute, 0, len(basicInfoMap)) for _, info := range basicInfoMap { authRes = append(authRes, meta.ResourceAttribute{Basic: &meta.Basic{Type: meta.Cvm, - Action: meta.Assign, ResourceID: info.AccountID}, BizID: req.BkBizID}) + Action: meta.Assign, ResourceID: info.AccountID}, BizID: cvmIDBizIDMap[info.ID]}) } err = svc.authorizer.AuthorizeWithPerm(cts.Kit, authRes...) if err != nil { return nil, err } - return nil, cvm.Assign(cts.Kit, svc.client.DataService(), req.CvmIDs, req.BkBizID) + return nil, cvm.Assign(cts.Kit, svc.client.DataService(), infos) +} + +// AssignCvmToBizPreview assign cvm to biz preview. +func (svc *cvmSvc) AssignCvmToBizPreview(cts *rest.Contexts) (interface{}, error) { + req := new(proto.AssignCvmToBizPreviewReq) + if err := cts.DecodeInto(req); err != nil { + return nil, err + } + if err := req.Validate(); err != nil { + return nil, errf.NewFromErr(errf.InvalidParameter, err) + } + + // 权限校验 + basicInfoReq := dataproto.ListResourceBasicInfoReq{ + ResourceType: enumor.CvmCloudResType, + IDs: req.CvmIDs, + } + basicInfoMap, err := svc.client.DataService().Global.Cloud.ListResBasicInfo(cts.Kit, basicInfoReq) + if err != nil { + return nil, err + } + err = handler.ResOperateAuth(cts, &handler.ValidWithAuthOption{Authorizer: svc.authorizer, + ResType: meta.Cvm, Action: meta.Assign, BasicInfos: basicInfoMap}) + if err != nil { + return nil, err + } + + previewMap, err := cvm.AssignPreview(cts.Kit, svc.client, req.CvmIDs) + if err != nil { + logs.Errorf("cvm assign preview failed, err: %v, cvm ids: %v, ", err, req.CvmIDs, cts.Kit.Rid) + return nil, err + } + + details := make([]proto.AssignCvmToBizPreviewDetail, 0, len(req.CvmIDs)) + for _, cvmID := range req.CvmIDs { + previewInfos, ok := previewMap[cvmID] + if !ok || len(previewInfos) == 0 { + details = append(details, proto.AssignCvmToBizPreviewDetail{CvmID: cvmID, MatchType: enumor.NoMatchCvm}) + continue + } + + if len(previewInfos) == 1 { + details = append(details, proto.AssignCvmToBizPreviewDetail{CvmID: cvmID, MatchType: enumor.AutoMatchCvm, + BkCloudID: converter.ValToPtr(previewInfos[0].BkCloudID), BizID: previewInfos[0].BkBizID}) + continue + } + + details = append(details, proto.AssignCvmToBizPreviewDetail{CvmID: cvmID, MatchType: enumor.ManualMatchCvm}) + } + + return proto.AssignCvmToBizPreviewData{Details: details}, nil +} + +// ListAssignedCvmMatchHost list assigned cvm match host. +func (svc *cvmSvc) ListAssignedCvmMatchHost(cts *rest.Contexts) (interface{}, error) { + req := new(proto.ListAssignedCvmMatchHostReq) + if err := cts.DecodeInto(req); err != nil { + return nil, err + } + if err := req.Validate(); err != nil { + return nil, errf.NewFromErr(errf.InvalidParameter, err) + } + + // 权限校验 + auth := meta.ResourceAttribute{Basic: &meta.Basic{Type: meta.Cvm, Action: meta.Assign, ResourceID: req.AccountID}} + if err := svc.authorizer.AuthorizeWithPerm(cts.Kit, auth); err != nil { + return nil, err + } + + cvmInfos := make([]cvm.PreviewAssignedCvmInfo, 0, len(req.PrivateIPv4Addresses)) + for _, innerIPv4 := range req.PrivateIPv4Addresses { + cvmInfos = append(cvmInfos, cvm.PreviewAssignedCvmInfo{InnerIPv4: innerIPv4}) + } + fields := []string{"bk_host_id", "bk_host_innerip", "bk_host_outerip", "bk_cloud_id", "bk_cloud_region", + "bk_host_name", "bk_os_name", "create_time"} + ccHosts, ccBizHostIDsMap, err := cvm.GetAssignedHostInfoFromCC(cts.Kit, cvmInfos, fields) + if err != nil { + logs.Errorf("get assign host from cc failed, err: %v, rid: %s", err, cts.Kit.Rid) + return nil, err + } + + accountReq := &dataproto.AccountListReq{ + Filter: tools.EqualExpression("id", req.AccountID), + Page: &core.BasePage{Start: 0, Limit: 1}, + } + resp, err := svc.client.DataService().Global.Account.List(cts.Kit.Ctx, cts.Kit.Header(), accountReq) + if err != nil { + logs.Errorf("list account failed, err: %v, account id: %s, rid: %s", err, req.AccountID, cts.Kit.Rid) + return nil, err + } + if resp == nil || len(resp.Details) == 0 { + return nil, fmt.Errorf("not found account by id(%s)", req.AccountID) + } + + details := make([]proto.ListAssignedCvmMatchHostDetail, 0) + for _, bizID := range resp.Details[0].BkBizIDs { + hostIDs, ok := ccBizHostIDsMap[bizID] + if !ok || len(hostIDs) == 0 { + continue + } + + for _, hostID := range hostIDs { + ccHost, ok := ccHosts[hostID] + if !ok { + continue + } + + detail := proto.ListAssignedCvmMatchHostDetail{ + BkHostID: ccHost.BkHostID, + PrivateIPv4Addresses: strings.Split(ccHost.BkHostInnerIP, ","), + PublicIPv4Addresses: strings.Split(ccHost.BkHostOuterIP, ","), + BkCloudID: ccHost.BkCloudID, + BkBizID: bizID, + Region: ccHost.BkCloudRegion, + BkHostName: ccHost.BkHostName, + BkOsName: ccHost.BkOSName, + CreateTime: ccHost.CreateTime, + } + details = append(details, detail) + } + } + + return &proto.ListAssignedCvmMatchHostData{Details: details}, nil } diff --git a/cmd/cloud-server/service/cvm/create.go b/cmd/cloud-server/service/cvm/create.go index e9a9a4c6f8..57d35bce57 100644 --- a/cmd/cloud-server/service/cvm/create.go +++ b/cmd/cloud-server/service/cvm/create.go @@ -36,6 +36,7 @@ import ( "hcm/pkg/iam/meta" "hcm/pkg/logs" "hcm/pkg/rest" + "hcm/pkg/tools/converter" ) // CreateCvm create cvm. @@ -105,7 +106,11 @@ func (svc *cvmSvc) buildCreateAzureCvmTasks(body json.RawMessage) ([]ts.CustomFl return nil, errf.NewFromErr(errf.InvalidParameter, err) } - tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.UnassignedBiz, 1, + opt := &actioncvm.AssignCvmOption{ + BizID: constant.UnassignedBiz, + BkCloudID: converter.ValToPtr(constant.UnassignedBkCloudID), + } + tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, 1, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = count return ts.CustomFlowTask{ @@ -132,8 +137,11 @@ func (svc *cvmSvc) buildCreateHuaWeiCvmTasks(body json.RawMessage) ([]ts.CustomF return nil, errf.NewFromErr(errf.InvalidParameter, err) } - tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.UnassignedBiz, - constant.BatchCreateCvmFromCloudMaxLimit, + opt := &actioncvm.AssignCvmOption{ + BizID: constant.UnassignedBiz, + BkCloudID: converter.ValToPtr(constant.UnassignedBkCloudID), + } + tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.BatchCreateCvmFromCloudMaxLimit, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = count return ts.CustomFlowTask{ @@ -160,8 +168,11 @@ func (svc *cvmSvc) buildCreateGcpCvmTasks(body json.RawMessage) ([]ts.CustomFlow return nil, errf.NewFromErr(errf.InvalidParameter, err) } - tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.UnassignedBiz, - constant.BatchCreateCvmFromCloudMaxLimit, + opt := &actioncvm.AssignCvmOption{ + BizID: constant.UnassignedBiz, + BkCloudID: converter.ValToPtr(constant.UnassignedBkCloudID), + } + tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.BatchCreateCvmFromCloudMaxLimit, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = count return ts.CustomFlowTask{ @@ -188,8 +199,11 @@ func (svc *cvmSvc) buildCreateAwsCvmTasks(body json.RawMessage) ([]ts.CustomFlow return nil, errf.NewFromErr(errf.InvalidParameter, err) } - tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.UnassignedBiz, - constant.BatchCreateCvmFromCloudMaxLimit, + opt := &actioncvm.AssignCvmOption{ + BizID: constant.UnassignedBiz, + BkCloudID: converter.ValToPtr(constant.UnassignedBkCloudID), + } + tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.BatchCreateCvmFromCloudMaxLimit, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = count return ts.CustomFlowTask{ @@ -216,8 +230,11 @@ func (svc *cvmSvc) buildCreateTCloudCvmTasks(body json.RawMessage) ([]ts.CustomF return nil, errf.NewFromErr(errf.InvalidParameter, err) } - tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.UnassignedBiz, - constant.BatchCreateCvmFromCloudMaxLimit, + opt := &actioncvm.AssignCvmOption{ + BizID: constant.UnassignedBiz, + BkCloudID: converter.ValToPtr(constant.UnassignedBkCloudID), + } + tasks := actioncvm.BuildCreateCvmTasks(req.RequiredCount, constant.BatchCreateCvmFromCloudMaxLimit, opt, func(actionID action.ActIDType, count int64) ts.CustomFlowTask { req.RequiredCount = count return ts.CustomFlowTask{ diff --git a/cmd/cloud-server/service/cvm/cvm.go b/cmd/cloud-server/service/cvm/cvm.go index 2ea8e828dd..a26da33e8d 100644 --- a/cmd/cloud-server/service/cvm/cvm.go +++ b/cmd/cloud-server/service/cvm/cvm.go @@ -52,6 +52,8 @@ func InitCvmService(c *capability.Capability) { h.Add("InquiryPriceCvm", http.MethodPost, "/cvms/prices/inquiry", svc.InquiryPriceCvm) h.Add("BatchDeleteCvm", http.MethodDelete, "/cvms/batch", svc.BatchDeleteCvm) h.Add("AssignCvmToBiz", http.MethodPost, "/cvms/assign/bizs", svc.AssignCvmToBiz) + h.Add("AssignCvmToBizPreview", http.MethodPost, "/cvms/assign/bizs/preview", svc.AssignCvmToBizPreview) + h.Add("ListAssignedCvmMatchHost", http.MethodPost, "/cvms/assign/hosts/match/list", svc.ListAssignedCvmMatchHost) h.Add("BatchStartCvm", http.MethodPost, "/cvms/batch/start", svc.BatchStartCvm) h.Add("BatchStopCvm", http.MethodPost, "/cvms/batch/stop", svc.BatchStopCvm) h.Add("BatchRebootCvm", http.MethodPost, "/cvms/batch/reboot", svc.BatchRebootCvm) diff --git a/cmd/cloud-server/service/service.go b/cmd/cloud-server/service/service.go index f956dcf515..7c9f3bb96c 100644 --- a/cmd/cloud-server/service/service.go +++ b/cmd/cloud-server/service/service.go @@ -167,8 +167,7 @@ func getCloudClientSvr(sd serviced.ServiceDiscover) (*client.ClientSet, esb.Clie // 创建ESB Client esbConfig := cc.CloudServer().Esb - esbClient, err := esb.NewClient(&esbConfig, metrics.Register()) - if err != nil { + if err = esb.InitEsbClient(&esbConfig, metrics.Register()); err != nil { return nil, nil, nil, err } @@ -198,13 +197,13 @@ func getCloudClientSvr(sd serviced.ServiceDiscover) (*client.ClientSet, esb.Clie authorizer: authorizer, audit: logicaudit.NewAudit(apiClientSet.DataService()), cipher: cipher, - esbClient: esbClient, + esbClient: esb.EsbClient(), itsmCli: itsmCli, bkBaseCli: bkbaseCli, cmsiCli: cmsiCli, } - return apiClientSet, esbClient, svr, nil + return apiClientSet, esb.EsbClient(), svr, nil } // newCipherFromConfig 根据配置文件里的加密配置,选择配置的算法并生成对应的加解密器 diff --git a/cmd/cloud-server/service/vpc/vpc.go b/cmd/cloud-server/service/vpc/vpc.go index f2ddfd4c56..50c9ad9fe6 100644 --- a/cmd/cloud-server/service/vpc/vpc.go +++ b/cmd/cloud-server/service/vpc/vpc.go @@ -63,7 +63,6 @@ func InitVpcService(c *capability.Capability) { h.Add("UpdateVpc", "PATCH", "/vpcs/{id}", svc.UpdateVpc) h.Add("DeleteVpc", "DELETE", "/vpcs/{id}", svc.DeleteVpc) h.Add("AssignVpcToBiz", "POST", "/vpcs/assign/bizs", svc.AssignVpcToBiz) - h.Add("BindVpcWithCloudArea", "POST", "/vpcs/bind/cloud_areas", svc.BindVpcWithCloudArea) h.Add("ListResVpcExt", "POST", "/vendors/{vendor}/vpcs/list", svc.ListResVpcExt) // vpc apis in biz @@ -532,7 +531,6 @@ func (svc *vpcSvc) AssignVpcToBiz(cts *rest.Contexts) (interface{}, error) { Rules: []filter.RuleFactory{ &filter.AtomRule{Field: "id", Op: filter.In.Factory(), Value: req.VpcIDs}, &filter.AtomRule{Field: "bk_biz_id", Op: filter.Equal.Factory(), Value: constant.UnassignedBiz}, - &filter.AtomRule{Field: "bk_cloud_id", Op: filter.NotEqual.Factory(), Value: constant.UnbindBkCloudID}, }, }, Page: core.NewCountPage(), @@ -573,86 +571,6 @@ func (svc *vpcSvc) AssignVpcToBiz(cts *rest.Contexts) (interface{}, error) { return nil, nil } -// BindVpcWithCloudArea bind vpcs with cloud areas. -func (svc *vpcSvc) BindVpcWithCloudArea(cts *rest.Contexts) (interface{}, error) { - req := make(csvpc.BindVpcWithCloudAreaReq, 0) - if err := cts.DecodeInto(&req); err != nil { - return nil, err - } - - if err := req.Validate(); err != nil { - return nil, errf.NewFromErr(errf.InvalidParameter, err) - } - - // authorize - ids := make([]string, 0, len(req)) - for _, rel := range req { - ids = append(ids, rel.VpcID) - } - - err := svc.authorizeVpcBatchOp(cts.Kit, meta.Update, ids, 0) - if err != nil { - return nil, err - } - - // check if all vpcs are not assigned to biz, cannot operate biz resource in account api - vpcFilter := &filter.AtomRule{Field: "id", Op: filter.In.Factory(), Value: ids} - err = svc.checkVpcsInBiz(cts.Kit, vpcFilter, constant.UnassignedBiz) - if err != nil { - return nil, err - } - - // check if all vpcs are not assigned - assignedReq := &core.ListReq{ - Filter: &filter.Expression{ - Op: filter.And, - Rules: []filter.RuleFactory{ - &filter.AtomRule{Field: "id", Op: filter.In.Factory(), Value: ids}, - &filter.AtomRule{Field: "bk_cloud_id", Op: filter.NotEqual.Factory(), Value: constant.UnbindBkCloudID}, - }, - }, - Page: &core.BasePage{Count: true}, - } - result, err := svc.client.DataService().Global.Vpc.List(cts.Kit.Ctx, cts.Kit.Header(), assignedReq) - if err != nil { - logs.Errorf("count assigned vpc failed, err: %v, rid: %s", err, cts.Kit.Rid) - return nil, err - } - - if result.Count != 0 { - return nil, fmt.Errorf("%d vpcs are already assigned", result.Count) - } - - // create assign audit. - auditOpt := make([]audit.ResCloudAreaBindOption, 0, len(req)) - for _, info := range req { - auditOpt = append(auditOpt, audit.ResCloudAreaBindOption{ResID: info.VpcID, CloudID: info.BkCloudID}) - } - err = svc.audit.ResCloudAreaBindAudit(cts.Kit, enumor.VpcCloudAuditResType, auditOpt) - if err != nil { - logs.Errorf("create assign audit failed, err: %v, rid: %s", err, cts.Kit.Rid) - return nil, err - } - - // update vpc cloud area relations - updateReqs := make([]cloud.VpcBaseInfoUpdateReq, 0, len(req)) - for _, rel := range req { - updateReqs = append(updateReqs, cloud.VpcBaseInfoUpdateReq{ - IDs: []string{rel.VpcID}, - Data: &cloud.VpcUpdateBaseInfo{BkCloudID: rel.BkCloudID}, - }) - } - - batchUpdateReq := &cloud.VpcBaseInfoBatchUpdateReq{Vpcs: updateReqs} - - err = svc.client.DataService().Global.Vpc.BatchUpdateBaseInfo(cts.Kit.Ctx, cts.Kit.Header(), batchUpdateReq) - if err != nil { - return nil, err - } - - return nil, nil -} - func (svc *vpcSvc) authorizeVpcBatchOp(kt *kit.Kit, action meta.Action, ids []string, bizID int64) error { basicInfoReq := cloud.ListResourceBasicInfoReq{ ResourceType: enumor.VpcCloudResType, diff --git a/cmd/data-service/service/cloud/cvm/cmdb.go b/cmd/data-service/service/cloud/cvm/cmdb.go index 45ab551e26..7f3cae7e29 100644 --- a/cmd/data-service/service/cloud/cvm/cmdb.go +++ b/cmd/data-service/service/cloud/cvm/cmdb.go @@ -24,6 +24,7 @@ import ( corecvm "hcm/pkg/api/core/cloud/cvm" "hcm/pkg/criteria/constant" "hcm/pkg/criteria/enumor" + "hcm/pkg/dal/dao/tools" "hcm/pkg/dal/table/cloud/cvm" "hcm/pkg/kit" "hcm/pkg/logs" @@ -50,11 +51,27 @@ func upsertCmdbHosts[T corecvm.Extension](svc *cvmSvc, kt *kit.Kit, vendor enumo for bizID, hosts := range bizHostMap { addCmdbReq := &cmdb.AddCloudHostToBizReq[T]{Vendor: vendor, BizID: bizID, Hosts: hosts} - if err := cmdb.AddCloudHostToBiz[T](svc.cmdbLogics, kt, addCmdbReq); err != nil { + hostIDs, err := cmdb.AddCloudHostToBiz[T](svc.cmdbLogics, kt, addCmdbReq) + if err != nil { logs.Errorf("[%s] add cmdb cloud hosts failed, err: %v, req: %+v, rid: %s", constant.CmdbSyncFailed, err, addCmdbReq, kt.Rid) return err } + + if len(hostIDs) != len(hosts) { + logs.Errorf("[%s] add cmdb cloud hosts len(hostIDs[%d]) != len(hosts[%d]), req: %+v, rid: %s", + constant.CmdbSyncFailed, len(hostIDs), len(hosts), addCmdbReq, kt.Rid) + return err + } + for i, host := range hosts { + updateFilter := tools.EqualExpression("id", host.ID) + updateField := &cvm.Table{BkHostID: hostIDs[i]} + if err = svc.dao.Cvm().Update(kt, updateFilter, updateField); err != nil { + logs.Errorf("[%s] update cvm failed, err: %v, filter: %+v, field: %+v, rid: %s", + constant.CmdbSyncFailed, err, *updateFilter, updateField, kt.Rid) + return err + } + } } return nil @@ -75,11 +92,28 @@ func upsertBaseCmdbHosts(svc *cvmSvc, kt *kit.Kit, models []*cvm.Table) error { for bizID, hosts := range bizHostMap { addCmdbReq := &cmdb.AddBaseCloudHostToBizReq{BizID: bizID, Hosts: hosts} - if err := cmdb.AddBaseCloudHostToBiz(svc.cmdbLogics, kt, addCmdbReq); err != nil { + hostIDs, err := cmdb.AddBaseCloudHostToBiz(svc.cmdbLogics, kt, addCmdbReq) + if err != nil { logs.Errorf("[%s] add cmdb base cloud hosts failed, err: %v, req: %+v, rid: %s", constant.CmdbSyncFailed, err, addCmdbReq, kt.Rid) return err } + + if len(hostIDs) != len(hosts) { + logs.Errorf("[%s] add cmdb base cloud hosts len(hostIDs[%d]) != len(hosts[%d]), req: %+v, rid: %s", + constant.CmdbSyncFailed, len(hostIDs), len(hosts), addCmdbReq, kt.Rid) + return err + } + + for i, host := range hosts { + updateFilter := tools.EqualExpression("id", host.ID) + updateField := &cvm.Table{BkHostID: hostIDs[i]} + if err = svc.dao.Cvm().Update(kt, updateFilter, updateField); err != nil { + logs.Errorf("[%s] update cvm failed, err: %v, filter: %+v, field: %+v, rid: %s", + constant.CmdbSyncFailed, err, *updateFilter, updateField, kt.Rid) + return err + } + } } return nil diff --git a/cmd/data-service/service/cloud/cvm/create.go b/cmd/data-service/service/cloud/cvm/create.go index a2daf2324e..678e9cd0f6 100644 --- a/cmd/data-service/service/cloud/cvm/create.go +++ b/cmd/data-service/service/cloud/cvm/create.go @@ -85,7 +85,8 @@ func batchCreateCvm[T corecvm.Extension](cts *rest.Contexts, svc *cvmSvc, vendor Name: one.Name, Vendor: vendor, BkBizID: one.BkBizID, - BkCloudID: one.BkCloudID, + BkHostID: one.BkHostID, + BkCloudID: &one.BkCloudID, AccountID: one.AccountID, Region: one.Region, Zone: one.Zone, diff --git a/cmd/data-service/service/cloud/cvm/query.go b/cmd/data-service/service/cloud/cvm/query.go index 0f1e2519ab..d0fc332848 100644 --- a/cmd/data-service/service/cloud/cvm/query.go +++ b/cmd/data-service/service/cloud/cvm/query.go @@ -34,6 +34,7 @@ import ( "hcm/pkg/kit" "hcm/pkg/logs" "hcm/pkg/rest" + "hcm/pkg/tools/converter" "hcm/pkg/tools/json" ) @@ -132,7 +133,8 @@ func convTableToBaseCvm(one *tablecvm.Table) *corecvm.BaseCvm { Name: one.Name, Vendor: one.Vendor, BkBizID: one.BkBizID, - BkCloudID: one.BkCloudID, + BkHostID: one.BkHostID, + BkCloudID: converter.PtrToVal(one.BkCloudID), AccountID: one.AccountID, Region: one.Region, Zone: one.Zone, diff --git a/cmd/data-service/service/cloud/cvm/update.go b/cmd/data-service/service/cloud/cvm/update.go index ee36de14fb..182055537d 100644 --- a/cmd/data-service/service/cloud/cvm/update.go +++ b/cmd/data-service/service/cloud/cvm/update.go @@ -91,7 +91,7 @@ func batchUpdateCvm[T corecvm.Extension](cts *rest.Contexts, svc *cvmSvc, vendor update := &tablecvm.Table{ Name: one.Name, BkBizID: one.BkBizID, - BkCloudID: one.BkCloudID, + BkHostID: one.BkHostID, CloudVpcIDs: one.CloudVpcIDs, CloudSubnetIDs: one.CloudSubnetIDs, CloudImageID: one.CloudImageID, @@ -107,6 +107,10 @@ func batchUpdateCvm[T corecvm.Extension](cts *rest.Contexts, svc *cvmSvc, vendor Reviser: cts.Kit.User, } + if one.BkCloudID != nil { + update.BkCloudID = one.BkCloudID + } + existCvm, exist := existCvmMap[one.ID] if !exist { continue @@ -125,7 +129,7 @@ func batchUpdateCvm[T corecvm.Extension](cts *rest.Contexts, svc *cvmSvc, vendor return nil, fmt.Errorf("update cvm failed, err: %v", err) } - if update.BkCloudID == 0 { + if update.BkCloudID == nil { update.BkCloudID = existCvm.BkCloudID } @@ -198,17 +202,33 @@ func (svc *cvmSvc) BatchUpdateCvmCommonInfo(cts *rest.Contexts) (interface{}, er return nil, errf.NewFromErr(errf.InvalidParameter, err) } - updateFilter := tools.ContainersExpression("id", req.IDs) - updateFiled := &tablecvm.Table{ - BkBizID: req.BkBizID, - } - if err := svc.dao.Cvm().Update(cts.Kit, updateFilter, updateFiled); err != nil { + ids := make([]string, 0, len(req.Cvms)) + _, err := svc.dao.Txn().AutoTxn(cts.Kit, func(txn *sqlx.Tx, opt *orm.TxnOption) (interface{}, error) { + for _, one := range req.Cvms { + update := &tablecvm.Table{ + BkBizID: one.BkBizID, + } + if one.BkCloudID != nil { + update.BkCloudID = one.BkCloudID + } + if err := svc.dao.Cvm().UpdateByIDWithTx(cts.Kit, txn, one.ID, update); err != nil { + logs.Errorf("update cvm by id failed, err: %v, id: %s, update field: %v, rid: %s", err, one.ID, update, + cts.Kit.Rid) + return nil, fmt.Errorf("update cvm failed, err: %v", err) + } + ids = append(ids, one.ID) + } + return nil, nil + }) + + if err != nil { + logs.Errorf("update cvm common info failed, err: %v, rid: %s", err, cts.Kit.Rid) return nil, err } // upsert cmdb cloud hosts opt := &types.ListOption{ - Filter: updateFilter, + Filter: tools.ContainersExpression("id", ids), Page: core.NewDefaultBasePage(), } listResp, err := svc.dao.Cvm().List(cts.Kit, opt) @@ -217,10 +237,6 @@ func (svc *cvmSvc) BatchUpdateCvmCommonInfo(cts *rest.Contexts) (interface{}, er return nil, fmt.Errorf("list cvm failed, err: %v", err) } - for idx := range listResp.Details { - listResp.Details[idx].BkBizID = req.BkBizID - } - err = upsertBaseCmdbHosts(svc, cts.Kit, converter.SliceToPtr(listResp.Details)) if err != nil { logs.Errorf("upsert base cmdb hosts failed, err: %v, rid: %s", err, cts.Kit.Rid) diff --git a/cmd/data-service/service/cloud/disk-cvm-rel/query.go b/cmd/data-service/service/cloud/disk-cvm-rel/query.go index 9e24000f97..db4353c97e 100644 --- a/cmd/data-service/service/cloud/disk-cvm-rel/query.go +++ b/cmd/data-service/service/cloud/disk-cvm-rel/query.go @@ -106,7 +106,8 @@ func (svc *relSvc) ListWithCvm(cts *rest.Contexts) (interface{}, error) { Name: one.Name, Vendor: one.Vendor, BkBizID: one.BkBizID, - BkCloudID: one.BkCloudID, + BkHostID: one.BkHostID, + BkCloudID: *one.BkCloudID, AccountID: one.AccountID, Region: one.Region, Zone: one.Zone, diff --git a/cmd/data-service/service/cloud/logics/cmdb/cmdb.go b/cmd/data-service/service/cloud/logics/cmdb/cmdb.go index 40cf56f6b4..dd26ae8396 100644 --- a/cmd/data-service/service/cloud/logics/cmdb/cmdb.go +++ b/cmd/data-service/service/cloud/logics/cmdb/cmdb.go @@ -39,20 +39,21 @@ func NewCmdbLogics(client cmdb.Client) *CmdbLogics { } // AddCloudHostToBiz add cmdb cloud host to biz, update cmdb host if exists. -func AddCloudHostToBiz[T cvm.Extension](c *CmdbLogics, kt *kit.Kit, req *AddCloudHostToBizReq[T]) error { +func AddCloudHostToBiz[T cvm.Extension](c *CmdbLogics, kt *kit.Kit, req *AddCloudHostToBizReq[T]) ([]int64, error) { if err := req.Validate(); err != nil { - return err + return nil, err } vendorCmdbHostStatusMap, exists := cmdb.HcmCmdbHostStatusMap[req.Vendor] if !exists { - return errf.Newf(errf.InvalidParameter, "vendor %s is invalid", req.Vendor) + return nil, errf.Newf(errf.InvalidParameter, "vendor %s is invalid", req.Vendor) } hosts := make([]cmdb.Host, 0, len(req.Hosts)) for _, host := range req.Hosts { if host.Vendor != "" && req.Vendor != host.Vendor { - return errf.Newf(errf.InvalidParameter, "host vendor %s not match req vendor %s", host.Vendor, req.Vendor) + return nil, errf.Newf(errf.InvalidParameter, "host vendor %s not match req vendor %s", host.Vendor, + req.Vendor) } if host.Vendor == "" { host.Vendor = req.Vendor @@ -81,24 +82,24 @@ func AddCloudHostToBiz[T cvm.Extension](c *CmdbLogics, kt *kit.Kit, req *AddClou BizID: req.BizID, HostInfo: hosts, } - _, err := c.client.AddCloudHostToBiz(kt, params) + result, err := c.client.AddCloudHostToBiz(kt, params) if err != nil { - return err + return nil, err } - return nil + return result.IDs, nil } // AddBaseCloudHostToBiz add cmdb cloud host basic info to biz, update cmdb host if exists. -func AddBaseCloudHostToBiz(c *CmdbLogics, kt *kit.Kit, req *AddBaseCloudHostToBizReq) error { +func AddBaseCloudHostToBiz(c *CmdbLogics, kt *kit.Kit, req *AddBaseCloudHostToBizReq) ([]int64, error) { if err := req.Validate(); err != nil { - return err + return nil, err } hosts := make([]cmdb.Host, 0, len(req.Hosts)) for _, host := range req.Hosts { if err := host.Vendor.Validate(); err != nil { - return err + return nil, err } status, exists := cmdb.HcmCmdbHostStatusMap[host.Vendor][host.Status] @@ -124,12 +125,12 @@ func AddBaseCloudHostToBiz(c *CmdbLogics, kt *kit.Kit, req *AddBaseCloudHostToBi BizID: req.BizID, HostInfo: hosts, } - _, err := c.client.AddCloudHostToBiz(kt, params) + result, err := c.client.AddCloudHostToBiz(kt, params) if err != nil { - return err + return nil, err } - return nil + return result.IDs, nil } // DeleteCloudHostFromBiz delete cmdb cloud host from biz. diff --git a/cmd/data-service/service/cloud/vpc.go b/cmd/data-service/service/cloud/vpc.go index 8d3e309627..eebfcca9ed 100644 --- a/cmd/data-service/service/cloud/vpc.go +++ b/cmd/data-service/service/cloud/vpc.go @@ -35,12 +35,10 @@ import ( "hcm/pkg/dal/dao/tools" "hcm/pkg/dal/dao/types" tablecloud "hcm/pkg/dal/table/cloud" - "hcm/pkg/dal/table/cloud/cvm" tabletype "hcm/pkg/dal/table/types" "hcm/pkg/kit" "hcm/pkg/logs" "hcm/pkg/rest" - "hcm/pkg/runtime/filter" "hcm/pkg/tools/converter" "hcm/pkg/tools/json" @@ -123,7 +121,6 @@ func batchCreateVpc[T protocloud.VpcCreateExtension](cts *rest.Contexts, vendor Category: createReq.Category, Memo: createReq.Memo, Extension: ext, - BkCloudID: createReq.BkCloudID, BkBizID: createReq.BkBizID, Creator: cts.Kit.User, Reviser: cts.Kit.User, @@ -216,7 +213,6 @@ func batchUpdateVpc[T protocloud.VpcUpdateExtension](cts *rest.Contexts, svc *vp vpc.Name = updateReq.Name vpc.Category = updateReq.Category vpc.Memo = updateReq.Memo - vpc.BkCloudID = updateReq.BkCloudID vpc.BkBizID = updateReq.BkBizID // update extension @@ -255,21 +251,6 @@ func (svc *vpcSvc) updateVpc(kt *kit.Kit, ids []string, vpc *tablecloud.VpcTable return fmt.Errorf("update vpc failed, err: %v", err) } - // update host cloud area in vpc - cvmFilter := &filter.Expression{ - Op: filter.And, - Rules: []filter.RuleFactory{ - filter.AtomRule{Field: "vpc_ids", Op: filter.JSONOverlaps.Factory(), Value: ids}, - filter.AtomRule{Field: "bk_cloud_id", Op: filter.NotEqual.Factory(), Value: vpc.BkCloudID}, - }, - } - cvmData := &cvm.Table{BkCloudID: vpc.BkCloudID} - err = svc.dao.Cvm().Update(kt, cvmFilter, cvmData) - if err != nil { - logs.Errorf("update cvm cloud area failed, err: %v, vpc ids: %+v, rid: %s", err, ids, kt.Rid) - return fmt.Errorf("update cvm cloud area failed, err: %v", err) - } - return nil } @@ -312,7 +293,6 @@ func (svc *vpcSvc) BatchUpdateVpcBaseInfo(cts *rest.Contexts) (interface{}, erro vpc.Name = updateReq.Data.Name vpc.Category = updateReq.Data.Category vpc.Memo = updateReq.Data.Memo - vpc.BkCloudID = updateReq.Data.BkCloudID vpc.BkBizID = updateReq.Data.BkBizID err = svc.updateVpc(cts.Kit, updateReq.IDs, vpc) @@ -436,7 +416,6 @@ func convertBaseVpc(dbVpc *tablecloud.VpcTable) *protocore.BaseVpc { Region: dbVpc.Region, Category: dbVpc.Category, Memo: dbVpc.Memo, - BkCloudID: dbVpc.BkCloudID, BkBizID: dbVpc.BkBizID, Revision: &core.Revision{ Creator: dbVpc.Creator, diff --git a/cmd/hc-service/logics/res-sync/aws/cvm.go b/cmd/hc-service/logics/res-sync/aws/cvm.go index 89eed626fa..cb1dc395a8 100644 --- a/cmd/hc-service/logics/res-sync/aws/cvm.go +++ b/cmd/hc-service/logics/res-sync/aws/cvm.go @@ -166,7 +166,8 @@ func (cli *client) createCvm(kt *kit.Kit, accountID string, region string, CloudID: converter.PtrToVal(one.InstanceId), Name: converter.PtrToVal(aws.GetCvmNameFromTags(one.Tags)), BkBizID: constant.UnassignedBiz, - BkCloudID: vpcMap[converter.PtrToVal(one.VpcId)].BkCloudID, + BkHostID: constant.UnBindBkHostID, + BkCloudID: constant.UnassignedBkCloudID, AccountID: accountID, Region: region, Zone: converter.PtrToVal(one.Placement.AvailabilityZone), @@ -310,7 +311,6 @@ func (cli *client) updateCvm(kt *kit.Kit, accountID string, region string, cvm := dataproto.CvmBatchUpdate[corecvm.AwsCvmExtension]{ ID: id, Name: converter.PtrToVal(aws.GetCvmNameFromTags(one.Tags)), - BkCloudID: vpcMap[converter.PtrToVal(one.VpcId)].BkCloudID, CloudVpcIDs: []string{converter.PtrToVal(one.VpcId)}, VpcIDs: []string{vpcMap[converter.PtrToVal(one.VpcId)].VpcID}, CloudSubnetIDs: []string{converter.PtrToVal(one.SubnetId)}, @@ -426,7 +426,6 @@ func (cli *client) getVpcMap(kt *kit.Kit, accountID string, region string, vpcMap[vpc.CloudID] = &common.VpcDB{ VpcCloudID: vpc.CloudID, VpcID: vpc.ID, - BkCloudID: vpc.BkCloudID, } } } @@ -807,11 +806,13 @@ func isCvmChange(cloud typescvm.AwsCvm, db corecvm.Cvm[cvm.AwsCvmExtension]) boo return true } - if !assert.IsPtrBoolEqual(db.Extension.PrivateDnsNameOptions.EnableResourceNameDnsARecord, cloud.PrivateDnsNameOptions.EnableResourceNameDnsARecord) { + if !assert.IsPtrBoolEqual(db.Extension.PrivateDnsNameOptions.EnableResourceNameDnsARecord, + cloud.PrivateDnsNameOptions.EnableResourceNameDnsARecord) { return true } - if !assert.IsPtrStringEqual(db.Extension.PrivateDnsNameOptions.HostnameType, cloud.PrivateDnsNameOptions.HostnameType) { + if !assert.IsPtrStringEqual(db.Extension.PrivateDnsNameOptions.HostnameType, + cloud.PrivateDnsNameOptions.HostnameType) { return true } diff --git a/cmd/hc-service/logics/res-sync/aws/vpc.go b/cmd/hc-service/logics/res-sync/aws/vpc.go index 5066eccc18..8a1d19c63a 100644 --- a/cmd/hc-service/logics/res-sync/aws/vpc.go +++ b/cmd/hc-service/logics/res-sync/aws/vpc.go @@ -195,7 +195,6 @@ func (cli *client) createVpc(kt *kit.Kit, accountID string, addVpcs []types.AwsV AccountID: accountID, CloudID: item.CloudID, BkBizID: constant.UnassignedBiz, - BkCloudID: constant.UnbindBkCloudID, Name: converter.ValToPtr(item.Name), Region: item.Region, Category: enumor.BizVpcCategory, diff --git a/cmd/hc-service/logics/res-sync/azure/cvm.go b/cmd/hc-service/logics/res-sync/azure/cvm.go index e8be4936e5..cb29a1b804 100644 --- a/cmd/hc-service/logics/res-sync/azure/cvm.go +++ b/cmd/hc-service/logics/res-sync/azure/cvm.go @@ -218,7 +218,8 @@ func (cli *client) createCvm(kt *kit.Kit, accountID string, resGroupName string, CloudID: converter.PtrToVal(one.ID), Name: converter.PtrToVal(one.Name), BkBizID: constant.UnassignedBiz, - BkCloudID: vpcMap[converter.PtrToVal(one.ID)].BkCloudID, + BkHostID: constant.UnBindBkHostID, + BkCloudID: constant.UnassignedBkCloudID, AccountID: accountID, Region: converter.PtrToVal(one.Location), // 云上不支持该字段,azure可用区非地域概念 @@ -347,7 +348,6 @@ func (cli *client) updateCvm(kt *kit.Kit, accountID string, resGroupName string, cvm := dataproto.CvmBatchUpdate[corecvm.AzureCvmExtension]{ ID: id, Name: converter.PtrToVal(one.Name), - BkCloudID: vpcMap[converter.PtrToVal(one.ID)].BkCloudID, CloudVpcIDs: []string{vpcMap[converter.PtrToVal(one.ID)].VpcCloudID}, VpcIDs: []string{vpcMap[converter.PtrToVal(one.ID)].VpcID}, CloudSubnetIDs: cloudMap[converter.PtrToVal(one.ID)].CloudSubnetIDs, @@ -453,7 +453,6 @@ func (cli *client) getVpcMap(kt *kit.Kit, accountID string, cloudVpcIDsMap map[s vpcMap[cvmID] = &common.VpcDB{ VpcCloudID: vpc.CloudID, VpcID: vpc.ID, - BkCloudID: vpc.BkCloudID, } } } @@ -484,7 +483,8 @@ func (cli *client) getNIAssResMapFromNI(kt *kit.Kit, niIDs []string, resGroupNam cloudMap[cvmID] = new(CloudData) if niData.CloudSubnetID != nil { cloudMap[cvmID].CloudSubnetIDs = make([]string, 0) - cloudMap[cvmID].CloudSubnetIDs = append(cloudMap[cvmID].CloudSubnetIDs, converter.PtrToVal(niData.CloudSubnetID)) + cloudMap[cvmID].CloudSubnetIDs = append(cloudMap[cvmID].CloudSubnetIDs, + converter.PtrToVal(niData.CloudSubnetID)) cloudSubnetIDsMap[cvmID] = converter.PtrToVal(niData.CloudSubnetID) } if niData.CloudVpcID != nil { diff --git a/cmd/hc-service/logics/res-sync/azure/vpc.go b/cmd/hc-service/logics/res-sync/azure/vpc.go index 9d4cd4fd3d..d4f3a2540d 100644 --- a/cmd/hc-service/logics/res-sync/azure/vpc.go +++ b/cmd/hc-service/logics/res-sync/azure/vpc.go @@ -261,7 +261,6 @@ func (cli *client) createVpc(kt *kit.Kit, accountID string, addVpc []types.Azure CloudID: one.CloudID, Name: converter.ValToPtr(one.Name), BkBizID: constant.UnassignedBiz, - BkCloudID: constant.UnbindBkCloudID, Region: one.Region, Category: enumor.BizVpcCategory, Memo: one.Memo, diff --git a/cmd/hc-service/logics/res-sync/common/types.go b/cmd/hc-service/logics/res-sync/common/types.go index 88552a9caf..f53e6396c9 100644 --- a/cmd/hc-service/logics/res-sync/common/types.go +++ b/cmd/hc-service/logics/res-sync/common/types.go @@ -23,7 +23,6 @@ package common type VpcDB struct { VpcCloudID string VpcID string - BkCloudID int64 } // OrderedRel 有序关系,用于比较类似于多个安全组但是有序的情况 diff --git a/cmd/hc-service/logics/res-sync/gcp/cvm.go b/cmd/hc-service/logics/res-sync/gcp/cvm.go index a5d3ddb465..22bbf69447 100644 --- a/cmd/hc-service/logics/res-sync/gcp/cvm.go +++ b/cmd/hc-service/logics/res-sync/gcp/cvm.go @@ -181,7 +181,8 @@ func (cli *client) createCvm(kt *kit.Kit, accountID string, region string, zone CloudID: fmt.Sprintf("%d", one.Id), Name: one.Name, BkBizID: constant.UnassignedBiz, - BkCloudID: vpcMap[inVpcSelfLinks[0]].BkCloudID, + BkHostID: constant.UnBindBkHostID, + BkCloudID: constant.UnassignedBkCloudID, AccountID: accountID, Region: region, Zone: zone, @@ -338,7 +339,6 @@ func (cli *client) updateCvm(kt *kit.Kit, accountID string, region string, zone cvm := dataproto.CvmBatchUpdate[corecvm.GcpCvmExtension]{ ID: id, Name: one.Name, - BkCloudID: vpcMap[inVpcSelfLinks[0]].BkCloudID, CloudVpcIDs: []string{vpcMap[inVpcSelfLinks[0]].VpcCloudID}, VpcIDs: []string{vpcMap[inVpcSelfLinks[0]].VpcID}, CloudSubnetIDs: cloudSubIDs, @@ -497,7 +497,6 @@ func (cli *client) getVpcMap(kt *kit.Kit, accountID string, selfLink []string) ( vpcMap[vpc.Extension.SelfLink] = &common.VpcDB{ VpcCloudID: vpc.CloudID, VpcID: vpc.ID, - BkCloudID: vpc.BkCloudID, } } } diff --git a/cmd/hc-service/logics/res-sync/gcp/vpc.go b/cmd/hc-service/logics/res-sync/gcp/vpc.go index 78cc79179c..d5dc3b494e 100644 --- a/cmd/hc-service/logics/res-sync/gcp/vpc.go +++ b/cmd/hc-service/logics/res-sync/gcp/vpc.go @@ -206,11 +206,10 @@ func (cli *client) updateVpc(kt *kit.Kit, accountID string, updateMap map[string tmpRes := cloud.VpcUpdateReq[cloud.GcpVpcUpdateExt]{ ID: id, VpcUpdateBaseInfo: cloud.VpcUpdateBaseInfo{ - Name: converter.ValToPtr(item.Name), - Category: enumor.BizVpcCategory, - Memo: item.Memo, - BkCloudID: 0, - BkBizID: 0, + Name: converter.ValToPtr(item.Name), + Category: enumor.BizVpcCategory, + Memo: item.Memo, + BkBizID: 0, }, Extension: &cloud.GcpVpcUpdateExt{ EnableUlaInternalIpv6: converter.ValToPtr(item.Extension.EnableUlaInternalIpv6), @@ -250,7 +249,6 @@ func (cli *client) createVpc(kt *kit.Kit, accountID string, addVpc []types.GcpVp AccountID: accountID, CloudID: item.CloudID, BkBizID: constant.UnassignedBiz, - BkCloudID: constant.UnbindBkCloudID, Name: converter.ValToPtr(item.Name), Category: enumor.BizVpcCategory, Memo: item.Memo, diff --git a/cmd/hc-service/logics/res-sync/huawei/cvm.go b/cmd/hc-service/logics/res-sync/huawei/cvm.go index a87a024ff3..7048f9620c 100644 --- a/cmd/hc-service/logics/res-sync/huawei/cvm.go +++ b/cmd/hc-service/logics/res-sync/huawei/cvm.go @@ -170,7 +170,6 @@ func (cli *client) updateCvm(kt *kit.Kit, accountID string, region string, cvm := dataproto.CvmBatchUpdate[corecvm.HuaWeiCvmExtension]{ ID: id, Name: one.Name, - BkCloudID: vpcMap[one.Metadata["vpc_id"]].BkCloudID, CloudVpcIDs: []string{one.Metadata["vpc_id"]}, VpcIDs: []string{vpcMap[one.Metadata["vpc_id"]].VpcID}, CloudSubnetIDs: cloudSubnetIDs, @@ -327,7 +326,8 @@ func (cli *client) createCvm(kt *kit.Kit, accountID string, region string, CloudID: one.Id, Name: one.Name, BkBizID: constant.UnassignedBiz, - BkCloudID: vpcMap[one.Metadata["vpc_id"]].BkCloudID, + BkHostID: constant.UnBindBkHostID, + BkCloudID: constant.UnassignedBkCloudID, AccountID: accountID, Region: region, Zone: one.OSEXTAZavailabilityZone, @@ -468,8 +468,7 @@ func (cli *client) getVpcMap(kt *kit.Kit, accountID string, region string, for _, vpc := range vpcFromDB { vpcMap[vpc.CloudID] = &common.VpcDB{ - VpcID: vpc.ID, - BkCloudID: vpc.BkCloudID, + VpcID: vpc.ID, } } } diff --git a/cmd/hc-service/logics/res-sync/huawei/vpc.go b/cmd/hc-service/logics/res-sync/huawei/vpc.go index 74f689d17d..ff88b8a5c8 100644 --- a/cmd/hc-service/logics/res-sync/huawei/vpc.go +++ b/cmd/hc-service/logics/res-sync/huawei/vpc.go @@ -259,7 +259,6 @@ func (cli *client) createVpc(kt *kit.Kit, accountID string, addVpc []types.HuaWe CloudID: one.CloudID, Name: converter.ValToPtr(one.Name), BkBizID: constant.UnassignedBiz, - BkCloudID: constant.UnbindBkCloudID, Region: one.Region, Category: enumor.BizVpcCategory, Memo: one.Memo, diff --git a/cmd/hc-service/logics/res-sync/tcloud/cvm.go b/cmd/hc-service/logics/res-sync/tcloud/cvm.go index 353762fe05..1cf9f66ec4 100644 --- a/cmd/hc-service/logics/res-sync/tcloud/cvm.go +++ b/cmd/hc-service/logics/res-sync/tcloud/cvm.go @@ -152,7 +152,6 @@ func (cli *client) updateCvm(kt *kit.Kit, accountID string, region string, updateOne := dataproto.CvmBatchUpdate[corecvm.TCloudCvmExtension]{ ID: id, Name: converter.PtrToVal(one.InstanceName), - BkCloudID: vpcMap[converter.PtrToVal(one.VirtualPrivateCloud.VpcId)].BkCloudID, CloudVpcIDs: []string{converter.PtrToVal(one.VirtualPrivateCloud.VpcId)}, VpcIDs: []string{vpcMap[converter.PtrToVal(one.VirtualPrivateCloud.VpcId)].VpcID}, CloudSubnetIDs: []string{converter.PtrToVal(one.VirtualPrivateCloud.SubnetId)}, @@ -276,7 +275,8 @@ func (cli *client) createCvm(kt *kit.Kit, accountID string, region string, CloudID: converter.PtrToVal(one.InstanceId), Name: converter.PtrToVal(one.InstanceName), BkBizID: constant.UnassignedBiz, - BkCloudID: vpcMap[converter.PtrToVal(one.VirtualPrivateCloud.VpcId)].BkCloudID, + BkHostID: constant.UnBindBkHostID, + BkCloudID: constant.UnassignedBkCloudID, AccountID: accountID, Region: region, Zone: converter.PtrToVal(one.Placement.Zone), @@ -371,8 +371,7 @@ func (cli *client) getVpcMap(kt *kit.Kit, accountID string, region string, for _, vpc := range vpcFromDB { vpcMap[vpc.CloudID] = &common.VpcDB{ - VpcID: vpc.ID, - BkCloudID: vpc.BkCloudID, + VpcID: vpc.ID, } } } diff --git a/cmd/hc-service/logics/res-sync/tcloud/subnet.go b/cmd/hc-service/logics/res-sync/tcloud/subnet.go index 65d6b29afe..fbaf7256d3 100644 --- a/cmd/hc-service/logics/res-sync/tcloud/subnet.go +++ b/cmd/hc-service/logics/res-sync/tcloud/subnet.go @@ -211,7 +211,7 @@ func (cli *client) createSubnet(kt *kit.Kit, accountID string, region string, AccountID: accountID, CloudVpcID: item.CloudVpcID, VpcID: vpcID, - BkBizID: constant.UnbindBkCloudID, + BkBizID: constant.UnassignedBiz, CloudID: item.CloudID, Name: converter.ValToPtr(item.Name), Region: item.Region, diff --git a/cmd/hc-service/logics/res-sync/tcloud/vpc.go b/cmd/hc-service/logics/res-sync/tcloud/vpc.go index 3f18f2b323..194401b18f 100644 --- a/cmd/hc-service/logics/res-sync/tcloud/vpc.go +++ b/cmd/hc-service/logics/res-sync/tcloud/vpc.go @@ -262,7 +262,6 @@ func (cli *client) createVpc(kt *kit.Kit, accountID string, addVpc []types.TClou CloudID: one.CloudID, Name: converter.ValToPtr(one.Name), BkBizID: constant.UnassignedBiz, - BkCloudID: constant.UnbindBkCloudID, Region: one.Region, Category: enumor.BizVpcCategory, Memo: one.Memo, diff --git a/cmd/hc-service/service/cvm/aws.go b/cmd/hc-service/service/cvm/aws.go index 4f878e7918..3dc902cea4 100644 --- a/cmd/hc-service/service/cvm/aws.go +++ b/cmd/hc-service/service/cvm/aws.go @@ -413,14 +413,16 @@ func (svc *cvmSvc) listAwsCvmNetworkInterfaceFromCloud(kt *kit.Kit, region, acco cloudID := converter.PtrToVal(detail.Attachment.InstanceId) id := cloudIDToIDMap[cloudID] if _, ok := result[id]; !ok { - result[id] = &protocvm.ListCvmNetworkInterfaceRespItem{} + result[id] = &protocvm.ListCvmNetworkInterfaceRespItem{ + MacAddressToPrivateIpAddresses: make(map[string][]string), + } } privateIPs := make([]string, 0) for _, set := range detail.PrivateIpAddresses { privateIPs = append(privateIPs, converter.PtrToVal(set.PrivateIpAddress)) } - result[id].MapAddressToPrivateIpAddresses[converter.PtrToVal(detail.MacAddress)] = privateIPs + result[id].MacAddressToPrivateIpAddresses[converter.PtrToVal(detail.MacAddress)] = privateIPs } if resp.NextToken == nil { break diff --git a/cmd/hc-service/service/cvm/cvm.go b/cmd/hc-service/service/cvm/cvm.go index 49315b6535..5dee5aab5a 100644 --- a/cmd/hc-service/service/cvm/cvm.go +++ b/cmd/hc-service/service/cvm/cvm.go @@ -54,7 +54,8 @@ type cvmSvc struct { client *client.ClientSet } -func (svc *cvmSvc) getCvms(kt *kit.Kit, vendor enumor.Vendor, region string, cvmIDs []string) ([]corecvm.BaseCvm, error) { +func (svc *cvmSvc) getCvms(kt *kit.Kit, vendor enumor.Vendor, region string, cvmIDs []string) ([]corecvm.BaseCvm, + error) { if len(cvmIDs) == 0 { return nil, nil } @@ -65,7 +66,7 @@ func (svc *cvmSvc) getCvms(kt *kit.Kit, vendor enumor.Vendor, region string, cvm Filter: tools.ExpressionAnd( tools.RuleEqual("vendor", vendor), tools.RuleEqual("region", region), - tools.RuleIn("cvm_id", ids), + tools.RuleIn("id", ids), ), Page: core.NewDefaultBasePage(), } diff --git a/cmd/hc-service/service/cvm/tcloud.go b/cmd/hc-service/service/cvm/tcloud.go index ba252b6c66..4a51d29d09 100644 --- a/cmd/hc-service/service/cvm/tcloud.go +++ b/cmd/hc-service/service/cvm/tcloud.go @@ -568,7 +568,7 @@ func (svc *cvmSvc) listTCloudNetworkInterfaceFromCloud(kt *kit.Kit, region, acco id := cloudIDToIDMap[cloudID] if _, ok := result[id]; !ok { result[id] = &protocvm.ListCvmNetworkInterfaceRespItem{ - MapAddressToPrivateIpAddresses: make(map[string][]string), + MacAddressToPrivateIpAddresses: make(map[string][]string), } } @@ -576,7 +576,7 @@ func (svc *cvmSvc) listTCloudNetworkInterfaceFromCloud(kt *kit.Kit, region, acco for _, set := range detail.PrivateIpAddressSet { privateIPs = append(privateIPs, cvt.PtrToVal(set.PrivateIpAddress)) } - result[id].MapAddressToPrivateIpAddresses[cvt.PtrToVal(detail.MacAddress)] = privateIPs + result[id].MacAddressToPrivateIpAddresses[cvt.PtrToVal(detail.MacAddress)] = privateIPs } if len(resp.Details) < adcore.TCloudQueryLimit { diff --git a/cmd/hc-service/service/vpc/aws.go b/cmd/hc-service/service/vpc/aws.go index 031fbcb61e..a3c96f9c89 100644 --- a/cmd/hc-service/service/vpc/aws.go +++ b/cmd/hc-service/service/vpc/aws.go @@ -129,7 +129,6 @@ func convertAwsVpcCreateReq(req *hcservice.VpcCreateReq[hcservice.AwsVpcCreateEx AccountID: req.AccountID, CloudID: data.CloudID, BkBizID: constant.UnassignedBiz, - BkCloudID: req.BkCloudID, Name: &data.Name, Region: data.Region, Category: req.Category, diff --git a/cmd/hc-service/service/vpc/azure.go b/cmd/hc-service/service/vpc/azure.go index 9ea53a1b90..02b0be1186 100644 --- a/cmd/hc-service/service/vpc/azure.go +++ b/cmd/hc-service/service/vpc/azure.go @@ -111,7 +111,6 @@ func convertVpcCreateReq(req *hcservice.VpcCreateReq[hcservice.AzureVpcCreateExt AccountID: req.AccountID, CloudID: data.CloudID, BkBizID: constant.UnassignedBiz, - BkCloudID: req.BkCloudID, Name: &data.Name, Region: data.Region, Category: req.Category, diff --git a/cmd/hc-service/service/vpc/gcp.go b/cmd/hc-service/service/vpc/gcp.go index b42c9ba52e..08552da510 100644 --- a/cmd/hc-service/service/vpc/gcp.go +++ b/cmd/hc-service/service/vpc/gcp.go @@ -218,7 +218,6 @@ func convertGcpVpcCreateReq(req *hcservice.VpcCreateReq[hcservice.GcpVpcCreateEx AccountID: req.AccountID, CloudID: data.CloudID, BkBizID: constant.UnassignedBiz, - BkCloudID: req.BkCloudID, Name: &data.Name, Region: data.Region, Category: req.Category, diff --git a/cmd/hc-service/service/vpc/huawei.go b/cmd/hc-service/service/vpc/huawei.go index 4d4e9630fd..d70c73afbf 100644 --- a/cmd/hc-service/service/vpc/huawei.go +++ b/cmd/hc-service/service/vpc/huawei.go @@ -147,7 +147,6 @@ func convertHuaWeiVpcCreateReq(req *hcservice.VpcCreateReq[hcservice.HuaWeiVpcCr AccountID: req.AccountID, CloudID: data.CloudID, BkBizID: constant.UnassignedBiz, - BkCloudID: req.BkCloudID, Name: &data.Name, Region: data.Region, Category: req.Category, diff --git a/cmd/hc-service/service/vpc/tcloud.go b/cmd/hc-service/service/vpc/tcloud.go index 3daa6bfd44..b3b41ee135 100644 --- a/cmd/hc-service/service/vpc/tcloud.go +++ b/cmd/hc-service/service/vpc/tcloud.go @@ -114,7 +114,6 @@ func convertTCloudVpcCreateReq(req *hcservice.VpcCreateReq[hcservice.TCloudVpcCr AccountID: req.AccountID, CloudID: data.CloudID, BkBizID: constant.UnassignedBiz, - BkCloudID: req.BkCloudID, Name: &data.Name, Region: data.Region, Category: req.Category, diff --git a/cmd/task-server/logics/action/cvm/assign.go b/cmd/task-server/logics/action/cvm/assign.go index bf331033a1..e31aa448d0 100644 --- a/cmd/task-server/logics/action/cvm/assign.go +++ b/cmd/task-server/logics/action/cvm/assign.go @@ -46,7 +46,8 @@ type AssignCvmAction struct{} // AssignCvmOption assign cvm option. type AssignCvmOption struct { - BizID int64 `json:"bk_biz_id" validate:"required"` + BizID int64 `json:"bk_biz_id" validate:"required"` + BkCloudID *int64 `json:"bk_cloud_id" validate:"required"` } // Validate AssignCvmOption. @@ -96,11 +97,19 @@ func (act AssignCvmAction) Run(kt run.ExecuteKit, params interface{}) (result in return cvm.ID }) - cvmIDs = append(cvmIDs, ids...) - if err = cvm.Assign(kt.Kit(), cli, ids, opt.BizID); err != nil { + assignedCvmInfo := make([]cvm.AssignedCvmInfo, 0, len(ids)) + for _, id := range ids { + assignedCvmInfo = append(assignedCvmInfo, cvm.AssignedCvmInfo{ + CvmID: id, + BkBizID: opt.BizID, + BkCloudID: *opt.BkCloudID, + }) + } + if err = cvm.Assign(kt.Kit(), cli, assignedCvmInfo); err != nil { logs.Errorf("assign cvm failed, err: %v, ids: %+v, rid: %s", err, ids, kt.Kit().Rid) return nil, err } + cvmIDs = append(cvmIDs, ids...) } return &AssignCvmResult{IDs: cvmIDs}, nil diff --git a/cmd/task-server/logics/action/cvm/build_task.go b/cmd/task-server/logics/action/cvm/build_task.go index 7ece971484..329b2f9d85 100644 --- a/cmd/task-server/logics/action/cvm/build_task.go +++ b/cmd/task-server/logics/action/cvm/build_task.go @@ -28,7 +28,7 @@ import ( ) // BuildCreateCvmTasks build create cvm tasks. -func BuildCreateCvmTasks(totalCount int64, bizID int64, limit int64, +func BuildCreateCvmTasks(totalCount int64, limit int64, assignCvmOpt *AssignCvmOption, convTask func(actionID action.ActIDType, count int64) ts.CustomFlowTask) []ts.CustomFlowTask { ctrFunc := counter.NewNumStringCounter(1, 10) @@ -48,14 +48,12 @@ func BuildCreateCvmTasks(totalCount int64, bizID int64, limit int64, } // 如果业务ID大于0,代表要分配主机到业务下 - if bizID > 0 { + if assignCvmOpt != nil && assignCvmOpt.BizID > 0 { tasks = append(tasks, ts.CustomFlowTask{ ActionID: action.ActIDType(uuid.UUID()), ActionName: enumor.ActionAssignCvm, - Params: &AssignCvmOption{ - BizID: bizID, - }, - DependOn: actionIDs, + Params: assignCvmOpt, + DependOn: actionIDs, }) } diff --git a/pkg/api/cloud-server/cvm/create_aws_cvm.go b/pkg/api/cloud-server/cvm/create_aws_cvm.go index 6125ecf2a9..202893db14 100644 --- a/pkg/api/cloud-server/cvm/create_aws_cvm.go +++ b/pkg/api/cloud-server/cvm/create_aws_cvm.go @@ -34,6 +34,7 @@ import ( type AwsCvmCreateReq struct { BkBizID int64 `json:"bk_biz_id" validate:"omitempty"` AccountID string `json:"account_id" validate:"required"` + BkCloudID *int64 `json:"bk_cloud_id" validate:"required"` Region string `json:"region" validate:"required"` Zone string `json:"zone" validate:"required"` Name string `json:"name" validate:"required,min=1,max=60"` @@ -65,15 +66,19 @@ type AwsCvmCreateReq struct { } // Validate ... -func (req *AwsCvmCreateReq) Validate(bizRequired bool) error { +func (req *AwsCvmCreateReq) Validate(isFromBiz bool) error { if err := validator.Validate.Struct(req); err != nil { return err } - if bizRequired && req.BkBizID == 0 { + if isFromBiz && req.BkBizID == 0 { return errors.New("biz is required") } + if isFromBiz && req.BkCloudID == nil { + return errors.New("bk_cloud_id is required") + } + if err := validator.ValidateCvmName(enumor.Aws, req.Name); err != nil { return errf.NewFromErr(errf.InvalidParameter, err) } diff --git a/pkg/api/cloud-server/cvm/create_azure_cvm.go b/pkg/api/cloud-server/cvm/create_azure_cvm.go index 106e592836..941c5a049a 100644 --- a/pkg/api/cloud-server/cvm/create_azure_cvm.go +++ b/pkg/api/cloud-server/cvm/create_azure_cvm.go @@ -48,6 +48,7 @@ var ( type AzureCvmCreateReq struct { BkBizID int64 `json:"bk_biz_id" validate:"omitempty"` AccountID string `json:"account_id" validate:"required"` + BkCloudID *int64 `json:"bk_cloud_id" validate:"required"` ResourceGroupName string `json:"resource_group_name" validate:"required,lowercase"` Region string `json:"region" validate:"required,lowercase"` Zone string `json:"zone" validate:"omitempty,lowercase"` @@ -84,15 +85,19 @@ type AzureCvmCreateReq struct { } // Validate ... -func (req *AzureCvmCreateReq) Validate(bizRequired bool) error { +func (req *AzureCvmCreateReq) Validate(isFromBiz bool) error { if err := validator.Validate.Struct(req); err != nil { return err } - if bizRequired && req.BkBizID == 0 { + if isFromBiz && req.BkBizID == 0 { return errors.New("bk_biz_id is required") } + if isFromBiz && req.BkCloudID == nil { + return errors.New("bk_cloud_id is required") + } + if err := validator.ValidateCvmName(enumor.Azure, req.Name); err != nil { return errf.NewFromErr(errf.InvalidParameter, err) } diff --git a/pkg/api/cloud-server/cvm/create_gcp_cvm.go b/pkg/api/cloud-server/cvm/create_gcp_cvm.go index 861436bf2c..116bb3e7c1 100644 --- a/pkg/api/cloud-server/cvm/create_gcp_cvm.go +++ b/pkg/api/cloud-server/cvm/create_gcp_cvm.go @@ -34,6 +34,7 @@ import ( type GcpCvmCreateReq struct { BkBizID int64 `json:"bk_biz_id" validate:"omitempty"` AccountID string `json:"account_id" validate:"required"` + BkCloudID *int64 `json:"bk_cloud_id" validate:"required"` Region string `json:"region" validate:"required"` Zone string `json:"zone" validate:"required"` Name string `json:"name" validate:"required,min=1,max=60"` @@ -66,15 +67,19 @@ type GcpCvmCreateReq struct { } // Validate ... -func (req *GcpCvmCreateReq) Validate(bizRequired bool) error { +func (req *GcpCvmCreateReq) Validate(isFromBiz bool) error { if err := validator.Validate.Struct(req); err != nil { return err } - if bizRequired && req.BkBizID == 0 { + if isFromBiz && req.BkBizID == 0 { return errors.New("bk_biz_id is required") } + if isFromBiz && req.BkCloudID == nil { + return errors.New("bk_cloud_id is required") + } + if err := validator.ValidateCvmName(enumor.Gcp, req.Name); err != nil { return errf.NewFromErr(errf.InvalidParameter, err) } diff --git a/pkg/api/cloud-server/cvm/create_huawei_cvm.go b/pkg/api/cloud-server/cvm/create_huawei_cvm.go index 13236954be..d54aaec8d2 100644 --- a/pkg/api/cloud-server/cvm/create_huawei_cvm.go +++ b/pkg/api/cloud-server/cvm/create_huawei_cvm.go @@ -46,6 +46,7 @@ var ( type HuaWeiCvmCreateReq struct { BkBizID int64 `json:"bk_biz_id" validate:"omitempty"` AccountID string `json:"account_id" validate:"required"` + BkCloudID *int64 `json:"bk_cloud_id" validate:"required"` Region string `json:"region" validate:"required"` Zone string `json:"zone" validate:"required"` Name string `json:"name" validate:"required,min=1,max=60"` @@ -81,15 +82,19 @@ type HuaWeiCvmCreateReq struct { } // Validate ... -func (req *HuaWeiCvmCreateReq) Validate(bizRequired bool) error { +func (req *HuaWeiCvmCreateReq) Validate(isFromBiz bool) error { if err := validator.Validate.Struct(req); err != nil { return err } - if bizRequired && req.BkBizID == 0 { + if isFromBiz && req.BkBizID == 0 { return errors.New("bk_biz_id is required") } + if isFromBiz && req.BkCloudID == nil { + return errors.New("bk_cloud_id is required") + } + if req.PublicIPAssigned { if err := req.Eip.Validate(); err != nil { return err diff --git a/pkg/api/cloud-server/cvm/create_tcloud_cvm.go b/pkg/api/cloud-server/cvm/create_tcloud_cvm.go index e62c586aca..7398f03223 100644 --- a/pkg/api/cloud-server/cvm/create_tcloud_cvm.go +++ b/pkg/api/cloud-server/cvm/create_tcloud_cvm.go @@ -29,6 +29,7 @@ import ( "hcm/pkg/criteria/enumor" "hcm/pkg/criteria/errf" "hcm/pkg/criteria/validator" + "hcm/pkg/tools/converter" "github.com/TencentBlueKing/gopkg/collection/set" ) @@ -46,6 +47,7 @@ var ( type TCloudCvmCreateReq struct { BkBizID int64 `json:"bk_biz_id" validate:"omitempty"` AccountID string `json:"account_id" validate:"required"` + BkCloudID *int64 `json:"bk_cloud_id"` Region string `json:"region" validate:"required"` Zone string `json:"zone" validate:"required"` Name string `json:"name" validate:"required,min=1,max=60"` @@ -84,15 +86,24 @@ type TCloudCvmCreateReq struct { } // Validate ... -func (req *TCloudCvmCreateReq) Validate(bizRequired bool) error { +func (req *TCloudCvmCreateReq) Validate(isFromBiz bool) error { if err := validator.Validate.Struct(req); err != nil { return err } - if bizRequired && req.BkBizID == 0 { + if isFromBiz && req.BkBizID == 0 { return errors.New("bk_biz_id is required") } + if isFromBiz && req.BkCloudID == nil { + return errors.New("bk_cloud_id is required") + } + + // todo 暂不支持管控区域为0 + if req.BkCloudID != nil && converter.PtrToVal(req.BkCloudID) == 0 { + return errors.New("bk_cloud_id should != 0") + } + if req.RequiredCount > constant.BatchOperationMaxLimit { return fmt.Errorf("required count should <= %d", constant.BatchOperationMaxLimit) } diff --git a/pkg/api/cloud-server/cvm/cvm.go b/pkg/api/cloud-server/cvm/cvm.go index 3944efdbff..bfbda75783 100644 --- a/pkg/api/cloud-server/cvm/cvm.go +++ b/pkg/api/cloud-server/cvm/cvm.go @@ -23,15 +23,17 @@ import ( "errors" "fmt" + "hcm/pkg/api/core" rr "hcm/pkg/api/core/recycle-record" "hcm/pkg/criteria/constant" + "hcm/pkg/criteria/enumor" "hcm/pkg/criteria/validator" + "hcm/pkg/tools/converter" ) // AssignCvmToBizReq define assign cvm to biz req. type AssignCvmToBizReq struct { - BkBizID int64 `json:"bk_biz_id" validate:"required"` - CvmIDs []string `json:"cvm_ids" validate:"required"` + Cvms []AssignCvmToBizData `json:"cvms" validate:"required,min=1,max=500"` } // Validate assign cvm to biz request. @@ -40,21 +42,100 @@ func (req *AssignCvmToBizReq) Validate() error { return err } + for _, cvm := range req.Cvms { + if err := cvm.Validate(); err != nil { + return err + } + } + + return nil +} + +// AssignCvmToBizData define assign cvm to biz data. +type AssignCvmToBizData struct { + CvmID string `json:"cvm_id" validate:"required"` + BkBizID int64 `json:"bk_biz_id" validate:"required"` + BkCloudID *int64 `json:"bk_cloud_id" validate:"required"` +} + +// Validate assign cvm to biz data. +func (req *AssignCvmToBizData) Validate() error { + if err := validator.Validate.Struct(req); err != nil { + return err + } + if req.BkBizID <= 0 { return errors.New("bk_biz_id should >= 0") } + // todo 本期暂不支持管控区域为0 + if req.BkCloudID != nil && converter.PtrToVal(req.BkCloudID) == 0 { + return errors.New("bk_cloud_id should != 0") + } + + return nil +} + +// AssignCvmToBizPreviewReq define assign cvm to biz preview req. +type AssignCvmToBizPreviewReq struct { + CvmIDs []string `json:"cvm_ids"` +} + +// Validate assign cvm to biz preview request. +func (req *AssignCvmToBizPreviewReq) Validate() error { if len(req.CvmIDs) == 0 { return errors.New("cvm ids is required") } - if len(req.CvmIDs) > constant.BatchOperationMaxLimit { - return fmt.Errorf("cvm ids should <= %d", constant.BatchOperationMaxLimit) + if len(req.CvmIDs) > int(core.DefaultMaxPageLimit) { + return fmt.Errorf("cvm ids length should <= %d", core.DefaultMaxPageLimit) } return nil } +// AssignCvmToBizPreviewData define assign cvm to biz preview data. +type AssignCvmToBizPreviewData struct { + Details []AssignCvmToBizPreviewDetail `json:"details"` +} + +// AssignCvmToBizPreviewDetail define assign cvm to biz preview detail. +type AssignCvmToBizPreviewDetail struct { + CvmID string `json:"cvm_id"` + MatchType enumor.CvmMatchType `json:"match_type"` + BizID int64 `json:"bk_biz_id,omitempty"` + BkCloudID *int64 `json:"bk_cloud_id,omitempty"` +} + +// ListAssignedCvmMatchHostReq list assigned cvm match host req. +type ListAssignedCvmMatchHostReq struct { + AccountID string `json:"account_id" validate:"required"` + PrivateIPv4Addresses []string `json:"private_ipv4_addresses" validate:"required,min=1,max=10"` +} + +// Validate list assigned cvm match host request. +func (req *ListAssignedCvmMatchHostReq) Validate() error { + return validator.Validate.Struct(req) +} + +// ListAssignedCvmMatchHostData define list assigned cvm match host data. +type ListAssignedCvmMatchHostData struct { + Details []ListAssignedCvmMatchHostDetail `json:"details"` +} + +// ListAssignedCvmMatchHostDetail define list assigned cvm match host detail. +type ListAssignedCvmMatchHostDetail struct { + BkHostID int64 `json:"bk_host_id"` + PrivateIPv4Addresses []string `json:"private_ipv4_addresses"` + PublicIPv4Addresses []string `json:"public_ipv4_addresses"` + BkCloudID int64 `json:"bk_cloud_id"` + BkBizID int64 `json:"bk_biz_id"` + Region string `json:"region"` + BkHostName string `json:"bk_host_name"` + BkOsName string `json:"bk_os_name"` + CreateTime string `json:"create_time"` +} + // BatchStartCvmReq batch start cvm req. type BatchStartCvmReq struct { IDs []string `json:"ids" validate:"required"` diff --git a/pkg/api/cloud-server/vpc/create_aws_vpc.go b/pkg/api/cloud-server/vpc/create_aws_vpc.go index 6b31534f56..6df1cd80ab 100644 --- a/pkg/api/cloud-server/vpc/create_aws_vpc.go +++ b/pkg/api/cloud-server/vpc/create_aws_vpc.go @@ -32,7 +32,6 @@ type AwsVpcCreateReq struct { Region string `json:"region" validate:"required"` Name string `json:"name" validate:"required,min=1,max=60"` IPv4Cidr string `json:"ipv4_cidr" validate:"required,cidrv4"` - BkCloudID int64 `json:"bk_cloud_id" validate:"required,min=1"` InstanceTenancy string `json:"instance_tenancy" validate:"required,oneof=default dedicated"` diff --git a/pkg/api/cloud-server/vpc/create_azure_vpc.go b/pkg/api/cloud-server/vpc/create_azure_vpc.go index 7079489907..a3440b6b2e 100644 --- a/pkg/api/cloud-server/vpc/create_azure_vpc.go +++ b/pkg/api/cloud-server/vpc/create_azure_vpc.go @@ -37,7 +37,6 @@ type AzureVpcCreateReq struct { Region string `json:"region" validate:"required,lowercase"` Name string `json:"name" validate:"required,min=1,max=60,lowercase"` IPv4Cidr string `json:"ipv4_cidr" validate:"required,cidrv4"` - BkCloudID int64 `json:"bk_cloud_id" validate:"required,min=1"` Subnet struct { Name string `json:"name" validate:"required,min=1,max=60,lowercase"` diff --git a/pkg/api/cloud-server/vpc/create_gcp_vpc.go b/pkg/api/cloud-server/vpc/create_gcp_vpc.go index 426b6e608e..6511e1d89e 100644 --- a/pkg/api/cloud-server/vpc/create_gcp_vpc.go +++ b/pkg/api/cloud-server/vpc/create_gcp_vpc.go @@ -31,7 +31,6 @@ type GcpVpcCreateReq struct { AccountID string `json:"account_id" validate:"required"` Region string `json:"region" validate:"required"` Name string `json:"name" validate:"required,min=1,max=60"` - BkCloudID int64 `json:"bk_cloud_id" validate:"required,min=1"` RoutingMode string `json:"routing_mode" validate:"required,oneof=REGIONAL GLOBAL"` Subnet struct { diff --git a/pkg/api/cloud-server/vpc/create_huawei_vpc.go b/pkg/api/cloud-server/vpc/create_huawei_vpc.go index ecec9f08a1..950198c20c 100644 --- a/pkg/api/cloud-server/vpc/create_huawei_vpc.go +++ b/pkg/api/cloud-server/vpc/create_huawei_vpc.go @@ -34,7 +34,6 @@ type HuaWeiVpcCreateReq struct { Region string `json:"region" validate:"required"` Name string `json:"name" validate:"required,min=1,max=60"` IPv4Cidr string `json:"ipv4_cidr" validate:"required,cidrv4"` - BkCloudID int64 `json:"bk_cloud_id" validate:"required,min=1"` Subnet struct { Name string `json:"name" validate:"required,min=1,max=60"` diff --git a/pkg/api/cloud-server/vpc/create_tcloud_vpc.go b/pkg/api/cloud-server/vpc/create_tcloud_vpc.go index 382e2fce04..47e7b78dbf 100644 --- a/pkg/api/cloud-server/vpc/create_tcloud_vpc.go +++ b/pkg/api/cloud-server/vpc/create_tcloud_vpc.go @@ -34,7 +34,6 @@ type TCloudVpcCreateReq struct { Region string `json:"region" validate:"required"` Name string `json:"name" validate:"required,min=1,max=60"` IPv4Cidr string `json:"ipv4_cidr" validate:"required,cidrv4"` - BkCloudID int64 `json:"bk_cloud_id" validate:"required,min=1"` Subnet struct { Name string `json:"name" validate:"required,min=1,max=60"` diff --git a/pkg/api/core/cloud/cvm/cvm.go b/pkg/api/core/cloud/cvm/cvm.go index 76848615bf..32bb24bbbf 100644 --- a/pkg/api/core/cloud/cvm/cvm.go +++ b/pkg/api/core/cloud/cvm/cvm.go @@ -31,6 +31,7 @@ type BaseCvm struct { Name string `json:"name"` Vendor enumor.Vendor `json:"vendor"` BkBizID int64 `json:"bk_biz_id"` + BkHostID int64 `json:"bk_host_id"` BkCloudID int64 `json:"bk_cloud_id"` AccountID string `json:"account_id"` Region string `json:"region"` diff --git a/pkg/api/core/cloud/vpc.go b/pkg/api/core/cloud/vpc.go index 6df7f184b5..c607e9d24a 100644 --- a/pkg/api/core/cloud/vpc.go +++ b/pkg/api/core/cloud/vpc.go @@ -50,7 +50,6 @@ type BaseVpc struct { Region string `json:"region"` Category enumor.VpcCategory `json:"category"` Memo *string `json:"memo,omitempty"` - BkCloudID int64 `json:"bk_cloud_id"` BkBizID int64 `json:"bk_biz_id"` *core.Revision `json:",inline"` } diff --git a/pkg/api/data-service/cloud/cvm.go b/pkg/api/data-service/cloud/cvm.go index 4c33067017..d779aeb650 100644 --- a/pkg/api/data-service/cloud/cvm.go +++ b/pkg/api/data-service/cloud/cvm.go @@ -20,7 +20,6 @@ package cloud import ( - "errors" "fmt" "hcm/pkg/api/core" @@ -43,6 +42,7 @@ type CvmBatchCreate[Extension corecvm.Extension] struct { CloudID string `json:"cloud_id" validate:"required"` Name string `json:"name"` BkBizID int64 `json:"bk_biz_id" validate:"required"` + BkHostID int64 `json:"bk_host_id" validate:"required"` BkCloudID int64 `json:"bk_cloud_id" validate:"required"` AccountID string `json:"account_id" validate:"required"` Region string `json:"region" validate:"required"` @@ -88,7 +88,8 @@ type CvmBatchUpdate[Extension corecvm.Extension] struct { ID string `json:"id" validate:"required"` Name string `json:"name"` BkBizID int64 `json:"bk_biz_id" validate:"required"` - BkCloudID int64 `json:"bk_cloud_id" validate:"required"` + BkHostID int64 `json:"bk_host_id" validate:"required"` + BkCloudID *int64 `json:"bk_cloud_id"` CloudVpcIDs []string `json:"cloud_vpc_ids"` VpcIDs []string `json:"vpc_ids"` CloudSubnetIDs []string `json:"cloud_subnet_ids"` @@ -117,8 +118,7 @@ func (req *CvmBatchUpdateReq[T]) Validate() error { // CvmCommonInfoBatchUpdateReq define cvm common info batch update req. type CvmCommonInfoBatchUpdateReq struct { - IDs []string `json:"ids" validate:"required"` - BkBizID int64 `json:"bk_biz_id" validate:"required"` + Cvms []CvmCommonInfoBatchUpdateData `json:"cvms" validate:"required"` } // Validate cvm common info batch update req. @@ -127,17 +127,19 @@ func (req *CvmCommonInfoBatchUpdateReq) Validate() error { return err } - if len(req.IDs) == 0 { - return errors.New("ids required") - } - - if len(req.IDs) > constant.BatchOperationMaxLimit { + if len(req.Cvms) > constant.BatchOperationMaxLimit { return fmt.Errorf("ids count should <= %d", constant.BatchOperationMaxLimit) } return nil } +type CvmCommonInfoBatchUpdateData struct { + ID string `json:"id" validate:"required"` + BkBizID int64 `json:"bk_biz_id"` + BkCloudID *int64 `json:"bk_cloud_id"` +} + // -------------------------- List -------------------------- // CvmListReq list req. diff --git a/pkg/api/data-service/cloud/vpc.go b/pkg/api/data-service/cloud/vpc.go index 31d677d61c..6769c87472 100644 --- a/pkg/api/data-service/cloud/vpc.go +++ b/pkg/api/data-service/cloud/vpc.go @@ -38,7 +38,6 @@ type VpcCreateReq[T VpcCreateExtension] struct { AccountID string `json:"account_id" validate:"required"` CloudID string `json:"cloud_id" validate:"required"` BkBizID int64 `json:"bk_biz_id" validate:"required"` - BkCloudID int64 `json:"bk_cloud_id" validate:"required"` Name *string `json:"name,omitempty" validate:"omitempty"` Region string `json:"region" validate:"omitempty"` Category enumor.VpcCategory `json:"category" validate:"required"` @@ -120,11 +119,10 @@ type VpcUpdateReq[T VpcUpdateExtension] struct { // VpcUpdateBaseInfo defines update vpc request base info. type VpcUpdateBaseInfo struct { - Name *string `json:"name,omitempty" validate:"omitempty"` - Category enumor.VpcCategory `json:"category,omitempty" validate:"omitempty"` - Memo *string `json:"memo,omitempty" validate:"omitempty"` - BkCloudID int64 `json:"bk_cloud_id,omitempty" validate:"omitempty"` - BkBizID int64 `json:"bk_biz_id,omitempty" validate:"omitempty"` + Name *string `json:"name,omitempty" validate:"omitempty"` + Category enumor.VpcCategory `json:"category,omitempty" validate:"omitempty"` + Memo *string `json:"memo,omitempty" validate:"omitempty"` + BkBizID int64 `json:"bk_biz_id,omitempty" validate:"omitempty"` } // VpcUpdateExtension defines vpc update request extensional info. diff --git a/pkg/api/hc-service/cvm/cvm.go b/pkg/api/hc-service/cvm/cvm.go index 349583a66b..bfcee02fbf 100644 --- a/pkg/api/hc-service/cvm/cvm.go +++ b/pkg/api/hc-service/cvm/cvm.go @@ -65,10 +65,7 @@ func (r *ListCvmNetworkInterfaceReq) Validate() error { return validator.Validate.Struct(r) } -// ListCvmNetworkInterfaceResp defines list cvm mac address response. -type ListCvmNetworkInterfaceResp = map[string]*ListCvmNetworkInterfaceRespItem - // ListCvmNetworkInterfaceRespItem defines list cvm mac address response item. type ListCvmNetworkInterfaceRespItem struct { - MapAddressToPrivateIpAddresses map[string][]string `json:"map_address_to_private_ip"` + MacAddressToPrivateIpAddresses map[string][]string `json:"mac_address_to_private_ip"` } diff --git a/pkg/api/hc-service/vpc/vpc.go b/pkg/api/hc-service/vpc/vpc.go index a45ad452c1..0f6f0d8e09 100644 --- a/pkg/api/hc-service/vpc/vpc.go +++ b/pkg/api/hc-service/vpc/vpc.go @@ -39,7 +39,6 @@ type BaseVpcCreateReq struct { Name string `json:"name" validate:"required"` Category enumor.VpcCategory `json:"category" validate:"required"` Memo *string `json:"memo,omitempty" validate:"omitempty"` - BkCloudID int64 `json:"bk_cloud_id" validate:"required"` BkBizID int64 `json:"bk_biz_id" validate:"omitempty"` } diff --git a/pkg/client/hc-service/aws/cvm.go b/pkg/client/hc-service/aws/cvm.go index ed5f3f7204..f504050e82 100644 --- a/pkg/client/hc-service/aws/cvm.go +++ b/pkg/client/hc-service/aws/cvm.go @@ -185,8 +185,8 @@ func (cli *CvmClient) BatchCreateCvm(kt *kit.Kit, request *protocvm.AwsBatchCrea // ListCvmNetworkInterface .... func (cli *CvmClient) ListCvmNetworkInterface(kt *kit.Kit, request *protocvm.ListCvmNetworkInterfaceReq) ( - *protocvm.ListCvmNetworkInterfaceResp, error) { + *map[string]*protocvm.ListCvmNetworkInterfaceRespItem, error) { - return common.Request[protocvm.ListCvmNetworkInterfaceReq, protocvm.ListCvmNetworkInterfaceResp]( + return common.Request[protocvm.ListCvmNetworkInterfaceReq, map[string]*protocvm.ListCvmNetworkInterfaceRespItem]( cli.client, rest.POST, kt, request, "/cvms/network_interfaces/list") } diff --git a/pkg/client/hc-service/tcloud/cvm.go b/pkg/client/hc-service/tcloud/cvm.go index 95a1d35de7..d34f7ae536 100644 --- a/pkg/client/hc-service/tcloud/cvm.go +++ b/pkg/client/hc-service/tcloud/cvm.go @@ -237,8 +237,8 @@ func (cli *CvmClient) InquiryPrice(kt *kit.Kit, request *protocvm.TCloudBatchCre // ListCvmNetworkInterface .... func (cli *CvmClient) ListCvmNetworkInterface(kt *kit.Kit, request *protocvm.ListCvmNetworkInterfaceReq) ( - *protocvm.ListCvmNetworkInterfaceResp, error) { + *map[string]*protocvm.ListCvmNetworkInterfaceRespItem, error) { - return common.Request[protocvm.ListCvmNetworkInterfaceReq, protocvm.ListCvmNetworkInterfaceResp]( + return common.Request[protocvm.ListCvmNetworkInterfaceReq, map[string]*protocvm.ListCvmNetworkInterfaceRespItem]( cli.client, rest.POST, kt, request, "/cvms/network_interfaces/list") } diff --git a/pkg/criteria/constant/cloud_area.go b/pkg/criteria/constant/cloud_area.go index 249c5854d2..86529db198 100644 --- a/pkg/criteria/constant/cloud_area.go +++ b/pkg/criteria/constant/cloud_area.go @@ -20,8 +20,8 @@ package constant const ( - // UnbindBkCloudID defines default value for unbind vpc's cloud id. - UnbindBkCloudID int64 = -1 + // UnassignedBkCloudID defines default value for unassign cvm's cloud id. + UnassignedBkCloudID int64 = -1 // TCloudDefaultRegion defineds default value for tcloud region. TCloudDefaultRegion = "ap-guangzhou" ) diff --git a/pkg/criteria/constant/cvm.go b/pkg/criteria/constant/cvm.go new file mode 100644 index 0000000000..09616c6a01 --- /dev/null +++ b/pkg/criteria/constant/cvm.go @@ -0,0 +1,25 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2022 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package constant + +const ( + // UnBindBkHostID defines default value for unbind cvm's host id. + UnBindBkHostID int64 = -1 +) diff --git a/pkg/criteria/enumor/cvm.go b/pkg/criteria/enumor/cvm.go new file mode 100644 index 0000000000..8fbad2b740 --- /dev/null +++ b/pkg/criteria/enumor/cvm.go @@ -0,0 +1,32 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2022 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +package enumor + +// CvmMatchType cvm匹配类型 +type CvmMatchType string + +const ( + // AutoMatchCvm 自动匹配cvm + AutoMatchCvm CvmMatchType = "auto" + // ManualMatchCvm 手动匹配cvm + ManualMatchCvm CvmMatchType = "manual" + // NoMatchCvm 待关联cvm + NoMatchCvm = "no_match" +) diff --git a/pkg/dal/table/cloud/cvm/cvm.go b/pkg/dal/table/cloud/cvm/cvm.go index 98a179bb9a..305d717f04 100644 --- a/pkg/dal/table/cloud/cvm/cvm.go +++ b/pkg/dal/table/cloud/cvm/cvm.go @@ -40,6 +40,7 @@ var TableColumnDescriptor = utils.ColumnDescriptors{ {Column: "name", NamedC: "name", Type: enumor.String}, {Column: "vendor", NamedC: "vendor", Type: enumor.String}, {Column: "bk_biz_id", NamedC: "bk_biz_id", Type: enumor.Numeric}, + {Column: "bk_host_id", NamedC: "bk_host_id", Type: enumor.Numeric}, {Column: "bk_cloud_id", NamedC: "bk_cloud_id", Type: enumor.Numeric}, {Column: "account_id", NamedC: "account_id", Type: enumor.String}, {Column: "region", NamedC: "region", Type: enumor.String}, @@ -76,7 +77,8 @@ type Table struct { Name string `db:"name" validate:"lte=255" json:"name"` Vendor enumor.Vendor `db:"vendor" validate:"lte=16" json:"vendor"` BkBizID int64 `db:"bk_biz_id" json:"bk_biz_id"` - BkCloudID int64 `db:"bk_cloud_id" json:"bk_cloud_id"` + BkHostID int64 `db:"bk_host_id" json:"bk_host_id"` + BkCloudID *int64 `db:"bk_cloud_id" json:"bk_cloud_id"` AccountID string `db:"account_id" validate:"lte=64" json:"account_id"` Region string `db:"region" validate:"lte=20" json:"region"` Zone string `db:"zone" validate:"lte=64" json:"zone"` diff --git a/pkg/dal/table/cloud/vpc.go b/pkg/dal/table/cloud/vpc.go index 891b316c3e..9f1ec4d65a 100644 --- a/pkg/dal/table/cloud/vpc.go +++ b/pkg/dal/table/cloud/vpc.go @@ -43,7 +43,6 @@ var VpcColumnDescriptor = utils.ColumnDescriptors{ {Column: "category", NamedC: "category", Type: enumor.String}, {Column: "memo", NamedC: "memo", Type: enumor.String}, {Column: "extension", NamedC: "extension", Type: enumor.Json}, - {Column: "bk_cloud_id", NamedC: "bk_cloud_id", Type: enumor.Numeric}, {Column: "bk_biz_id", NamedC: "bk_biz_id", Type: enumor.Numeric}, {Column: "creator", NamedC: "creator", Type: enumor.String}, {Column: "reviser", NamedC: "reviser", Type: enumor.String}, @@ -71,8 +70,6 @@ type VpcTable struct { Memo *string `db:"memo" validate:"omitempty,max=255" json:"memo"` // Extension 云厂商差异扩展字段 Extension types.JsonField `db:"extension" validate:"-" json:"extension"` - // BkCloudID 云区域ID - BkCloudID int64 `db:"bk_cloud_id" validate:"min=-1" json:"bk_cloud_id"` // BkBizID 业务ID BkBizID int64 `db:"bk_biz_id" validate:"min=-1" json:"bk_biz_id"` // Creator 创建者 @@ -112,10 +109,6 @@ func (v VpcTable) InsertValidate() error { return err } - if v.BkCloudID == 0 { - return errors.New("blueking cloud id can not be empty") - } - if v.BkBizID == 0 { return errors.New("biz id can not be empty") } @@ -133,8 +126,7 @@ func (v VpcTable) UpdateValidate() error { return err } - if v.Name == nil && len(v.Category) == 0 && len(v.Extension) == 0 && v.BkCloudID == 0 && v.BkBizID == 0 && - v.Memo == nil { + if v.Name == nil && len(v.Category) == 0 && len(v.Extension) == 0 && v.BkBizID == 0 && v.Memo == nil { return errors.New("at least one of the update fields must be set") } diff --git a/pkg/thirdparty/esb/cmdb/cmdb.go b/pkg/thirdparty/esb/cmdb/cmdb.go index 3f2afec652..f934ca6e88 100644 --- a/pkg/thirdparty/esb/cmdb/cmdb.go +++ b/pkg/thirdparty/esb/cmdb/cmdb.go @@ -33,6 +33,7 @@ type Client interface { AddCloudHostToBiz(kt *kit.Kit, params *AddCloudHostToBizParams) (*BatchCreateResult, error) DeleteCloudHostFromBiz(kt *kit.Kit, params *DeleteCloudHostFromBizParams) error ListBizHost(kt *kit.Kit, params *ListBizHostParams) (*ListBizHostResult, error) + ListHostWithoutBiz(kt *kit.Kit, req *ListHostWithoutBizParams) (*ListHostWithoutBizResult, error) GetBizBriefCacheTopo(kt *kit.Kit, params *GetBizBriefCacheTopoParams) (*GetBizBriefCacheTopoResult, error) FindHostTopoRelation(kt *kit.Kit, params *FindHostTopoRelationParams) (*HostTopoRelationResult, error) SearchModule(kt *kit.Kit, params *SearchModuleParams) (*ModuleInfoResult, error) @@ -92,6 +93,13 @@ func (c *cmdb) ListBizHost(kt *kit.Kit, params *ListBizHostParams) (*ListBizHost "/cc/list_biz_hosts/") } +// ListHostWithoutBiz list cmdb host without biz. +func (c *cmdb) ListHostWithoutBiz(kt *kit.Kit, params *ListHostWithoutBizParams) (*ListHostWithoutBizResult, error) { + + return types.EsbCall[ListHostWithoutBizParams, ListHostWithoutBizResult](c.client, c.config, rest.POST, kt, params, + "/cc/list_hosts_without_biz/") +} + // FindHostTopoRelation 获取主机拓扑 func (c *cmdb) FindHostTopoRelation(kt *kit.Kit, params *FindHostTopoRelationParams) ( *HostTopoRelationResult, error) { diff --git a/pkg/thirdparty/esb/cmdb/types.go b/pkg/thirdparty/esb/cmdb/types.go index d3c5db12f8..69f1382e1f 100644 --- a/pkg/thirdparty/esb/cmdb/types.go +++ b/pkg/thirdparty/esb/cmdb/types.go @@ -236,6 +236,19 @@ type ListBizHostResult struct { Info []Host `json:"info"` } +// ListHostWithoutBizParams is esb list cmdb host without biz parameter. +type ListHostWithoutBizParams struct { + Fields []string `json:"fields"` + Page BasePage `json:"page"` + HostPropertyFilter *QueryFilter `json:"host_property_filter,omitempty"` +} + +// ListHostWithoutBizResult is cmdb list cmdb host without biz result. +type ListHostWithoutBizResult struct { + Count int64 `json:"count"` + Info []Host `json:"info"` +} + // Host defines cmdb host info. type Host struct { BkHostID int64 `json:"bk_host_id"` @@ -249,10 +262,13 @@ type Host struct { BkHostOuterIP string `json:"bk_host_outerip"` BkHostInnerIPv6 string `json:"bk_host_innerip_v6"` BkHostOuterIPv6 string `json:"bk_host_outerip_v6"` - Operator string `json:"operator"` - BkBakOperator string `json:"bk_bak_operator"` + Operator string `json:"operator,omitempty"` + BkBakOperator string `json:"bk_bak_operator,omitempty"` BkHostName string `json:"bk_host_name"` BkComment *string `json:"bk_comment,omitempty"` + BkOSName string `json:"bk_os_name,omitempty"` + BkMac string `json:"bk_mac,omitempty"` + CreateTime string `json:"create_time,omitempty"` } // HostFields cmdb common fields @@ -464,6 +480,16 @@ var HuaWeiCmdbStatusMap = map[string]CloudHostStatus{ "UNKNOWN": UnknownCloudHostStatus, } +// BkAddressing cc主机寻址方式. +type BkAddressing string + +const ( + // StaticAddressing 静态寻址 + StaticAddressing BkAddressing = "static" + // DynamicAddressing 动态寻址 + DynamicAddressing BkAddressing = "dynamic" +) + // EventType is cmdb watch event type. type EventType string diff --git a/scripts/sql/9999_20241226_2007_assign_host.sql b/scripts/sql/9999_20241226_2007_assign_host.sql new file mode 100644 index 0000000000..5779fab922 --- /dev/null +++ b/scripts/sql/9999_20241226_2007_assign_host.sql @@ -0,0 +1,37 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2022 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +/* + SQLVER=9999,HCMVER=v9.9.9 + + Notes: + 1. 删除vpc表的bk_cloud_id字段 + 2. cvm表添加bk_host_id字段 +*/ + +START TRANSACTION; + +alter table vpc drop column bk_cloud_id; + +alter table cvm add column bk_host_id bigint DEFAULT -1 COMMENT '主机ID'; + +CREATE OR REPLACE VIEW `hcm_version`(`hcm_ver`, `sql_ver`) AS +SELECT 'v9.9.9' as `hcm_ver`, '9999' as `sql_ver`; + +COMMIT;