diff --git a/pkg/cloudcommon/db/fetch.go b/pkg/cloudcommon/db/fetch.go index 0aec7872e8..e3b0a5b1b3 100644 --- a/pkg/cloudcommon/db/fetch.go +++ b/pkg/cloudcommon/db/fetch.go @@ -309,7 +309,14 @@ type IScopedResourceManager interface { FetchOwnerId(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) } -func FetchCheckQueryOwnerScope(ctx context.Context, userCred mcclient.TokenCredential, data jsonutils.JSONObject, manager IScopedResourceManager, action string, doCheckRbac bool) (mcclient.IIdentityProvider, rbacutils.TRbacScope, error, rbacutils.SPolicyResult) { +func FetchCheckQueryOwnerScope( + ctx context.Context, + userCred mcclient.TokenCredential, + data jsonutils.JSONObject, + manager IScopedResourceManager, + action string, + doCheckRbac bool, +) (mcclient.IIdentityProvider, rbacutils.TRbacScope, error, rbacutils.SPolicyResult) { var scope rbacutils.TRbacScope var allowScope rbacutils.TRbacScope diff --git a/pkg/cloudcommon/db/metadataresource.go b/pkg/cloudcommon/db/metadataresource.go index 5c411c601b..f4b38737df 100644 --- a/pkg/cloudcommon/db/metadataresource.go +++ b/pkg/cloudcommon/db/metadataresource.go @@ -24,12 +24,37 @@ import ( "yunion.io/x/onecloud/pkg/apis" "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" "yunion.io/x/onecloud/pkg/util/tagutils" ) type SMetadataResourceBaseModelManager struct{} +func ObjectIdQueryWithPolicyResult(q *sqlchemy.SQuery, manager IModelManager, result rbacutils.SPolicyResult) *sqlchemy.SQuery { + scope := manager.ResourceScope() + if scope == rbacutils.ScopeDomain || scope == rbacutils.ScopeProject { + if !result.DomainTags.IsEmpty() { + tagFilters := tagutils.STagFilters{} + tagFilters.AddFilters(result.DomainTags) + q = ObjectIdQueryWithTagFilters(q, "domain_id", "domain", tagFilters) + } + } + if scope == rbacutils.ScopeProject { + if !result.ProjectTags.IsEmpty() { + tagFilters := tagutils.STagFilters{} + tagFilters.AddFilters(result.ProjectTags) + q = ObjectIdQueryWithTagFilters(q, "tenant_id", "project", tagFilters) + } + } + if !result.ProjectTags.IsEmpty() { + tagFilters := tagutils.STagFilters{} + tagFilters.AddFilters(result.ObjectTags) + q = ObjectIdQueryWithTagFilters(q, "id", manager.Keyword(), tagFilters) + } + return q +} + func ObjectIdQueryWithTagFilters(q *sqlchemy.SQuery, idField string, modelName string, filters tagutils.STagFilters) *sqlchemy.SQuery { log.Debugf("Filters: %s", jsonutils.Marshal(filters)) if len(filters.Filters) > 0 { diff --git a/pkg/compute/models/buckets.go b/pkg/compute/models/buckets.go index 51df47bba5..9c49e063ab 100644 --- a/pkg/compute/models/buckets.go +++ b/pkg/compute/models/buckets.go @@ -1705,9 +1705,12 @@ type SBucketUsages struct { Bytes int64 } -func (manager *SBucketManager) TotalCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) SBucketUsages { +func (manager *SBucketManager) TotalCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) SBucketUsages { usage := SBucketUsages{} - buckets := manager.Query().SubQuery() + bq := manager.Query() + bq = db.ObjectIdQueryWithPolicyResult(bq, manager, policyResult) + bq = scopeOwnerIdFilter(bq, scope, ownerId) + buckets := bq.SubQuery() bucketsQ := buckets.Query( sqlchemy.NewFunction( sqlchemy.NewCase().When( @@ -1725,7 +1728,6 @@ func (manager *SBucketManager) TotalCount(scope rbacutils.TRbacScope, ownerId mc ), ) bucketsQ = manager.usageQ(bucketsQ, rangeObjs, providers, brands, cloudEnv) - bucketsQ = scopeOwnerIdFilter(bucketsQ, scope, ownerId) buckets = bucketsQ.SubQuery() q := buckets.Query( sqlchemy.COUNT("buckets"), diff --git a/pkg/compute/models/dbinstances.go b/pkg/compute/models/dbinstances.go index d6e8821376..1b29af4b89 100644 --- a/pkg/compute/models/dbinstances.go +++ b/pkg/compute/models/dbinstances.go @@ -1787,16 +1787,20 @@ func (man *SDBInstanceManager) TotalCount( ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, + policyResult rbacutils.SPolicyResult, ) (SRdsCountStat, error) { - sq := man.Query().SubQuery() + dbq := man.Query() + dbq = scopeOwnerIdFilter(dbq, scope, ownerId) + dbq = CloudProviderFilter(dbq, dbq.Field("manager_id"), providers, brands, cloudEnv) + dbq = RangeObjectsFilter(dbq, rangeObjs, dbq.Field("cloudregion_id"), nil, dbq.Field("manager_id"), nil, nil) + dbq = db.ObjectIdQueryWithPolicyResult(dbq, man, policyResult) + + sq := dbq.SubQuery() + q := sq.Query(sqlchemy.COUNT("total_rds_count"), sqlchemy.SUM("total_cpu_count", sq.Field("vcpu_count")), sqlchemy.SUM("total_mem_size_mb", sq.Field("vmem_size_mb"))) - q = scopeOwnerIdFilter(q, scope, ownerId) - q = CloudProviderFilter(q, q.Field("manager_id"), providers, brands, cloudEnv) - q = RangeObjectsFilter(q, rangeObjs, q.Field("cloudregion_id"), nil, q.Field("manager_id"), nil, nil) - stat := SRdsCountStat{} row := q.Row() err := q.Row2Struct(row, &stat) diff --git a/pkg/compute/models/elastic_search.go b/pkg/compute/models/elastic_search.go index ddc0a126f9..0e0d969542 100644 --- a/pkg/compute/models/elastic_search.go +++ b/pkg/compute/models/elastic_search.go @@ -323,16 +323,19 @@ func (man *SElasticSearchManager) TotalCount( ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, + policyResult rbacutils.SPolicyResult, ) (SEsCountStat, error) { - sq := man.Query().SubQuery() + esq := man.Query() + esq = scopeOwnerIdFilter(esq, scope, ownerId) + esq = CloudProviderFilter(esq, esq.Field("manager_id"), providers, brands, cloudEnv) + esq = RangeObjectsFilter(esq, rangeObjs, esq.Field("cloudregion_id"), nil, esq.Field("manager_id"), nil, nil) + esq = db.ObjectIdQueryWithPolicyResult(esq, man, policyResult) + + sq := esq.SubQuery() q := sq.Query(sqlchemy.COUNT("total_es_count"), sqlchemy.SUM("total_cpu_count", sq.Field("vcpu_count")), sqlchemy.SUM("total_mem_size_gb", sq.Field("vmem_size_gb"))) - q = scopeOwnerIdFilter(q, scope, ownerId) - q = CloudProviderFilter(q, q.Field("manager_id"), providers, brands, cloudEnv) - q = RangeObjectsFilter(q, rangeObjs, q.Field("cloudregion_id"), nil, q.Field("manager_id"), nil, nil) - stat := SEsCountStat{} row := q.Row() err := q.Row2Struct(row, &stat) diff --git a/pkg/compute/models/elasticcache_instances.go b/pkg/compute/models/elasticcache_instances.go index cac5b473db..70580af3a7 100644 --- a/pkg/compute/models/elasticcache_instances.go +++ b/pkg/compute/models/elasticcache_instances.go @@ -1688,8 +1688,10 @@ func (man *SElasticcacheManager) TotalCount( ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, + policyResult rbacutils.SPolicyResult, ) (int, error) { q := man.Query() + q = db.ObjectIdQueryWithPolicyResult(q, man, policyResult) vpcs := VpcManager.Query().SubQuery() q = q.Join(vpcs, sqlchemy.Equals(q.Field("vpc_id"), vpcs.Field("id"))) q = scopeOwnerIdFilter(q, scope, ownerId) diff --git a/pkg/compute/models/elasticips.go b/pkg/compute/models/elasticips.go index 578f48154c..f6b69feef5 100644 --- a/pkg/compute/models/elasticips.go +++ b/pkg/compute/models/elasticips.go @@ -1643,43 +1643,41 @@ func (manager *SElasticipManager) usageQByRanges(q *sqlchemy.SQuery, rangeObjs [ return RangeObjectsFilter(q, rangeObjs, q.Field("cloudregion_id"), nil, q.Field("manager_id"), nil, nil) } -func (manager *SElasticipManager) usageQ(q *sqlchemy.SQuery, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) *sqlchemy.SQuery { +func (manager *SElasticipManager) usageQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, q *sqlchemy.SQuery, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) *sqlchemy.SQuery { q = manager.usageQByRanges(q, rangeObjs) q = manager.usageQByCloudEnv(q, providers, brands, cloudEnv) + switch scope { + case rbacutils.ScopeSystem: + // do nothing + case rbacutils.ScopeDomain: + q = q.Equals("domain_id", ownerId.GetProjectDomainId()) + case rbacutils.ScopeProject: + q = q.Equals("tenant_id", ownerId.GetProjectId()) + } + q = db.ObjectIdQueryWithPolicyResult(q, manager, policyResult) return q } -func (manager *SElasticipManager) TotalCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) EipUsage { +func (manager *SElasticipManager) TotalCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) EipUsage { usage := EipUsage{} q1sq := manager.Query().SubQuery() q1 := q1sq.Query( sqlchemy.COUNT("public_ip_count", q1sq.Field("id")), sqlchemy.SUM("public_ip_bandwidth", q1sq.Field("bandwidth")), ).Equals("mode", api.EIP_MODE_INSTANCE_PUBLICIP) - q1 = manager.usageQ(q1, rangeObjs, providers, brands, cloudEnv) + q1 = manager.usageQ(scope, ownerId, q1, rangeObjs, providers, brands, cloudEnv, policyResult) q2sq := manager.Query().SubQuery() q2 := q2sq.Query( sqlchemy.COUNT("eip_count", q2sq.Field("id")), sqlchemy.SUM("eip_bandwidth", q2sq.Field("bandwidth")), ).Equals("mode", api.EIP_MODE_STANDALONE_EIP) - q2 = manager.usageQ(q2, rangeObjs, providers, brands, cloudEnv) + q2 = manager.usageQ(scope, ownerId, q2, rangeObjs, providers, brands, cloudEnv, policyResult) q3sq := manager.Query().SubQuery() q3 := q3sq.Query( sqlchemy.COUNT("eip_used_count", q3sq.Field("id")), ).Equals("mode", api.EIP_MODE_STANDALONE_EIP).IsNotEmpty("associate_id") - q3 = manager.usageQ(q3, rangeObjs, providers, brands, cloudEnv) - switch scope { - case rbacutils.ScopeSystem: - // do nothing - case rbacutils.ScopeDomain: - q1 = q1.Equals("domain_id", ownerId.GetProjectDomainId()) - q2 = q2.Equals("domain_id", ownerId.GetProjectDomainId()) - q3 = q3.Equals("domain_id", ownerId.GetProjectDomainId()) - case rbacutils.ScopeProject: - q1 = q1.Equals("tenant_id", ownerId.GetProjectId()) - q2 = q2.Equals("tenant_id", ownerId.GetProjectId()) - q3 = q3.Equals("tenant_id", ownerId.GetProjectId()) - } + q3 = manager.usageQ(scope, ownerId, q3, rangeObjs, providers, brands, cloudEnv, policyResult) + err := q1.First(&usage) if err != nil { log.Errorf("q.First error: %v", err) diff --git a/pkg/compute/models/guests.go b/pkg/compute/models/guests.go index 9c70966a98..0334d0f213 100644 --- a/pkg/compute/models/guests.go +++ b/pkg/compute/models/guests.go @@ -2690,8 +2690,9 @@ func (manager *SGuestManager) TotalCount( includeSystem bool, pendingDelete bool, hostTypes []string, resourceTypes []string, providers []string, brands []string, cloudEnv string, since *time.Time, + policyResult rbacutils.SPolicyResult, ) SGuestCountStat { - return usageTotalGuestResouceCount(scope, ownerId, rangeObjs, status, hypervisors, includeSystem, pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv, since) + return usageTotalGuestResouceCount(scope, ownerId, rangeObjs, status, hypervisors, includeSystem, pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv, since, policyResult) } func (self *SGuest) detachNetworks(ctx context.Context, userCred mcclient.TokenCredential, gns []SGuestnetwork, reserve bool, deploy bool) error { @@ -3254,9 +3255,12 @@ func usageTotalGuestResouceCount( resourceTypes []string, providers []string, brands []string, cloudEnv string, since *time.Time, + policyResult rbacutils.SPolicyResult, ) SGuestCountStat { q, guests := _guestResourceCountQuery(scope, ownerId, rangeObjs, status, hypervisors, - pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv, since) + pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv, since, + policyResult, + ) if !includeSystem { q = q.Filter(sqlchemy.OR( sqlchemy.IsNull(guests.Field("is_system")), sqlchemy.IsFalse(guests.Field("is_system")))) @@ -3285,6 +3289,7 @@ func _guestResourceCountQuery( resourceTypes []string, providers []string, brands []string, cloudEnv string, since *time.Time, + policyResult rbacutils.SPolicyResult, ) (*sqlchemy.SQuery, *sqlchemy.SSubQuery) { guestdisks := GuestdiskManager.Query().SubQuery() @@ -3311,12 +3316,46 @@ func _guestResourceCountQuery( isoDevSubQuery := isoDevQuery.SubQuery() - var guests *sqlchemy.SSubQuery + var gq *sqlchemy.SQuery if since != nil && !since.IsZero() { - guests = GuestManager.RawQuery().SubQuery() + gq = GuestManager.RawQuery() } else { - guests = GuestManager.Query().SubQuery() + gq = GuestManager.Query() } + + if len(rangeObjs) > 0 || len(hostTypes) > 0 || len(resourceTypes) > 0 || len(providers) > 0 || len(brands) > 0 || len(cloudEnv) > 0 { + gq = filterGuestByRange(gq, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv) + } + + switch scope { + case rbacutils.ScopeSystem: + case rbacutils.ScopeDomain: + gq = gq.Filter(sqlchemy.Equals(gq.Field("domain_id"), ownerId.GetProjectDomainId())) + case rbacutils.ScopeProject: + gq = gq.Filter(sqlchemy.Equals(gq.Field("tenant_id"), ownerId.GetProjectId())) + } + + if len(status) > 0 { + gq = gq.Filter(sqlchemy.In(gq.Field("status"), status)) + } + if len(hypervisors) > 0 { + gq = gq.Filter(sqlchemy.In(gq.Field("hypervisor"), hypervisors)) + } + + if pendingDelete { + gq = gq.Filter(sqlchemy.IsTrue(gq.Field("pending_deleted"))) + } else { + gq = gq.Filter(sqlchemy.OR(sqlchemy.IsNull(gq.Field("pending_deleted")), sqlchemy.IsFalse(gq.Field("pending_deleted")))) + } + + if since != nil && !since.IsZero() { + gq = gq.Filter(sqlchemy.GT(gq.Field("created_at"), *since)) + } + + gq = db.ObjectIdQueryWithPolicyResult(gq, GuestManager, policyResult) + + guests := gq.SubQuery() + guestBackupSubQuery := GuestManager.Query( "id", "vcpu_count", @@ -3341,35 +3380,6 @@ func _guestResourceCountQuery( q = q.LeftJoin(isoDevSubQuery, sqlchemy.Equals(isoDevSubQuery.Field("guest_id"), guests.Field("id"))) - if len(rangeObjs) > 0 || len(hostTypes) > 0 || len(resourceTypes) > 0 || len(providers) > 0 || len(brands) > 0 || len(cloudEnv) > 0 { - q = filterGuestByRange(q, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv) - } - - switch scope { - case rbacutils.ScopeSystem: - case rbacutils.ScopeDomain: - q = q.Filter(sqlchemy.Equals(guests.Field("domain_id"), ownerId.GetProjectDomainId())) - case rbacutils.ScopeProject: - q = q.Filter(sqlchemy.Equals(guests.Field("tenant_id"), ownerId.GetProjectId())) - } - - if len(status) > 0 { - q = q.Filter(sqlchemy.In(guests.Field("status"), status)) - } - if len(hypervisors) > 0 { - q = q.Filter(sqlchemy.In(guests.Field("hypervisor"), hypervisors)) - } - - if pendingDelete { - q = q.Filter(sqlchemy.IsTrue(guests.Field("pending_deleted"))) - } else { - q = q.Filter(sqlchemy.OR(sqlchemy.IsNull(guests.Field("pending_deleted")), sqlchemy.IsFalse(guests.Field("pending_deleted")))) - } - - if since != nil && !since.IsZero() { - q = q.Filter(sqlchemy.GT(guests.Field("created_at"), *since)) - } - return q, guests } diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index c86ba86778..269bf9f0d4 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -2535,6 +2535,7 @@ func (manager *SHostManager) totalCountQ( resourceTypes []string, providers []string, brands []string, cloudEnv string, enabled, isBaremetal tristate.TriState, + policyResult rbacutils.SPolicyResult, ) *sqlchemy.SQuery { hosts := manager.Query().SubQuery() q := hosts.Query( @@ -2575,6 +2576,9 @@ func (manager *SHostManager) totalCountQ( )) } } + + q = db.ObjectIdQueryWithPolicyResult(q, HostManager, policyResult) + isolatedDevices := IsolatedDeviceManager.Query().SubQuery() iq := isolatedDevices.Query( isolatedDevices.Field("host_id"), @@ -2703,6 +2707,7 @@ func (manager *SHostManager) TotalCount( resourceTypes []string, providers []string, brands []string, cloudEnv string, enabled, isBaremetal tristate.TriState, + policyResult rbacutils.SPolicyResult, ) HostsCountStat { return manager.calculateCount( manager.totalCountQ( @@ -2718,6 +2723,7 @@ func (manager *SHostManager) TotalCount( cloudEnv, enabled, isBaremetal, + policyResult, ), ) } diff --git a/pkg/compute/models/infrasquota.go b/pkg/compute/models/infrasquota.go index 092de2420c..873a4261f3 100644 --- a/pkg/compute/models/infrasquota.go +++ b/pkg/compute/models/infrasquota.go @@ -142,7 +142,7 @@ func (self *SInfrasQuota) FetchUsage(ctx context.Context) error { brands = []string{regionKeys.Brand} } - hostStat := HostManager.TotalCount(ownerId, scope, rangeObjs, "", "", nil, nil, providers, brands, regionKeys.CloudEnv, tristate.None, tristate.None) + hostStat := HostManager.TotalCount(ownerId, scope, rangeObjs, "", "", nil, nil, providers, brands, regionKeys.CloudEnv, tristate.None, tristate.None, rbacutils.SPolicyResult{}) self.Host = int(hostStat.Count) self.Vpc = VpcManager.totalCount(ownerId, scope, rangeObjs, providers, brands, regionKeys.CloudEnv) diff --git a/pkg/compute/models/instance_snapshots.go b/pkg/compute/models/instance_snapshots.go index 94f9b6eda1..916ceb6f79 100644 --- a/pkg/compute/models/instance_snapshots.go +++ b/pkg/compute/models/instance_snapshots.go @@ -469,7 +469,7 @@ func (self *SInstanceSnapshot) GetUsages() []db.IUsage { } } -func TotalInstanceSnapshotCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) (int, error) { +func TotalInstanceSnapshotCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) (int, error) { q := InstanceSnapshotManager.Query() switch scope { @@ -480,6 +480,8 @@ func TotalInstanceSnapshotCount(scope rbacutils.TRbacScope, ownerId mcclient.IId q = q.Equals("tenant_id", ownerId.GetProjectId()) } + q = db.ObjectIdQueryWithPolicyResult(q, InstanceSnapshotManager, policyResult) + q = RangeObjectsFilter(q, rangeObjs, q.Field("cloudregion_id"), nil, q.Field("manager_id"), nil, nil) q = CloudProviderFilter(q, q.Field("manager_id"), providers, brands, cloudEnv) return q.CountWithError() diff --git a/pkg/compute/models/isolated_devices.go b/pkg/compute/models/isolated_devices.go index a2ecad0464..aa2b392045 100644 --- a/pkg/compute/models/isolated_devices.go +++ b/pkg/compute/models/isolated_devices.go @@ -532,13 +532,16 @@ func (manager *SIsolatedDeviceManager) totalCountQ( resourceTypes []string, providers []string, brands []string, cloudEnv string, rangeObjs []db.IStandaloneModel, + policyResult rbacutils.SPolicyResult, ) *sqlchemy.SQuery { - hosts := HostManager.Query().SubQuery() + hq := HostManager.Query() + if scope == rbacutils.ScopeDomain { + hq = hq.Filter(sqlchemy.Equals(hq.Field("domain_id"), ownerId.GetProjectDomainId())) + } + hq = db.ObjectIdQueryWithPolicyResult(hq, HostManager, policyResult) + hosts := hq.SubQuery() devs := manager.Query().SubQuery() q := devs.Query().Join(hosts, sqlchemy.Equals(devs.Field("host_id"), hosts.Field("id"))) - if scope == rbacutils.ScopeDomain { - q = q.Filter(sqlchemy.Equals(hosts.Field("domain_id"), ownerId.GetProjectDomainId())) - } q = q.Filter(sqlchemy.IsTrue(hosts.Field("enabled"))) if len(devType) != 0 { q = q.Filter(sqlchemy.In(devs.Field("dev_type"), devType)) @@ -561,6 +564,7 @@ func (manager *SIsolatedDeviceManager) totalCount( brands []string, cloudEnv string, rangeObjs []db.IStandaloneModel, + policyResult rbacutils.SPolicyResult, ) (int, error) { return manager.totalCountQ( scope, @@ -572,6 +576,7 @@ func (manager *SIsolatedDeviceManager) totalCount( brands, cloudEnv, rangeObjs, + policyResult, ).CountWithError() } @@ -584,19 +589,20 @@ func (manager *SIsolatedDeviceManager) TotalCount( brands []string, cloudEnv string, rangeObjs []db.IStandaloneModel, + policyResult rbacutils.SPolicyResult, ) (IsolatedDeviceCountStat, error) { stat := IsolatedDeviceCountStat{} devCnt, err := manager.totalCount( scope, ownerId, nil, hostType, resourceTypes, providers, brands, cloudEnv, - rangeObjs) + rangeObjs, policyResult) if err != nil { return stat, err } gpuCnt, err := manager.totalCount( scope, ownerId, VALID_GPU_TYPES, hostType, resourceTypes, providers, brands, cloudEnv, - rangeObjs) + rangeObjs, policyResult) if err != nil { return stat, err } diff --git a/pkg/compute/models/kafka.go b/pkg/compute/models/kafka.go index f204c5b12a..a27f08a3bb 100644 --- a/pkg/compute/models/kafka.go +++ b/pkg/compute/models/kafka.go @@ -321,15 +321,18 @@ func (man *SKafkaManager) TotalCount( ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, + policyResult rbacutils.SPolicyResult, ) (SKafkaCountStat, error) { - sq := man.Query().SubQuery() + kq := man.Query() + kq = scopeOwnerIdFilter(kq, scope, ownerId) + kq = CloudProviderFilter(kq, kq.Field("manager_id"), providers, brands, cloudEnv) + kq = RangeObjectsFilter(kq, rangeObjs, kq.Field("cloudregion_id"), nil, kq.Field("manager_id"), nil, nil) + kq = db.ObjectIdQueryWithPolicyResult(kq, man, policyResult) + + sq := kq.SubQuery() q := sq.Query(sqlchemy.COUNT("total_kafka_count"), sqlchemy.SUM("total_disk_size_gb", sq.Field("disk_size_gb"))) - q = scopeOwnerIdFilter(q, scope, ownerId) - q = CloudProviderFilter(q, q.Field("manager_id"), providers, brands, cloudEnv) - q = RangeObjectsFilter(q, rangeObjs, q.Field("cloudregion_id"), nil, q.Field("manager_id"), nil, nil) - stat := SKafkaCountStat{} row := q.Row() err := q.Row2Struct(row, &stat) diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index 517d47afff..a698c59ac2 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -1324,8 +1324,10 @@ func (man *SLoadbalancerManager) TotalCount( ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, + policyResult rbacutils.SPolicyResult, ) (int, error) { q := man.Query() + q = db.ObjectIdQueryWithPolicyResult(q, man, policyResult) q = scopeOwnerIdFilter(q, scope, ownerId) q = CloudProviderFilter(q, q.Field("manager_id"), providers, brands, cloudEnv) q = RangeObjectsFilter(q, rangeObjs, nil, q.Field("zone_id"), q.Field("manager_id"), nil, nil) diff --git a/pkg/compute/models/mongodb.go b/pkg/compute/models/mongodb.go index d7a31fa398..daad176873 100644 --- a/pkg/compute/models/mongodb.go +++ b/pkg/compute/models/mongodb.go @@ -651,16 +651,20 @@ func (man *SMongoDBManager) TotalCount( ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, + policyResult rbacutils.SPolicyResult, ) (SMongoDBCountStat, error) { - sq := man.Query().SubQuery() + mgq := man.Query() + + mgq = scopeOwnerIdFilter(mgq, scope, ownerId) + mgq = CloudProviderFilter(mgq, mgq.Field("manager_id"), providers, brands, cloudEnv) + mgq = RangeObjectsFilter(mgq, rangeObjs, mgq.Field("cloudregion_id"), nil, mgq.Field("manager_id"), nil, nil) + mgq = db.ObjectIdQueryWithPolicyResult(mgq, man, policyResult) + + sq := mgq.SubQuery() q := sq.Query(sqlchemy.COUNT("total_mongodb_count"), sqlchemy.SUM("total_cpu_count", sq.Field("vcpu_count")), sqlchemy.SUM("total_mem_size_mb", sq.Field("vmem_size_mb"))) - q = scopeOwnerIdFilter(q, scope, ownerId) - q = CloudProviderFilter(q, q.Field("manager_id"), providers, brands, cloudEnv) - q = RangeObjectsFilter(q, rangeObjs, q.Field("cloudregion_id"), nil, q.Field("manager_id"), nil, nil) - stat := SMongoDBCountStat{} row := q.Row() err := q.Row2Struct(row, &stat) diff --git a/pkg/compute/models/networks.go b/pkg/compute/models/networks.go index 586f2bcc3d..d922348b4c 100644 --- a/pkg/compute/models/networks.go +++ b/pkg/compute/models/networks.go @@ -883,6 +883,7 @@ func (manager *SNetworkManager) totalPortCountQ( brands []string, cloudEnv string, rangeObjs []db.IStandaloneModel, + policyResult rbacutils.SPolicyResult, ) *sqlchemy.SQuery { q := manager.allNetworksQ(providers, brands, cloudEnv, rangeObjs) switch scope { @@ -892,6 +893,7 @@ func (manager *SNetworkManager) totalPortCountQ( case rbacutils.ScopeProject: q = q.Equals("tenant_id", userCred.GetProjectId()) } + q = db.ObjectIdQueryWithPolicyResult(q, manager, policyResult) return manager.Query().In("id", q.Distinct().SubQuery()) } @@ -905,6 +907,7 @@ func (manager *SNetworkManager) TotalPortCount( userCred mcclient.IIdentityProvider, providers []string, brands []string, cloudEnv string, rangeObjs []db.IStandaloneModel, + policyResult rbacutils.SPolicyResult, ) map[string]NetworkPortStat { nets := make([]SNetwork, 0) err := manager.totalPortCountQ( @@ -912,6 +915,7 @@ func (manager *SNetworkManager) TotalPortCount( userCred, providers, brands, cloudEnv, rangeObjs, + policyResult, ).All(&nets) if err != nil { log.Errorf("TotalPortCount: %v", err) diff --git a/pkg/compute/models/quotas.go b/pkg/compute/models/quotas.go index ef7f1bec00..b65ff26d1a 100644 --- a/pkg/compute/models/quotas.go +++ b/pkg/compute/models/quotas.go @@ -188,7 +188,7 @@ func (self *SQuota) FetchUsage(ctx context.Context) error { diskSize := totalDiskSize(scope, ownerId, tristate.None, tristate.None, false, false, rangeObjs, providers, brands, keys.CloudEnv, hypervisors) - guest := usageTotalGuestResouceCount(scope, ownerId, rangeObjs, nil, hypervisors, false, false, nil, nil, providers, brands, keys.CloudEnv, nil) + guest := usageTotalGuestResouceCount(scope, ownerId, rangeObjs, nil, hypervisors, false, false, nil, nil, providers, brands, keys.CloudEnv, nil, rbacutils.SPolicyResult{}) self.Count = guest.TotalGuestCount self.Cpu = guest.TotalCpuCount diff --git a/pkg/compute/models/regionquota.go b/pkg/compute/models/regionquota.go index a70d0467ca..5f7de9a98e 100644 --- a/pkg/compute/models/regionquota.go +++ b/pkg/compute/models/regionquota.go @@ -184,7 +184,7 @@ func (self *SRegionQuota) FetchUsage(ctx context.Context) error { lbnic, _ := totalLBNicCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) - eipUsage := ElasticipManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) + eipUsage := ElasticipManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv, rbacutils.SPolicyResult{}) self.Eip = eipUsage.Total() self.Port = net.InternalNicCount + net.InternalVirtualNicCount + lbnic @@ -192,24 +192,24 @@ func (self *SRegionQuota) FetchUsage(ctx context.Context) error { // self.Bw = net.InternalBandwidth // self.Ebw = net.ExternalBandwidth - snapshotCount, _ := TotalSnapshotCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) + snapshotCount, _ := TotalSnapshotCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv, rbacutils.SPolicyResult{}) self.Snapshot = snapshotCount - instanceSnapshotCount, _ := TotalInstanceSnapshotCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) + instanceSnapshotCount, _ := TotalInstanceSnapshotCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv, rbacutils.SPolicyResult{}) self.InstanceSnapshot = instanceSnapshotCount - bucketUsage := BucketManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) + bucketUsage := BucketManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv, rbacutils.SPolicyResult{}) self.Bucket = bucketUsage.Buckets self.ObjectGB = int(bucketUsage.Bytes / 1000 / 1000 / 1000) self.ObjectCnt = bucketUsage.Objects - rdsUsage, _ := DBInstanceManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) + rdsUsage, _ := DBInstanceManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv, rbacutils.SPolicyResult{}) self.Rds = rdsUsage.TotalRdsCount - self.Cache, _ = ElasticcacheManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) - mongodbUsage, _ := MongoDBManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) + self.Cache, _ = ElasticcacheManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv, rbacutils.SPolicyResult{}) + mongodbUsage, _ := MongoDBManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv, rbacutils.SPolicyResult{}) self.Mongodb = mongodbUsage.TotalMongodbCount - self.Loadbalancer, _ = LoadbalancerManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv) + self.Loadbalancer, _ = LoadbalancerManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv, rbacutils.SPolicyResult{}) return nil } diff --git a/pkg/compute/models/snapshots.go b/pkg/compute/models/snapshots.go index c8c9defef1..4587493c22 100644 --- a/pkg/compute/models/snapshots.go +++ b/pkg/compute/models/snapshots.go @@ -795,7 +795,7 @@ func (self *SSnapshotManager) DeleteDiskSnapshots(ctx context.Context, userCred return nil } -func TotalSnapshotCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) (int, error) { +func TotalSnapshotCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) (int, error) { q := SnapshotManager.Query() switch scope { @@ -806,6 +806,8 @@ func TotalSnapshotCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityPr q = q.Equals("tenant_id", ownerId.GetProjectId()) } + q = db.ObjectIdQueryWithPolicyResult(q, SnapshotManager, policyResult) + q = RangeObjectsFilter(q, rangeObjs, q.Field("cloudregion_id"), nil, q.Field("manager_id"), nil, nil) q = CloudProviderFilter(q, q.Field("manager_id"), providers, brands, cloudEnv) q = q.Equals("created_by", api.SNAPSHOT_MANUAL) diff --git a/pkg/compute/models/storages.go b/pkg/compute/models/storages.go index 907680e57e..4aa7d707b0 100644 --- a/pkg/compute/models/storages.go +++ b/pkg/compute/models/storages.go @@ -1017,12 +1017,43 @@ func (manager *SStorageManager) totalCapacityQ( scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool, storageOwnership bool, + policyResult rbacutils.SPolicyResult, ) *sqlchemy.SQuery { stmt := manager.disksReadyQ(scope, ownerId, pendingDeleted, includeSystem) stmt2 := manager.disksFailedQ(scope, ownerId, pendingDeleted, includeSystem) attachedDisks := manager.diskAttachedQ(scope, ownerId, pendingDeleted, includeSystem) detachedDisks := manager.diskDetachedQ(scope, ownerId, pendingDeleted, includeSystem) - storages := manager.Query().SubQuery() + + sq := manager.Query() + + if len(hostTypes) > 0 || len(resourceTypes) > 0 || len(rangeObjs) > 0 { + hosts := HostManager.Query().SubQuery() + subq := HoststorageManager.Query("storage_id") + subq = subq.Join(hosts, sqlchemy.Equals(hosts.Field("id"), subq.Field("host_id"))) + subq = subq.Filter(sqlchemy.IsTrue(hosts.Field("enabled"))) + subq = subq.Filter(sqlchemy.Equals(hosts.Field("host_status"), api.HOST_ONLINE)) + subq = AttachUsageQuery(subq, hosts, hostTypes, resourceTypes, nil, nil, "", rangeObjs) + + sq = sq.Filter(sqlchemy.In(sq.Field("id"), subq.SubQuery())) + } + + if len(rangeObjs) > 0 || len(providers) > 0 || len(brands) > 0 || cloudEnv != "" { + sq = CloudProviderFilter(sq, sq.Field("manager_id"), providers, brands, cloudEnv) + sq = RangeObjectsFilter(sq, rangeObjs, nil, sq.Field("zone_id"), sq.Field("manager_id"), nil, sq.Field("id")) + } + + if storageOwnership { + switch scope { + case rbacutils.ScopeSystem: + case rbacutils.ScopeDomain, rbacutils.ScopeProject: + sq = sq.Equals("domain_id", ownerId.GetProjectDomainId()) + } + } + + sq = db.ObjectIdQueryWithPolicyResult(sq, manager, policyResult) + + storages := sq.SubQuery() + q := storages.Query( storages.Field("capacity"), storages.Field("reserved"), @@ -1043,30 +1074,6 @@ func (manager *SStorageManager) totalCapacityQ( q = q.LeftJoin(attachedDisks, sqlchemy.Equals(attachedDisks.Field("storage_id"), storages.Field("id"))) q = q.LeftJoin(detachedDisks, sqlchemy.Equals(detachedDisks.Field("storage_id"), storages.Field("id"))) - if len(hostTypes) > 0 || len(resourceTypes) > 0 || len(rangeObjs) > 0 { - hosts := HostManager.Query().SubQuery() - subq := HoststorageManager.Query("storage_id") - subq = subq.Join(hosts, sqlchemy.Equals(hosts.Field("id"), subq.Field("host_id"))) - subq = subq.Filter(sqlchemy.IsTrue(hosts.Field("enabled"))) - subq = subq.Filter(sqlchemy.Equals(hosts.Field("host_status"), api.HOST_ONLINE)) - subq = AttachUsageQuery(subq, hosts, hostTypes, resourceTypes, nil, nil, "", rangeObjs) - - q = q.Filter(sqlchemy.In(storages.Field("id"), subq.SubQuery())) - } - - if len(rangeObjs) > 0 || len(providers) > 0 || len(brands) > 0 || cloudEnv != "" { - q = CloudProviderFilter(q, storages.Field("manager_id"), providers, brands, cloudEnv) - q = RangeObjectsFilter(q, rangeObjs, nil, storages.Field("zone_id"), storages.Field("manager_id"), nil, storages.Field("id")) - } - - if storageOwnership { - switch scope { - case rbacutils.ScopeSystem: - case rbacutils.ScopeDomain, rbacutils.ScopeProject: - q = q.Equals("domain_id", ownerId.GetProjectDomainId()) - } - } - return q } @@ -1196,6 +1203,7 @@ func (manager *SStorageManager) TotalCapacity( ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool, storageOwnership bool, + policyResult rbacutils.SPolicyResult, ) StoragesCapacityStat { res1 := manager.calculateCapacity( manager.totalCapacityQ( @@ -1206,6 +1214,7 @@ func (manager *SStorageManager) TotalCapacity( scope, ownerId, pendingDeleted, includeSystem, storageOwnership, + policyResult, ), ) return res1 diff --git a/pkg/compute/models/wires.go b/pkg/compute/models/wires.go index 121fe8c2fa..046fe50fbf 100644 --- a/pkg/compute/models/wires.go +++ b/pkg/compute/models/wires.go @@ -521,8 +521,11 @@ func (manager *SWireManager) totalCountQ( providers []string, brands []string, cloudEnv string, scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, + policyResult rbacutils.SPolicyResult, ) *sqlchemy.SQuery { - guestsQ := filterByScopeOwnerId(GuestManager.Query(), scope, ownerId, false) + guestsQ := GuestManager.Query() + guestsQ = filterByScopeOwnerId(guestsQ, scope, ownerId, false) + guestsQ = db.ObjectIdQueryWithPolicyResult(guestsQ, GuestManager, policyResult) guests := guestsQ.SubQuery() // hosts no filter, for guest networks @@ -540,6 +543,7 @@ func (manager *SWireManager) totalCountQ( // hosts filter by owner, for host networks hostsQ2 := HostManager.Query() + hostsQ2 = db.ObjectIdQueryWithPolicyResult(hostsQ2, HostManager, policyResult) hostsQ2 = filterByScopeOwnerId(hostsQ2, scope, ownerId, true) if len(hostTypes) > 0 { hostsQ2 = hostsQ2.In("host_type", hostTypes) @@ -552,9 +556,14 @@ func (manager *SWireManager) totalCountQ( } hosts2 := hostsQ2.SubQuery() - groups := filterByScopeOwnerId(GroupManager.Query(), scope, ownerId, false).SubQuery() + groupsQ := GroupManager.Query() + groupsQ = db.ObjectIdQueryWithPolicyResult(groupsQ, GroupManager, policyResult) + groupsQ = filterByScopeOwnerId(groupsQ, scope, ownerId, false) + groups := groupsQ.SubQuery() - lbsQ := filterByScopeOwnerId(LoadbalancerManager.Query(), scope, ownerId, false) + lbsQ := LoadbalancerManager.Query() + lbsQ = db.ObjectIdQueryWithPolicyResult(lbsQ, LoadbalancerManager, policyResult) + lbsQ = filterByScopeOwnerId(lbsQ, scope, ownerId, false) if len(providers) > 0 || len(brands) > 0 || len(cloudEnv) > 0 { lbsQ = CloudProviderFilter(lbsQ, lbsQ.Field("manager_id"), providers, brands, cloudEnv) } @@ -563,7 +572,9 @@ func (manager *SWireManager) totalCountQ( } lbs := lbsQ.SubQuery() - dbsQ := filterByScopeOwnerId(DBInstanceManager.Query(), scope, ownerId, false) + dbsQ := DBInstanceManager.Query() + dbsQ = db.ObjectIdQueryWithPolicyResult(dbsQ, DBInstanceManager, policyResult) + dbsQ = filterByScopeOwnerId(dbsQ, scope, ownerId, false) if len(providers) > 0 || len(brands) > 0 || len(cloudEnv) > 0 { dbsQ = CloudProviderFilter(dbsQ, dbsQ.Field("manager_id"), providers, brands, cloudEnv) } @@ -606,6 +617,7 @@ func (manager *SWireManager) totalCountQ( lbNicQ = lbNicQ.Filter(sqlchemy.IsFalse(lbs.Field("pending_deleted"))) eipNicsQ := ElasticipManager.Query().IsNotEmpty("network_id") + eipNicsQ = db.ObjectIdQueryWithPolicyResult(eipNicsQ, ElasticipManager, policyResult) eipNics := filterByScopeOwnerId(eipNicsQ, scope, ownerId, false).SubQuery() eipNicQ := eipNics.Query( eipNics.Field("network_id"), @@ -619,6 +631,7 @@ func (manager *SWireManager) totalCountQ( } netifsQ := NetworkInterfaceManager.Query() + netifsQ = db.ObjectIdQueryWithPolicyResult(netifsQ, NetworkInterfaceManager, policyResult) netifsQ = filterByScopeOwnerId(netifsQ, scope, ownerId, true) if len(providers) > 0 || len(brands) > 0 || len(cloudEnv) > 0 { netifsQ = CloudProviderFilter(netifsQ, netifsQ.Field("manager_id"), providers, brands, cloudEnv) @@ -678,6 +691,7 @@ func (manager *SWireManager) totalCountQ2( providers []string, brands []string, cloudEnv string, scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, + policyResult rbacutils.SPolicyResult, ) *sqlchemy.SQuery { revIps := filterExpiredReservedIps(ReservedipManager.Query()).SubQuery() revQ := revIps.Query( @@ -687,7 +701,9 @@ func (manager *SWireManager) totalCountQ2( revSQ := revQ.GroupBy(revIps.Field("network_id")).SubQuery() - ownerNetworks := filterByScopeOwnerId(NetworkManager.Query(), scope, ownerId, false).SubQuery() + ownerNetQ1 := NetworkManager.Query() + ownerNetQ1 = db.ObjectIdQueryWithPolicyResult(ownerNetQ1, NetworkManager, policyResult) + ownerNetworks := filterByScopeOwnerId(ownerNetQ1, scope, ownerId, false).SubQuery() ownerNetQ := ownerNetworks.Query( ownerNetworks.Field("wire_id"), sqlchemy.COUNT("id").Label("net_count"), @@ -712,7 +728,10 @@ func (manager *SWireManager) totalCountQ3( providers []string, brands []string, cloudEnv string, scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, + policyResult rbacutils.SPolicyResult, ) *sqlchemy.SQuery { + wiresQ := WireManager.Query() + wiresQ = db.ObjectIdQueryWithPolicyResult(wiresQ, WireManager, policyResult) wires := filterByScopeOwnerId(WireManager.Query(), scope, ownerId, true).SubQuery() q := wires.Query( sqlchemy.COUNT("id").Label("wires_count"), @@ -770,6 +789,7 @@ func (manager *SWireManager) TotalCount( providers []string, brands []string, cloudEnv string, scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, + policyResult rbacutils.SPolicyResult, ) WiresCountStat { vmwareP, hostProviders := fixVmwareProvider(providers) vmwareB, hostBrands := fixVmwareProvider(brands) @@ -804,6 +824,7 @@ func (manager *SWireManager) TotalCount( hostTypes, hostProviders, hostBrands, providers, brands, cloudEnv, scope, ownerId, + policyResult, ).First(&stat) if err != nil { log.Errorf("Wire total count: %v", err) @@ -813,6 +834,7 @@ func (manager *SWireManager) TotalCount( hostTypes, providers, brands, cloudEnv, scope, ownerId, + policyResult, ).First(&stat) if err != nil { log.Errorf("Wire total count 2: %v", err) @@ -822,6 +844,7 @@ func (manager *SWireManager) TotalCount( hostTypes, providers, brands, cloudEnv, scope, ownerId, + policyResult, ).First(&stat) if err != nil { log.Errorf("Wire total count 2: %v", err) diff --git a/pkg/compute/usages/handler.go b/pkg/compute/usages/handler.go index e31a9f3f02..7c7ad3945d 100644 --- a/pkg/compute/usages/handler.go +++ b/pkg/compute/usages/handler.go @@ -64,7 +64,7 @@ func (u Usage) Include(nus ...Usage) Usage { return u } -type objUsageFunc func(rbacutils.TRbacScope, mcclient.IIdentityProvider, bool, []db.IStandaloneModel, []string, []string, []string, string, bool) (Usage, error) +type objUsageFunc func(rbacutils.TRbacScope, mcclient.IIdentityProvider, bool, []db.IStandaloneModel, []string, []string, []string, string, bool, rbacutils.SPolicyResult) (Usage, error) func getRangeObjId(ctx context.Context) (string, error) { params := appctx.AppContextParams(ctx) @@ -101,7 +101,7 @@ func rangeObjHandler( httperrors.NotFoundError(ctx, w, "%v", err) return } - ownerId, scope, err, _ := db.FetchUsageOwnerScope(ctx, userCred, getQuery(r)) + ownerId, scope, err, result := db.FetchUsageOwnerScope(ctx, userCred, getQuery(r)) if err != nil { httperrors.GeneralServerError(ctx, w, err) return @@ -110,7 +110,7 @@ func rangeObjHandler( if scope == rbacutils.ScopeDomain && obj != nil && db.IsObjectRbacAllowed(ctx, obj, userCred, policy.PolicyActionGet, "usage") == nil { isOwner = true } - log.Debugf("%s %v %s", ownerId, isOwner, scope) + log.Debugf("ownerId: %s isOwner: %v scope: %s result: %s", ownerId, isOwner, scope, result.String()) query := getQuery(r) hostTypes := json.GetQueryStringArray(query, "host_type") // resourceTypes := json.GetQueryStringArray(query, "resource_type") @@ -131,7 +131,7 @@ func rangeObjHandler( return } } - usage, err := reporter(scope, ownerId, isOwner, rangeObjs, hostTypes, providers, brands, cloudEnv, includeSystem) + usage, err := reporter(scope, ownerId, isOwner, rangeObjs, hostTypes, providers, brands, cloudEnv, includeSystem, result) if err != nil { httperrors.GeneralServerError(ctx, w, err) return @@ -182,36 +182,39 @@ func getQuery(r *http.Request) json.JSONObject { return query } -func ReportHostUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, hosts []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool) (Usage, error) { - return ReportGeneralUsage(scope, userCred, isOwner, hosts, hostTypes, providers, brands, cloudEnv, includeSystem) +func ReportHostUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, hosts []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool, policyResult rbacutils.SPolicyResult) (Usage, error) { + return ReportGeneralUsage(scope, userCred, isOwner, hosts, hostTypes, providers, brands, cloudEnv, includeSystem, policyResult) } -func ReportWireUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, wires []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool) (Usage, error) { - return ReportGeneralUsage(scope, userCred, isOwner, wires, hostTypes, providers, brands, cloudEnv, includeSystem) +func ReportWireUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, wires []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool, policyResult rbacutils.SPolicyResult) (Usage, error) { + return ReportGeneralUsage(scope, userCred, isOwner, wires, hostTypes, providers, brands, cloudEnv, includeSystem, policyResult) } -func ReportCloudAccountUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, accounts []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool) (Usage, error) { - return ReportGeneralUsage(scope, userCred, isOwner, accounts, hostTypes, providers, brands, cloudEnv, includeSystem) +func ReportCloudAccountUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, accounts []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool, policyResult rbacutils.SPolicyResult) (Usage, error) { + return ReportGeneralUsage(scope, userCred, isOwner, accounts, hostTypes, providers, brands, cloudEnv, includeSystem, policyResult) } -func ReportCloudProviderUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, managers []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool) (Usage, error) { - return ReportGeneralUsage(scope, userCred, isOwner, managers, hostTypes, providers, brands, cloudEnv, includeSystem) +func ReportCloudProviderUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, managers []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool, policyResult rbacutils.SPolicyResult) (Usage, error) { + return ReportGeneralUsage(scope, userCred, isOwner, managers, hostTypes, providers, brands, cloudEnv, includeSystem, policyResult) } -func ReportSchedtagUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, schedtags []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool) (Usage, error) { - return ReportGeneralUsage(scope, userCred, isOwner, schedtags, hostTypes, providers, brands, cloudEnv, includeSystem) +func ReportSchedtagUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, schedtags []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool, policyResult rbacutils.SPolicyResult) (Usage, error) { + return ReportGeneralUsage(scope, userCred, isOwner, schedtags, hostTypes, providers, brands, cloudEnv, includeSystem, policyResult) } -func ReportZoneUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, zones []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool) (Usage, error) { - return ReportGeneralUsage(scope, userCred, isOwner, zones, hostTypes, providers, brands, cloudEnv, includeSystem) +func ReportZoneUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, zones []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool, policyResult rbacutils.SPolicyResult) (Usage, error) { + return ReportGeneralUsage(scope, userCred, isOwner, zones, hostTypes, providers, brands, cloudEnv, includeSystem, policyResult) } -func ReportCloudRegionUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, cloudRegions []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool) (Usage, error) { - return ReportGeneralUsage(scope, userCred, isOwner, cloudRegions, hostTypes, providers, brands, cloudEnv, includeSystem) +func ReportCloudRegionUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, isOwner bool, cloudRegions []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, includeSystem bool, policyResult rbacutils.SPolicyResult) (Usage, error) { + return ReportGeneralUsage(scope, userCred, isOwner, cloudRegions, hostTypes, providers, brands, cloudEnv, includeSystem, policyResult) } -func getSystemGeneralUsage(userCred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, - providers []string, brands []string, cloudEnv string, includeSystem bool) (Usage, error) { +func getSystemGeneralUsage( + userCred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, + providers []string, brands []string, cloudEnv string, includeSystem bool, + policyResult rbacutils.SPolicyResult, +) (Usage, error) { count := RegionUsage(rangeObjs, providers, brands, cloudEnv) zone := ZoneUsage(rangeObjs, providers, brands, cloudEnv) count.Include(zone) @@ -219,7 +222,7 @@ func getSystemGeneralUsage(userCred mcclient.IIdentityProvider, rangeObjs []db.I var pmemTotal float64 var pcpuTotal float64 - hostEnabledUsage := HostEnabledUsage("", userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv) + hostEnabledUsage := HostEnabledUsage("", userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, policyResult) pmemTotal = float64(hostEnabledUsage.Get("enabled_hosts.memory").(int64)) pcpuTotal = float64(hostEnabledUsage.Get("enabled_hosts.cpu").(int64)) if len(rangeObjs) > 0 && rangeObjs[0].Keyword() == "host" { @@ -232,7 +235,7 @@ func getSystemGeneralUsage(userCred mcclient.IIdentityProvider, rangeObjs []db.I count.Add("cpu.virtual", host.GetVirtualCPUCount()) } - guestRunningUsage := GuestRunningUsage("all.running_servers", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem) + guestRunningUsage := GuestRunningUsage("all.running_servers", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, policyResult) runningMem := guestRunningUsage.Get("all.running_servers.memory").(int) runningCpu := guestRunningUsage.Get("all.running_servers.cpu").(int) @@ -253,12 +256,13 @@ func getSystemGeneralUsage(userCred mcclient.IIdentityProvider, rangeObjs []db.I count.Add("all.cpu_commit_rate.running", runningCpuCmtRate) lastWeek := time.Now().Add(-7 * 24 * time.Hour) + lastMonth := time.Now().Add(-30 * 24 * time.Hour) count.Include( - VpcUsage("all", providers, brands, cloudEnv, nil, rbacutils.ScopeSystem, rangeObjs), + VpcUsage("all", providers, brands, cloudEnv, nil, rbacutils.ScopeSystem, rangeObjs, policyResult), - DnsZoneUsage("", nil, rbacutils.ScopeSystem), + DnsZoneUsage("", nil, rbacutils.ScopeSystem, policyResult), - HostAllUsage("", userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv), + HostAllUsage("", userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, policyResult), // HostAllUsage("prepaid_pool", userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv), // HostAllUsage("any_pool", userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, nil, providers, brands, cloudEnv), @@ -266,23 +270,25 @@ func getSystemGeneralUsage(userCred mcclient.IIdentityProvider, rangeObjs []db.I // HostEnabledUsage("prepaid_pool", userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv), // HostEnabledUsage("any_pool", userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, nil, providers, brands, cloudEnv), - BaremetalUsage(userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, providers, brands, cloudEnv), + BaremetalUsage(userCred, rbacutils.ScopeSystem, rangeObjs, hostTypes, providers, brands, cloudEnv, policyResult), - StorageUsage("", rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, includeSystem, rbacutils.ScopeSystem, nil), - StorageUsage("system", rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, true, rbacutils.ScopeSystem, nil), + StorageUsage("", rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, includeSystem, rbacutils.ScopeSystem, nil, policyResult), + StorageUsage("system", rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, true, rbacutils.ScopeSystem, nil, policyResult), // StorageUsage("prepaid_pool", rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false, includeSystem, rbacutils.ScopeSystem, nil), // StorageUsage("any_pool", rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false, includeSystem, rbacutils.ScopeSystem, nil), // StorageUsage("any_pool.system", rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false, true, rbacutils.ScopeSystem, nil), // StorageUsage("any_pool.pending_delete", rangeObjs, hostTypes, nil, providers, brands, cloudEnv, true, includeSystem, rbacutils.ScopeSystem, nil), // StorageUsage("any_pool.pending_delete.system", rangeObjs, hostTypes, nil, providers, brands, cloudEnv, true, true, rbacutils.ScopeSystem, nil), - GuestNormalUsage("all.servers", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, nil), - GuestNormalUsage("all.servers.last_week", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, &lastWeek), + GuestNormalUsage("all.servers", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, nil, policyResult), + GuestNormalUsage("all.servers.last_week", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, &lastWeek, policyResult), + GuestNormalUsage("all.servers.last_month", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, &lastMonth, policyResult), // GuestNormalUsage("all.servers.prepaid_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, includeSystem), // GuestNormalUsage("all.servers.any_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, includeSystem), - GuestPendingDeleteUsage("all.pending_delete_servers", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, nil), - GuestPendingDeleteUsage("all.pending_delete_servers.last_week", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, &lastWeek), + GuestPendingDeleteUsage("all.pending_delete_servers", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, nil, policyResult), + GuestPendingDeleteUsage("all.pending_delete_servers.last_week", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, &lastWeek, policyResult), + GuestPendingDeleteUsage("all.pending_delete_servers.last_month", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, &lastMonth, policyResult), // GuestPendingDeleteUsage("all.pending_delete_servers.prepaid_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, includeSystem), // GuestNormalUsage("all.servers.prepaid_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, includeSystem), // GuestNormalUsage("all.servers.any_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, includeSystem), @@ -291,7 +297,7 @@ func getSystemGeneralUsage(userCred mcclient.IIdentityProvider, rangeObjs []db.I // GuestPendingDeleteUsage("all.pending_delete_servers.prepaid_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, includeSystem), // GuestPendingDeleteUsage("all.pending_delete_servers.any_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, includeSystem), - GuestReadyUsage("all.ready_servers", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem), + GuestReadyUsage("all.ready_servers", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, includeSystem, policyResult), // GuestReadyUsage("all.ready_servers.prepaid_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, includeSystem), // GuestReadyUsage("all.ready_servers.any_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, includeSystem), // GuestRunningUsage("all.running_servers.prepaid_pool", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, includeSystem), @@ -300,49 +306,50 @@ func getSystemGeneralUsage(userCred mcclient.IIdentityProvider, rangeObjs []db.I guestRunningUsage, // containerRunningUsage, - IsolatedDeviceUsage("", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv), + IsolatedDeviceUsage("", rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, policyResult), // IsolatedDeviceUsage("prepaid_pool", rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv), // IsolatedDeviceUsage("any_pool", rangeObjs, hostTypes, nil, providers, brands, cloudEnv), - WireUsage(rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, providers, brands, cloudEnv), - NetworkUsage("all", rbacutils.ScopeSystem, nil, providers, brands, cloudEnv, rangeObjs), + WireUsage(rbacutils.ScopeSystem, nil, rangeObjs, hostTypes, providers, brands, cloudEnv, policyResult), + NetworkUsage("all", rbacutils.ScopeSystem, nil, providers, brands, cloudEnv, rangeObjs, policyResult), - EipUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + EipUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - BucketUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + BucketUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - SnapshotUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + SnapshotUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - InstanceSnapshotUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + InstanceSnapshotUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - LoadbalancerUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + LoadbalancerUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - DBInstanceUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + DBInstanceUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - MongoDBUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + MongoDBUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - ElasticSearchUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + ElasticSearchUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - KafkaUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + KafkaUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), - ElasticCacheUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv), + ElasticCacheUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv, policyResult), ) return count, nil } -func getDomainGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string) (Usage, error) { +func getDomainGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) (Usage, error) { lastWeek := time.Now().Add(-7 * 24 * time.Hour) - count := GuestNormalUsage(getKey(scope, "servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, nil) + lastMonth := time.Now().Add(-30 * 24 * time.Hour) + count := GuestNormalUsage(getKey(scope, "servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, nil, policyResult) var pmemTotal float64 var pcpuTotal float64 - hostEnabledUsage := HostEnabledUsage("", cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv) + hostEnabledUsage := HostEnabledUsage("", cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, policyResult) pmemTotal = float64(hostEnabledUsage.Get("domain.enabled_hosts.memory").(int64)) pcpuTotal = float64(hostEnabledUsage.Get("domain.enabled_hosts.cpu").(int64)) - guestRunningUsage := GuestRunningUsage("domain.running_servers", rbacutils.ScopeDomain, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false) + guestRunningUsage := GuestRunningUsage("domain.running_servers", rbacutils.ScopeDomain, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, policyResult) runningMem := guestRunningUsage.Get("domain.running_servers.memory").(int) runningCpu := guestRunningUsage.Get("domain.running_servers.cpu").(int) @@ -358,11 +365,11 @@ func getDomainGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityPr count.Add("domain.cpu_commit_rate.running", runningCpuCmtRate) count.Include( - VpcUsage("domain", providers, brands, cloudEnv, cred, rbacutils.ScopeDomain, rangeObjs), + VpcUsage("domain", providers, brands, cloudEnv, cred, rbacutils.ScopeDomain, rangeObjs, policyResult), - DnsZoneUsage("domain", cred, rbacutils.ScopeDomain), + DnsZoneUsage("domain", cred, rbacutils.ScopeDomain, policyResult), - HostAllUsage("", cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv), + HostAllUsage("", cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, policyResult), // HostAllUsage("prepaid_pool", cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv), // HostAllUsage("any_pool", cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, nil, providers, brands, cloudEnv), @@ -370,17 +377,18 @@ func getDomainGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityPr // HostEnabledUsage("prepaid_pool", cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv), // HostEnabledUsage("any_pool", cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, nil, providers, brands, cloudEnv), - BaremetalUsage(cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, providers, brands, cloudEnv), + BaremetalUsage(cred, rbacutils.ScopeDomain, rangeObjs, hostTypes, providers, brands, cloudEnv, policyResult), - StorageUsage("", rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, false, rbacutils.ScopeDomain, cred), - StorageUsage("system", rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, true, rbacutils.ScopeDomain, cred), + StorageUsage("", rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, false, rbacutils.ScopeDomain, cred, policyResult), + StorageUsage("system", rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, true, rbacutils.ScopeDomain, cred, policyResult), // StorageUsage("prepaid_pool", rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false, false, rbacutils.ScopeDomain, cred), // StorageUsage("any_pool", rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false, false, rbacutils.ScopeDomain, cred), // StorageUsage("any_pool.system", rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false, true, rbacutils.ScopeDomain, cred), // StorageUsage("any_pool.pending_delete", rangeObjs, hostTypes, nil, providers, brands, cloudEnv, true, false, rbacutils.ScopeDomain, cred), // StorageUsage("any_pool.pending_delete.system", rangeObjs, hostTypes, nil, providers, brands, cloudEnv, true, true, rbacutils.ScopeDomain, cred), - GuestNormalUsage(getKey(scope, "servers.last_week"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastWeek), + GuestNormalUsage(getKey(scope, "servers.last_week"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastWeek, policyResult), + GuestNormalUsage(getKey(scope, "servers.last_month"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastMonth, policyResult), // GuestNormalUsage(getKey(scope, "servers.prepaid_pool"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false), // GuestNormalUsage(getKey(scope, "servers.any_pool"), scope, cred, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false), @@ -389,95 +397,99 @@ func getDomainGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityPr // GuestRunningUsage(getKey(scope, "running_servers.prepaid_pool"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false), // GuestRunningUsage(getKey(scope, "running_servers.any_pool"), scope, cred, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false), - GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, nil), - GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.last_week"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastWeek), + GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, nil, policyResult), + GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.last_week"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastWeek, policyResult), + GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.last_month"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastMonth, policyResult), // GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.prepaid_pool"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false), // GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.any_pool"), scope, cred, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false), - GuestReadyUsage(getKey(scope, "ready_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false), + GuestReadyUsage(getKey(scope, "ready_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, policyResult), // GuestReadyUsage(getKey(scope, "ready_servers.prepaid_pool"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false), // GuestReadyUsage(getKey(scope, "ready_servers.any_pool"), scope, cred, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false), - WireUsage(scope, cred, rangeObjs, hostTypes, providers, brands, cloudEnv), - NetworkUsage(getKey(scope, ""), scope, cred, providers, brands, cloudEnv, rangeObjs), + WireUsage(scope, cred, rangeObjs, hostTypes, providers, brands, cloudEnv, policyResult), + NetworkUsage(getKey(scope, ""), scope, cred, providers, brands, cloudEnv, rangeObjs, policyResult), - IsolatedDeviceUsage("", scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv), + IsolatedDeviceUsage("", scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, policyResult), - EipUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + EipUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - BucketUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + BucketUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), // nicsUsage("domain", rangeObjs, hostTypes, providers, brands, cloudEnv, scope, cred), - SnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + SnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - InstanceSnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + InstanceSnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - LoadbalancerUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + LoadbalancerUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - DBInstanceUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + DBInstanceUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - MongoDBUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + MongoDBUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - ElasticSearchUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + ElasticSearchUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - KafkaUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + KafkaUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - ElasticCacheUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + ElasticCacheUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), ) return count, nil } -func getProjectGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string) (Usage, error) { +func getProjectGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) (Usage, error) { lastWeek := time.Now().Add(-7 * 24 * time.Hour) - count := GuestNormalUsage(getKey(scope, "servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, nil) + lastMonth := time.Now().Add(-30 * 24 * time.Hour) + count := GuestNormalUsage(getKey(scope, "servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, nil, policyResult) count.Include( - GuestNormalUsage(getKey(scope, "servers.last_week"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastWeek), + GuestNormalUsage(getKey(scope, "servers.last_week"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastWeek, policyResult), + GuestNormalUsage(getKey(scope, "servers.last_month"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastMonth, policyResult), // GuestNormalUsage(getKey(scope, "servers.prepaid_pool"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false), // GuestNormalUsage(getKey(scope, "servers.any_pool"), scope, cred, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false), - GuestRunningUsage(getKey(scope, "running_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false), + GuestRunningUsage(getKey(scope, "running_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, policyResult), // GuestRunningUsage(getKey(scope, "running_servers.prepaid_pool"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false), // GuestRunningUsage(getKey(scope, "running_servers.any_pool"), scope, cred, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false), - GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, nil), - GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.last_week"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastWeek), + GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, nil, policyResult), + GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.last_week"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastWeek, policyResult), + GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.last_month"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, &lastMonth, policyResult), // GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.prepaid_pool"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false), // GuestPendingDeleteUsage(getKey(scope, "pending_delete_servers.any_pool"), scope, cred, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false), - GuestReadyUsage(getKey(scope, "ready_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false), + GuestReadyUsage(getKey(scope, "ready_servers"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypeShared}, providers, brands, cloudEnv, false, policyResult), // GuestReadyUsage(getKey(scope, "ready_servers.prepaid_pool"), scope, cred, rangeObjs, hostTypes, []string{api.HostResourceTypePrepaidRecycle}, providers, brands, cloudEnv, false), // GuestReadyUsage(getKey(scope, "ready_servers.any_pool"), scope, cred, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, false), - WireUsage(scope, cred, rangeObjs, hostTypes, providers, brands, cloudEnv), - NetworkUsage(getKey(scope, ""), scope, cred, providers, brands, cloudEnv, rangeObjs), + WireUsage(scope, cred, rangeObjs, hostTypes, providers, brands, cloudEnv, policyResult), + NetworkUsage(getKey(scope, ""), scope, cred, providers, brands, cloudEnv, rangeObjs, policyResult), - EipUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + EipUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - BucketUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + BucketUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - DisksUsage(getKey(scope, "disks"), rangeObjs, hostTypes, nil, providers, brands, cloudEnv, scope, cred, false, false), - DisksUsage(getKey(scope, "disks.system"), rangeObjs, hostTypes, nil, providers, brands, cloudEnv, scope, cred, false, true), - DisksUsage(getKey(scope, "pending_delete_disks"), rangeObjs, hostTypes, nil, providers, brands, cloudEnv, scope, cred, true, false), - DisksUsage(getKey(scope, "pending_delete_disks.system"), rangeObjs, hostTypes, nil, providers, brands, cloudEnv, scope, cred, true, true), + DisksUsage(getKey(scope, "disks"), rangeObjs, hostTypes, nil, providers, brands, cloudEnv, scope, cred, false, false, policyResult), + DisksUsage(getKey(scope, "disks.system"), rangeObjs, hostTypes, nil, providers, brands, cloudEnv, scope, cred, false, true, policyResult), + DisksUsage(getKey(scope, "pending_delete_disks"), rangeObjs, hostTypes, nil, providers, brands, cloudEnv, scope, cred, true, false, policyResult), + DisksUsage(getKey(scope, "pending_delete_disks.system"), rangeObjs, hostTypes, nil, providers, brands, cloudEnv, scope, cred, true, true, policyResult), // nicsUsage("", rangeObjs, hostTypes, providers, brands, cloudEnv, scope, cred), - SnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + SnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - InstanceSnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + InstanceSnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - LoadbalancerUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + LoadbalancerUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - DBInstanceUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + DBInstanceUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - MongoDBUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + MongoDBUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - ElasticSearchUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + ElasticSearchUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - KafkaUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + KafkaUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), - ElasticCacheUsage(scope, cred, rangeObjs, providers, brands, cloudEnv), + ElasticCacheUsage(scope, cred, rangeObjs, providers, brands, cloudEnv, policyResult), ) return count, nil @@ -493,12 +505,13 @@ func ReportGeneralUsage( brands []string, cloudEnv string, includeSystem bool, + policyResult rbacutils.SPolicyResult, ) (count Usage, err error) { count = make(map[string]interface{}) // if scope == rbacutils.ScopeSystem || isOwner { if scope == rbacutils.ScopeSystem { - count, err = getSystemGeneralUsage(userCred, rangeObjs, hostTypes, providers, brands, cloudEnv, includeSystem) + count, err = getSystemGeneralUsage(userCred, rangeObjs, hostTypes, providers, brands, cloudEnv, includeSystem, policyResult) if err != nil { return } @@ -506,7 +519,7 @@ func ReportGeneralUsage( // if scope.HigherEqual(rbacutils.ScopeDomain) && len(userCred.GetProjectDomainId()) > 0 { if scope == rbacutils.ScopeDomain && len(userCred.GetProjectDomainId()) > 0 { - commonUsage, err := getDomainGeneralUsage(rbacutils.ScopeDomain, userCred, rangeObjs, hostTypes, providers, brands, cloudEnv) + commonUsage, err := getDomainGeneralUsage(rbacutils.ScopeDomain, userCred, rangeObjs, hostTypes, providers, brands, cloudEnv, policyResult) if err == nil { count.Include(commonUsage) } @@ -514,7 +527,7 @@ func ReportGeneralUsage( // if scope.HigherEqual(rbacutils.ScopeProject) && len(userCred.GetProjectId()) > 0 { if scope == rbacutils.ScopeProject && len(userCred.GetProjectId()) > 0 { - commonUsage, err := getProjectGeneralUsage(rbacutils.ScopeProject, userCred, rangeObjs, hostTypes, providers, brands, cloudEnv) + commonUsage, err := getProjectGeneralUsage(rbacutils.ScopeProject, userCred, rangeObjs, hostTypes, providers, brands, cloudEnv, policyResult) if err == nil { count.Include(commonUsage) } @@ -552,7 +565,7 @@ func ZoneUsage(rangeObjs []db.IStandaloneModel, providers []string, brands []str return count } -func VpcUsage(prefix string, providers []string, brands []string, cloudEnv string, ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, rangeObjs []db.IStandaloneModel) Usage { +func VpcUsage(prefix string, providers []string, brands []string, cloudEnv string, ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, rangeObjs []db.IStandaloneModel, policyResult rbacutils.SPolicyResult) Usage { q := models.VpcManager.Query().IsFalse("is_emulated") if len(rangeObjs) > 0 || len(providers) > 0 || len(brands) > 0 || len(cloudEnv) > 0 { q = models.CloudProviderFilter(q, q.Field("manager_id"), providers, brands, cloudEnv) @@ -562,6 +575,8 @@ func VpcUsage(prefix string, providers []string, brands []string, cloudEnv strin q = q.Equals("domain_id", ownerId.GetProjectDomainId()) } + q = db.ObjectIdQueryWithPolicyResult(q, models.VpcManager, policyResult) + count := make(map[string]interface{}) key := "vpcs" if len(prefix) > 0 { @@ -571,12 +586,14 @@ func VpcUsage(prefix string, providers []string, brands []string, cloudEnv strin return count } -func DnsZoneUsage(prefix string, ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope) Usage { +func DnsZoneUsage(prefix string, ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope, policyResult rbacutils.SPolicyResult) Usage { q := models.DnsZoneManager.Query() if scope == rbacutils.ScopeDomain { q = q.Equals("domain_id", ownerId.GetProjectDomainId()) } + q = db.ObjectIdQueryWithPolicyResult(q, models.DnsZoneManager, policyResult) + count := make(map[string]interface{}) key := "dns_zones" if len(prefix) > 0 { @@ -593,6 +610,7 @@ func StorageUsage( providers []string, brands []string, cloudEnv string, pendingDeleted bool, includeSystem bool, scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, + policyResult rbacutils.SPolicyResult, ) Usage { sPrefix := getSysKey(scope, "storages") dPrefix := getKey(scope, "disks") @@ -608,6 +626,7 @@ func StorageUsage( scope, ownerId, pendingDeleted, includeSystem, true, + policyResult, ) count[sPrefix] = result.Capacity for s, capa := range result.StorageTypeCapacity { @@ -655,6 +674,7 @@ func StorageUsage( scope, ownerId, pendingDeleted, includeSystem, false, + policyResult, ) count[fmt.Sprintf("%s", dPrefix)] = result.CapacityUsed @@ -701,9 +721,10 @@ func DisksUsage( cloudEnv string, scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool, + policyResult rbacutils.SPolicyResult, ) Usage { count := make(map[string]interface{}) - result := models.StorageManager.TotalCapacity(rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, scope, ownerId, pendingDeleted, includeSystem, false) + result := models.StorageManager.TotalCapacity(rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, scope, ownerId, pendingDeleted, includeSystem, false, policyResult) count[dPrefix] = result.CapacityUsed count[fmt.Sprintf("%s.storage", dPrefix)] = result.Capacity count[fmt.Sprintf("%s.storage.virtual", dPrefix)] = result.CapacityVirtual @@ -721,9 +742,9 @@ func DisksUsage( return count } -func WireUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string) Usage { +func WireUsage(scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { count := make(map[string]interface{}) - result := models.WireManager.TotalCount(rangeObjs, hostTypes, providers, brands, cloudEnv, scope, userCred) + result := models.WireManager.TotalCount(rangeObjs, hostTypes, providers, brands, cloudEnv, scope, userCred, policyResult) count[getKey(scope, "wires")] = result.WiresCount - result.EmulatedWiresCount count[getKey(scope, "networks")] = result.NetCount // include nics for pending_deleted guests @@ -765,9 +786,9 @@ func prefixKey(prefix string, key string) string { } } -func NetworkUsage(prefix string, scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, providers []string, brands []string, cloudEnv string, rangeObjs []db.IStandaloneModel) Usage { +func NetworkUsage(prefix string, scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, providers []string, brands []string, cloudEnv string, rangeObjs []db.IStandaloneModel, policyResult rbacutils.SPolicyResult) Usage { count := make(map[string]interface{}) - ret := models.NetworkManager.TotalPortCount(scope, userCred, providers, brands, cloudEnv, rangeObjs) + ret := models.NetworkManager.TotalPortCount(scope, userCred, providers, brands, cloudEnv, rangeObjs, policyResult) for k, v := range ret { if len(k) > 0 { count[prefixKey(prefix, fmt.Sprintf("ports.%s", k))] = v.Count @@ -781,27 +802,27 @@ func NetworkUsage(prefix string, scope rbacutils.TRbacScope, userCred mcclient.I } func HostAllUsage(pref string, userCred mcclient.IIdentityProvider, scope rbacutils.TRbacScope, rangeObjs []db.IStandaloneModel, - hostTypes []string, resourceTypes []string, providers []string, brands []string, cloudEnv string) Usage { + hostTypes []string, resourceTypes []string, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { prefix := getSysKey(scope, "hosts") if len(pref) > 0 { prefix = fmt.Sprintf("%s.%s", prefix, pref) } - return hostUsage(userCred, scope, prefix, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, tristate.None, tristate.False) + return hostUsage(userCred, scope, prefix, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, tristate.None, tristate.False, policyResult) } func HostEnabledUsage(pref string, userCred mcclient.IIdentityProvider, scope rbacutils.TRbacScope, rangeObjs []db.IStandaloneModel, - hostTypes []string, resourceTypes []string, providers []string, brands []string, cloudEnv string) Usage { + hostTypes []string, resourceTypes []string, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { prefix := getSysKey(scope, "enabled_hosts") if len(pref) > 0 { prefix = fmt.Sprintf("%s.%s", prefix, pref) } - return hostUsage(userCred, scope, prefix, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, tristate.True, tristate.False) + return hostUsage(userCred, scope, prefix, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, tristate.True, tristate.False, policyResult) } func BaremetalUsage(userCred mcclient.IIdentityProvider, scope rbacutils.TRbacScope, rangeObjs []db.IStandaloneModel, - hostTypes []string, providers []string, brands []string, cloudEnv string) Usage { + hostTypes []string, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { prefix := getSysKey(scope, "baremetals") - count := hostUsage(userCred, scope, prefix, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, tristate.None, tristate.True) + count := hostUsage(userCred, scope, prefix, rangeObjs, hostTypes, nil, providers, brands, cloudEnv, tristate.None, tristate.True, policyResult) delete(count, fmt.Sprintf("%s.memory.virtual", prefix)) delete(count, fmt.Sprintf("%s.cpu.virtual", prefix)) return count @@ -812,10 +833,11 @@ func hostUsage( rangeObjs []db.IStandaloneModel, hostTypes []string, resourceTypes []string, providers []string, brands []string, cloudEnv string, enabled, isBaremetal tristate.TriState, + policyResult rbacutils.SPolicyResult, ) Usage { count := make(map[string]interface{}) - result := models.HostManager.TotalCount(userCred, scope, rangeObjs, "", "", hostTypes, resourceTypes, providers, brands, cloudEnv, enabled, isBaremetal) + result := models.HostManager.TotalCount(userCred, scope, rangeObjs, "", "", hostTypes, resourceTypes, providers, brands, cloudEnv, enabled, isBaremetal, policyResult) count[prefix] = result.Count count[fmt.Sprintf("%s.any_pool", prefix)] = result.Count count[fmt.Sprintf("%s.memory", prefix)] = result.Memory @@ -834,26 +856,28 @@ func hostUsage( func GuestNormalUsage(prefix string, scope rbacutils.TRbacScope, cred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, resourceTypes []string, providers []string, - brands []string, cloudEnv string, includeSystem bool, since *time.Time) Usage { - return guestUsage(prefix, scope, cred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, nil, false, includeSystem, since) + brands []string, cloudEnv string, includeSystem bool, since *time.Time, policyResult rbacutils.SPolicyResult) Usage { + return guestUsage(prefix, scope, cred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, nil, false, includeSystem, since, policyResult) } func GuestPendingDeleteUsage(prefix string, scope rbacutils.TRbacScope, cred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, resourceTypes []string, providers []string, - brands []string, cloudEnv string, includeSystem bool, since *time.Time) Usage { - return guestUsage(prefix, scope, cred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, nil, true, includeSystem, since) + brands []string, cloudEnv string, includeSystem bool, since *time.Time, policyResult rbacutils.SPolicyResult) Usage { + return guestUsage(prefix, scope, cred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, nil, true, includeSystem, since, policyResult) } func GuestRunningUsage(prefix string, scope rbacutils.TRbacScope, cred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, resourceTypes []string, providers []string, - brands []string, cloudEnv string, includeSystem bool) Usage { - return guestUsage(prefix, scope, cred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, []string{api.VM_RUNNING}, false, includeSystem, nil) + brands []string, cloudEnv string, includeSystem bool, + policyResult rbacutils.SPolicyResult, +) Usage { + return guestUsage(prefix, scope, cred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, []string{api.VM_RUNNING}, false, includeSystem, nil, policyResult) } func GuestReadyUsage(prefix string, scope rbacutils.TRbacScope, cred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, resourceTypes []string, providers []string, - brands []string, cloudEnv string, includeSystem bool) Usage { - return guestUsage(prefix, scope, cred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, []string{api.VM_READY}, false, includeSystem, nil) + brands []string, cloudEnv string, includeSystem bool, policyResult rbacutils.SPolicyResult) Usage { + return guestUsage(prefix, scope, cred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, []string{api.VM_READY}, false, includeSystem, nil, policyResult) } func guestHypervisorsUsage( @@ -865,11 +889,14 @@ func guestHypervisorsUsage( status, hypervisors []string, pendingDelete, includeSystem bool, since *time.Time, + policyResult rbacutils.SPolicyResult, ) Usage { // temporarily hide system resources // XXX needs more work later guest := models.GuestManager.TotalCount(scope, ownerId, rangeObjs, status, hypervisors, - includeSystem, pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv, since) + includeSystem, pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv, since, + policyResult, + ) count := make(map[string]interface{}) count[prefix] = guest.TotalGuestCount count[fmt.Sprintf("%s.any_pool", prefix)] = guest.TotalGuestCount @@ -893,10 +920,12 @@ func guestHypervisorsUsage( func guestUsage(prefix string, scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostTypes []string, resourceTypes []string, providers []string, brands []string, cloudEnv string, - status []string, pendingDelete, includeSystem bool, since *time.Time) Usage { + status []string, pendingDelete, includeSystem bool, since *time.Time, + policyResult rbacutils.SPolicyResult, +) Usage { hypervisors := sets.NewString(api.HYPERVISORS...) hypervisors.Delete(api.HYPERVISOR_CONTAINER) - return guestHypervisorsUsage(prefix, scope, userCred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, status, hypervisors.List(), pendingDelete, includeSystem, since) + return guestHypervisorsUsage(prefix, scope, userCred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, status, hypervisors.List(), pendingDelete, includeSystem, since, policyResult) } /*func containerUsage(prefix string, scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, @@ -905,12 +934,12 @@ func guestUsage(prefix string, scope rbacutils.TRbacScope, userCred mcclient.IId return guestHypervisorsUsage(prefix, scope, userCred, rangeObjs, hostTypes, resourceTypes, providers, brands, cloudEnv, nil, hypervisors, false) }*/ -func IsolatedDeviceUsage(pref string, scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostType []string, resourceTypes []string, providers []string, brands []string, cloudEnv string) Usage { +func IsolatedDeviceUsage(pref string, scope rbacutils.TRbacScope, userCred mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, hostType []string, resourceTypes []string, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { prefix := "isolated_devices" if len(pref) > 0 { prefix = fmt.Sprintf("%s.%s", prefix, pref) } - ret, _ := models.IsolatedDeviceManager.TotalCount(scope, userCred, hostType, resourceTypes, providers, brands, cloudEnv, rangeObjs) + ret, _ := models.IsolatedDeviceManager.TotalCount(scope, userCred, hostType, resourceTypes, providers, brands, cloudEnv, rangeObjs, policyResult) count := make(map[string]interface{}) count[prefix] = ret.Devices return count @@ -955,8 +984,8 @@ func _getKey(scope rbacutils.TRbacScope, key string, includeSystem bool) string } } -func EipUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - eipUsage := models.ElasticipManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func EipUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + eipUsage := models.ElasticipManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "eip")] = eipUsage.Total() count[getKey(scope, "eip.public_ip")] = eipUsage.PublicIPCount @@ -969,8 +998,8 @@ func EipUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, ra return count } -func BucketUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - bucketUsage := models.BucketManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func BucketUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + bucketUsage := models.BucketManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "buckets")] = bucketUsage.Buckets count[getKey(scope, "bucket_objects")] = bucketUsage.Objects @@ -978,29 +1007,29 @@ func BucketUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, return count } -func SnapshotUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - cnt, _ := models.TotalSnapshotCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func SnapshotUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + cnt, _ := models.TotalSnapshotCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "snapshot")] = cnt return count } -func InstanceSnapshotUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - cnt, _ := models.TotalInstanceSnapshotCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func InstanceSnapshotUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + cnt, _ := models.TotalInstanceSnapshotCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "instance_snapshot")] = cnt return count } -func LoadbalancerUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - cnt, _ := models.LoadbalancerManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func LoadbalancerUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + cnt, _ := models.LoadbalancerManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "loadbalancer")] = cnt return count } -func DBInstanceUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - cnt, _ := models.DBInstanceManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func DBInstanceUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + cnt, _ := models.DBInstanceManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "rds")] = cnt.TotalRdsCount count[getKey(scope, "rds.cpu")] = cnt.TotalCpuCount @@ -1008,8 +1037,8 @@ func DBInstanceUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvi return count } -func MongoDBUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - cnt, _ := models.MongoDBManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func MongoDBUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + cnt, _ := models.MongoDBManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "mongodb")] = cnt.TotalMongodbCount count[getKey(scope, "mongodb.cpu")] = cnt.TotalCpuCount @@ -1017,8 +1046,8 @@ func MongoDBUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider return count } -func ElasticSearchUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - cnt, _ := models.ElasticSearchManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func ElasticSearchUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + cnt, _ := models.ElasticSearchManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "es")] = cnt.TotalEsCount count[getKey(scope, "es.cpu")] = cnt.TotalCpuCount @@ -1026,16 +1055,16 @@ func ElasticSearchUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityPr return count } -func KafkaUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - cnt, _ := models.KafkaManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func KafkaUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + cnt, _ := models.KafkaManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "kafka")] = cnt.TotalKafkaCount count[getKey(scope, "kafka.disk")] = cnt.TotalDiskSizeGb return count } -func ElasticCacheUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) Usage { - cnt, _ := models.ElasticcacheManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv) +func ElasticCacheUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string, policyResult rbacutils.SPolicyResult) Usage { + cnt, _ := models.ElasticcacheManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, cloudEnv, policyResult) count := make(map[string]interface{}) count[getKey(scope, "cache")] = cnt return count diff --git a/pkg/image/models/image_guest.go b/pkg/image/models/image_guest.go index 66eeef3eb4..f6c6651da0 100644 --- a/pkg/image/models/image_guest.go +++ b/pkg/image/models/image_guest.go @@ -610,9 +610,9 @@ func (manager *SGuestImageManager) QueryDistinctExtraField(q *sqlchemy.SQuery, f return q, httperrors.ErrNotFound } -func (manager *SGuestImageManager) Usage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, prefix string) map[string]int64 { +func (manager *SGuestImageManager) Usage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, prefix string, policyResult rbacutils.SPolicyResult) map[string]int64 { usages := make(map[string]int64) - count := ImageManager.count(scope, ownerId, api.IMAGE_STATUS_ACTIVE, tristate.False, false, tristate.True) + count := ImageManager.count(scope, ownerId, api.IMAGE_STATUS_ACTIVE, tristate.False, false, tristate.True, policyResult) expandUsageCount(usages, prefix, "guest_image", "", count) sq := manager.Query() switch scope { diff --git a/pkg/image/models/images.go b/pkg/image/models/images.go index 8a0b862789..87f5433c54 100644 --- a/pkg/image/models/images.go +++ b/pkg/image/models/images.go @@ -892,8 +892,9 @@ type SImageUsage struct { Size int64 } -func (manager *SImageManager) count(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, status string, isISO tristate.TriState, pendingDelete bool, guestImage tristate.TriState) map[string]SImageUsage { +func (manager *SImageManager) count(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, status string, isISO tristate.TriState, pendingDelete bool, guestImage tristate.TriState, policyResult rbacutils.SPolicyResult) map[string]SImageUsage { sq := manager.Query("id") + sq = db.ObjectIdQueryWithPolicyResult(sq, manager, policyResult) switch scope { case rbacutils.ScopeSystem: // do nothing @@ -969,19 +970,19 @@ func expandUsageCount(usages map[string]int64, prefix, imgType, state string, co } } -func (manager *SImageManager) Usage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, prefix string) map[string]int64 { +func (manager *SImageManager) Usage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, prefix string, policyResult rbacutils.SPolicyResult) map[string]int64 { usages := make(map[string]int64) - count := manager.count(scope, ownerId, api.IMAGE_STATUS_ACTIVE, tristate.False, false, tristate.False) + count := manager.count(scope, ownerId, api.IMAGE_STATUS_ACTIVE, tristate.False, false, tristate.False, policyResult) expandUsageCount(usages, prefix, "img", "", count) - count = manager.count(scope, ownerId, api.IMAGE_STATUS_ACTIVE, tristate.True, false, tristate.False) + count = manager.count(scope, ownerId, api.IMAGE_STATUS_ACTIVE, tristate.True, false, tristate.False, policyResult) expandUsageCount(usages, prefix, string(qemuimg.ISO), "", count) - count = manager.count(scope, ownerId, api.IMAGE_STATUS_ACTIVE, tristate.None, false, tristate.False) + count = manager.count(scope, ownerId, api.IMAGE_STATUS_ACTIVE, tristate.None, false, tristate.False, policyResult) expandUsageCount(usages, prefix, "imgiso", "", count) - count = manager.count(scope, ownerId, "", tristate.False, true, tristate.False) + count = manager.count(scope, ownerId, "", tristate.False, true, tristate.False, policyResult) expandUsageCount(usages, prefix, "img", "pending_delete", count) - count = manager.count(scope, ownerId, "", tristate.True, true, tristate.False) + count = manager.count(scope, ownerId, "", tristate.True, true, tristate.False, policyResult) expandUsageCount(usages, prefix, string(qemuimg.ISO), "pending_delete", count) - count = manager.count(scope, ownerId, "", tristate.None, true, tristate.False) + count = manager.count(scope, ownerId, "", tristate.None, true, tristate.False, policyResult) expandUsageCount(usages, prefix, "imgiso", "pending_delete", count) return usages } diff --git a/pkg/image/models/quotas.go b/pkg/image/models/quotas.go index 80d8a77bbb..a705b27fd5 100644 --- a/pkg/image/models/quotas.go +++ b/pkg/image/models/quotas.go @@ -135,7 +135,7 @@ func (self *SQuota) FetchUsage(ctx context.Context) error { isISO = tristate.None } - count := ImageManager.count(scope, ownerId, "", isISO, false, tristate.None) + count := ImageManager.count(scope, ownerId, "", isISO, false, tristate.None, rbacutils.SPolicyResult{}) self.Image = int(count["total"].Count) return nil } diff --git a/pkg/image/usages/handler.go b/pkg/image/usages/handler.go index f30f067f53..d7b9330b56 100644 --- a/pkg/image/usages/handler.go +++ b/pkg/image/usages/handler.go @@ -39,7 +39,7 @@ func ReportGeneralUsage(ctx context.Context, w http.ResponseWriter, r *http.Requ _, query, _ := appsrv.FetchEnv(ctx, w, r) userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential) - ownerId, scope, err, _ := db.FetchUsageOwnerScope(ctx, userCred, query) + ownerId, scope, err, result := db.FetchUsageOwnerScope(ctx, userCred, query) if err != nil { httperrors.GeneralServerError(ctx, w, err) return @@ -47,23 +47,23 @@ func ReportGeneralUsage(ctx context.Context, w http.ResponseWriter, r *http.Requ usages := jsonutils.NewDict() if scope == rbacutils.ScopeSystem { - adminUsage := models.ImageManager.Usage(rbacutils.ScopeSystem, ownerId, "all") + adminUsage := models.ImageManager.Usage(rbacutils.ScopeSystem, ownerId, "all", result) usages.Update(jsonutils.Marshal(adminUsage)) - adminUsage = models.GuestImageManager.Usage(rbacutils.ScopeSystem, ownerId, "all") + adminUsage = models.GuestImageManager.Usage(rbacutils.ScopeSystem, ownerId, "all", result) usages.Update(jsonutils.Marshal(adminUsage)) } if scope.HigherEqual(rbacutils.ScopeDomain) { - domainUsage := models.ImageManager.Usage(rbacutils.ScopeDomain, ownerId, "domain") + domainUsage := models.ImageManager.Usage(rbacutils.ScopeDomain, ownerId, "domain", result) usages.Update(jsonutils.Marshal(domainUsage)) - domainUsage = models.GuestImageManager.Usage(rbacutils.ScopeDomain, ownerId, "domain") + domainUsage = models.GuestImageManager.Usage(rbacutils.ScopeDomain, ownerId, "domain", result) usages.Update(jsonutils.Marshal(domainUsage)) } if scope.HigherEqual(rbacutils.ScopeProject) { - projectUsage := models.ImageManager.Usage(rbacutils.ScopeProject, ownerId, "") + projectUsage := models.ImageManager.Usage(rbacutils.ScopeProject, ownerId, "", result) usages.Update(jsonutils.Marshal(projectUsage)) - projectUsage = models.GuestImageManager.Usage(rbacutils.ScopeProject, ownerId, "") + projectUsage = models.GuestImageManager.Usage(rbacutils.ScopeProject, ownerId, "", result) usages.Update(jsonutils.Marshal(projectUsage)) } diff --git a/pkg/keystone/models/usages.go b/pkg/keystone/models/usages.go index b6b48fc820..482da0192d 100644 --- a/pkg/keystone/models/usages.go +++ b/pkg/keystone/models/usages.go @@ -16,27 +16,41 @@ package models import ( api "yunion.io/x/onecloud/pkg/apis/identity" + "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/util/rbacutils" ) -func Usage() map[string]int { +func Usage(result rbacutils.SPolicyResult) map[string]int { results := make(map[string]int) - domCnt, _ := DomainManager.Query().IsTrue("is_domain").NotEquals("id", api.KeystoneDomainRoot).CountWithError() + dq := DomainManager.Query() + dq = db.ObjectIdQueryWithPolicyResult(dq, DomainManager, result) + domCnt, _ := dq.IsTrue("is_domain").NotEquals("id", api.KeystoneDomainRoot).CountWithError() results["domains"] = domCnt - projCnt, _ := ProjectManager.Query().IsFalse("is_domain").CountWithError() + pq := ProjectManager.Query() + pq = db.ObjectIdQueryWithPolicyResult(pq, ProjectManager, result) + projCnt, _ := pq.IsFalse("is_domain").CountWithError() results["projects"] = projCnt - roleCnt, _ := RoleManager.Query().CountWithError() + rq := RoleManager.Query() + rq = db.ObjectIdQueryWithPolicyResult(rq, RoleManager, result) + roleCnt, _ := rq.CountWithError() results["roles"] = roleCnt - usrCnt, _ := UserManager.Query().CountWithError() + uq := UserManager.Query() + uq = db.ObjectIdQueryWithPolicyResult(uq, UserManager, result) + usrCnt, _ := uq.CountWithError() results["users"] = usrCnt - grpCnt, _ := GroupManager.Query().CountWithError() + gq := GroupManager.Query() + gq = db.ObjectIdQueryWithPolicyResult(gq, GroupManager, result) + grpCnt, _ := gq.CountWithError() results["groups"] = grpCnt - policy, _ := PolicyManager.Query().CountWithError() + pcq := PolicyManager.Query() + pcq = db.ObjectIdQueryWithPolicyResult(pcq, PolicyManager, result) + policy, _ := pcq.CountWithError() results["policies"] = policy return results diff --git a/pkg/keystone/usages/handler.go b/pkg/keystone/usages/handler.go index 297630504c..ac39c89cfb 100644 --- a/pkg/keystone/usages/handler.go +++ b/pkg/keystone/usages/handler.go @@ -23,6 +23,7 @@ import ( "yunion.io/x/onecloud/pkg/appsrv" "yunion.io/x/onecloud/pkg/cloudcommon/consts" + "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/policy" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/keystone/models" @@ -36,9 +37,15 @@ func AddUsageHandler(prefix string, app *appsrv.Application) { } func ReportGeneralUsage(ctx context.Context, w http.ResponseWriter, r *http.Request) { - // _, query, _ := appsrv.FetchEnv(ctx, w, r) + _, query, _ := appsrv.FetchEnv(ctx, w, r) userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential) + _, _, err, result := db.FetchUsageOwnerScope(ctx, userCred, query) + if err != nil { + httperrors.GeneralServerError(ctx, w, err) + return + } + isAdmin := false if policy.PolicyManager.Allow(rbacutils.ScopeSystem, userCred, consts.GetServiceType(), "usages", policy.PolicyActionGet).Result.IsAllow() { @@ -48,25 +55,20 @@ func ReportGeneralUsage(ctx context.Context, w http.ResponseWriter, r *http.Requ var adminUsage map[string]int // var projectUsage map[string]int64 if isAdmin { - adminUsage = models.Usage() + adminUsage = models.Usage(result) } - /* isProject := false - if consts.IsRbacEnabled() { - if policy.PolicyManager.Allow(false, userCred, consts.GetServiceType(), - "usages", policy.PolicyActionGet) == rbacutils.Deny { - isProject = false - } else { - isProject = true - } + /*isProject := false + if policy.PolicyManager.Allow(false, userCred, consts.GetServiceType(), + "usages", policy.PolicyActionGet) == rbacutils.Deny { + isProject = false } else { isProject = true } if isProject { projectUsage = models.Usage(userCred.GetProjectId(), "") - } - */ + }*/ // if !isAdmin && !isProject { if !isAdmin { @@ -76,7 +78,7 @@ func ReportGeneralUsage(ctx context.Context, w http.ResponseWriter, r *http.Requ usages := jsonutils.NewDict() // if isProject { - // usages.Update(jsonutils.Marshal(projectUsage)) + // usages.Update(jsonutils.Marshal(projectUsage)) // } if isAdmin {