handler.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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 quotas
  15. import (
  16. "context"
  17. "database/sql"
  18. "fmt"
  19. "net/http"
  20. "reflect"
  21. "sort"
  22. "strings"
  23. "yunion.io/x/jsonutils"
  24. "yunion.io/x/log"
  25. "yunion.io/x/pkg/appctx"
  26. "yunion.io/x/pkg/errors"
  27. "yunion.io/x/pkg/util/rbacscope"
  28. "yunion.io/x/pkg/util/reflectutils"
  29. "yunion.io/x/onecloud/pkg/appsrv"
  30. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  31. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  32. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  33. "yunion.io/x/onecloud/pkg/httperrors"
  34. "yunion.io/x/onecloud/pkg/mcclient"
  35. "yunion.io/x/onecloud/pkg/mcclient/auth"
  36. )
  37. const (
  38. QUOTA_ACTION_ADD = "add"
  39. QUOTA_ACTION_SUB = "sub"
  40. QUOTA_ACTION_RESET = "reset"
  41. QUOTA_ACTION_REPLACE = "replace"
  42. QUOTA_ACTION_UPDATE = "update"
  43. QUOTA_ACTION_DELETE = "delete"
  44. )
  45. type SBaseQuotaSetInput struct {
  46. // 设置配额操作
  47. //
  48. // | action | 说明 |
  49. // |---------|-------------------------------------------|
  50. // | add | 增量增加配额 |
  51. // | sub | 增量减少配额 |
  52. // | reset | 重置所有配额为0 |
  53. // | replace | 替换所有配额,对于不存在的配额项,设置为0 |
  54. // | update | 更新存在的配额 |
  55. // | delete | 删除配额 |
  56. //
  57. Action string `json:"action"`
  58. }
  59. type SBaseQuotaQueryInput struct {
  60. // 只列出主配额
  61. // require:false
  62. Primary bool `json:"primary"`
  63. // 强制刷新使用量
  64. // require:false
  65. Refresh bool `json:"refresh"`
  66. }
  67. func AddQuotaHandler(manager *SQuotaBaseManager, prefix string, app *appsrv.Application, isSlave bool) {
  68. app.AddHandler2("GET",
  69. fmt.Sprintf("%s/%s", prefix, manager.KeywordPlural()),
  70. auth.Authenticate(manager.getQuotaHandler), nil, "get_quota", nil)
  71. app.AddHandler2("GET",
  72. fmt.Sprintf("%s/%s/domains", prefix, manager.KeywordPlural()),
  73. auth.Authenticate(manager.listDomainQuotaHandler), nil, "list_quotas_for_all_domains", nil)
  74. app.AddHandler2("GET",
  75. fmt.Sprintf("%s/%s/domains/<domainid>", prefix, manager.KeywordPlural()),
  76. auth.Authenticate(manager.getQuotaHandler), nil, "get_quota_for_domain", nil)
  77. if !isSlave {
  78. app.AddHandler2("POST",
  79. fmt.Sprintf("%s/%s", prefix, manager.KeywordPlural()),
  80. auth.Authenticate(manager.setQuotaHandler), nil, "set_quota", nil)
  81. app.AddHandler2("POST",
  82. fmt.Sprintf("%s/%s/domains/<domainid>", prefix, manager.KeywordPlural()),
  83. auth.Authenticate(manager.setQuotaHandler), nil, "set_quota_for_domain", nil)
  84. app.AddHandler2("DELETE",
  85. fmt.Sprintf("%s/%s/pending", prefix, manager.KeywordPlural()),
  86. auth.Authenticate(manager.cleanPendingUsageHandler), nil, "clean_pending_usage", nil)
  87. app.AddHandler2("DELETE",
  88. fmt.Sprintf("%s/%s/domains/<domainid>/pending", prefix, manager.KeywordPlural()),
  89. auth.Authenticate(manager.cleanPendingUsageHandler), nil, "clean_pending_usage_for_domain", nil)
  90. }
  91. if manager.scope == rbacscope.ScopeProject {
  92. app.AddHandler2("GET",
  93. fmt.Sprintf("%s/%s/<tenantid>", prefix, manager.KeywordPlural()),
  94. auth.Authenticate(manager.getQuotaHandler), nil, "get_quota_for_project", nil)
  95. app.AddHandler2("GET",
  96. fmt.Sprintf("%s/%s/projects", prefix, manager.KeywordPlural()),
  97. auth.Authenticate(manager.listProjectQuotaHandler), nil, "list_quotas_for_all_projects", nil)
  98. app.AddHandler2("GET",
  99. fmt.Sprintf("%s/%s/projects/<tenantid>", prefix, manager.KeywordPlural()),
  100. auth.Authenticate(manager.getQuotaHandler), nil, "get_quota_for_project", nil)
  101. if !isSlave {
  102. app.AddHandler2("POST",
  103. fmt.Sprintf("%s/%s/<tenantid>", prefix, manager.KeywordPlural()),
  104. auth.Authenticate(manager.setQuotaHandler), nil, "set_quota_for_project", nil)
  105. app.AddHandler2("POST",
  106. fmt.Sprintf("%s/%s/projects/<tenantid>", prefix, manager.KeywordPlural()),
  107. auth.Authenticate(manager.setQuotaHandler), nil, "set_quota_for_project", nil)
  108. app.AddHandler2("DELETE",
  109. fmt.Sprintf("%s/%s/projects/<tenantid>/pending", prefix, manager.KeywordPlural()),
  110. auth.Authenticate(manager.cleanPendingUsageHandler), nil, "clean_pending_usage_for_project", nil)
  111. }
  112. }
  113. }
  114. func (manager *SQuotaBaseManager) queryQuota(ctx context.Context, quota IQuota, refresh bool) (*jsonutils.JSONDict, error) {
  115. ret := jsonutils.NewDict()
  116. keys := quota.GetKeys()
  117. ret.Update(jsonutils.Marshal(keys))
  118. ret.Update(quota.ToJSON(""))
  119. if !consts.EnableQuotaCheck() {
  120. return ret, nil
  121. }
  122. usage := manager.newQuota()
  123. err := manager.usageStore.GetQuota(ctx, keys, usage)
  124. if err != nil {
  125. return nil, errors.Wrap(err, "manager.usageStore.GetQuota")
  126. }
  127. if refresh {
  128. usageChan := make(chan IQuota)
  129. manager.PostUsageJob(keys, usageChan, true)
  130. usage = <-usageChan
  131. }
  132. pendings, err := manager.GetPendingUsages(ctx, keys)
  133. if err != nil {
  134. return nil, errors.Wrap(err, "manager.GetPendingUsages")
  135. }
  136. if usage != nil {
  137. ret.Update(usage.ToJSON("usage"))
  138. }
  139. if len(pendings) > 0 {
  140. pendingArray := jsonutils.NewArray()
  141. for _, q := range pendings {
  142. pending := q.ToJSON("")
  143. pending.(*jsonutils.JSONDict).Update(jsonutils.Marshal(q.GetKeys()))
  144. pendingArray.Add(pending)
  145. }
  146. ret.Add(pendingArray, "pending")
  147. }
  148. return ret, nil
  149. }
  150. func (manager *SQuotaBaseManager) getQuotaHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  151. params, query, _ := appsrv.FetchEnv(ctx, w, r)
  152. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  153. var ownerId mcclient.IIdentityProvider
  154. var scope rbacscope.TRbacScope
  155. var err error
  156. projectId := params["<tenantid>"]
  157. domainId := params["<domainid>"]
  158. if len(projectId) > 0 || len(domainId) > 0 {
  159. data := jsonutils.NewDict()
  160. if len(domainId) > 0 {
  161. data.Add(jsonutils.NewString(domainId), "project_domain")
  162. } else if len(projectId) > 0 {
  163. data.Add(jsonutils.NewString(projectId), "project")
  164. }
  165. ownerId, scope, err, _ = db.FetchCheckQueryOwnerScope(ctx, userCred, data, manager.GetIQuotaManager(), policy.PolicyActionGet, true)
  166. if err != nil {
  167. httperrors.GeneralServerError(ctx, w, err)
  168. return
  169. }
  170. } else {
  171. scopeStr, _ := query.GetString("scope")
  172. if scopeStr == "project" && manager.scope == rbacscope.ScopeProject {
  173. scope = rbacscope.ScopeProject
  174. } else if scopeStr == "domain" {
  175. scope = rbacscope.ScopeDomain
  176. } else {
  177. scope = manager.scope
  178. }
  179. ownerId = userCred
  180. }
  181. keys := OwnerIdProjectQuotaKeys(scope, ownerId)
  182. refresh := jsonutils.QueryBoolean(query, "refresh", false)
  183. primary := jsonutils.QueryBoolean(query, "primary", false)
  184. quotaList, err := manager.listQuotas(ctx, userCred, keys.DomainId, keys.ProjectId, scope == rbacscope.ScopeDomain, primary, refresh)
  185. if err != nil {
  186. httperrors.GeneralServerError(ctx, w, err)
  187. return
  188. }
  189. if len(quotaList) == 0 {
  190. quota := manager.newQuota()
  191. var baseKeys IQuotaKeys
  192. if manager.scope == rbacscope.ScopeProject {
  193. baseKeys = OwnerIdProjectQuotaKeys(scope, ownerId)
  194. } else {
  195. baseKeys = OwnerIdDomainQuotaKeys(ownerId)
  196. }
  197. reflectutils.FillEmbededStructValue(reflect.Indirect(reflect.ValueOf(quota)), reflect.ValueOf(baseKeys))
  198. quota.FetchSystemQuota()
  199. manager.SetQuota(ctx, userCred, quota)
  200. quotaList, err = manager.listQuotas(ctx, userCred, keys.DomainId, keys.ProjectId, scope == rbacscope.ScopeDomain, primary, refresh)
  201. if err != nil {
  202. httperrors.GeneralServerError(ctx, w, err)
  203. return
  204. }
  205. }
  206. manager.sendQuotaList(w, quotaList)
  207. }
  208. func (manager *SQuotaBaseManager) fetchSetQuotaScope(
  209. ctx context.Context,
  210. userCred mcclient.TokenCredential,
  211. data jsonutils.JSONObject,
  212. isBaseQuotaKeys bool,
  213. ) (
  214. mcclient.IIdentityProvider,
  215. rbacscope.TRbacScope,
  216. rbacscope.TRbacScope,
  217. error,
  218. ) {
  219. var scope rbacscope.TRbacScope
  220. ownerId, err := db.FetchProjectInfo(ctx, data)
  221. if err != nil {
  222. return nil, scope, scope, err
  223. }
  224. var requestScope rbacscope.TRbacScope
  225. if ownerId != nil {
  226. if len(ownerId.GetProjectId()) > 0 {
  227. // project level
  228. scope = rbacscope.ScopeProject
  229. if ownerId.GetProjectDomainId() == userCred.GetProjectDomainId() {
  230. if isBaseQuotaKeys || ownerId.GetProjectId() != userCred.GetProjectId() {
  231. requestScope = rbacscope.ScopeDomain
  232. } else {
  233. requestScope = rbacscope.ScopeProject
  234. }
  235. } else {
  236. requestScope = rbacscope.ScopeSystem
  237. }
  238. } else {
  239. // domain level if len(ownerId.GetProjectDomainId()) > 0 {
  240. scope = rbacscope.ScopeDomain
  241. if isBaseQuotaKeys || ownerId.GetProjectDomainId() != userCred.GetProjectDomainId() {
  242. requestScope = rbacscope.ScopeSystem
  243. } else {
  244. requestScope = rbacscope.ScopeDomain
  245. }
  246. }
  247. } else {
  248. ownerId = userCred
  249. scope = rbacscope.ScopeProject
  250. if isBaseQuotaKeys {
  251. requestScope = rbacscope.ScopeDomain
  252. } else {
  253. requestScope = rbacscope.ScopeProject
  254. }
  255. }
  256. return ownerId, scope, requestScope, nil
  257. }
  258. func (manager *SQuotaBaseManager) cleanPendingUsageHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  259. params, query, _ := appsrv.FetchEnv(ctx, w, r)
  260. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  261. var ownerId mcclient.IIdentityProvider
  262. var scope rbacscope.TRbacScope
  263. var err error
  264. projectId := params["<tenantid>"]
  265. domainId := params["<domainid>"]
  266. if len(projectId) > 0 || len(domainId) > 0 {
  267. data := jsonutils.NewDict()
  268. if len(domainId) > 0 {
  269. data.Add(jsonutils.NewString(domainId), "project_domain")
  270. } else if len(projectId) > 0 {
  271. data.Add(jsonutils.NewString(projectId), "project")
  272. }
  273. ownerId, scope, err, _ = db.FetchCheckQueryOwnerScope(ctx, userCred, data, manager, policy.PolicyActionGet, true)
  274. if err != nil {
  275. httperrors.GeneralServerError(ctx, w, err)
  276. return
  277. }
  278. } else {
  279. scopeStr, _ := query.GetString("scope")
  280. if scopeStr == "project" {
  281. scope = rbacscope.ScopeProject
  282. } else if scopeStr == "domain" {
  283. scope = rbacscope.ScopeDomain
  284. } else {
  285. scope = manager.scope
  286. }
  287. ownerId = userCred
  288. }
  289. var keys IQuotaKeys
  290. if manager.scope == rbacscope.ScopeProject {
  291. keys = OwnerIdProjectQuotaKeys(scope, ownerId)
  292. } else {
  293. keys = OwnerIdDomainQuotaKeys(ownerId)
  294. }
  295. err = manager.cleanPendingUsage(ctx, userCred, keys)
  296. if err != nil {
  297. httperrors.GeneralServerError(ctx, w, err)
  298. return
  299. }
  300. rbody := jsonutils.NewDict()
  301. appsrv.SendJSON(w, rbody)
  302. }
  303. func (manager *SQuotaBaseManager) setQuotaHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  304. params, _, body := appsrv.FetchEnv(ctx, w, r)
  305. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  306. projectId := params["<tenantid>"]
  307. domainId := params["<domainid>"]
  308. data := jsonutils.NewDict()
  309. if len(projectId) > 0 {
  310. data.Add(jsonutils.NewString(projectId), "project")
  311. } else if len(domainId) > 0 {
  312. data.Add(jsonutils.NewString(domainId), "project_domain")
  313. }
  314. quota := manager.newQuota()
  315. err := body.Unmarshal(quota, manager.KeywordPlural())
  316. if err != nil {
  317. log.Errorf("Fail to decode JSON request body: %s", err)
  318. httperrors.InvalidInputError(appctx.WithRequestLang(ctx, r), w, "fail to decode body")
  319. return
  320. }
  321. // check is there any nonempty key other than domain_id and project_id
  322. isBaseQuota := false
  323. if manager.scope == rbacscope.ScopeDomain {
  324. isBaseQuota = IsBaseDomainQuotaKeys(quota.GetKeys())
  325. } else {
  326. isBaseQuota = IsBaseProjectQuotaKeys(quota.GetKeys())
  327. }
  328. ownerId, scope, requestScope, err := manager.fetchSetQuotaScope(ctx, userCred, data, isBaseQuota)
  329. if err != nil {
  330. httperrors.GeneralServerError(ctx, w, err)
  331. return
  332. }
  333. // fill project_id and domain_id
  334. var baseKeys IQuotaKeys
  335. if manager.scope == rbacscope.ScopeDomain {
  336. baseKeys = OwnerIdDomainQuotaKeys(ownerId)
  337. } else {
  338. baseKeys = OwnerIdProjectQuotaKeys(scope, ownerId)
  339. }
  340. reflectutils.FillEmbededStructValue(reflect.Indirect(reflect.ValueOf(quota)), reflect.ValueOf(baseKeys))
  341. keys := quota.GetKeys()
  342. isNew := false
  343. oquota := manager.newQuota()
  344. oquota.SetKeys(keys)
  345. err = manager.getQuotaByKeys(ctx, keys, oquota)
  346. if err != nil {
  347. if errors.Cause(err) != sql.ErrNoRows {
  348. log.Errorf("get quota %s fail %s", QuotaKeyString(keys), err)
  349. httperrors.GeneralServerError(ctx, w, err)
  350. return
  351. } else {
  352. isNew = true
  353. }
  354. }
  355. action, _ := body.GetString(manager.KeywordPlural(), "action")
  356. var policyAction string
  357. if action == QUOTA_ACTION_DELETE {
  358. if isNew {
  359. // no need to delete
  360. httperrors.NotFoundError(ctx, w, "Quota %s not found", QuotaKeyString(keys))
  361. return
  362. } else if isBaseQuota {
  363. // base quota is not deletable
  364. httperrors.ForbiddenError(ctx, w, "Default quota %s not allow to delete", QuotaKeyString(keys))
  365. return
  366. }
  367. policyAction = policy.PolicyActionDelete
  368. } else {
  369. if isNew {
  370. policyAction = policy.PolicyActionCreate
  371. } else {
  372. policyAction = policy.PolicyActionUpdate
  373. }
  374. }
  375. log.Debugf("is_new: %v action: %s origin: %s current: %s", isNew, action, jsonutils.Marshal(oquota), jsonutils.Marshal(quota))
  376. // check rbac policy
  377. ownerScope, policyResult := policy.PolicyManager.AllowScope(userCred, consts.GetServiceType(), manager.KeywordPlural(), policyAction)
  378. if policyResult.Result.IsAllow() && requestScope.HigherThan(ownerScope) {
  379. httperrors.ForbiddenError(ctx, w, "not enough privilleges")
  380. return
  381. }
  382. if action == QUOTA_ACTION_DELETE {
  383. err = manager.DeleteQuota(ctx, userCred, keys)
  384. if err != nil {
  385. httperrors.GeneralServerError(ctx, w, err)
  386. return
  387. }
  388. } else {
  389. switch action {
  390. case QUOTA_ACTION_ADD:
  391. oquota.Add(quota)
  392. case QUOTA_ACTION_SUB:
  393. oquota.Sub(quota)
  394. case QUOTA_ACTION_RESET:
  395. oquota.FetchSystemQuota()
  396. case QUOTA_ACTION_REPLACE:
  397. oquota = quota
  398. case QUOTA_ACTION_UPDATE:
  399. fallthrough
  400. default:
  401. oquota.Update(quota)
  402. }
  403. log.Debugf("To set %s", jsonutils.Marshal(oquota))
  404. err = manager.SetQuota(ctx, userCred, oquota)
  405. if err != nil {
  406. log.Errorf("set quota fail %s", err)
  407. httperrors.GeneralServerError(ctx, w, err)
  408. return
  409. }
  410. }
  411. quotaList, err := manager.listQuotas(ctx, userCred, baseKeys.OwnerId().GetProjectDomainId(), baseKeys.OwnerId().GetProjectId(), scope == rbacscope.ScopeDomain, false, true)
  412. if err != nil {
  413. httperrors.GeneralServerError(ctx, w, err)
  414. return
  415. }
  416. manager.sendQuotaList(w, quotaList)
  417. }
  418. func (manager *SQuotaBaseManager) listDomainQuotaHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  419. _, query, _ := appsrv.FetchEnv(ctx, w, r)
  420. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  421. allowScope, policyResult := policy.PolicyManager.AllowScope(userCred, consts.GetServiceType(), manager.KeywordPlural(), policy.PolicyActionList)
  422. if policyResult.Result.IsAllow() && allowScope != rbacscope.ScopeSystem {
  423. httperrors.ForbiddenError(ctx, w, "not allow to list domain quotas")
  424. return
  425. }
  426. refresh := jsonutils.QueryBoolean(query, "refresh", false)
  427. primary := jsonutils.QueryBoolean(query, "primary", false)
  428. quotaList, err := manager.listQuotas(ctx, userCred, "", "", false, primary, refresh)
  429. if err != nil {
  430. httperrors.GeneralServerError(ctx, w, err)
  431. return
  432. }
  433. manager.sendQuotaList(w, sortQuotaByUsage(quotaList))
  434. }
  435. func (manager *SQuotaBaseManager) sendQuotaList(w http.ResponseWriter, quotaList []jsonutils.JSONObject) {
  436. rbody := jsonutils.NewDict()
  437. rbody.Set(manager.KeywordPlural(), jsonutils.NewArray(quotaList...))
  438. appsrv.SendJSON(w, rbody)
  439. }
  440. func (manager *SQuotaBaseManager) listProjectQuotaHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  441. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  442. _, query, _ := appsrv.FetchEnv(ctx, w, r)
  443. owner, err := db.FetchDomainInfo(ctx, query)
  444. if err != nil {
  445. httperrors.GeneralServerError(ctx, w, err)
  446. return
  447. }
  448. if owner == nil {
  449. owner = userCred
  450. }
  451. allowScope, _ := policy.PolicyManager.AllowScope(userCred, consts.GetServiceType(), manager.KeywordPlural(), policy.PolicyActionList)
  452. if (allowScope == rbacscope.ScopeDomain && userCred.GetProjectDomainId() == owner.GetProjectDomainId()) || allowScope == rbacscope.ScopeSystem {
  453. } else {
  454. httperrors.ForbiddenError(ctx, w, "not allow to list project quotas")
  455. return
  456. }
  457. domainId := owner.GetProjectDomainId()
  458. refresh := jsonutils.QueryBoolean(query, "refresh", false)
  459. primary := jsonutils.QueryBoolean(query, "primary", false)
  460. quotaList, err := manager.listQuotas(ctx, userCred, domainId, "", false, primary, refresh)
  461. if err != nil {
  462. httperrors.GeneralServerError(ctx, w, err)
  463. return
  464. }
  465. manager.sendQuotaList(w, sortQuotaByUsage(quotaList))
  466. }
  467. func (manager *SQuotaBaseManager) listQuotas(ctx context.Context, userCred mcclient.TokenCredential, targetDomainId string, targetProjectId string, domainOnly bool, primaryOnly bool, refresh bool) ([]jsonutils.JSONObject, error) {
  468. q := manager.Query()
  469. if len(targetDomainId) > 0 {
  470. q = q.Equals("domain_id", targetDomainId)
  471. if domainOnly {
  472. if manager.scope == rbacscope.ScopeProject {
  473. q = q.IsEmpty("tenant_id")
  474. }
  475. } else {
  476. if len(targetProjectId) > 0 {
  477. q = q.Equals("tenant_id", targetProjectId)
  478. }
  479. // otherwise, list all projects quotas for a domain
  480. // also include the domain's quota
  481. // else {
  482. // q = q.IsNotEmpty("tenant_id")
  483. // }
  484. }
  485. } else {
  486. // domain only
  487. q = q.IsNotEmpty("domain_id")
  488. if manager.scope == rbacscope.ScopeProject {
  489. q = q.IsNullOrEmpty("tenant_id")
  490. }
  491. }
  492. if primaryOnly {
  493. // list primary quota for domain or project
  494. fields := manager.getQuotaFields()
  495. for _, f := range fields {
  496. if f != "domain_id" && f != "tenant_id" {
  497. q = q.IsNullOrEmpty(f)
  498. }
  499. }
  500. }
  501. rows, err := q.Rows()
  502. if err != nil {
  503. if errors.Cause(err) != sql.ErrNoRows {
  504. return nil, httperrors.NewInternalServerError("query quotas %s", err)
  505. } else {
  506. return nil, nil
  507. }
  508. }
  509. defer rows.Close()
  510. quotaList := make([]IQuota, 0)
  511. keyList := make([]IQuotaKeys, 0)
  512. ret := make([]jsonutils.JSONObject, 0)
  513. for rows.Next() {
  514. quota := manager.newQuota()
  515. err := q.Row2Struct(rows, quota)
  516. if err != nil {
  517. return nil, errors.Wrap(err, "q.Row2Struct")
  518. }
  519. quotaList = append(quotaList, quota)
  520. keyList = append(keyList, quota.GetKeys())
  521. }
  522. idNameMap, err := manager.keyList2IdNameMap(ctx, keyList)
  523. var fields []string
  524. for i := range quotaList {
  525. keys := quotaList[i].GetKeys()
  526. keyJson := jsonutils.Marshal(keys).(*jsonutils.JSONDict)
  527. if idNameMap != nil {
  528. // no error, do check
  529. if len(fields) == 0 {
  530. fields = keys.Fields()
  531. }
  532. values := keys.Values()
  533. for i := range fields {
  534. if strings.HasSuffix(fields[i], "_id") && len(values[i]) > 0 {
  535. if len(idNameMap[fields[i]][values[i]]) == 0 {
  536. manager.DeleteAllQuotas(ctx, userCred, keys)
  537. manager.pendingStore.DeleteAllQuotas(ctx, userCred, keys)
  538. manager.usageStore.DeleteAllQuotas(ctx, userCred, keys)
  539. continue
  540. } else {
  541. keyJson.Add(jsonutils.NewString(idNameMap[fields[i]][values[i]]), fields[i][:len(fields[i])-3])
  542. }
  543. }
  544. }
  545. }
  546. quotaJson, err := manager.queryQuota(ctx, quotaList[i], refresh)
  547. if err != nil {
  548. return nil, errors.Wrap(err, "manager.queryQuota")
  549. }
  550. quotaJson.Update(keyJson)
  551. ret = append(ret, quotaJson)
  552. }
  553. return ret, nil
  554. }
  555. type tQuotaResultList []jsonutils.JSONObject
  556. func (a tQuotaResultList) Len() int { return len(a) }
  557. func (a tQuotaResultList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  558. func (a tQuotaResultList) Less(i, j int) bool { return usageRateOfQuota(a[i]) > usageRateOfQuota(a[j]) }
  559. func usageRateOfQuota(quota jsonutils.JSONObject) float32 {
  560. maxRate := float32(0)
  561. quotaMap, _ := quota.GetMap()
  562. for k, v := range quotaMap {
  563. usageK := fmt.Sprintf("usage.%s", k)
  564. if usageV, ok := quotaMap[usageK]; ok {
  565. intV, _ := v.Int()
  566. if intV > 0 {
  567. intUsageV, _ := usageV.Int()
  568. rate := float32(intUsageV) / float32(intV)
  569. if maxRate < rate {
  570. maxRate = rate
  571. }
  572. }
  573. }
  574. }
  575. return maxRate
  576. }
  577. func sortQuotaByUsage(quotaList []jsonutils.JSONObject) []jsonutils.JSONObject {
  578. sort.Sort(tQuotaResultList(quotaList))
  579. return quotaList
  580. }