mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #5097 from rainzm/feature/create_vm_by_provider
feature: Specify cloud provider when creating vm
This commit is contained in:
@@ -231,6 +231,9 @@ type BaremetalDiskConfig struct {
|
||||
}
|
||||
|
||||
type ServerConfigs struct {
|
||||
// 调度使用指定的云账号
|
||||
PreferManager string `json:"prefer_manager_id"`
|
||||
|
||||
// 调度到指定区域,优先级低于prefer_zone_id
|
||||
PreferRegion string `json:"prefer_region_id"`
|
||||
|
||||
|
||||
@@ -476,7 +476,7 @@ func (self *SGuest) PerformClone(ctx context.Context, userCred mcclient.TokenCre
|
||||
createInput.EipBw = cloneInput.EipBw
|
||||
createInput.Eip = cloneInput.Eip
|
||||
createInput.EipChargeType = cloneInput.EipChargeType
|
||||
if err := GuestManager.validateEip(userCred, createInput, createInput.PreferRegion); err != nil {
|
||||
if err := GuestManager.validateEip(userCred, createInput, createInput.PreferRegion, createInput.PreferManager); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -1255,7 +1255,7 @@ func (manager *SGuestManager) validateCreateData(
|
||||
}
|
||||
|
||||
preferRegionId, _ := data.GetString("prefer_region_id")
|
||||
if err := manager.validateEip(userCred, input, preferRegionId); err != nil {
|
||||
if err := manager.validateEip(userCred, input, preferRegionId, input.PreferManager); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1311,7 +1311,8 @@ func (manager *SGuestManager) ValidateCreateData(ctx context.Context, userCred m
|
||||
return input.JSON(input), nil
|
||||
}
|
||||
|
||||
func (manager *SGuestManager) validateEip(userCred mcclient.TokenCredential, input *api.ServerCreateInput, preferRegionId string) error {
|
||||
func (manager *SGuestManager) validateEip(userCred mcclient.TokenCredential, input *api.ServerCreateInput,
|
||||
preferRegionId string, preferManagerId string) error {
|
||||
eipStr := input.Eip
|
||||
eipBw := input.EipBw
|
||||
if len(eipStr) > 0 || eipBw > 0 {
|
||||
@@ -1337,6 +1338,12 @@ func (manager *SGuestManager) validateEip(userCred mcclient.TokenCredential, inp
|
||||
}
|
||||
input.Eip = eipObj.GetId()
|
||||
|
||||
eipCloudprovider := eip.GetCloudprovider()
|
||||
if len(preferManagerId) > 0 && preferManagerId != eipCloudprovider.Id {
|
||||
return httperrors.NewConflictError("cannot assoicate with eip %s: different cloudprovider", eipStr)
|
||||
}
|
||||
input.PreferManager = eipCloudprovider.Id
|
||||
|
||||
eipRegion := eip.GetRegion()
|
||||
// preferRegionId, _ := data.GetString("prefer_region_id")
|
||||
if len(preferRegionId) > 0 && preferRegionId != eipRegion.Id {
|
||||
|
||||
@@ -124,6 +124,7 @@ func ParseServerDeployInfoList(list []string) ([]*computeapi.DeployConfig, error
|
||||
}
|
||||
|
||||
type ServerConfigs struct {
|
||||
Manager string `help:"Preferred cloudprovider where virtual server should bd created" json:"prefer_manager"`
|
||||
Region string `help:"Preferred region where virtual server should be created" json:"prefer_region"`
|
||||
Zone string `help:"Preferred zone where virtual server should be created" json:"prefer_zone"`
|
||||
Wire string `help:"Preferred wire where virtual server should be created" json:"prefer_wire"`
|
||||
@@ -148,6 +149,7 @@ type ServerConfigs struct {
|
||||
|
||||
func (o ServerConfigs) Data() (*computeapi.ServerConfigs, error) {
|
||||
data := &computeapi.ServerConfigs{
|
||||
PreferManager: o.Manager,
|
||||
PreferRegion: o.Region,
|
||||
PreferZone: o.Zone,
|
||||
PreferWire: o.Wire,
|
||||
|
||||
@@ -30,6 +30,7 @@ type CandidateGetArgs struct {
|
||||
ResType string
|
||||
RegionID string
|
||||
ZoneID string
|
||||
ManagerID string
|
||||
HostTypes []string
|
||||
}
|
||||
|
||||
@@ -205,7 +206,7 @@ func (cm *CandidateManager) GetCandidates(args CandidateGetArgs) ([]core.Candida
|
||||
result := []core.Candidater{}
|
||||
|
||||
matchZone := func(r core.Candidater, zoneId string) bool {
|
||||
if args.ZoneID != "" {
|
||||
if zoneId != "" {
|
||||
if r.Getter().Zone().GetId() == zoneId {
|
||||
return true
|
||||
}
|
||||
@@ -215,7 +216,7 @@ func (cm *CandidateManager) GetCandidates(args CandidateGetArgs) ([]core.Candida
|
||||
}
|
||||
|
||||
matchRegion := func(r core.Candidater, regionId string) bool {
|
||||
if args.RegionID != "" {
|
||||
if regionId != "" {
|
||||
if r.Getter().Region().GetId() == regionId {
|
||||
return true
|
||||
}
|
||||
@@ -224,6 +225,16 @@ func (cm *CandidateManager) GetCandidates(args CandidateGetArgs) ([]core.Candida
|
||||
return true
|
||||
}
|
||||
|
||||
matchCloudprovider := func(r core.Candidater, managerId string) bool {
|
||||
if managerId != "" {
|
||||
if r.Getter().Cloudprovider().GetId() == managerId {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
matchHostTypes := func(c core.Candidater, hostTypes []string) bool {
|
||||
if len(hostTypes) == 0 {
|
||||
return true
|
||||
@@ -242,6 +253,10 @@ func (cm *CandidateManager) GetCandidates(args CandidateGetArgs) ([]core.Candida
|
||||
continue
|
||||
}
|
||||
|
||||
if !matchCloudprovider(r, args.ManagerID) {
|
||||
continue
|
||||
}
|
||||
|
||||
if !matchHostTypes(r, args.HostTypes) {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ func candidatesByProvider(provider CandidatesProvider, schedData *api.SchedInfo)
|
||||
ResType: provider.CandidateType(),
|
||||
ZoneID: schedData.PreferZone,
|
||||
RegionID: schedData.PreferRegion,
|
||||
ManagerID: schedData.PreferManager,
|
||||
HostTypes: schedData.GetCandidateHostTypes(),
|
||||
}
|
||||
hosts, err = candidateManager.GetCandidates(args)
|
||||
|
||||
Reference in New Issue
Block a user