handler.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 usages
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/util/rbacscope"
  21. "yunion.io/x/onecloud/pkg/appsrv"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/image/models"
  26. "yunion.io/x/onecloud/pkg/mcclient/auth"
  27. "yunion.io/x/onecloud/pkg/util/rbacutils"
  28. )
  29. func AddUsageHandler(prefix string, app *appsrv.Application) {
  30. prefix = fmt.Sprintf("%s/usages", prefix)
  31. app.AddHandler2("GET", prefix, auth.Authenticate(ReportGeneralUsage), nil, "get_usage", nil)
  32. }
  33. func ReportGeneralUsage(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  34. _, query, _ := appsrv.FetchEnv(ctx, w, r)
  35. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  36. ownerId, scope, err, result := db.FetchUsageOwnerScope(ctx, userCred, query)
  37. if err != nil {
  38. httperrors.GeneralServerError(ctx, w, err)
  39. return
  40. }
  41. tags := rbacutils.SPolicyResult{Result: rbacutils.Allow}
  42. query.Unmarshal(&tags)
  43. result = result.Merge(tags)
  44. usages := jsonutils.NewDict()
  45. if scope == rbacscope.ScopeSystem {
  46. adminUsage := models.ImageManager.Usage(ctx, rbacscope.ScopeSystem, ownerId, "all", result)
  47. usages.Update(jsonutils.Marshal(adminUsage))
  48. adminUsage = models.GuestImageManager.Usage(ctx, rbacscope.ScopeSystem, ownerId, "all", result)
  49. usages.Update(jsonutils.Marshal(adminUsage))
  50. }
  51. if scope.HigherEqual(rbacscope.ScopeDomain) {
  52. domainUsage := models.ImageManager.Usage(ctx, rbacscope.ScopeDomain, ownerId, "domain", result)
  53. usages.Update(jsonutils.Marshal(domainUsage))
  54. domainUsage = models.GuestImageManager.Usage(ctx, rbacscope.ScopeDomain, ownerId, "domain", result)
  55. usages.Update(jsonutils.Marshal(domainUsage))
  56. }
  57. if scope.HigherEqual(rbacscope.ScopeProject) {
  58. projectUsage := models.ImageManager.Usage(ctx, rbacscope.ScopeProject, ownerId, "", result)
  59. usages.Update(jsonutils.Marshal(projectUsage))
  60. projectUsage = models.GuestImageManager.Usage(ctx, rbacscope.ScopeProject, ownerId, "", result)
  61. usages.Update(jsonutils.Marshal(projectUsage))
  62. }
  63. body := jsonutils.NewDict()
  64. body.Add(usages, "usage")
  65. appsrv.SendJSON(w, body)
  66. }