Skip to content

Commit

Permalink
fix: capabilities get auto alloc network count (#7483)
Browse files Browse the repository at this point in the history
Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
  • Loading branch information
swordqiu and Qiu Jian committed Aug 4, 2020
1 parent 0a580b3 commit d4a93f0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
39 changes: 20 additions & 19 deletions pkg/compute/models/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type SCapabilities struct {

func GetCapabilities(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, region *SCloudregion, zone *SZone) (SCapabilities, error) {
capa := SCapabilities{}
var ownerId mcclient.IIdentityProvider
scopeStr := jsonutils.GetAnyString(query, []string{"scope"})
scope := rbacutils.String2Scope(scopeStr)
var domainId string
Expand All @@ -84,8 +85,11 @@ func GetCapabilities(ctx context.Context, userCred mcclient.TokenCredential, que
return capa, httperrors.NewGeneralError(err)
}
domainId = domain.GetId()
ownerId = &db.SOwnerId{DomainId: domainId}
scope = rbacutils.ScopeDomain
} else {
domainId = userCred.GetProjectDomainId()
ownerId = userCred
}
if scope == rbacutils.ScopeSystem {
result := policy.PolicyManager.Allow(scope, userCred, consts.GetServiceType(), "capabilities", policy.PolicyActionList)
Expand All @@ -109,7 +113,7 @@ func GetCapabilities(ctx context.Context, userCred mcclient.TokenCredential, que
capa.MinDataDiskCount = getMinDataDiskCount(region, zone)
capa.MaxDataDiskCount = getMaxDataDiskCount(region, zone)
capa.DBInstance = getDBInstanceInfo(region, zone)
capa.Usable = isUsable(region, zone, domainId)
capa.Usable = isUsable(ownerId, scope, region, zone)
if query == nil {
query = jsonutils.NewDict()
}
Expand All @@ -124,7 +128,7 @@ func GetCapabilities(ctx context.Context, userCred mcclient.TokenCredential, que
}
var err error
serverType := jsonutils.GetAnyString(query, []string{"host_type", "server_type"})
publicNetworkCount, _ := getNetworkPublicCount(region, zone, domainId, serverType)
publicNetworkCount, _ := getAutoAllocNetworkCount(ownerId, scope, region, zone, serverType)
capa.PublicNetworkCount = publicNetworkCount
mans := []ISpecModelManager{HostManager, IsolatedDeviceManager}
capa.Specs, err = GetModelsSpecs(ctx, userCred, query.(*jsonutils.JSONDict), mans...)
Expand Down Expand Up @@ -602,29 +606,22 @@ func getGPUs(region *SCloudregion, zone *SZone, domainId string) []string {
return gpus
}

func getNetworkCount(region *SCloudregion, zone *SZone, domainId string) (int, error) {
return getNetworkCountByFilter(region, zone, domainId, tristate.None, "")
func getNetworkCount(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, region *SCloudregion, zone *SZone) (int, error) {
return getNetworkCountByFilter(ownerId, scope, region, zone, tristate.None, "")
}

func getNetworkPublicCount(region *SCloudregion, zone *SZone, domainId, serverType string) (int, error) {
return getNetworkCountByFilter(region, zone, domainId, tristate.True, serverType)
func getAutoAllocNetworkCount(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, region *SCloudregion, zone *SZone, serverType string) (int, error) {
return getNetworkCountByFilter(ownerId, scope, region, zone, tristate.True, serverType)
}

func getNetworkCountByFilter(region *SCloudregion, zone *SZone, domainId string, isPublic tristate.TriState, serverType string) (int, error) {
func getNetworkCountByFilter(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, region *SCloudregion, zone *SZone, isAutoAlloc tristate.TriState, serverType string) (int, error) {
if zone != nil && region == nil {
region = zone.GetRegion()
}

networks := NetworkManager.Query().SubQuery()

q := networks.Query()
if !isPublic.IsNone() {
if isPublic.IsTrue() {
q = q.IsTrue("is_public")
} else {
q = q.IsFalse("is_public")
}
}

if zone != nil && !utils.IsInStringArray(region.Provider, api.REGIONAL_NETWORK_PROVIDERS) {
wires := WireManager.Query("id").Equals("zone_id", zone.Id)
Expand All @@ -644,9 +641,13 @@ func getNetworkCountByFilter(region *SCloudregion, zone *SZone, domainId string,
}
}

if len(domainId) > 0 {
ownerId := &db.SOwnerId{DomainId: domainId}
q = NetworkManager.FilterByOwner(q, ownerId, rbacutils.ScopeDomain)
q = NetworkManager.FilterByOwner(q, ownerId, scope)
if !isAutoAlloc.IsNone() {
if isAutoAlloc.IsTrue() {
q = q.IsTrue("is_auto_alloc")
} else {
q = q.IsFalse("is_auto_alloc")
}
}
if len(serverType) > 0 {
q = q.Filter(sqlchemy.Equals(networks.Field("server_type"), serverType))
Expand Down Expand Up @@ -700,8 +701,8 @@ func getMaxDataDiskCount(region *SCloudregion, zone *SZone) int {
return 0
}

func isUsable(region *SCloudregion, zone *SZone, domainId string) bool {
cnt, err := getNetworkCount(region, zone, domainId)
func isUsable(ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, region *SCloudregion, zone *SZone) bool {
cnt, err := getNetworkCount(ownerId, scope, region, zone)
if err != nil {
return false
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/compute/models/cloudregions.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/rbacutils"
"yunion.io/x/onecloud/pkg/util/stringutils2"
)

Expand Down Expand Up @@ -890,7 +891,7 @@ func (self *SCloudregion) GetDetailsCapability(ctx context.Context, userCred mcc
}

func (self *SCloudregion) GetNetworkCount() (int, error) {
return getNetworkCount(self, nil, "")
return getNetworkCount(nil, rbacutils.ScopeSystem, self, nil)
}

func (self *SCloudregion) getMinNicCount() int {
Expand Down
3 changes: 2 additions & 1 deletion pkg/compute/models/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/rbacutils"
"yunion.io/x/onecloud/pkg/util/stringutils2"
)

Expand Down Expand Up @@ -139,7 +140,7 @@ func (zone *SZone) getStorageCount() (int, error) {
}

func (zone *SZone) getNetworkCount() (int, error) {
return getNetworkCount(nil, zone, "")
return getNetworkCount(nil, rbacutils.ScopeSystem, nil, zone)
}

func (manager *SZoneManager) FetchCustomizeColumns(
Expand Down

0 comments on commit d4a93f0

Please sign in to comment.