yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/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 zstack
    16  
    17  import (
    18  	"net/url"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  )
    24  
    25  /*
    26     "uuid":"132e0ad32b2242c4a648440be917ec4a",
    27     "name":"pci.num",
    28     "identityUuid":"2dce5dc485554d21a3796500c1db007a",
    29     "identityType":"AccountVO",
    30     "value":0,
    31     "lastOpDate":"Dec 28, 2019 2:26:03 PM",
    32     "createDate":"Dec 28, 2019 2:26:03 PM"
    33  */
    34  
    35  type SQuota struct {
    36  	Uuid         string
    37  	Name         string
    38  	IdentityUuid string
    39  	IdentityType string
    40  	Value        int
    41  }
    42  
    43  func (q *SQuota) GetGlobalId() string {
    44  	return q.Uuid
    45  }
    46  
    47  func (q *SQuota) GetName() string {
    48  	return q.Name
    49  }
    50  
    51  func (q *SQuota) GetDesc() string {
    52  	return ""
    53  }
    54  
    55  func (q *SQuota) GetQuotaType() string {
    56  	return q.Name
    57  }
    58  
    59  func (q *SQuota) GetMaxQuotaCount() int {
    60  	return q.Value
    61  }
    62  
    63  func (q *SQuota) GetCurrentQuotaUsedCount() int {
    64  	return -1
    65  }
    66  
    67  func (region *SRegion) GetQuotas() ([]SQuota, error) {
    68  	quotas := []SQuota{}
    69  	params := url.Values{}
    70  	err := region.client.listAll("accounts/quotas", params, &quotas)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return quotas, nil
    75  }
    76  
    77  type SUserAccount struct {
    78  	Uuid string
    79  }
    80  
    81  func (region *SRegion) GetUserAccount(name string) ([]SUserAccount, error) {
    82  	users := []SUserAccount{}
    83  	params := url.Values{}
    84  	if len(name) > 0 {
    85  		params.Add("q", "name="+name)
    86  	}
    87  	err := region.client.listAll("accounts/users", params, &users)
    88  	if err != nil {
    89  		return nil, errors.Wrap(err, "users.list")
    90  	}
    91  	return users, nil
    92  }
    93  
    94  func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
    95  	quotas, err := region.GetQuotas()
    96  	if err != nil {
    97  		return nil, errors.Wrap(err, "GetQuotas")
    98  	}
    99  	ret := []cloudprovider.ICloudQuota{}
   100  	for i := range quotas {
   101  		ret = append(ret, &quotas[i])
   102  	}
   103  	return ret, nil
   104  }