service_setting.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 handler
  15. import (
  16. "context"
  17. "net/http"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/appsrv"
  20. "yunion.io/x/onecloud/pkg/httperrors"
  21. "yunion.io/x/onecloud/pkg/mcclient/auth"
  22. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  23. )
  24. var settingNames = map[string][]string{
  25. "identity": []string{"no_action_logout_seconds"},
  26. "yunionapi": []string{"enable_organization", "totp_issuer"},
  27. "image": []string{"enable_pending_delete"},
  28. "compute_v2": []string{"enable_pending_delete"},
  29. "meter": []string{"cost_conversion_available", "enable_prediction", "share_resource_type"},
  30. "common": []string{"api_server", "enable_quota_check", "enable_watermark", "enable_cloud_shell"},
  31. }
  32. func (mh *MiscHandler) getServiceSettings(ctx context.Context, w http.ResponseWriter, req *http.Request) {
  33. s := auth.GetAdminSession(ctx, FetchRegion(req))
  34. types := []string{}
  35. for typ := range settingNames {
  36. types = append(types, typ)
  37. }
  38. params := map[string]interface{}{
  39. "limit": 20,
  40. "scope": "system",
  41. "type": types,
  42. }
  43. resp, err := identity.ServicesV3.List(s, jsonutils.Marshal(params))
  44. if err != nil {
  45. e := httperrors.NewInternalServerError("%s", err.Error())
  46. httperrors.JsonClientError(ctx, w, e)
  47. return
  48. }
  49. services := []struct {
  50. Id string
  51. Type string
  52. }{}
  53. err = jsonutils.Update(&services, resp.Data)
  54. if err != nil {
  55. e := httperrors.NewInternalServerError("%s", err.Error())
  56. httperrors.JsonClientError(ctx, w, e)
  57. return
  58. }
  59. result := map[string]map[string]interface{}{}
  60. for _, service := range services {
  61. result[service.Type] = map[string]interface{}{}
  62. data, err := identity.ServicesV3.GetSpecific(s, service.Id, "config", nil)
  63. if err != nil {
  64. e := httperrors.NewInternalServerError("%s", err.Error())
  65. httperrors.JsonClientError(ctx, w, e)
  66. return
  67. }
  68. names, _ := settingNames[service.Type]
  69. for _, name := range names {
  70. v, _ := data.Get("config", "default", name)
  71. result[service.Type][name] = v
  72. }
  73. }
  74. appsrv.SendJSON(w, jsonutils.Marshal(result))
  75. }