Files
cloudpods/pkg/multicloud/openstack/quota.go
2020-03-30 10:24:05 +08:00

170 lines
5.4 KiB
Go

// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openstack
import (
"fmt"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/cloudprovider"
"yunion.io/x/onecloud/pkg/util/version"
)
type QuotaDetail struct {
Reserved int
Limit int
InUse int
}
type QuotaSet struct {
InjectedFileContentBytes QuotaDetail
MetadataItems QuotaDetail
ServerGroupMembers QuotaDetail
ServerGroups QuotaDetail
Ram QuotaDetail
FloatingIps QuotaDetail
KeyPairs QuotaDetail
Id string
Instances QuotaDetail
SecurityGroupRules QuotaDetail
InjectedFiles QuotaDetail
Cores QuotaDetail
FixedIps QuotaDetail
InjectedFilePathBytes QuotaDetail
SecurityGroups QuotaDetail
}
type SQuota struct {
Cores int
Instances int
KeyPairs int
FixedIps int
MetadataItems int
Ram int
ServerGroups int
ServerGroupMembers int
InjectedFileContentBytes int
InjectedFilePathBytes int
InjectedFiles int
Floatingips int
Networks int
Port int
RbacPolicy int
Router int
SecurityGroups int
SecurityGroupRules int
}
func (region *SRegion) GetQuota() (*QuotaSet, error) {
_, resp, err := region.Get("compute", fmt.Sprintf("/os-quota-sets/%s/detail", region.client.tokenCredential.GetTenantId()), "", nil)
if err != nil {
return nil, err
}
quota := &QuotaSet{}
return quota, resp.Unmarshal(quota, "quota_set")
}
func (region *SRegion) SetQuota(quota *SQuota) error {
_, maxVersion, _ := region.GetVersion("compute")
params := map[string]map[string]interface{}{
"quota_set": {
"force": "True",
},
}
if version.GE(maxVersion, "2.35") {
if quota.Floatingips > 0 {
params["quota_set"]["floating_ips"] = quota.Floatingips
}
if quota.SecurityGroups > 0 {
params["quota_set"]["security_group"] = quota.SecurityGroups
}
if quota.SecurityGroupRules > 0 {
params["quota_set"]["security_group_rules"] = quota.SecurityGroupRules
}
if quota.FixedIps > 0 {
params["quota_set"]["fixed_ips"] = quota.FixedIps
}
if quota.Networks > 0 {
params["quota_set"]["networks"] = quota.Networks
}
}
_, _, err := region.Update("compute", "/os-quota-sets/"+region.client.tokenCredential.GetTenantId(), maxVersion, jsonutils.Marshal(params))
return err
}
type IQuota struct {
Name string
Limit int
InUse int
}
func (iq *IQuota) GetGlobalId() string {
return iq.Name
}
func (iq *IQuota) GetName() string {
return iq.Name
}
func (iq *IQuota) GetDesc() string {
return ""
}
func (iq *IQuota) GetQuotaType() string {
return iq.Name
}
func (iq *IQuota) GetMaxQuotaCount() int {
return iq.Limit
}
func (iq *IQuota) GetCurrentQuotaUsedCount() int {
return iq.InUse
}
func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
quota, err := region.GetQuota()
if err != nil {
return nil, errors.Wrap(err, "GetQuota")
}
ret := []cloudprovider.ICloudQuota{}
ret = append(ret, &IQuota{Name: "injected_file_content_bytes", Limit: quota.InjectedFileContentBytes.Limit, InUse: quota.InjectedFileContentBytes.InUse})
ret = append(ret, &IQuota{Name: "metadata_items", Limit: quota.MetadataItems.Limit, InUse: quota.MetadataItems.InUse})
ret = append(ret, &IQuota{Name: "server_group_members", Limit: quota.ServerGroupMembers.Limit, InUse: quota.ServerGroupMembers.InUse})
ret = append(ret, &IQuota{Name: "server_groups", Limit: quota.ServerGroups.Limit, InUse: quota.ServerGroups.InUse})
ret = append(ret, &IQuota{Name: "ram", Limit: quota.Ram.Limit, InUse: quota.Ram.InUse})
ret = append(ret, &IQuota{Name: "floating_ips", Limit: quota.FloatingIps.Limit, InUse: quota.FloatingIps.InUse})
ret = append(ret, &IQuota{Name: "key_pairs", Limit: quota.KeyPairs.Limit, InUse: quota.KeyPairs.InUse})
ret = append(ret, &IQuota{Name: "instances", Limit: quota.Instances.Limit, InUse: quota.Instances.InUse})
ret = append(ret, &IQuota{Name: "security_group_rules", Limit: quota.SecurityGroupRules.Limit, InUse: quota.SecurityGroupRules.InUse})
ret = append(ret, &IQuota{Name: "injected_files", Limit: quota.InjectedFiles.Limit, InUse: quota.InjectedFiles.InUse})
ret = append(ret, &IQuota{Name: "cores", Limit: quota.Cores.Limit, InUse: quota.Cores.InUse})
ret = append(ret, &IQuota{Name: "fixed_ips", Limit: quota.FixedIps.Limit, InUse: quota.FixedIps.InUse})
ret = append(ret, &IQuota{Name: "injected_file_path_bytes", Limit: quota.InjectedFilePathBytes.Limit, InUse: quota.InjectedFilePathBytes.InUse})
ret = append(ret, &IQuota{Name: "security_groups", Limit: quota.SecurityGroups.Limit, InUse: quota.SecurityGroups.InUse})
return ret, nil
}