mod_usages.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 compute
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  20. "yunion.io/x/onecloud/pkg/mcclient/modules"
  21. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  22. "yunion.io/x/onecloud/pkg/mcclient/modules/image"
  23. )
  24. type TUsageManager string
  25. const (
  26. UsageManagerImage TUsageManager = "image"
  27. UsageManagerIdentity TUsageManager = "identity"
  28. UsageManagerK8s TUsageManager = "k8s"
  29. )
  30. type IUsageManager interface {
  31. GetUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error)
  32. }
  33. type UsageManager struct {
  34. modulebase.ResourceManager
  35. managers map[TUsageManager]IUsageManager
  36. }
  37. type HistoryUsageManager struct {
  38. modulebase.ResourceManager
  39. managers map[TUsageManager]IUsageManager
  40. }
  41. func (this *UsageManager) RegisterManager(manType TUsageManager, man IUsageManager) {
  42. if this.managers == nil {
  43. this.managers = make(map[TUsageManager]IUsageManager, 0)
  44. }
  45. this.managers[manType] = man
  46. }
  47. func (this *HistoryUsageManager) RegisterManager(manType TUsageManager, man IUsageManager) {
  48. if this.managers == nil {
  49. this.managers = make(map[TUsageManager]IUsageManager, 0)
  50. }
  51. this.managers[manType] = man
  52. }
  53. func (this *UsageManager) GetGeneralUsage(session *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  54. url := "/usages"
  55. if params != nil {
  56. range_type, _ := params.GetString("range_type")
  57. range_id, _ := params.GetString("range_id")
  58. if len(range_type) > 0 && len(range_id) > 0 {
  59. url = fmt.Sprintf("%s/%s/%s", url, range_type, range_id)
  60. }
  61. dict := params.(*jsonutils.JSONDict)
  62. dict.Remove("range_type")
  63. dict.Remove("range_id")
  64. qs := dict.QueryString()
  65. if len(qs) > 0 {
  66. url = fmt.Sprintf("%s?%s", url, qs)
  67. }
  68. }
  69. return modulebase.Get(this.ResourceManager, session, url, this.Keyword)
  70. }
  71. func (this *UsageManager) GetManagerByType(t TUsageManager) IUsageManager {
  72. return this.managers[t]
  73. }
  74. func (this *UsageManager) getManagerUsage(manType TUsageManager, s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  75. return this.GetManagerByType(manType).GetUsage(s, params)
  76. }
  77. func (this *UsageManager) GetK8sUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  78. return this.getManagerUsage(UsageManagerK8s, s, params)
  79. }
  80. func (this *UsageManager) GetIdentityUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  81. return this.getManagerUsage(UsageManagerIdentity, s, params)
  82. }
  83. func (this *UsageManager) GetImageUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  84. return this.getManagerUsage(UsageManagerImage, s, params)
  85. }
  86. var (
  87. Usages *UsageManager
  88. HistoryUsages *HistoryUsageManager
  89. )
  90. func init() {
  91. Usages = &UsageManager{
  92. ResourceManager: modules.NewComputeManager("usage", "usages",
  93. []string{},
  94. []string{}),
  95. }
  96. modules.RegisterCompute(Usages)
  97. HistoryUsages = &HistoryUsageManager{
  98. ResourceManager: modules.NewComputeManager("history-usage", "history-usages",
  99. []string{},
  100. []string{}),
  101. }
  102. modules.RegisterCompute(HistoryUsages)
  103. }
  104. func InitUsages() {
  105. Usages.RegisterManager(UsageManagerImage, &image.Images)
  106. Usages.RegisterManager(UsageManagerIdentity, &identity.IdentityUsages)
  107. }