handler.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/consts"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. "yunion.io/x/onecloud/pkg/keystone/models"
  27. "yunion.io/x/onecloud/pkg/mcclient/auth"
  28. "yunion.io/x/onecloud/pkg/util/tagutils"
  29. )
  30. func AddUsageHandler(prefix string, app *appsrv.Application) {
  31. prefix = fmt.Sprintf("%s/usages", prefix)
  32. app.AddHandler2("GET", prefix, auth.Authenticate(ReportGeneralUsage), nil, "get_usage", nil)
  33. }
  34. func ReportGeneralUsage(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  35. _, query, _ := appsrv.FetchEnv(ctx, w, r)
  36. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  37. _, _, err, result := db.FetchUsageOwnerScope(ctx, userCred, query)
  38. if err != nil {
  39. httperrors.GeneralServerError(ctx, w, err)
  40. return
  41. }
  42. projectTags := &tagutils.TTagSetList{}
  43. query.Unmarshal(projectTags, "project_tags")
  44. for i := range result.ProjectTags {
  45. projectTags.Append(result.ProjectTags[i])
  46. }
  47. result.ProjectTags = *projectTags
  48. isAdmin := false
  49. if policy.PolicyManager.Allow(rbacscope.ScopeSystem, userCred, consts.GetServiceType(),
  50. "usages", policy.PolicyActionGet).Result.IsAllow() {
  51. isAdmin = true
  52. }
  53. var adminUsage map[string]int
  54. // var projectUsage map[string]int64
  55. if isAdmin {
  56. adminUsage = models.Usage(ctx, result)
  57. }
  58. /*isProject := false
  59. if policy.PolicyManager.Allow(false, userCred, consts.GetServiceType(),
  60. "usages", policy.PolicyActionGet) == rbacutils.Deny {
  61. isProject = false
  62. } else {
  63. isProject = true
  64. }
  65. if isProject {
  66. projectUsage = models.Usage(userCred.GetProjectId(), "")
  67. }*/
  68. // if !isAdmin && !isProject {
  69. if !isAdmin {
  70. httperrors.ForbiddenError(ctx, w, "not allow to get usage")
  71. return
  72. }
  73. usages := jsonutils.NewDict()
  74. // if isProject {
  75. // usages.Update(jsonutils.Marshal(projectUsage))
  76. // }
  77. if isAdmin {
  78. usages.Update(jsonutils.Marshal(adminUsage))
  79. }
  80. body := jsonutils.NewDict()
  81. body.Add(usages, "usage")
  82. appsrv.SendJSON(w, body)
  83. }