Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增主机分配相关功能 --story=121245671 #1247

Open
wants to merge 1 commit into
base: feat-assign-host
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
554 changes: 487 additions & 67 deletions cmd/cloud-server/logics/cvm/assign.go

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions cmd/cloud-server/logics/cvm/cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package aws
import (
"fmt"
"strings"

"hcm/pkg/tools/converter"
)

type formItem struct {
Expand Down Expand Up @@ -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
}
Expand Down
62 changes: 2 additions & 60 deletions cmd/cloud-server/service/application/handlers/cvm/aws/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package azure
import (
"fmt"
"strings"

"hcm/pkg/tools/converter"
)

type formItem struct {
Expand Down Expand Up @@ -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
}
Expand Down
87 changes: 2 additions & 85 deletions cmd/cloud-server/service/application/handlers/cvm/azure/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

typecvm "hcm/pkg/adaptor/types/cvm"
"hcm/pkg/tools/converter"
)

type formItem struct {
Expand Down Expand Up @@ -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
}
Expand Down
83 changes: 2 additions & 81 deletions cmd/cloud-server/service/application/handlers/cvm/gcp/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"fmt"
"strconv"
"strings"

"hcm/pkg/tools/converter"
)

type formItem struct {
Expand Down Expand Up @@ -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
}
Expand Down
Loading