quota.go 2.3 KB

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