mergeconf.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 options
  15. import (
  16. "context"
  17. "database/sql"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/sqlchemy"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
  26. )
  27. func GetServiceIdByType(s *mcclient.ClientSession, typeStr string, verStr string) (string, error) {
  28. params := jsonutils.NewDict()
  29. if len(verStr) > 0 && verStr != "v1" {
  30. typeStr += "_" + verStr
  31. }
  32. params.Add(jsonutils.NewString(typeStr), "type")
  33. result, err := identity.ServicesV3.List(s, params)
  34. if err != nil {
  35. return "", errors.Wrap(err, "modules.ServicesV3.List")
  36. }
  37. if len(result.Data) == 0 {
  38. return "", errors.Wrap(sql.ErrNoRows, "modules.ServicesV3.List")
  39. } else if len(result.Data) > 1 {
  40. return "", errors.Wrap(sqlchemy.ErrDuplicateEntry, "modules.ServicesV3.List")
  41. }
  42. return result.Data[0].GetString("id")
  43. }
  44. func GetServiceConfig(s *mcclient.ClientSession, serviceId string) (jsonutils.JSONObject, error) {
  45. conf, err := identity.ServicesV3.GetSpecific(s, serviceId, "config", nil)
  46. if err != nil {
  47. return nil, errors.Wrap(err, "modules.ServicesV3.GetSpecific config")
  48. }
  49. defConf, _ := conf.Get("config", "default")
  50. return defConf, nil
  51. }
  52. type IServiceConfigSession interface {
  53. Merge(opts interface{}, serviceType string, serviceVersion string) bool
  54. Upload()
  55. IsRemote() bool
  56. }
  57. type mcclientServiceConfigSession struct {
  58. session *mcclient.ClientSession
  59. serviceId string
  60. config *jsonutils.JSONDict
  61. commonServiceId string
  62. }
  63. func newServiceConfigSession() IServiceConfigSession {
  64. return &mcclientServiceConfigSession{}
  65. }
  66. func (s *mcclientServiceConfigSession) Merge(opts interface{}, serviceType string, serviceVersion string) bool {
  67. merged := false
  68. s.config = jsonutils.Marshal(opts).(*jsonutils.JSONDict)
  69. region, _ := s.config.GetString("region")
  70. // epType, _ := s.config.GetString("session_endpoint_type")
  71. s.session = auth.GetAdminSession(context.Background(), region)
  72. if len(serviceType) > 0 {
  73. s.serviceId, _ = GetServiceIdByType(s.session, serviceType, serviceVersion)
  74. if len(s.serviceId) > 0 {
  75. serviceConf, err := GetServiceConfig(s.session, s.serviceId)
  76. if err != nil {
  77. log.Errorf("GetServiceConfig for %s failed: %s", serviceType, err)
  78. } else if serviceConf != nil {
  79. s.config.Update(serviceConf)
  80. merged = true
  81. } else {
  82. // not initialized
  83. // s.Upload()
  84. }
  85. }
  86. }
  87. s.commonServiceId, _ = GetServiceIdByType(s.session, consts.COMMON_SERVICE, "")
  88. if len(s.commonServiceId) > 0 {
  89. commonConf, err := GetServiceConfig(s.session, s.commonServiceId)
  90. if err != nil {
  91. log.Errorf("GetServiceConfig for %s failed: %s", consts.COMMON_SERVICE, err)
  92. } else if commonConf != nil {
  93. s.config.Update(commonConf)
  94. merged = true
  95. } else {
  96. // common not initialized
  97. }
  98. }
  99. if merged {
  100. err := s.config.Unmarshal(opts)
  101. if err == nil {
  102. return true
  103. }
  104. log.Errorf("s.config.Unmarshal fail %s", err)
  105. }
  106. return false
  107. }
  108. func (s *mcclientServiceConfigSession) Upload() {
  109. // upload service config
  110. if len(s.serviceId) > 0 {
  111. nconf := jsonutils.NewDict()
  112. nconf.Add(s.config, "config", "default")
  113. _, err := identity.ServicesV3.PerformAction(s.session, s.serviceId, "config", nconf)
  114. if err != nil {
  115. // ignore the error
  116. log.Errorf("fail to save config: %s", err)
  117. }
  118. _, err = identity.ServicesV3.PerformAction(s.session, s.commonServiceId, "config", nconf)
  119. if err != nil {
  120. // ignore the error
  121. log.Errorf("fail to save common config: %s", err)
  122. }
  123. }
  124. }
  125. func (s *mcclientServiceConfigSession) IsRemote() bool {
  126. return true
  127. }