fix: guess brand on guest create

This commit is contained in:
Qiu Jian
2020-01-07 15:05:10 +08:00
parent bcf3407110
commit a6ecd32f35
3 changed files with 43 additions and 2 deletions
+39
View File
@@ -1709,3 +1709,42 @@ func (manager *SCloudaccountManager) FilterByOwner(q *sqlchemy.SQuery, owner mcc
}
return q
}
func (manager *SCloudaccountManager) getBrandsOfProvider(provider string) ([]string, error) {
q := manager.Query().Equals("provider", provider)
cloudaccounts := make([]SCloudaccount, 0)
err := db.FetchModelObjects(manager, q, &cloudaccounts)
if err != nil {
return nil, errors.Wrap(err, "db.FetchModelObjects")
}
ret := make([]string, 0)
for i := range cloudaccounts {
if cloudaccounts[i].IsAvailable() && !utils.IsInStringArray(cloudaccounts[i].Brand, ret) {
ret = append(ret, cloudaccounts[i].Brand)
}
}
return ret, nil
}
func guessBrandForHypervisor(hypervisor string) string {
driver := GetDriver(hypervisor)
if driver == nil {
log.Errorf("guestBrandFromHypervisor: fail to find driver for hypervisor %s", hypervisor)
return ""
}
provider := driver.GetProvider()
if len(provider) == 0 {
log.Errorf("guestBrandFromHypervisor: fail to find provider for hypervisor %s", hypervisor)
return ""
}
brands, err := CloudaccountManager.getBrandsOfProvider(provider)
if err != nil {
log.Errorf("guestBrandFromHypervisor: fail to find brands for hypervisor %s", hypervisor)
return ""
}
if len(brands) != 1 {
log.Errorf("guestBrandFromHypervisor: find mistached number of brands for hypervisor %s %s", hypervisor, brands)
return ""
}
return brands[0]
}
+2 -1
View File
@@ -404,10 +404,11 @@ func (self *SDisk) ValidateUpdateData(ctx context.Context, userCred mcclient.Tok
func diskCreateInput2ComputeQuotaKeys(input api.DiskCreateInput, ownerId mcclient.IIdentityProvider) SComputeResourceKeys {
// input.Hypervisor must be set
brand := guessBrandForHypervisor(input.Hypervisor)
keys := GetDriver(input.Hypervisor).GetComputeQuotaKeys(
rbacutils.ScopeProject,
ownerId,
"",
brand,
)
if len(input.PreferHost) > 0 {
hostObj, _ := HostManager.FetchById(input.PreferHost)
+2 -1
View File
@@ -872,10 +872,11 @@ func (self *SGuest) ValidateUpdateData(ctx context.Context, userCred mcclient.To
func serverCreateInput2ComputeQuotaKeys(input api.ServerCreateInput, ownerId mcclient.IIdentityProvider) SComputeResourceKeys {
// input.Hypervisor must be set
brand := guessBrandForHypervisor(input.Hypervisor)
keys := GetDriver(input.Hypervisor).GetComputeQuotaKeys(
rbacutils.ScopeProject,
ownerId,
"",
brand,
)
if len(input.PreferHost) > 0 {
hostObj, _ := HostManager.FetchById(input.PreferHost)