quotas.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 quota
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/log"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/quota"
  21. )
  22. type ComputeQuotaKeys struct {
  23. ZoneQuotaKeys
  24. Hypervisor string `help:"hypervisor" choices:"kvm|baremetal"`
  25. }
  26. type ComputeQuotaOptions struct {
  27. ComputeQuotaKeys
  28. Count int64 `help:"server count" json:"count,omitzero"`
  29. Cpu int64 `help:"CPU count" json:"cpu,omitzero"`
  30. Memory int64 `help:"Memory size in MB" json:"memory,omitzero"`
  31. Storage int64 `help:"Storage size in MB" json:"storage,omitzero"`
  32. IsolatedDevice int64 `help:"Isolated device count" json:"isolated_device,omitzero"`
  33. }
  34. type RegionQuotaKeys struct {
  35. Provider string `help:"cloud provider" json:"provider,omitempty"`
  36. Brand string `help:"cloud brand" json:"brand,omitempty"`
  37. CloudEnv string `help:"cloud environment" json:"cloud_env,omitempty" choices:"onpremise|private|public"`
  38. AccountId string `help:"cloud account id" json:"account_id,omitempty"`
  39. ManagerId string `help:"cloud provider id" json:"manager_id,omitempty"`
  40. RegionId string `help:"region id" json:"region_id,omitempty"`
  41. }
  42. type RegionQuotaOptions struct {
  43. RegionQuotaKeys
  44. Port int64 `help:"Internal NIC count" json:"port,omitzero"`
  45. Eport int64 `help:"External NIC count" json:"eport,omitzero"`
  46. // Bw int64 `help:"Internal bandwidth in Mbps" json:"bw,omitzero"`
  47. // Ebw int64 `help:"External bandwidth in Mbps" json:"ebw,omitzero"`
  48. Eip int64 `help:"Elastic IP count" json:"eip,omitzero"`
  49. Snapshot int64 `help:"Snapshot count" json:"snapshot,omitzero"`
  50. InstanceSnapshot int64 `help:"Instance snapshot count" json:"instance_snapshot,omitzero"`
  51. Bucket int64 `help:"bucket count" json:"bucket,omitzero"`
  52. ObjectGB int64 `help:"object size in GB" json:"object_gb,omitzero"`
  53. ObjectCnt int64 `help:"object count" json:"object_cnt,omitzero"`
  54. Rds int64 `help:"Rds count" json:"rds,omitzero"`
  55. Cache int64 `help:"redis count" json:"cache,omitzero"`
  56. Loadbalancer int64 `help:"loadbalancer instance count" json:"loadbalancer,omitzero"`
  57. }
  58. type ZoneQuotaKeys struct {
  59. RegionQuotaKeys
  60. ZoneId string `help:"zone id" json:"zone_id,omitempty"`
  61. }
  62. type ZoneQuotaOptions struct {
  63. ZoneQuotaKeys
  64. }
  65. type ProjectQuotaOptions struct {
  66. Secgroup int64 `help:"Secgroup count" json:"secgroup,omitzero"`
  67. }
  68. type ImageQuotaKeys struct {
  69. Type string `help:"image type, either iso or image" choices:"iso|image" json:"type,omitempty"`
  70. }
  71. type ImageQuotaOptions struct {
  72. ImageQuotaKeys
  73. Image int64 `help:"Template count" json:"image,omitzero"`
  74. }
  75. type DomainQuotaOptions struct {
  76. Cloudaccount int64 `help:"cloudaccount count" json:"cloudaccount,omitzero"`
  77. }
  78. type InfrasQuotaOptions struct {
  79. RegionQuotaKeys
  80. Host int64 `help:"host count" json:"host,omitzero"`
  81. Vpc int64 `help:"vpc count" json:"vpc,omitzero"`
  82. }
  83. type IdentityQuotaOptions struct {
  84. User int64 `help:"user count" json:"user,omitzero"`
  85. Group int64 `help:"group count" json:"group,omitzero"`
  86. Project int64 `help:"project count" json:"project,omitzero"`
  87. Role int64 `help:"role count" json:"role,omitzero"`
  88. Policy int64 `help:"policy count" json:"policy,omitzero"`
  89. }
  90. type QuotaSetBaseOptions struct {
  91. Project string `help:"Tenant name or ID to set quota" json:"tenant,omitempty"`
  92. Domain string `help:"Domain name or ID to set quota" json:"domain,omitempty"`
  93. Action string `help:"quota set action" choices:"add|sub|reset|replace|delete|update"`
  94. }
  95. func printQuotaList(result jsonutils.JSONObject) {
  96. printList(modulebase.JSON2ListResult(result), nil)
  97. }
  98. func init() {
  99. type QuotaOptions struct {
  100. Scope string `help:"scope" choices:"domain|project"`
  101. Project string `help:"Tenant name or ID" json:"tenant"`
  102. Domain string `help:"Domain name or ID" json:"domain"`
  103. Refresh bool `help:"refresh" json:"refresh,omitfalse"`
  104. Primary bool `help:"get primary quota ONLY" json:"primary,omitfalse"`
  105. }
  106. R(&QuotaOptions{}, "quota", "Show quota for current user or tenant", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  107. params := jsonutils.Marshal(args)
  108. log.Debugf("%s", params)
  109. quotas, err := modules.Quotas.GetQuota(s, params)
  110. if err != nil {
  111. return err
  112. }
  113. printQuotaList(quotas)
  114. return nil
  115. })
  116. R(&QuotaOptions{}, "project-quota", "Show project-quota for current user or tenant", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  117. params := jsonutils.Marshal(args)
  118. quotas, err := modules.ProjectQuotas.GetQuota(s, params)
  119. if err != nil {
  120. return err
  121. }
  122. printQuotaList(quotas)
  123. return nil
  124. })
  125. R(&QuotaOptions{}, "region-quota", "Show region-quota for current user or tenant", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  126. params := jsonutils.Marshal(args)
  127. quotas, err := modules.RegionQuotas.GetQuota(s, params)
  128. if err != nil {
  129. return err
  130. }
  131. printQuotaList(quotas)
  132. return nil
  133. })
  134. R(&QuotaOptions{}, "zone-quota", "Show zone-quota for current user or tenant", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  135. params := jsonutils.Marshal(args)
  136. quotas, err := modules.ZoneQuotas.GetQuota(s, params)
  137. if err != nil {
  138. return err
  139. }
  140. printQuotaList(quotas)
  141. return nil
  142. })
  143. R(&QuotaOptions{}, "project-quota", "Show project-quota for current user or tenant", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  144. params := jsonutils.Marshal(args)
  145. result, err := modules.ProjectQuotas.GetQuota(s, params)
  146. if err != nil {
  147. return err
  148. }
  149. printQuotaList(result)
  150. return nil
  151. })
  152. R(&QuotaOptions{}, "image-quota", "Show image quota for current user or tenant", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  153. params := jsonutils.Marshal(args)
  154. quotas, err := modules.ImageQuotas.GetQuota(s, params)
  155. if err != nil {
  156. return err
  157. }
  158. printQuotaList(quotas)
  159. return nil
  160. })
  161. R(&QuotaOptions{}, "domain-quota", "Show domain-quota for current domain", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  162. params := jsonutils.Marshal(args)
  163. result, err := modules.DomainQuotas.GetQuota(s, params)
  164. if err != nil {
  165. return err
  166. }
  167. printQuotaList(result)
  168. return nil
  169. })
  170. R(&QuotaOptions{}, "infras-quota", "Show infras-quota for current domain", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  171. params := jsonutils.Marshal(args)
  172. result, err := modules.InfrasQuotas.GetQuota(s, params)
  173. if err != nil {
  174. return err
  175. }
  176. printQuotaList(result)
  177. return nil
  178. })
  179. R(&QuotaOptions{}, "identity-quota", "Show identity-quota for current domain", func(s *mcclient.ClientSession, args *QuotaOptions) error {
  180. params := jsonutils.Marshal(args)
  181. result, err := modules.IdentityQuotas.GetQuota(s, params)
  182. if err != nil {
  183. return err
  184. }
  185. printQuotaList(result)
  186. return nil
  187. })
  188. type ComputeQuotaSetOptions struct {
  189. QuotaSetBaseOptions
  190. ComputeQuotaOptions
  191. }
  192. R(&ComputeQuotaSetOptions{}, "quota-set", "Set quota for tenant", func(s *mcclient.ClientSession, args *ComputeQuotaSetOptions) error {
  193. params := jsonutils.Marshal(args)
  194. quotas, e := modules.Quotas.DoQuotaSet(s, params)
  195. if e != nil {
  196. return e
  197. }
  198. printQuotaList(quotas)
  199. return nil
  200. })
  201. type RegionQuotaSetOptions struct {
  202. QuotaSetBaseOptions
  203. RegionQuotaOptions
  204. }
  205. R(&RegionQuotaSetOptions{}, "region-quota-set", "Set regional quota for tenant", func(s *mcclient.ClientSession, args *RegionQuotaSetOptions) error {
  206. params := jsonutils.Marshal(args)
  207. quotas, e := modules.RegionQuotas.DoQuotaSet(s, params)
  208. if e != nil {
  209. return e
  210. }
  211. printQuotaList(quotas)
  212. return nil
  213. })
  214. type ZoneQuotaSetOptions struct {
  215. QuotaSetBaseOptions
  216. ZoneQuotaOptions
  217. }
  218. R(&ZoneQuotaSetOptions{}, "zone-quota-set", "Set zonal quota for tenant", func(s *mcclient.ClientSession, args *ZoneQuotaSetOptions) error {
  219. params := jsonutils.Marshal(args)
  220. quotas, e := modules.ZoneQuotas.DoQuotaSet(s, params)
  221. if e != nil {
  222. return e
  223. }
  224. printQuotaList(quotas)
  225. return nil
  226. })
  227. type ProjectQuotaSetOptions struct {
  228. QuotaSetBaseOptions
  229. ProjectQuotaOptions
  230. }
  231. R(&ProjectQuotaSetOptions{}, "project-quota-set", "Set project quota for tenant", func(s *mcclient.ClientSession, args *ProjectQuotaSetOptions) error {
  232. params := jsonutils.Marshal(args)
  233. quotas, e := modules.ProjectQuotas.DoQuotaSet(s, params)
  234. if e != nil {
  235. return e
  236. }
  237. printQuotaList(quotas)
  238. return nil
  239. })
  240. type ImageQuotaSetOptions struct {
  241. QuotaSetBaseOptions
  242. ImageQuotaOptions
  243. }
  244. R(&ImageQuotaSetOptions{}, "image-quota-set", "Set image quota for tenant", func(s *mcclient.ClientSession, args *ImageQuotaSetOptions) error {
  245. params := jsonutils.Marshal(args)
  246. quotas, e := modules.ImageQuotas.DoQuotaSet(s, params)
  247. if e != nil {
  248. return e
  249. }
  250. printQuotaList(quotas)
  251. return nil
  252. })
  253. type DomainQuotaSetOptions struct {
  254. QuotaSetBaseOptions
  255. DomainQuotaOptions
  256. }
  257. R(&DomainQuotaSetOptions{}, "domain-quota-set", "Set domain quota for domain", func(s *mcclient.ClientSession, args *DomainQuotaSetOptions) error {
  258. params := jsonutils.Marshal(args)
  259. quotas, e := modules.DomainQuotas.DoQuotaSet(s, params)
  260. if e != nil {
  261. return e
  262. }
  263. printQuotaList(quotas)
  264. return nil
  265. })
  266. type InfrasQuotaSetOptions struct {
  267. QuotaSetBaseOptions
  268. InfrasQuotaOptions
  269. }
  270. R(&InfrasQuotaSetOptions{}, "infras-quota-set", "Set infrastructure quota for domain", func(s *mcclient.ClientSession, args *InfrasQuotaSetOptions) error {
  271. params := jsonutils.Marshal(args)
  272. quotas, e := modules.InfrasQuotas.DoQuotaSet(s, params)
  273. if e != nil {
  274. return e
  275. }
  276. printQuotaList(quotas)
  277. return nil
  278. })
  279. type IdentityQuotaSetOptions struct {
  280. QuotaSetBaseOptions
  281. IdentityQuotaOptions
  282. }
  283. R(&IdentityQuotaSetOptions{}, "identity-quota-set", "Set identity quota for domain", func(s *mcclient.ClientSession, args *IdentityQuotaSetOptions) error {
  284. params := jsonutils.Marshal(args)
  285. quotas, e := modules.IdentityQuotas.DoQuotaSet(s, params)
  286. if e != nil {
  287. return e
  288. }
  289. printQuotaList(quotas)
  290. return nil
  291. })
  292. type QuotaListOptions struct {
  293. ProjectDomain string `help:"domain name or ID to query project quotas"`
  294. Refresh bool `help:"refresh" json:"refresh,omitfalse"`
  295. Primary bool `help:"list primary quota ONLY" json:"primary,omitfalse"`
  296. }
  297. R(&QuotaListOptions{}, "quota-list", "List quota of domains or projects of a domain", func(s *mcclient.ClientSession, args *QuotaListOptions) error {
  298. params := jsonutils.Marshal(args)
  299. result, e := modules.Quotas.GetQuotaList(s, params)
  300. if e != nil {
  301. return e
  302. }
  303. printQuotaList(result)
  304. return nil
  305. })
  306. R(&QuotaListOptions{}, "region-quota-list", "List region quota of domains or projects of a domain", func(s *mcclient.ClientSession, args *QuotaListOptions) error {
  307. params := jsonutils.Marshal(args)
  308. result, e := modules.RegionQuotas.GetQuotaList(s, params)
  309. if e != nil {
  310. return e
  311. }
  312. printQuotaList(result)
  313. return nil
  314. })
  315. R(&QuotaListOptions{}, "zone-quota-list", "List zone quota of domains or projects of a domain", func(s *mcclient.ClientSession, args *QuotaListOptions) error {
  316. params := jsonutils.Marshal(args)
  317. result, e := modules.ZoneQuotas.GetQuotaList(s, params)
  318. if e != nil {
  319. return e
  320. }
  321. printQuotaList(result)
  322. return nil
  323. })
  324. R(&QuotaListOptions{}, "project-quota-list", "List project quota of domains or projects of a domain", func(s *mcclient.ClientSession, args *QuotaListOptions) error {
  325. params := jsonutils.Marshal(args)
  326. result, e := modules.ProjectQuotas.GetQuotaList(s, params)
  327. if e != nil {
  328. return e
  329. }
  330. printQuotaList(result)
  331. return nil
  332. })
  333. R(&QuotaListOptions{}, "image-quota-list", "List image quota of domains or projects of a domain", func(s *mcclient.ClientSession, args *QuotaListOptions) error {
  334. params := jsonutils.Marshal(args)
  335. result, e := modules.ImageQuotas.GetQuotaList(s, params)
  336. if e != nil {
  337. return e
  338. }
  339. printQuotaList(result)
  340. return nil
  341. })
  342. R(&QuotaListOptions{}, "domain-quota-list", "List domain quota of domains", func(s *mcclient.ClientSession, args *QuotaListOptions) error {
  343. params := jsonutils.Marshal(args)
  344. result, e := modules.DomainQuotas.GetQuotaList(s, params)
  345. if e != nil {
  346. return e
  347. }
  348. printQuotaList(result)
  349. return nil
  350. })
  351. R(&QuotaListOptions{}, "infras-quota-list", "List infrastructure quota of domains", func(s *mcclient.ClientSession, args *QuotaListOptions) error {
  352. params := jsonutils.Marshal(args)
  353. result, e := modules.InfrasQuotas.GetQuotaList(s, params)
  354. if e != nil {
  355. return e
  356. }
  357. printQuotaList(result)
  358. return nil
  359. })
  360. R(&QuotaListOptions{}, "identity-quota-list", "List identity quota of domains", func(s *mcclient.ClientSession, args *QuotaListOptions) error {
  361. params := jsonutils.Marshal(args)
  362. result, e := modules.IdentityQuotas.GetQuotaList(s, params)
  363. if e != nil {
  364. return e
  365. }
  366. printQuotaList(result)
  367. return nil
  368. })
  369. type CleanPendingUsageOptions struct {
  370. Scope string `help:"scope" choices:"domain|project"`
  371. Project string `help:"Tenant name or ID" json:"tenant"`
  372. Domain string `help:"Domain name or ID" json:"domain"`
  373. }
  374. R(&CleanPendingUsageOptions{}, "clean-pending-usage", "Clean pending usage for project or domain", func(s *mcclient.ClientSession, args *CleanPendingUsageOptions) error {
  375. params := jsonutils.Marshal(args)
  376. _, err := modules.Quotas.DoCleanPendingUsage(s, params)
  377. if err != nil {
  378. return err
  379. }
  380. return nil
  381. })
  382. R(&CleanPendingUsageOptions{}, "clean-region-pending-usage", "Clean pending usage for project or domain", func(s *mcclient.ClientSession, args *CleanPendingUsageOptions) error {
  383. params := jsonutils.Marshal(args)
  384. _, err := modules.RegionQuotas.DoCleanPendingUsage(s, params)
  385. if err != nil {
  386. return err
  387. }
  388. return nil
  389. })
  390. R(&CleanPendingUsageOptions{}, "clean-zone-pending-usage", "Clean pending usage for project or domain", func(s *mcclient.ClientSession, args *CleanPendingUsageOptions) error {
  391. params := jsonutils.Marshal(args)
  392. _, err := modules.ZoneQuotas.DoCleanPendingUsage(s, params)
  393. if err != nil {
  394. return err
  395. }
  396. return nil
  397. })
  398. R(&CleanPendingUsageOptions{}, "clean-project-pending-usage", "Clean pending usage for project or domain", func(s *mcclient.ClientSession, args *CleanPendingUsageOptions) error {
  399. params := jsonutils.Marshal(args)
  400. _, err := modules.ProjectQuotas.DoCleanPendingUsage(s, params)
  401. if err != nil {
  402. return err
  403. }
  404. return nil
  405. })
  406. R(&CleanPendingUsageOptions{}, "clean-image-pending-usage", "Clean pending usage for project or domain", func(s *mcclient.ClientSession, args *CleanPendingUsageOptions) error {
  407. params := jsonutils.Marshal(args)
  408. _, err := modules.ImageQuotas.DoCleanPendingUsage(s, params)
  409. if err != nil {
  410. return err
  411. }
  412. return nil
  413. })
  414. R(&CleanPendingUsageOptions{}, "clean-domain-pending-usage", "Clean pending usage for project or domain", func(s *mcclient.ClientSession, args *CleanPendingUsageOptions) error {
  415. params := jsonutils.Marshal(args)
  416. _, err := modules.DomainQuotas.DoCleanPendingUsage(s, params)
  417. if err != nil {
  418. return err
  419. }
  420. return nil
  421. })
  422. R(&CleanPendingUsageOptions{}, "clean-infras-pending-usage", "Clean pending usage for project or domain", func(s *mcclient.ClientSession, args *CleanPendingUsageOptions) error {
  423. params := jsonutils.Marshal(args)
  424. _, err := modules.InfrasQuotas.DoCleanPendingUsage(s, params)
  425. if err != nil {
  426. return err
  427. }
  428. return nil
  429. })
  430. R(&CleanPendingUsageOptions{}, "clean-identity-pending-usage", "Clean pending usage for project or domain", func(s *mcclient.ClientSession, args *CleanPendingUsageOptions) error {
  431. params := jsonutils.Marshal(args)
  432. _, err := modules.IdentityQuotas.DoCleanPendingUsage(s, params)
  433. if err != nil {
  434. return err
  435. }
  436. return nil
  437. })
  438. }