mod_parameters.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 yunionconf
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/httputils"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/auth"
  21. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  22. "yunion.io/x/onecloud/pkg/mcclient/modules"
  23. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  24. )
  25. type ParametersManager struct {
  26. modulebase.ResourceManager
  27. }
  28. var (
  29. Parameters ParametersManager
  30. )
  31. func init() {
  32. Parameters = ParametersManager{modules.NewYunionConfManager("parameter", "parameters",
  33. []string{"id", "created_at", "update_at", "name", "value"},
  34. []string{"namespace", "namespace_id", "created_by", "updated_by"},
  35. )}
  36. modules.Register(&Parameters)
  37. }
  38. func (m *ParametersManager) GetGlobalSettings(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  39. return m.getParametersRpc(s, "global-settings", params)
  40. }
  41. func (m *ParametersManager) GetWidgetSettings(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  42. return m.getParametersRpc(s, "widget-settings", params)
  43. }
  44. func (m *ParametersManager) getParametersRpc(s *mcclient.ClientSession, key string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  45. adminSession := auth.GetAdminSession(context.Background(), "")
  46. p := jsonutils.NewDict()
  47. p.Add(jsonutils.NewString("system"), "scope")
  48. p.Add(jsonutils.NewString(key), "name")
  49. parameters, err := m.ListInContext(adminSession, p, &identity.ServicesV3, "yunionagent")
  50. if err != nil {
  51. return nil, err
  52. }
  53. if parameters.Total == 0 {
  54. // if no such setting, create one
  55. empty := jsonutils.NewDict()
  56. empty.Add(jsonutils.NewString(key), "name")
  57. empty.Add(jsonutils.NewDict(), "value")
  58. empty.Add(jsonutils.NewString("yunionagent"), "service_id")
  59. _, err := m.Create(adminSession, empty)
  60. if err != nil && httputils.ErrorCode(err) != 409 {
  61. return nil, err
  62. }
  63. return m.getParametersRpc(s, key, params)
  64. }
  65. return parameters.Data[0], nil
  66. }