usages.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/util/printutils"
  20. "yunion.io/x/onecloud/cmd/climc/shell"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  24. "yunion.io/x/onecloud/pkg/mcclient/modules/image"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
  26. "yunion.io/x/onecloud/pkg/mcclient/options"
  27. )
  28. type GeneralUsageOptions struct {
  29. HostType []string `help:"Host types" choices:"hypervisor|baremetal|esxi|xen|kubelet|hyperv|aliyun|azure|aws|huawei|qcloud|openstack|ucloud|zstack|google|ctyun|cnware"`
  30. Provider []string `help:"Provider" choices:"OneCloud|VMware|Aliyun|Azure|Aws|Qcloud|Huawei|OpenStack|Ucloud|VolcEngine|ZStack|Google|Ctyun|CNWare"`
  31. Brand []string `help:"Brands" choices:"OneCloud|VMware|Aliyun|Azure|Aws|Qcloud|Huawei|OpenStack|Ucloud|VolcEngine|ZStack|Google|Ctyun|CNWare"`
  32. Project string `help:"show usage of specified project"`
  33. ProjectDomain string `help:"show usage of specified domain"`
  34. CloudEnv string `help:"show usage of specified cloudenv" choices:"public|private|onpremise"`
  35. Scope string `help:"show usage of specified privilege scope" choices:"system|domain|project"`
  36. Refresh bool `help:"force refresh usage statistics"`
  37. }
  38. func fetchHostTypeOptions(args *GeneralUsageOptions) *jsonutils.JSONDict {
  39. params := jsonutils.NewDict()
  40. if len(args.HostType) > 0 {
  41. params.Add(jsonutils.NewStringArray(args.HostType), "host_type")
  42. }
  43. if len(args.Provider) > 0 {
  44. params.Add(jsonutils.NewStringArray(args.Provider), "provider")
  45. }
  46. if len(args.Brand) > 0 {
  47. params.Add(jsonutils.NewStringArray(args.Brand), "brand")
  48. }
  49. if len(args.CloudEnv) > 0 {
  50. params.Add(jsonutils.NewString(args.CloudEnv), "cloud_env")
  51. }
  52. if args.Refresh {
  53. params.Add(jsonutils.JSONTrue, "refresh")
  54. }
  55. return params
  56. }
  57. type HistoryUsageListOptions struct {
  58. options.ExtraListOptions
  59. StartDate time.Time
  60. EndDate time.Time
  61. Interval string `choices:"hour|day|month|year" default:"day"`
  62. }
  63. func (opts *HistoryUsageListOptions) Params() (jsonutils.JSONObject, error) {
  64. if opts.StartDate.IsZero() || opts.EndDate.IsZero() {
  65. opts.EndDate = time.Now()
  66. switch opts.Interval {
  67. case "hour":
  68. opts.StartDate = time.Now().Add(time.Hour * -24)
  69. case "day":
  70. opts.StartDate = time.Now().AddDate(0, 0, -30)
  71. case "month":
  72. opts.StartDate = time.Now().AddDate(0, -12, 0)
  73. case "year":
  74. opts.StartDate = time.Now().AddDate(-3, 0, 0)
  75. }
  76. }
  77. return jsonutils.Marshal(opts), nil
  78. }
  79. func (o *HistoryUsageListOptions) GetExportKeys() string {
  80. return ""
  81. }
  82. func (o *HistoryUsageListOptions) GetId() string {
  83. return ""
  84. }
  85. func init() {
  86. cmd := shell.NewResourceCmd(modules.HistoryUsages)
  87. cmd.GetWithCustomShow("list", func(data jsonutils.JSONObject) {
  88. ret := map[string][]jsonutils.JSONObject{}
  89. data.Unmarshal(&ret)
  90. for k, d := range ret {
  91. fmt.Println(k)
  92. printutils.PrintJSONList(&printutils.ListResult{Data: d}, nil)
  93. }
  94. }, &HistoryUsageListOptions{})
  95. R(&GeneralUsageOptions{}, "usage", "Show general usage", func(s *mcclient.ClientSession, args *GeneralUsageOptions) error {
  96. params := fetchHostTypeOptions(args)
  97. if args.Project != "" {
  98. params.Add(jsonutils.NewString(args.Project), "project")
  99. } else if args.ProjectDomain != "" {
  100. params.Add(jsonutils.NewString(args.ProjectDomain), "project_domain")
  101. }
  102. if len(args.Scope) > 0 {
  103. params.Add(jsonutils.NewString(args.Scope), "scope")
  104. }
  105. result, err := modules.Usages.GetGeneralUsage(s, params)
  106. if err != nil {
  107. return err
  108. }
  109. printObject(result)
  110. return nil
  111. })
  112. type ResourceUsageOptions struct {
  113. GeneralUsageOptions
  114. ID string `help:"ID or name of resource"`
  115. }
  116. R(&ResourceUsageOptions{}, "zone-usage", "Show general usage of zone", func(s *mcclient.ClientSession, args *ResourceUsageOptions) error {
  117. params := fetchHostTypeOptions(&args.GeneralUsageOptions)
  118. params.Add(jsonutils.NewString("zones"), "range_type")
  119. params.Add(jsonutils.NewString(args.ID), "range_id")
  120. result, err := modules.Usages.GetGeneralUsage(s, params)
  121. if err != nil {
  122. return err
  123. }
  124. printObject(result)
  125. return nil
  126. })
  127. R(&ResourceUsageOptions{}, "wire-usage", "Show general usage of wire", func(s *mcclient.ClientSession, args *ResourceUsageOptions) error {
  128. params := fetchHostTypeOptions(&args.GeneralUsageOptions)
  129. params.Add(jsonutils.NewString("wires"), "range_type")
  130. params.Add(jsonutils.NewString(args.ID), "range_id")
  131. result, err := modules.Usages.GetGeneralUsage(s, params)
  132. if err != nil {
  133. return err
  134. }
  135. printObject(result)
  136. return nil
  137. })
  138. R(&ResourceUsageOptions{}, "cloud-provider-usage", "Show general usage of vcenter", func(s *mcclient.ClientSession, args *ResourceUsageOptions) error {
  139. params := fetchHostTypeOptions(&args.GeneralUsageOptions)
  140. params.Add(jsonutils.NewString("cloudproviders"), "range_type")
  141. params.Add(jsonutils.NewString(args.ID), "range_id")
  142. result, err := modules.Usages.GetGeneralUsage(s, params)
  143. if err != nil {
  144. return err
  145. }
  146. printObject(result)
  147. return nil
  148. })
  149. R(&ResourceUsageOptions{}, "cloud-account-usage", "Show general usage of vcenter", func(s *mcclient.ClientSession, args *ResourceUsageOptions) error {
  150. params := fetchHostTypeOptions(&args.GeneralUsageOptions)
  151. params.Add(jsonutils.NewString("cloudaccounts"), "range_type")
  152. params.Add(jsonutils.NewString(args.ID), "range_id")
  153. result, err := modules.Usages.GetGeneralUsage(s, params)
  154. if err != nil {
  155. return err
  156. }
  157. printObject(result)
  158. return nil
  159. })
  160. R(&ResourceUsageOptions{}, "schedtag-usage", "Show general usage of a scheduler tag", func(s *mcclient.ClientSession, args *ResourceUsageOptions) error {
  161. params := fetchHostTypeOptions(&args.GeneralUsageOptions)
  162. params.Add(jsonutils.NewString("schedtags"), "range_type")
  163. params.Add(jsonutils.NewString(args.ID), "range_id")
  164. result, err := modules.Usages.GetGeneralUsage(s, params)
  165. if err != nil {
  166. return err
  167. }
  168. printObject(result)
  169. return nil
  170. })
  171. R(&ResourceUsageOptions{}, "cloud-region-usage", "Show general usage of a cloud region", func(s *mcclient.ClientSession, args *ResourceUsageOptions) error {
  172. params := fetchHostTypeOptions(&args.GeneralUsageOptions)
  173. params.Add(jsonutils.NewString("cloudregions"), "range_type")
  174. params.Add(jsonutils.NewString(args.ID), "range_id")
  175. result, err := modules.Usages.GetGeneralUsage(s, params)
  176. if err != nil {
  177. return err
  178. }
  179. printObject(result)
  180. return nil
  181. })
  182. type ImageUsageOptions struct {
  183. Project string `help:"check image usage of a project"`
  184. Domain string `help:"check image usage of a domain"`
  185. Scope string `help:"query scope" choices:"project|domain|system"`
  186. }
  187. R(&ImageUsageOptions{}, "image-usage", "Show general usage of images", func(s *mcclient.ClientSession, args *ImageUsageOptions) error {
  188. params := jsonutils.NewDict()
  189. if args.Project != "" {
  190. params.Add(jsonutils.NewString(args.Project), "project")
  191. } else if args.Domain != "" {
  192. params.Add(jsonutils.NewString(args.Domain), "domain")
  193. }
  194. if args.Scope != "" {
  195. params.Add(jsonutils.NewString(args.Scope), "scope")
  196. }
  197. result, err := image.Images.GetUsage(s, params)
  198. if err != nil {
  199. return err
  200. }
  201. printObject(result)
  202. return nil
  203. })
  204. type IdentityUsageOptions struct {
  205. }
  206. R(&IdentityUsageOptions{}, "identity-usage", "Show general usage of identity", func(s *mcclient.ClientSession, args *IdentityUsageOptions) error {
  207. result, err := identity.IdentityUsages.GetUsage(s, nil)
  208. if err != nil {
  209. return err
  210. }
  211. printObject(result)
  212. return nil
  213. })
  214. type K8sUsageOptions struct{}
  215. R(&K8sUsageOptions{}, "k8s-usage", "Show general usage of k8s", func(s *mcclient.ClientSession, args *K8sUsageOptions) error {
  216. result, err := k8s.Usages.GetUsage(s, nil)
  217. if err != nil {
  218. return err
  219. }
  220. printObject(result)
  221. return nil
  222. })
  223. }