mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-30 17:13:08 +08:00
feat(region): add instance_snapshot for regionquota
This commit is contained in:
@@ -58,7 +58,8 @@ type RegionQuotaOptions struct {
|
||||
|
||||
Eip int64 `help:"Elastic IP count" json:"eip,omitzero"`
|
||||
|
||||
Snapshot int64 `help:"Snapshot count" json:"snapshot,omitzero"`
|
||||
Snapshot int64 `help:"Snapshot count" json:"snapshot,omitzero"`
|
||||
InstanceSnapshot int64 `help:"Instance snapshot count" json:"instance_snapshot,omitzero"`
|
||||
|
||||
Bucket int64 `help:"bucket count" json:"bucket,omitzero"`
|
||||
ObjectGB int64 `help:"object size in GB" json:"object_gb,omitzero"`
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -435,6 +436,43 @@ func (self *SInstanceSnapshot) GetSnapshots() ([]SSnapshot, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SInstanceSnapshot) GetQuotaKeys() quotas.IQuotaKeys {
|
||||
return fetchRegionalQuotaKeys(
|
||||
rbacutils.ScopeProject,
|
||||
self.GetOwnerId(),
|
||||
self.GetRegion(),
|
||||
self.GetCloudprovider(),
|
||||
)
|
||||
}
|
||||
|
||||
func (self *SInstanceSnapshot) GetUsages() []db.IUsage {
|
||||
if self.PendingDeleted || self.Deleted {
|
||||
return nil
|
||||
}
|
||||
usage := SRegionQuota{InstanceSnapshot: 1}
|
||||
keys := self.GetQuotaKeys()
|
||||
usage.SetKeys(keys)
|
||||
return []db.IUsage{
|
||||
&usage,
|
||||
}
|
||||
}
|
||||
|
||||
func TotalInstanceSnapshotCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) (int, error) {
|
||||
q := InstanceSnapshotManager.Query()
|
||||
|
||||
switch scope {
|
||||
case rbacutils.ScopeSystem:
|
||||
case rbacutils.ScopeDomain:
|
||||
q = q.Equals("domain_id", ownerId.GetProjectDomainId())
|
||||
case rbacutils.ScopeProject:
|
||||
q = q.Equals("tenant_id", ownerId.GetProjectId())
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func (self *SInstanceSnapshot) GetInstanceSnapshotJointAt(diskIndex int) (*SInstanceSnapshotJoint, error) {
|
||||
ispj := new(SInstanceSnapshotJoint)
|
||||
err := InstanceSnapshotJointManager.Query().
|
||||
|
||||
@@ -79,7 +79,8 @@ type SRegionQuota struct {
|
||||
// Bw int `default:"-1" allow_zero:"true"`
|
||||
// Ebw int `default:"-1" allow_zero:"true"`
|
||||
|
||||
Snapshot int `default:"-1" allow_zero:"true" json:"snapshot"`
|
||||
Snapshot int `default:"-1" allow_zero:"true" json:"snapshot"`
|
||||
InstanceSnapshot int `default:"-1" allow_zero:"true" json:"instance_snapshot"`
|
||||
|
||||
Bucket int `default:"-1" allow_zero:"true" json:"bucket"`
|
||||
ObjectGB int `default:"-1" allow_zero:"true" json:"object_gb"`
|
||||
@@ -131,6 +132,7 @@ func (self *SRegionQuota) FetchSystemQuota() {
|
||||
// self.Bw = defaultValue(options.Options.DefaultBwQuota)
|
||||
// self.Ebw = defaultValue(options.Options.DefaultEbwQuota)
|
||||
self.Snapshot = defaultValue(options.Options.DefaultSnapshotQuota)
|
||||
self.InstanceSnapshot = defaultValue(options.Options.DefaultInstanceSnapshotQuota)
|
||||
self.Bucket = defaultValue(options.Options.DefaultBucketQuota)
|
||||
self.ObjectGB = defaultValue(options.Options.DefaultObjectGBQuota)
|
||||
self.ObjectCnt = defaultValue(options.Options.DefaultObjectCntQuota)
|
||||
@@ -191,6 +193,9 @@ func (self *SRegionQuota) FetchUsage(ctx context.Context) error {
|
||||
snapshotCount, _ := TotalSnapshotCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv)
|
||||
self.Snapshot = snapshotCount
|
||||
|
||||
instanceSnapshotCount, _ := TotalInstanceSnapshotCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv)
|
||||
self.InstanceSnapshot = instanceSnapshotCount
|
||||
|
||||
bucketUsage := BucketManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, regionKeys.CloudEnv)
|
||||
self.Bucket = bucketUsage.Buckets
|
||||
self.ObjectGB = int(bucketUsage.Bytes / 1000 / 1000 / 1000)
|
||||
@@ -224,6 +229,9 @@ func (self *SRegionQuota) ResetNegative() {
|
||||
if self.Snapshot < 0 {
|
||||
self.Snapshot = 0
|
||||
}
|
||||
if self.InstanceSnapshot < 0 {
|
||||
self.InstanceSnapshot = 0
|
||||
}
|
||||
if self.Bucket < 0 {
|
||||
self.Bucket = 0
|
||||
}
|
||||
@@ -263,6 +271,9 @@ func (self *SRegionQuota) IsEmpty() bool {
|
||||
if self.Snapshot > 0 {
|
||||
return false
|
||||
}
|
||||
if self.InstanceSnapshot > 0 {
|
||||
return false
|
||||
}
|
||||
if self.Bucket > 0 {
|
||||
return false
|
||||
}
|
||||
@@ -292,6 +303,7 @@ func (self *SRegionQuota) Add(quota quotas.IQuota) {
|
||||
// self.Bw = self.Bw + quotas.NonNegative(squota.Bw)
|
||||
// self.Ebw = self.Ebw + quotas.NonNegative(squota.Ebw)
|
||||
self.Snapshot = self.Snapshot + quotas.NonNegative(squota.Snapshot)
|
||||
self.InstanceSnapshot = self.InstanceSnapshot + quotas.NonNegative(squota.InstanceSnapshot)
|
||||
self.Bucket = self.Bucket + quotas.NonNegative(squota.Bucket)
|
||||
self.ObjectGB = self.ObjectGB + quotas.NonNegative(squota.ObjectGB)
|
||||
self.ObjectCnt = self.ObjectCnt + quotas.NonNegative(squota.ObjectCnt)
|
||||
@@ -308,6 +320,7 @@ func (self *SRegionQuota) Sub(quota quotas.IQuota) {
|
||||
// self.Bw = nonNegative(self.Bw - squota.Bw)
|
||||
// self.Ebw = nonNegative(self.Ebw - squota.Ebw)
|
||||
self.Snapshot = nonNegative(self.Snapshot - squota.Snapshot)
|
||||
self.InstanceSnapshot = nonNegative(self.InstanceSnapshot - squota.InstanceSnapshot)
|
||||
self.Bucket = nonNegative(self.Bucket - squota.Bucket)
|
||||
self.ObjectGB = nonNegative(self.ObjectGB - squota.ObjectGB)
|
||||
self.ObjectCnt = nonNegative(self.ObjectCnt - squota.ObjectCnt)
|
||||
@@ -337,6 +350,9 @@ func (self *SRegionQuota) Allocable(request quotas.IQuota) int {
|
||||
if self.Snapshot >= 0 && squota.Snapshot > 0 && (cnt < 0 || cnt > self.Snapshot/squota.Snapshot) {
|
||||
cnt = self.Snapshot / squota.Snapshot
|
||||
}
|
||||
if self.InstanceSnapshot >= 0 && squota.InstanceSnapshot > 0 && (cnt < 0 || cnt > self.InstanceSnapshot/squota.InstanceSnapshot) {
|
||||
cnt = self.InstanceSnapshot / squota.InstanceSnapshot
|
||||
}
|
||||
if self.Bucket >= 0 && squota.Bucket > 0 && (cnt < 0 || cnt > self.Bucket/squota.Bucket) {
|
||||
cnt = self.Bucket / squota.Bucket
|
||||
}
|
||||
@@ -378,6 +394,9 @@ func (self *SRegionQuota) Update(quota quotas.IQuota) {
|
||||
if squota.Snapshot > 0 {
|
||||
self.Snapshot = squota.Snapshot
|
||||
}
|
||||
if squota.InstanceSnapshot > 0 {
|
||||
self.InstanceSnapshot = squota.InstanceSnapshot
|
||||
}
|
||||
if squota.Bucket > 0 {
|
||||
self.Bucket = squota.Bucket
|
||||
}
|
||||
@@ -420,6 +439,9 @@ func (used *SRegionQuota) Exceed(request quotas.IQuota, quota quotas.IQuota) err
|
||||
if quotas.Exceed(used.Snapshot, sreq.Snapshot, squota.Snapshot) {
|
||||
err.Add("snapshot", squota.Snapshot, used.Snapshot, sreq.Snapshot)
|
||||
}
|
||||
if quotas.Exceed(used.InstanceSnapshot, sreq.InstanceSnapshot, squota.InstanceSnapshot) {
|
||||
err.Add("instance_snapshot", squota.InstanceSnapshot, used.InstanceSnapshot, sreq.InstanceSnapshot)
|
||||
}
|
||||
if quotas.Exceed(used.Bucket, sreq.Bucket, squota.Bucket) {
|
||||
err.Add("bucket", squota.Bucket, used.Bucket, sreq.Bucket)
|
||||
}
|
||||
@@ -453,6 +475,7 @@ func (self *SRegionQuota) ToJSON(prefix string) jsonutils.JSONObject {
|
||||
//ret.Add(jsonutils.NewInt(int64(self.Bw)), keyName(prefix, "bw"))
|
||||
//ret.Add(jsonutils.NewInt(int64(self.Ebw)), keyName(prefix, "ebw"))
|
||||
ret.Add(jsonutils.NewInt(int64(self.Snapshot)), keyName(prefix, "snapshot"))
|
||||
ret.Add(jsonutils.NewInt(int64(self.InstanceSnapshot)), keyName(prefix, "instance_snapshot"))
|
||||
ret.Add(jsonutils.NewInt(int64(self.Bucket)), keyName(prefix, "bucket"))
|
||||
ret.Add(jsonutils.NewInt(int64(self.ObjectGB)), keyName(prefix, "object_gb"))
|
||||
ret.Add(jsonutils.NewInt(int64(self.ObjectCnt)), keyName(prefix, "object_cnt"))
|
||||
|
||||
@@ -55,20 +55,21 @@ type ComputeOptions struct {
|
||||
DefaultBandwidth int `default:"1000" help:"Default bandwidth"`
|
||||
DefaultMtu int `default:"1500" help:"Default network mtu"`
|
||||
|
||||
DefaultServerQuota int `default:"50" help:"Common Server quota per tenant, default 50"`
|
||||
DefaultCpuQuota int `default:"200" help:"Common CPU quota per tenant, default 200"`
|
||||
DefaultMemoryQuota int `default:"204800" help:"Common memory quota per tenant in MB, default 200G"`
|
||||
DefaultStorageQuota int `default:"12288000" help:"Common storage quota per tenant in MB, default 12T"`
|
||||
DefaultPortQuota int `default:"200" help:"Common network port quota per tenant, default 200"`
|
||||
DefaultEipQuota int `default:"10" help:"Common floating IP quota per tenant, default 10"`
|
||||
DefaultEportQuota int `default:"200" help:"Common exit network port quota per tenant, default 200"`
|
||||
DefaultBwQuota int `default:"2000000" help:"Common network port bandwidth in mbps quota per tenant, default 200*10Gbps"`
|
||||
DefaultEbwQuota int `default:"4000" help:"Common exit network port bandwidth quota per tenant, default 4Gbps"`
|
||||
DefaultKeypairQuota int `default:"50" help:"Common keypair quota per tenant, default 50"`
|
||||
DefaultGroupQuota int `default:"50" help:"Common group quota per tenant, default 50"`
|
||||
DefaultSecgroupQuota int `default:"50" help:"Common security group quota per tenant, default 50"`
|
||||
DefaultIsolatedDeviceQuota int `default:"200" help:"Common isolated device quota per tenant, default 200"`
|
||||
DefaultSnapshotQuota int `default:"10" help:"Common snapshot quota per tenant, default 10"`
|
||||
DefaultServerQuota int `default:"50" help:"Common Server quota per tenant, default 50"`
|
||||
DefaultCpuQuota int `default:"200" help:"Common CPU quota per tenant, default 200"`
|
||||
DefaultMemoryQuota int `default:"204800" help:"Common memory quota per tenant in MB, default 200G"`
|
||||
DefaultStorageQuota int `default:"12288000" help:"Common storage quota per tenant in MB, default 12T"`
|
||||
DefaultPortQuota int `default:"200" help:"Common network port quota per tenant, default 200"`
|
||||
DefaultEipQuota int `default:"10" help:"Common floating IP quota per tenant, default 10"`
|
||||
DefaultEportQuota int `default:"200" help:"Common exit network port quota per tenant, default 200"`
|
||||
DefaultBwQuota int `default:"2000000" help:"Common network port bandwidth in mbps quota per tenant, default 200*10Gbps"`
|
||||
DefaultEbwQuota int `default:"4000" help:"Common exit network port bandwidth quota per tenant, default 4Gbps"`
|
||||
DefaultKeypairQuota int `default:"50" help:"Common keypair quota per tenant, default 50"`
|
||||
DefaultGroupQuota int `default:"50" help:"Common group quota per tenant, default 50"`
|
||||
DefaultSecgroupQuota int `default:"50" help:"Common security group quota per tenant, default 50"`
|
||||
DefaultIsolatedDeviceQuota int `default:"200" help:"Common isolated device quota per tenant, default 200"`
|
||||
DefaultSnapshotQuota int `default:"10" help:"Common snapshot quota per tenant, default 10"`
|
||||
DefaultInstanceSnapshotQuota int `default:"10" help:"Common instance snapshot quota per tenant, default 10"`
|
||||
|
||||
DefaultBucketQuota int `default:"100" help:"Common bucket quota per tenant, default 100"`
|
||||
DefaultObjectGBQuota int `default:"500" help:"Common object size quota per tenant in GB, default 500GB"`
|
||||
|
||||
@@ -295,6 +295,8 @@ func getSystemGeneralUsage(userCred mcclient.IIdentityProvider, rangeObjs []db.I
|
||||
|
||||
SnapshotUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
InstanceSnapshotUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
LoadbalancerUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
DBInstanceUsage(rbacutils.ScopeSystem, nil, rangeObjs, providers, brands, cloudEnv),
|
||||
@@ -379,6 +381,8 @@ func getDomainGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityPr
|
||||
|
||||
SnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
InstanceSnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
LoadbalancerUsage(scope, cred, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
DBInstanceUsage(scope, cred, rangeObjs, providers, brands, cloudEnv),
|
||||
@@ -421,6 +425,8 @@ func getProjectGeneralUsage(scope rbacutils.TRbacScope, cred mcclient.IIdentityP
|
||||
|
||||
SnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
InstanceSnapshotUsage(scope, cred, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
LoadbalancerUsage(scope, cred, rangeObjs, providers, brands, cloudEnv),
|
||||
|
||||
DBInstanceUsage(scope, cred, rangeObjs, providers, brands, cloudEnv),
|
||||
@@ -869,6 +875,13 @@ func SnapshotUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvide
|
||||
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)
|
||||
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)
|
||||
count := make(map[string]interface{})
|
||||
|
||||
Reference in New Issue
Block a user