services.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 models
  15. import (
  16. "context"
  17. "database/sql"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/gotypes"
  21. "yunion.io/x/pkg/tristate"
  22. "yunion.io/x/sqlchemy"
  23. api "yunion.io/x/onecloud/pkg/apis/identity"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/options"
  27. "yunion.io/x/onecloud/pkg/httperrors"
  28. "yunion.io/x/onecloud/pkg/mcclient"
  29. "yunion.io/x/onecloud/pkg/util/logclient"
  30. "yunion.io/x/onecloud/pkg/util/stringutils2"
  31. )
  32. type SServiceManager struct {
  33. db.SStandaloneResourceBaseManager
  34. }
  35. var ServiceManager *SServiceManager
  36. func init() {
  37. ServiceManager = &SServiceManager{
  38. SStandaloneResourceBaseManager: db.NewStandaloneResourceBaseManager(
  39. SService{},
  40. "service",
  41. "service",
  42. "services",
  43. ),
  44. }
  45. ServiceManager.SetVirtualObject(ServiceManager)
  46. }
  47. /*
  48. +------------+--------------+------+-----+---------+-------+
  49. | Field | Type | Null | Key | Default | Extra |
  50. +------------+--------------+------+-----+---------+-------+
  51. | id | varchar(64) | NO | PRI | NULL | |
  52. | type | varchar(255) | YES | | NULL | |
  53. | enabled | tinyint(1) | NO | | 1 | |
  54. | extra | text | YES | | NULL | |
  55. | created_at | datetime | YES | | NULL | |
  56. +------------+--------------+------+-----+---------+-------+
  57. */
  58. type SService struct {
  59. db.SStandaloneResourceBase
  60. Type string `width:"255" charset:"utf8" list:"admin" create:"admin_required"`
  61. Enabled tristate.TriState `default:"true" list:"admin" update:"admin" create:"admin_optional"`
  62. Extra *jsonutils.JSONDict `nullable:"true" list:"admin"`
  63. ConfigVersion int `list:"admin" nullable:"false" default:"0"`
  64. }
  65. func (manager *SServiceManager) InitializeData() error {
  66. q := manager.Query()
  67. q = q.IsNullOrEmpty("name")
  68. srvs := make([]SService, 0)
  69. err := db.FetchModelObjects(manager, q, &srvs)
  70. if err != nil {
  71. return err
  72. }
  73. for i := range srvs {
  74. if gotypes.IsNil(srvs[i].Extra) {
  75. continue
  76. }
  77. name, _ := srvs[i].Extra.GetString("name")
  78. desc, _ := srvs[i].Extra.GetString("description")
  79. if len(name) == 0 {
  80. name = srvs[i].Type
  81. }
  82. db.Update(&srvs[i], func() error {
  83. srvs[i].Name = name
  84. srvs[i].Description = desc
  85. return nil
  86. })
  87. }
  88. return nil
  89. }
  90. func (service *SService) GetEndpointCount() (int, error) {
  91. q := EndpointManager.Query().Equals("service_id", service.Id)
  92. return q.CountWithError()
  93. }
  94. func (service *SService) ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error {
  95. epCnt, _ := service.GetEndpointCount()
  96. if epCnt > 0 {
  97. return httperrors.NewNotEmptyError("service contains endpoints")
  98. }
  99. if service.Enabled.IsTrue() {
  100. return httperrors.NewInvalidStatusError("service is enabled")
  101. }
  102. return service.SStandaloneResourceBase.ValidateDeleteCondition(ctx, nil)
  103. }
  104. func (manager *SServiceManager) FetchCustomizeColumns(
  105. ctx context.Context,
  106. userCred mcclient.TokenCredential,
  107. query jsonutils.JSONObject,
  108. objs []interface{},
  109. fields stringutils2.SSortedStrings,
  110. isList bool,
  111. ) []api.ServiceDetails {
  112. rows := make([]api.ServiceDetails, len(objs))
  113. stdRows := manager.SStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
  114. for i := range rows {
  115. rows[i] = api.ServiceDetails{
  116. StandaloneResourceDetails: stdRows[i],
  117. }
  118. rows[i].EndpointCount, _ = objs[i].(*SService).GetEndpointCount()
  119. }
  120. return rows
  121. }
  122. func (service *SService) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
  123. service.SStandaloneResourceBase.PostCreate(ctx, userCred, ownerId, query, data)
  124. logclient.AddActionLogWithContext(ctx, service, logclient.ACT_CREATE, data, userCred, true)
  125. }
  126. func (service *SService) PostUpdate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) {
  127. service.SStandaloneResourceBase.PostUpdate(ctx, userCred, query, data)
  128. logclient.AddActionLogWithContext(ctx, service, logclient.ACT_UPDATE, data, userCred, true)
  129. }
  130. func (service *SService) PostDelete(ctx context.Context, userCred mcclient.TokenCredential) {
  131. service.SStandaloneResourceBase.PostDelete(ctx, userCred)
  132. logclient.AddActionLogWithContext(ctx, service, logclient.ACT_DELETE, nil, userCred, true)
  133. }
  134. func (service *SService) GetDetailsConfig(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  135. var whiteList, blackList map[string][]string
  136. if service.isCommonService() {
  137. // whitelist common options
  138. whiteList = api.CommonWhitelistOptionMap
  139. } else {
  140. // blacklist common options
  141. blackList = api.CommonWhitelistOptionMap
  142. }
  143. conf, err := GetConfigs(service, false, whiteList, blackList)
  144. if err != nil {
  145. return nil, err
  146. }
  147. result := jsonutils.NewDict()
  148. result.Add(jsonutils.Marshal(conf), "config")
  149. return result, nil
  150. }
  151. func (service *SService) isCommonService() bool {
  152. if service.Type == consts.COMMON_SERVICE {
  153. return true
  154. } else {
  155. return false
  156. }
  157. }
  158. func (service *SService) PerformConfig(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.PerformConfigInput) (jsonutils.JSONObject, error) {
  159. var err error
  160. var changed bool
  161. action := input.Action
  162. opts := input.Config
  163. if service.isCommonService() {
  164. changed, err = saveConfigs(userCred, action, service, opts, api.CommonWhitelistOptionMap, nil, nil)
  165. } else {
  166. changed, err = saveConfigs(userCred, action, service, opts, nil, api.MergeServiceConfigOptions(api.CommonWhitelistOptionMap, api.ServiceBlacklistOptionMap), nil)
  167. }
  168. if err != nil {
  169. return nil, err
  170. }
  171. if changed {
  172. diff := SService{ConfigVersion: 1}
  173. err = ServiceManager.TableSpec().Increment(ctx, diff, service)
  174. if err != nil {
  175. return nil, httperrors.NewInternalServerError("update config version fail %s", err)
  176. }
  177. if service.Type == api.SERVICE_TYPE || service.Type == consts.COMMON_SERVICE {
  178. options.OptionManager.SyncOnce(false, false)
  179. }
  180. }
  181. return service.GetDetailsConfig(ctx, userCred, query)
  182. }
  183. func (manager *SServiceManager) fetchServiceByType(typeStr string) (*SService, error) {
  184. q := manager.Query().Equals("type", typeStr)
  185. cnt, err := q.CountWithError()
  186. if err != nil && errors.Cause(err) != sql.ErrNoRows {
  187. return nil, errors.Wrap(err, "CountWithError")
  188. }
  189. if cnt == 0 {
  190. return nil, sql.ErrNoRows
  191. } else if cnt > 1 {
  192. return nil, sqlchemy.ErrDuplicateEntry
  193. }
  194. srvObj, err := db.NewModelObject(manager)
  195. if err != nil {
  196. return nil, errors.Wrap(err, "db.NewModelObject")
  197. }
  198. err = q.First(srvObj)
  199. if err != nil {
  200. return nil, errors.Wrap(err, "q.First")
  201. }
  202. return srvObj.(*SService), nil
  203. }
  204. // 服务列表
  205. func (manager *SServiceManager) ListItemFilter(
  206. ctx context.Context,
  207. q *sqlchemy.SQuery,
  208. userCred mcclient.TokenCredential,
  209. query api.ServiceListInput,
  210. ) (*sqlchemy.SQuery, error) {
  211. q, err := manager.SStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, query.StandaloneResourceListInput)
  212. if err != nil {
  213. return nil, errors.Wrap(err, "SStandaloneResourceBaseManager.ListItemFilter")
  214. }
  215. if len(query.Type) > 0 {
  216. q = q.In("type", query.Type)
  217. }
  218. if query.Enabled != nil {
  219. if *query.Enabled {
  220. q = q.IsTrue("enabled")
  221. } else {
  222. q = q.IsFalse("enabled")
  223. }
  224. }
  225. return q, nil
  226. }
  227. func (manager *SServiceManager) OrderByExtraFields(
  228. ctx context.Context,
  229. q *sqlchemy.SQuery,
  230. userCred mcclient.TokenCredential,
  231. query api.RegionListInput,
  232. ) (*sqlchemy.SQuery, error) {
  233. var err error
  234. q, err = manager.SStandaloneResourceBaseManager.OrderByExtraFields(ctx, q, userCred, query.StandaloneResourceListInput)
  235. if err != nil {
  236. return nil, errors.Wrap(err, "SStandaloneResourceBaseManager.OrderByExtraFields")
  237. }
  238. return q, nil
  239. }
  240. func (manager *SServiceManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
  241. var err error
  242. q, err = manager.SStandaloneResourceBaseManager.QueryDistinctExtraField(q, field)
  243. if err == nil {
  244. return q, nil
  245. }
  246. return q, httperrors.ErrNotFound
  247. }