interface.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 db
  15. import (
  16. "context"
  17. "net/http"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/object"
  21. "yunion.io/x/pkg/util/rbacscope"
  22. "yunion.io/x/sqlchemy"
  23. "yunion.io/x/onecloud/pkg/apis"
  24. "yunion.io/x/onecloud/pkg/appsrv"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  26. "yunion.io/x/onecloud/pkg/mcclient"
  27. "yunion.io/x/onecloud/pkg/util/splitable"
  28. "yunion.io/x/onecloud/pkg/util/stringutils2"
  29. )
  30. type IModelManager interface {
  31. lockman.ILockedClass
  32. object.IObject
  33. IsStandaloneManager() bool
  34. GetContextManagers() [][]IModelManager
  35. GetIModelManager() IModelManager
  36. GetMutableInstance(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) IModelManager
  37. GetImmutableInstance(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) IModelManager
  38. // Table() *sqlchemy.STable
  39. TableSpec() ITableSpec
  40. // Keyword() string
  41. // KeywordPlural() string
  42. Alias() string
  43. AliasPlural() string
  44. SetAlias(alias string, aliasPlural string)
  45. HasName() bool
  46. ValidateName(name string) error
  47. EnableGenerateName() bool
  48. // list hooks
  49. // AllowListItems(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool
  50. ValidateListConditions(ctx context.Context, userCred mcclient.TokenCredential, query *jsonutils.JSONDict) (*jsonutils.JSONDict, error)
  51. // ListItemFilter dynamic called by dispatcher
  52. // ListItemFilter(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*sqlchemy.SQuery, error)
  53. CustomizeFilterList(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*CustomizeListFilters, error)
  54. ExtraSearchConditions(ctx context.Context, q *sqlchemy.SQuery, like string) []sqlchemy.ICondition
  55. GetExportExtraKeys(ctx context.Context, keys stringutils2.SSortedStrings, rowMap map[string]string) *jsonutils.JSONDict
  56. ListItemExportKeys(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, keys stringutils2.SSortedStrings) (*sqlchemy.SQuery, error)
  57. // OrderByExtraFields dynmically called by dispatcher
  58. // OrderByExtraFields(ctx context.Context, q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (*sqlchemy.SQuery, error)
  59. NewQuery(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, useRawQuery bool) *sqlchemy.SQuery
  60. // fetch hook
  61. Query(val ...string) *sqlchemy.SQuery
  62. // RawQuery(val ...string) *sqlchemy.SQuery
  63. FilterById(q *sqlchemy.SQuery, idStr string) *sqlchemy.SQuery
  64. FilterByNotId(q *sqlchemy.SQuery, idStr string) *sqlchemy.SQuery
  65. FilterByName(q *sqlchemy.SQuery, name string) *sqlchemy.SQuery
  66. FilterByOwnerProvider
  67. FilterBySystemAttributes(q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject, scope rbacscope.TRbacScope) *sqlchemy.SQuery
  68. FilterByHiddenSystemAttributes(q *sqlchemy.SQuery, userCred mcclient.TokenCredential, query jsonutils.JSONObject, scope rbacscope.TRbacScope) *sqlchemy.SQuery
  69. FilterByUniqValues(q *sqlchemy.SQuery, uniqValues jsonutils.JSONObject) *sqlchemy.SQuery
  70. // GetOwnerId(userCred mcclient.IIdentityProvider) mcclient.IIdentityProvider
  71. // RawFetchById(idStr string) (IModel, error)
  72. FetchById(idStr string) (IModel, error)
  73. FetchByName(ctx context.Context, userCred mcclient.IIdentityProvider, idStr string) (IModel, error)
  74. FetchByIdOrName(ctx context.Context, userCred mcclient.IIdentityProvider, idStr string) (IModel, error)
  75. // create hooks
  76. // AllowCreateItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool
  77. // BatchCreateValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error)
  78. // ValidateCreateData dynamic called by dispatcher
  79. // ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error)
  80. OnCreateComplete(ctx context.Context, items []IModel, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data []jsonutils.JSONObject)
  81. BatchPreValidate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider,
  82. query jsonutils.JSONObject, data *jsonutils.JSONDict, count int) error
  83. OnCreateFailed(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error
  84. // allow perform action
  85. PerformAction(ctx context.Context, userCred mcclient.TokenCredential, action string, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error)
  86. // DoCreate(ctx context.Context, userCred mcclient.TokenCredential, kwargs jsonutils.JSONObject, data jsonutils.JSONObject, realManager IModelManager) (IModel, error)
  87. InitializeData() error
  88. CustomizeHandlerInfo(info *appsrv.SHandlerInfo)
  89. SetHandlerProcessTimeout(info *appsrv.SHandlerInfo, r *http.Request) time.Duration
  90. FetchCreateHeaderData(ctx context.Context, header http.Header) (jsonutils.JSONObject, error)
  91. FetchUpdateHeaderData(ctx context.Context, header http.Header) (jsonutils.JSONObject, error)
  92. IsCustomizedGetDetailsBody() bool
  93. ListSkipLog(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool
  94. GetSkipLog(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool
  95. // list extend colums hook
  96. // FetchCustomizeColumns dynamically called by dispatcher
  97. // FetchCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, objs []interface{}, fields stringutils2.SSortedStrings, isList bool) []*jsonutils.JSONDict
  98. // fetch owner Id from query when create resource
  99. FetchOwnerId(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error)
  100. FetchUniqValues(ctx context.Context, data jsonutils.JSONObject) jsonutils.JSONObject
  101. /* name uniqueness scope, system/domain/project, default is system */
  102. NamespaceScope() rbacscope.TRbacScope
  103. ResourceScope() rbacscope.TRbacScope
  104. // 如果error为非空,说明没有匹配的field,如果为空,说明匹配上了
  105. QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error)
  106. QueryDistinctExtraFields(q *sqlchemy.SQuery, resource string, fields []string) (*sqlchemy.SQuery, error)
  107. GetPagingConfig() *SPagingConfig
  108. GetI18N(ctx context.Context, idstr string, resObj jsonutils.JSONObject) *jsonutils.JSONDict
  109. GetSplitTable() *splitable.SSplitTableSpec
  110. CreateByInsertOrUpdate() bool
  111. CustomizedTotalCount(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, totalQ *sqlchemy.SQuery) (int, jsonutils.JSONObject, error)
  112. PrepareQueryContext(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) context.Context
  113. RegisterExtraHook(eh IModelManagerExtraHook)
  114. GetExtraHook() IModelManagerExtraHook
  115. }
  116. type IModelManagerExtraHook interface {
  117. AfterPostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, model IModel, query jsonutils.JSONObject, data jsonutils.JSONObject) error
  118. AfterPostUpdate(ctx context.Context, userCred mcclient.TokenCredential, model IModel, query jsonutils.JSONObject, data jsonutils.JSONObject) error
  119. AfterPostDelete(ctx context.Context, userCred mcclient.TokenCredential, model IModel, query jsonutils.JSONObject) error
  120. }
  121. type IModel interface {
  122. lockman.ILockedObject
  123. object.IObject
  124. GetName() string
  125. GetUpdateVersion() int
  126. GetUpdatedAt() time.Time
  127. GetDeleted() bool
  128. KeywordPlural() string
  129. GetModelManager() IModelManager
  130. SetModelManager(IModelManager, IModel)
  131. GetIModel() IModel
  132. GetShortDesc(ctx context.Context) *jsonutils.JSONDict
  133. // list hooks
  134. //GetCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) *jsonutils.JSONDict
  135. // get hooks
  136. GetExtraDetailsHeaders(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) map[string]string
  137. // before create hooks
  138. CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error
  139. // after create hooks
  140. PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject)
  141. // allow perform action
  142. PerformAction(ctx context.Context, userCred mcclient.TokenCredential, action string, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error)
  143. PreCheckPerformAction(ctx context.Context, userCred mcclient.TokenCredential, action string, query jsonutils.JSONObject, data jsonutils.JSONObject) error
  144. // update hooks
  145. ValidateUpdateCondition(ctx context.Context) error
  146. // AllowUpdateItem(ctx context.Context, userCred mcclient.TokenCredential) bool
  147. // ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error)
  148. PreUpdate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject)
  149. PostUpdate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject)
  150. UpdateInContext(ctx context.Context, userCred mcclient.TokenCredential, ctxObjs []IModel, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error)
  151. // delete hooks
  152. // AllowDeleteItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool
  153. // ValidateDeleteCondition(ctx context.Context, info jsonutils.JSONObject) error
  154. // CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error
  155. PreDelete(ctx context.Context, userCred mcclient.TokenCredential)
  156. MarkDelete() error
  157. Delete(ctx context.Context, userCred mcclient.TokenCredential) error
  158. PostDelete(ctx context.Context, userCred mcclient.TokenCredential)
  159. DeleteInContext(ctx context.Context, userCred mcclient.TokenCredential, ctxObjs []IModel, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error)
  160. GetOwnerId() mcclient.IIdentityProvider
  161. GetUniqValues() jsonutils.JSONObject
  162. IsSharable(reqCred mcclient.IIdentityProvider) bool
  163. CustomizedGetDetailsBody(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error)
  164. MarkDeletePreventionOn()
  165. MarkDeletePreventionOff()
  166. GetUsages() []IUsage
  167. GetI18N(ctx context.Context) *jsonutils.JSONDict
  168. }
  169. type IResourceModelManager interface {
  170. IModelManager
  171. GetIResourceModelManager() IResourceModelManager
  172. }
  173. type IResourceModel interface {
  174. IModel
  175. GetIResourceModel() IResourceModel
  176. }
  177. type IJointModelManager interface {
  178. IResourceModelManager
  179. GetIJointModelManager() IJointModelManager
  180. GetMasterManager() IStandaloneModelManager
  181. GetSlaveManager() IStandaloneModelManager
  182. GetMasterFieldName() string
  183. GetSlaveFieldName() string
  184. // FetchByIds(masterId string, slaveId string, query jsonutils.JSONObject) (IJointModel, error)
  185. FilterByParams(q *sqlchemy.SQuery, params jsonutils.JSONObject) *sqlchemy.SQuery
  186. // AllowListDescendent(ctx context.Context, userCred mcclient.TokenCredential, model IStandaloneModel, query jsonutils.JSONObject) bool
  187. // AllowAttach(ctx context.Context, userCred mcclient.TokenCredential, master IStandaloneModel, slave IStandaloneModel) bool
  188. }
  189. type IJointModel interface {
  190. IResourceModel
  191. GetJointModelManager() IJointModelManager
  192. GetIJointModel() IJointModel
  193. // AllowDetach(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool
  194. Detach(ctx context.Context, userCred mcclient.TokenCredential) error
  195. // AllowGetJointDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, item IJointModel) bool
  196. // AllowUpdateJointItem(ctx context.Context, userCred mcclient.TokenCredential, item IJointModel) bool
  197. }
  198. type IMetadataBaseModelManager interface {
  199. GetMetadataHiddenKeys() []string
  200. }
  201. type IMetadataBaseModel interface {
  202. OnMetadataUpdated(ctx context.Context, userCred mcclient.TokenCredential)
  203. }
  204. type IStandaloneModelManager interface {
  205. IResourceModelManager
  206. GetIStandaloneModelManager() IStandaloneModelManager
  207. // GenerateName(ownerProjId string, hint string) string
  208. // ValidateName(name string) error
  209. // IsNewNameUnique(name string, projectId string) bool
  210. // FetchByExternalId(idStr string) (IStandaloneModel, error)
  211. IMetadataBaseModelManager
  212. }
  213. type IStandaloneModel interface {
  214. IResourceModel
  215. // IsAlterNameUnique(name string, projectId string) bool
  216. // GetExternalId() string
  217. SetName(name string)
  218. MarkPendingDeleted()
  219. CancelPendingDeleted()
  220. StandaloneModelManager() IStandaloneModelManager
  221. GetIStandaloneModel() IStandaloneModel
  222. ClearSchedDescCache() error
  223. GetMetadata(ctx context.Context, key string, userCred mcclient.TokenCredential) string
  224. GetMetadataJson(ctx context.Context, key string, userCred mcclient.TokenCredential) jsonutils.JSONObject
  225. SetMetadata(ctx context.Context, key string, value interface{}, userCred mcclient.TokenCredential) error
  226. SetAllMetadata(ctx context.Context, dictstore map[string]interface{}, userCred mcclient.TokenCredential) error
  227. SetUserMetadataValues(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error
  228. SetUserMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error
  229. SetCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential, readOnly bool) error
  230. SetOrganizationMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error
  231. SetSysCloudMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential, readOnly bool) error
  232. RemoveMetadata(ctx context.Context, key string, userCred mcclient.TokenCredential) error
  233. RemoveAllMetadata(ctx context.Context, userCred mcclient.TokenCredential) error
  234. GetAllMetadata(ctx context.Context, userCred mcclient.TokenCredential) (map[string]string, error)
  235. GetAllClassMetadata() (map[string]string, error)
  236. IsShared() bool
  237. IMetadataBaseModel
  238. }
  239. type IDomainLevelModelManager interface {
  240. IStandaloneModelManager
  241. GetIDomainLevelModelManager() IDomainLevelModelManager
  242. GetResourceCount() ([]SScopeResourceCount, error)
  243. }
  244. type IDomainLevelModel interface {
  245. IStandaloneModel
  246. IsOwner(userCred mcclient.TokenCredential) bool
  247. SyncCloudDomainId(userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider)
  248. GetIDomainLevelModel() IDomainLevelModel
  249. IOwnerResourceBaseModel
  250. }
  251. type IInfrasModelManager interface {
  252. IDomainLevelModelManager
  253. GetIInfrasModelManager() IInfrasModelManager
  254. }
  255. type IInfrasModel interface {
  256. IDomainLevelModel
  257. ISharableBase
  258. GetIInfrasModel() IInfrasModel
  259. }
  260. type IVirtualModelManager interface {
  261. IStandaloneModelManager
  262. GetIVirtualModelManager() IVirtualModelManager
  263. GetResourceCount() ([]SScopeResourceCount, error)
  264. }
  265. type IUserModelManager interface {
  266. IStandaloneModelManager
  267. GetIUserModelManager() IUserModelManager
  268. GetResourceCount() ([]SScopeResourceCount, error)
  269. }
  270. type IVirtualModel interface {
  271. IStandaloneModel
  272. IPendingDeletable
  273. IsOwner(userCred mcclient.TokenCredential) bool
  274. // IsAdmin(userCred mcclient.TokenCredential) bool
  275. SetProjectSrc(apis.TOwnerSource)
  276. SyncCloudProjectId(userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider)
  277. GetIVirtualModel() IVirtualModel
  278. IOwnerResourceBaseModel
  279. }
  280. type ISharableVirtualModelManager interface {
  281. IVirtualModelManager
  282. GetISharableVirtualModelManager() ISharableVirtualModelManager
  283. }
  284. type ISharableVirtualModel interface {
  285. IVirtualModel
  286. ISharableBase
  287. GetISharableVirtualModel() ISharableVirtualModel
  288. GetSharedProjects() []string
  289. }
  290. type IAdminSharableVirtualModelManager interface {
  291. ISharableVirtualModelManager
  292. GetIAdminSharableVirtualModelManager() IAdminSharableVirtualModelManager
  293. GetRecordsSeparator() string
  294. GetRecordsLimit() int
  295. ParseInputInfo(data *jsonutils.JSONDict) ([]string, error)
  296. }
  297. type IAdminSharableVirtualModel interface {
  298. ISharableVirtualModel
  299. GetInfo() []string
  300. GetIAdminSharableVirtualModel() IAdminSharableVirtualModel
  301. }
  302. type IStatusStandaloneModel interface {
  303. IStandaloneModel
  304. IStatusBase
  305. }
  306. type IStatusDomainLevelModel interface {
  307. IDomainLevelModel
  308. IStatusBase
  309. }
  310. type IStatusInfrasModel interface {
  311. IInfrasModel
  312. IStatusBase
  313. }