yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/quota.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package openstack
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  )
    24  
    25  type QuotaDetail struct {
    26  	Reserved int
    27  	Limit    int
    28  	InUse    int
    29  }
    30  
    31  type QuotaSet struct {
    32  	InjectedFileContentBytes QuotaDetail
    33  	MetadataItems            QuotaDetail
    34  	ServerGroupMembers       QuotaDetail
    35  	ServerGroups             QuotaDetail
    36  	Ram                      QuotaDetail
    37  	FloatingIps              QuotaDetail
    38  	KeyPairs                 QuotaDetail
    39  	Id                       string
    40  	Instances                QuotaDetail
    41  	SecurityGroupRules       QuotaDetail
    42  	InjectedFiles            QuotaDetail
    43  	Cores                    QuotaDetail
    44  	FixedIps                 QuotaDetail
    45  	InjectedFilePathBytes    QuotaDetail
    46  	SecurityGroups           QuotaDetail
    47  }
    48  
    49  type SQuota struct {
    50  	Cores                    int
    51  	Instances                int
    52  	KeyPairs                 int
    53  	FixedIps                 int
    54  	MetadataItems            int
    55  	Ram                      int
    56  	ServerGroups             int
    57  	ServerGroupMembers       int
    58  	InjectedFileContentBytes int
    59  	InjectedFilePathBytes    int
    60  	InjectedFiles            int
    61  	Floatingips              int
    62  	Networks                 int
    63  	Port                     int
    64  	RbacPolicy               int
    65  	Router                   int
    66  	SecurityGroups           int
    67  	SecurityGroupRules       int
    68  }
    69  
    70  func (region *SRegion) GetQuota() (*QuotaSet, error) {
    71  	resource := fmt.Sprintf("/os-quota-sets/%s/detail", region.client.tokenCredential.GetTenantId())
    72  	resp, err := region.ecsGet(resource)
    73  	if err != nil {
    74  		return nil, errors.Wrap(err, "ecsGet")
    75  	}
    76  	quota := &QuotaSet{}
    77  	return quota, resp.Unmarshal(quota, "quota_set")
    78  }
    79  
    80  func (region *SRegion) SetQuota(quota *SQuota) error {
    81  	params := map[string]map[string]interface{}{
    82  		"quota_set": {
    83  			"force": "True",
    84  		},
    85  	}
    86  
    87  	if quota.Floatingips > 0 {
    88  		params["quota_set"]["floating_ips"] = quota.Floatingips
    89  	}
    90  
    91  	if quota.SecurityGroups > 0 {
    92  		params["quota_set"]["security_group"] = quota.SecurityGroups
    93  	}
    94  
    95  	if quota.SecurityGroupRules > 0 {
    96  		params["quota_set"]["security_group_rules"] = quota.SecurityGroupRules
    97  	}
    98  
    99  	if quota.FixedIps > 0 {
   100  		params["quota_set"]["fixed_ips"] = quota.FixedIps
   101  	}
   102  
   103  	if quota.Networks > 0 {
   104  		params["quota_set"]["networks"] = quota.Networks
   105  	}
   106  
   107  	resource := "/os-quota-sets/" + region.client.tokenCredential.GetTenantId()
   108  	_, err := region.ecsUpdate(resource, params)
   109  	return err
   110  }
   111  
   112  type IQuota struct {
   113  	Name  string
   114  	Limit int
   115  	InUse int
   116  }
   117  
   118  func (iq *IQuota) GetGlobalId() string {
   119  	return iq.Name
   120  }
   121  
   122  func (iq *IQuota) GetName() string {
   123  	return iq.Name
   124  }
   125  
   126  func (iq *IQuota) GetDesc() string {
   127  	return ""
   128  }
   129  
   130  func (iq *IQuota) GetQuotaType() string {
   131  	return iq.Name
   132  }
   133  
   134  func (iq *IQuota) GetMaxQuotaCount() int {
   135  	return iq.Limit
   136  }
   137  
   138  func (iq *IQuota) GetCurrentQuotaUsedCount() int {
   139  	return iq.InUse
   140  }
   141  
   142  func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
   143  	quota, err := region.GetQuota()
   144  	if err != nil {
   145  		return nil, errors.Wrap(err, "GetQuota")
   146  	}
   147  
   148  	ret := []cloudprovider.ICloudQuota{}
   149  
   150  	ret = append(ret, &IQuota{Name: "injected_file_content_bytes", Limit: quota.InjectedFileContentBytes.Limit, InUse: quota.InjectedFileContentBytes.InUse})
   151  	ret = append(ret, &IQuota{Name: "metadata_items", Limit: quota.MetadataItems.Limit, InUse: quota.MetadataItems.InUse})
   152  	ret = append(ret, &IQuota{Name: "server_group_members", Limit: quota.ServerGroupMembers.Limit, InUse: quota.ServerGroupMembers.InUse})
   153  	ret = append(ret, &IQuota{Name: "server_groups", Limit: quota.ServerGroups.Limit, InUse: quota.ServerGroups.InUse})
   154  	ret = append(ret, &IQuota{Name: "ram", Limit: quota.Ram.Limit, InUse: quota.Ram.InUse})
   155  	ret = append(ret, &IQuota{Name: "floating_ips", Limit: quota.FloatingIps.Limit, InUse: quota.FloatingIps.InUse})
   156  	ret = append(ret, &IQuota{Name: "key_pairs", Limit: quota.KeyPairs.Limit, InUse: quota.KeyPairs.InUse})
   157  	ret = append(ret, &IQuota{Name: "instances", Limit: quota.Instances.Limit, InUse: quota.Instances.InUse})
   158  	ret = append(ret, &IQuota{Name: "security_group_rules", Limit: quota.SecurityGroupRules.Limit, InUse: quota.SecurityGroupRules.InUse})
   159  	ret = append(ret, &IQuota{Name: "injected_files", Limit: quota.InjectedFiles.Limit, InUse: quota.InjectedFiles.InUse})
   160  	ret = append(ret, &IQuota{Name: "cores", Limit: quota.Cores.Limit, InUse: quota.Cores.InUse})
   161  	ret = append(ret, &IQuota{Name: "fixed_ips", Limit: quota.FixedIps.Limit, InUse: quota.FixedIps.InUse})
   162  	ret = append(ret, &IQuota{Name: "injected_file_path_bytes", Limit: quota.InjectedFilePathBytes.Limit, InUse: quota.InjectedFilePathBytes.InUse})
   163  	ret = append(ret, &IQuota{Name: "security_groups", Limit: quota.SecurityGroups.Limit, InUse: quota.SecurityGroups.InUse})
   164  	return ret, nil
   165  }