handlers.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 service
  15. import (
  16. "context"
  17. "encoding/base64"
  18. "fmt"
  19. "net/http"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/util/httputils"
  22. "yunion.io/x/onecloud/pkg/appsrv"
  23. "yunion.io/x/onecloud/pkg/appsrv/dispatcher"
  24. "yunion.io/x/onecloud/pkg/baremetal/options"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  26. "yunion.io/x/onecloud/pkg/mcclient/auth"
  27. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  28. "yunion.io/x/onecloud/pkg/yunionconf/models"
  29. )
  30. func InitHandlers(app *appsrv.Application, isSlave bool) {
  31. db.InitAllManagers()
  32. db.AddScopeResourceCountHandler("", app)
  33. addBugReportHandler("", app, isSlave)
  34. for _, manager := range []db.IModelManager{
  35. db.UserCacheManager,
  36. db.TenantCacheManager,
  37. db.SharedResourceManager,
  38. db.OpsLog,
  39. } {
  40. db.RegisterModelManager(manager)
  41. }
  42. for _, manager := range []db.IModelManager{
  43. db.Metadata,
  44. models.ParameterManager,
  45. models.ScopedPolicyBindingManager,
  46. models.ScopedPolicyManager,
  47. models.TagManager,
  48. } {
  49. db.RegisterModelManager(manager)
  50. handler := db.NewModelHandler(manager)
  51. dispatcher.AddModelDispatcher("", app, handler, isSlave)
  52. if manager == models.ParameterManager {
  53. dispatcher.AddModelDispatcher("/users/<user_id>", app, handler, isSlave)
  54. dispatcher.AddModelDispatcher("/services/<service_id>", app, handler, isSlave)
  55. }
  56. }
  57. }
  58. func addBugReportHandler(prefix string, app *appsrv.Application, isSlave bool) {
  59. app.AddHandler("GET", fmt.Sprintf("%s/bug-report-status", prefix), bugReportStatusHandler)
  60. if isSlave {
  61. return
  62. }
  63. app.AddHandler("POST", fmt.Sprintf("%s/enable-bug-report", prefix), enableBugReportHandler)
  64. app.AddHandler("POST", fmt.Sprintf("%s/disable-bug-report", prefix), disableBugReportHandler)
  65. app.AddHandler("POST", fmt.Sprintf("%s/send-bug-report", prefix), sendBugReportHandler)
  66. }
  67. func bugReportStatusHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  68. enabled := models.ParameterManager.GetBugReportEnabled()
  69. appsrv.SendJSON(w, jsonutils.Marshal(map[string]bool{"enabled": enabled}))
  70. }
  71. func enableBugReportHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  72. enabled := models.ParameterManager.EnableBugReport(ctx)
  73. appsrv.SendJSON(w, jsonutils.Marshal(map[string]bool{"enabled": enabled}))
  74. }
  75. func disableBugReportHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  76. models.ParameterManager.DisableBugReport(ctx)
  77. appsrv.SendJSON(w, jsonutils.Marshal(map[string]bool{"enabled": false}))
  78. }
  79. func sendBugReportHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  80. if !models.ParameterManager.GetBugReportEnabled() {
  81. appsrv.SendJSON(w, jsonutils.Marshal(map[string]bool{"status": false}))
  82. return
  83. }
  84. _, _, body := appsrv.FetchEnv(ctx, w, r)
  85. apiServer := options.Options.ApiServer
  86. if len(apiServer) == 0 {
  87. s := auth.GetAdminSession(ctx, options.Options.Region)
  88. commonCfg, _ := identity.ServicesV3.GetSpecific(s, "common", "config", nil)
  89. if commonCfg != nil {
  90. _apiServer, _ := commonCfg.GetString("config", "default", "api_server")
  91. if len(_apiServer) > 0 {
  92. apiServer = _apiServer
  93. }
  94. }
  95. }
  96. params := jsonutils.NewDict()
  97. params.Set("api_server", jsonutils.NewString(apiServer))
  98. params.Update(body)
  99. url, _ := base64.StdEncoding.DecodeString("aHR0cHM6Ly9jbG91ZC55dW5pb24uY24vYXBpL3YyL2J1Zy1yZXBvcnQ=")
  100. _, _, err := httputils.JSONRequest(nil, ctx, httputils.POST, string(url), nil, params, false)
  101. if err != nil {
  102. appsrv.SendJSON(w, jsonutils.Marshal(map[string]bool{"status": false}))
  103. return
  104. }
  105. appsrv.SendJSON(w, jsonutils.Marshal(map[string]bool{"status": true}))
  106. }