usage.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 azure
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. )
  22. type UsageName struct {
  23. Value string
  24. LocalizedValue string
  25. }
  26. // {"value":[{"unit":"Count","currentValue":0,"limit":250,"name":{"value":"StorageAccounts","localizedValue":"Storage Accounts"}}]}
  27. type SUsage struct {
  28. Unit string
  29. CurrentValue int
  30. Limit int
  31. Name UsageName
  32. }
  33. func (u *SUsage) GetGlobalId() string {
  34. return strings.ToLower(u.Name.Value)
  35. }
  36. func (u *SUsage) GetQuotaType() string {
  37. return u.Name.Value
  38. }
  39. func (u *SUsage) GetDesc() string {
  40. return u.Name.LocalizedValue
  41. }
  42. func (u *SUsage) GetMaxQuotaCount() int {
  43. return u.Limit
  44. }
  45. func (u *SUsage) GetCurrentQuotaUsedCount() int {
  46. return u.CurrentValue
  47. }
  48. func (region *SRegion) GetUsage(resourceType string) ([]SUsage, error) {
  49. usage := []SUsage{}
  50. resource := fmt.Sprintf("%s/locations/%s/usages", resourceType, region.Name)
  51. err := region.client.list(resource, url.Values{}, &usage)
  52. if err != nil {
  53. return nil, errors.Wrapf(err, "ListAll(%s)", resource)
  54. }
  55. return usage, nil
  56. }
  57. func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
  58. ret := []cloudprovider.ICloudQuota{}
  59. for _, resourceType := range []string{"Microsoft.Network", "Microsoft.Storage", "Microsoft.Compute"} {
  60. usages, err := region.GetUsage(resourceType)
  61. if err != nil {
  62. return nil, errors.Wrap(err, "GetUsage")
  63. }
  64. for i := range usages {
  65. ret = append(ret, &usages[i])
  66. }
  67. }
  68. return ret, nil
  69. }