elasticcache_accounts.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. "fmt"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/compare"
  23. "yunion.io/x/pkg/util/rbacscope"
  24. "yunion.io/x/pkg/utils"
  25. "yunion.io/x/sqlchemy"
  26. "yunion.io/x/onecloud/pkg/apis"
  27. api "yunion.io/x/onecloud/pkg/apis/compute"
  28. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  29. "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
  30. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  31. "yunion.io/x/onecloud/pkg/cloudcommon/validators"
  32. "yunion.io/x/onecloud/pkg/httperrors"
  33. "yunion.io/x/onecloud/pkg/mcclient"
  34. "yunion.io/x/onecloud/pkg/util/choices"
  35. "yunion.io/x/onecloud/pkg/util/seclib2"
  36. "yunion.io/x/onecloud/pkg/util/stringutils2"
  37. )
  38. // +onecloud:swagger-gen-model-singular=elasticcacheaccount
  39. // +onecloud:swagger-gen-model-plural=elasticcacheaccounts
  40. type SElasticcacheAccountManager struct {
  41. db.SStatusStandaloneResourceBaseManager
  42. db.SExternalizedResourceBaseManager
  43. SElasticcacheResourceBaseManager
  44. }
  45. var ElasticcacheAccountManager *SElasticcacheAccountManager
  46. func init() {
  47. ElasticcacheAccountManager = &SElasticcacheAccountManager{
  48. SStatusStandaloneResourceBaseManager: db.NewStatusStandaloneResourceBaseManager(
  49. SElasticcacheAccount{},
  50. "elasticcacheaccounts_tbl",
  51. "elasticcacheaccount",
  52. "elasticcacheaccounts",
  53. ),
  54. }
  55. ElasticcacheAccountManager.SetVirtualObject(ElasticcacheAccountManager)
  56. }
  57. type SElasticcacheAccount struct {
  58. db.SStatusStandaloneResourceBase
  59. db.SExternalizedResourceBase
  60. SElasticcacheResourceBase `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  61. // ElasticcacheId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"` // elastic cache instance id
  62. AccountType string `width:"16" charset:"ascii" nullable:"false" list:"user" update:"user" create:"optional"` // 账号类型 normal |admin
  63. AccountPrivilege string `width:"16" charset:"ascii" nullable:"false" list:"user" update:"user" create:"optional"` // 账号权限 read | write | repl(复制, 复制权限支持读写,且开放SYNC/PSYNC命令)
  64. Password string `width:"256" charset:"ascii" nullable:"false" list:"user" create:"optional"` // 账号密码
  65. }
  66. func (manager *SElasticcacheAccountManager) SyncElasticcacheAccounts(ctx context.Context, userCred mcclient.TokenCredential, elasticcache *SElasticcache, cloudElasticcacheAccounts []cloudprovider.ICloudElasticcacheAccount) compare.SyncResult {
  67. lockman.LockRawObject(ctx, "elastic-cache-accounts", elasticcache.Id)
  68. defer lockman.ReleaseRawObject(ctx, "elastic-cache-accounts", elasticcache.Id)
  69. syncResult := compare.SyncResult{}
  70. dbAccounts, err := elasticcache.GetElasticcacheAccounts()
  71. if err != nil {
  72. syncResult.Error(err)
  73. return syncResult
  74. }
  75. removed := make([]SElasticcacheAccount, 0)
  76. commondb := make([]SElasticcacheAccount, 0)
  77. commonext := make([]cloudprovider.ICloudElasticcacheAccount, 0)
  78. added := make([]cloudprovider.ICloudElasticcacheAccount, 0)
  79. if err := compare.CompareSets(dbAccounts, cloudElasticcacheAccounts, &removed, &commondb, &commonext, &added); err != nil {
  80. syncResult.Error(err)
  81. return syncResult
  82. }
  83. for i := 0; i < len(removed); i++ {
  84. err := removed[i].syncRemoveCloudElasticcacheAccount(ctx, userCred)
  85. if err != nil {
  86. syncResult.DeleteError(err)
  87. } else {
  88. syncResult.Delete()
  89. }
  90. }
  91. for i := 0; i < len(commondb); i++ {
  92. err := commondb[i].SyncWithCloudElasticcacheAccount(ctx, userCred, commonext[i])
  93. if err != nil {
  94. syncResult.UpdateError(err)
  95. continue
  96. }
  97. syncResult.Update()
  98. }
  99. for i := 0; i < len(added); i++ {
  100. _, err := manager.newFromCloudElasticcacheAccount(ctx, userCred, elasticcache, added[i])
  101. if err != nil {
  102. syncResult.AddError(err)
  103. continue
  104. }
  105. syncResult.Add()
  106. }
  107. return syncResult
  108. }
  109. func (self *SElasticcacheAccount) syncRemoveCloudElasticcacheAccount(ctx context.Context, userCred mcclient.TokenCredential) error {
  110. lockman.LockObject(ctx, self)
  111. defer lockman.ReleaseObject(ctx, self)
  112. err := self.ValidateDeleteCondition(ctx, nil)
  113. if err != nil {
  114. return errors.Wrapf(err, "newFromCloudElasticcacheAccount.Remove")
  115. }
  116. return self.Delete(ctx, userCred)
  117. }
  118. func (self *SElasticcacheAccount) SyncWithCloudElasticcacheAccount(ctx context.Context, userCred mcclient.TokenCredential, extAccount cloudprovider.ICloudElasticcacheAccount) error {
  119. _, err := db.UpdateWithLock(ctx, self, func() error {
  120. self.Status = extAccount.GetStatus()
  121. self.AccountType = extAccount.GetAccountType()
  122. self.AccountPrivilege = extAccount.GetAccountPrivilege()
  123. return nil
  124. })
  125. if err != nil {
  126. return errors.Wrapf(err, "SyncWithCloudElasticcacheAccount.UpdateWithLock")
  127. }
  128. return nil
  129. }
  130. func (self *SElasticcacheAccount) GetRegion() *SCloudregion {
  131. iec, err := db.FetchById(ElasticcacheManager, self.ElasticcacheId)
  132. if err != nil {
  133. return nil
  134. }
  135. region, _ := iec.(*SElasticcache).GetRegion()
  136. return region
  137. }
  138. func (self *SElasticcacheAccount) GetOwnerId() mcclient.IIdentityProvider {
  139. return ElasticcacheManager.GetOwnerIdByElasticcacheId(self.ElasticcacheId)
  140. }
  141. func (manager *SElasticcacheAccountManager) newFromCloudElasticcacheAccount(ctx context.Context, userCred mcclient.TokenCredential, elasticcache *SElasticcache, extAccount cloudprovider.ICloudElasticcacheAccount) (*SElasticcacheAccount, error) {
  142. lockman.LockClass(ctx, manager, db.GetLockClassKey(manager, userCred))
  143. defer lockman.ReleaseClass(ctx, manager, db.GetLockClassKey(manager, userCred))
  144. account := SElasticcacheAccount{}
  145. account.SetModelManager(manager, &account)
  146. account.ElasticcacheId = elasticcache.GetId()
  147. account.Name = extAccount.GetName()
  148. account.ExternalId = extAccount.GetGlobalId()
  149. account.Status = extAccount.GetStatus()
  150. account.AccountType = extAccount.GetAccountType()
  151. account.AccountPrivilege = extAccount.GetAccountPrivilege()
  152. err := manager.TableSpec().Insert(ctx, &account)
  153. if err != nil {
  154. return nil, errors.Wrapf(err, "newFromCloudElasticcacheAccount.Insert")
  155. }
  156. return &account, nil
  157. }
  158. func (self *SElasticcacheAccount) GetUniqValues() jsonutils.JSONObject {
  159. return jsonutils.Marshal(map[string]string{"elasticcache_id": self.ElasticcacheId})
  160. }
  161. func (manager *SElasticcacheAccountManager) FetchUniqValues(ctx context.Context, data jsonutils.JSONObject) jsonutils.JSONObject {
  162. cacheId := jsonutils.GetAnyString(data, []string{"elasticcache_id", "elasticcache"})
  163. return jsonutils.Marshal(map[string]string{"elasticcache_id": cacheId})
  164. }
  165. func (manager *SElasticcacheAccountManager) ResourceScope() rbacscope.TRbacScope {
  166. return rbacscope.ScopeProject
  167. }
  168. func (manager *SElasticcacheAccountManager) FetchOwnerId(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) {
  169. return elasticcacheSubResourceFetchOwnerId(ctx, data)
  170. }
  171. func (manager *SElasticcacheAccountManager) FilterByOwner(ctx context.Context, q *sqlchemy.SQuery, man db.FilterByOwnerProvider, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, scope rbacscope.TRbacScope) *sqlchemy.SQuery {
  172. return elasticcacheSubResourceFetchOwner(ctx, q, ownerId, scope)
  173. }
  174. func (manager *SElasticcacheAccountManager) FilterByUniqValues(q *sqlchemy.SQuery, values jsonutils.JSONObject) *sqlchemy.SQuery {
  175. cacheId, _ := values.GetString("elasticcache_id")
  176. if len(cacheId) > 0 {
  177. q = q.Equals("elasticcache_id", cacheId)
  178. }
  179. return q
  180. }
  181. func (manager *SElasticcacheAccountManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) {
  182. var region *SCloudregion
  183. if id, _ := data.GetString("elasticcache"); len(id) > 0 {
  184. ec, err := db.FetchByIdOrName(ctx, ElasticcacheManager, userCred, id)
  185. if err != nil {
  186. return nil, fmt.Errorf("getting elastic cache instance failed")
  187. }
  188. region, _ = ec.(*SElasticcache).GetRegion()
  189. } else {
  190. return nil, httperrors.NewMissingParameterError("elasticcache_id")
  191. }
  192. input := apis.StatusStandaloneResourceCreateInput{}
  193. var err error
  194. err = data.Unmarshal(&input)
  195. if err != nil {
  196. return nil, httperrors.NewInternalServerError("unmarshal StandaloneResourceCreateInput fail %s", err)
  197. }
  198. input, err = manager.SStatusStandaloneResourceBaseManager.ValidateCreateData(ctx, userCred, ownerId, query, input)
  199. if err != nil {
  200. return nil, err
  201. }
  202. data.Update(jsonutils.Marshal(input))
  203. passwd, _ := data.GetString("password")
  204. if reset, _ := data.Bool("reset_password"); reset && len(passwd) == 0 {
  205. passwd = seclib2.RandomPassword2(12)
  206. data.Set("password", jsonutils.NewString(passwd))
  207. }
  208. return region.GetDriver().ValidateCreateElasticcacheAccountData(ctx, userCred, ownerId, data)
  209. }
  210. func (self *SElasticcacheAccount) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
  211. self.SStatusStandaloneResourceBase.PostCreate(ctx, userCred, ownerId, query, data)
  212. if len(self.Password) > 0 {
  213. self.SavePassword(self.Password)
  214. }
  215. self.SetStatus(ctx, userCred, api.ELASTIC_CACHE_ACCOUNT_STATUS_CREATING, "")
  216. if err := self.StartElasticcacheAccountCreateTask(ctx, userCred, data.(*jsonutils.JSONDict), ""); err != nil {
  217. log.Errorf("Failed to create elastic account cache error: %v", err)
  218. }
  219. }
  220. func (self *SElasticcacheAccount) StartElasticcacheAccountCreateTask(ctx context.Context, userCred mcclient.TokenCredential, data *jsonutils.JSONDict, parentTaskId string) error {
  221. task, err := taskman.TaskManager.NewTask(ctx, "ElasticcacheAccountCreateTask", self, userCred, jsonutils.NewDict(), parentTaskId, "", nil)
  222. if err != nil {
  223. return err
  224. }
  225. task.ScheduleRun(nil)
  226. return nil
  227. }
  228. func (self *SElasticcacheAccount) GetCreateAliyunElasticcacheAccountParams() (cloudprovider.SCloudElasticCacheAccountInput, error) {
  229. ret := cloudprovider.SCloudElasticCacheAccountInput{}
  230. ret.AccountName = self.Name
  231. ret.Description = self.Description
  232. passwd, err := self.GetDecodedPassword()
  233. if err != nil {
  234. return ret, err
  235. }
  236. ret.AccountPassword = passwd
  237. switch self.AccountPrivilege {
  238. case "read":
  239. ret.AccountPrivilege = "RoleReadOnly"
  240. case "write":
  241. ret.AccountPrivilege = "RoleReadWrite"
  242. case "repl":
  243. ret.AccountPrivilege = "RoleRepl"
  244. }
  245. return ret, nil
  246. }
  247. func (self *SElasticcacheAccount) GetCreateQcloudElasticcacheAccountParams() (cloudprovider.SCloudElasticCacheAccountInput, error) {
  248. ret := cloudprovider.SCloudElasticCacheAccountInput{}
  249. ret.AccountName = self.Name
  250. ret.Description = self.Description
  251. passwd, err := self.GetDecodedPassword()
  252. if err != nil {
  253. return ret, err
  254. }
  255. ret.AccountPassword = passwd
  256. switch self.AccountPrivilege {
  257. case "read":
  258. ret.AccountPrivilege = "r"
  259. case "write":
  260. ret.AccountPrivilege = "rw"
  261. default:
  262. return ret, fmt.Errorf("ElasticcacheAccount.GetUpdateQcloudElasticcacheAccountParams invalid account_privilege %s", self.AccountPrivilege)
  263. }
  264. return ret, nil
  265. }
  266. func (self *SElasticcacheAccount) GetUpdateAliyunElasticcacheAccountParams(data jsonutils.JSONDict) (cloudprovider.SCloudElasticCacheAccountUpdateInput, error) {
  267. ret := cloudprovider.SCloudElasticCacheAccountUpdateInput{}
  268. if desc, _ := data.GetString("description"); len(desc) > 0 {
  269. ret.Description = &desc
  270. }
  271. if password, _ := data.GetString("password"); len(password) > 0 {
  272. ret.Password = &password
  273. }
  274. if ok := data.Contains("no_password_access"); ok {
  275. passwordAccess, _ := data.Bool("no_password_access")
  276. ret.NoPasswordAccess = &passwordAccess
  277. }
  278. if privilege, _ := data.GetString("account_privilege"); len(privilege) > 0 {
  279. var p string
  280. switch privilege {
  281. case "read":
  282. p = "RoleReadOnly"
  283. case "write":
  284. p = "RoleReadWrite"
  285. case "repl":
  286. p = "RoleRepl"
  287. default:
  288. return ret, fmt.Errorf("ElasticcacheAccount.GetUpdateAliyunElasticcacheAccountParams invalid account_privilege %s", privilege)
  289. }
  290. ret.AccountPrivilege = &p
  291. }
  292. return ret, nil
  293. }
  294. func (self *SElasticcacheAccount) GetUpdateHuaweiElasticcacheAccountParams(data jsonutils.JSONDict) (cloudprovider.SCloudElasticCacheAccountUpdateInput, error) {
  295. ret := cloudprovider.SCloudElasticCacheAccountUpdateInput{}
  296. if desc, _ := data.GetString("description"); len(desc) > 0 {
  297. ret.Description = &desc
  298. }
  299. if password, _ := data.GetString("password"); len(password) > 0 {
  300. ret.Password = &password
  301. }
  302. if oldpasswd, _ := data.GetString("old_password"); len(oldpasswd) > 0 {
  303. ret.OldPassword = &oldpasswd
  304. } else {
  305. oldpasswd, err := self.GetDecodedPassword()
  306. if err != nil {
  307. // can not update password, if old password is emtpy
  308. return ret, errors.Wrap(err, "ElasticcacheAccount.GetUpdateHuaweiElasticcacheAccountParams.OldPassword")
  309. }
  310. ret.OldPassword = &oldpasswd
  311. }
  312. if ok := data.Contains("no_password_access"); ok {
  313. passwordAccess, _ := data.Bool("no_password_access")
  314. ret.NoPasswordAccess = &passwordAccess
  315. }
  316. return ret, nil
  317. }
  318. func (self *SElasticcacheAccount) GetUpdateQcloudElasticcacheAccountParams(data jsonutils.JSONDict) (cloudprovider.SCloudElasticCacheAccountUpdateInput, error) {
  319. ret := cloudprovider.SCloudElasticCacheAccountUpdateInput{}
  320. if desc, _ := data.GetString("description"); len(desc) > 0 {
  321. ret.Description = &desc
  322. }
  323. if password, _ := data.GetString("password"); len(password) > 0 {
  324. ret.Password = &password
  325. }
  326. if ok := data.Contains("no_password_access"); ok {
  327. passwordAccess, _ := data.Bool("no_password_access")
  328. if self.AccountType == api.ELASTIC_CACHE_ACCOUNT_TYPE_ADMIN {
  329. ret.NoPasswordAccess = &passwordAccess
  330. } else {
  331. if passwordAccess == false {
  332. return ret, fmt.Errorf("ElasticcacheAccount.GetUpdateQcloudElasticcacheAccountParams normal account not support no auth access")
  333. }
  334. }
  335. }
  336. if privilege, _ := data.GetString("account_privilege"); len(privilege) > 0 {
  337. var p string
  338. switch privilege {
  339. case "read":
  340. p = "r"
  341. case "write":
  342. p = "rw"
  343. default:
  344. return ret, fmt.Errorf("ElasticcacheAccount.GetUpdateQcloudElasticcacheAccountParams invalid account_privilege %s", privilege)
  345. }
  346. ret.AccountPrivilege = &p
  347. }
  348. return ret, nil
  349. }
  350. func (self *SElasticcacheAccount) SavePassword(passwd string) error {
  351. passwd, err := utils.EncryptAESBase64(self.Id, passwd)
  352. if err != nil {
  353. return err
  354. }
  355. _, err = db.Update(self, func() error {
  356. self.Password = passwd
  357. return nil
  358. })
  359. return err
  360. }
  361. func (self *SElasticcacheAccount) GetDecodedPassword() (string, error) {
  362. return utils.DescryptAESBase64(self.Id, self.Password)
  363. }
  364. func (self *SElasticcacheAccount) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
  365. self.SetStatus(ctx, userCred, api.ELASTIC_CACHE_ACCOUNT_STATUS_DELETING, "")
  366. return self.StartDeleteElasticcacheAccountTask(ctx, userCred, jsonutils.NewDict(), "")
  367. }
  368. func (self *SElasticcacheAccount) StartDeleteElasticcacheAccountTask(ctx context.Context, userCred mcclient.TokenCredential, params *jsonutils.JSONDict, parentTaskId string) error {
  369. task, err := taskman.TaskManager.NewTask(ctx, "ElasticcacheAccountDeleteTask", self, userCred, params, parentTaskId, "", nil)
  370. if err != nil {
  371. return err
  372. }
  373. task.ScheduleRun(nil)
  374. return nil
  375. }
  376. func (self *SElasticcacheAccount) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
  377. return nil
  378. }
  379. func (self *SElasticcacheAccount) ValidatorResetPasswordData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  380. if reset, _ := data.Bool("reset_password"); reset {
  381. if _, err := data.GetString("password"); err != nil {
  382. randomPasswd := seclib2.RandomPassword2(12)
  383. data.(*jsonutils.JSONDict).Set("password", jsonutils.NewString(randomPasswd))
  384. }
  385. }
  386. passwd, _ := data.GetString("password")
  387. if len(passwd) > 0 {
  388. err := seclib2.ValidatePassword(passwd)
  389. if err != nil {
  390. return nil, err
  391. }
  392. }
  393. privilegeV := validators.NewStringChoicesValidator("account_privilege", choices.NewChoices(api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_READ, api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_WRITE, api.ELASTIC_CACHE_ACCOUNT_PRIVILEGE_REPL)).Optional(true)
  394. if err := privilegeV.Validate(ctx, data.(*jsonutils.JSONDict)); err != nil {
  395. return nil, err
  396. }
  397. return data, nil
  398. }
  399. func (self *SElasticcacheAccount) PerformResetPassword(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  400. self.SetStatus(ctx, userCred, api.ELASTIC_CACHE_STATUS_CHANGING, "")
  401. data, err := self.ValidatorResetPasswordData(ctx, userCred, query, data)
  402. if err != nil {
  403. return nil, err
  404. }
  405. return nil, self.StartResetPasswordTask(ctx, userCred, data.(*jsonutils.JSONDict), "")
  406. }
  407. func (self *SElasticcacheAccount) StartResetPasswordTask(ctx context.Context, userCred mcclient.TokenCredential, params *jsonutils.JSONDict, parentTaskId string) error {
  408. task, err := taskman.TaskManager.NewTask(ctx, "ElasticcacheAccountResetPasswordTask", self, userCred, params, parentTaskId, "", nil)
  409. if err != nil {
  410. return err
  411. }
  412. task.ScheduleRun(nil)
  413. return nil
  414. }
  415. func (self *SElasticcacheAccount) GetDetailsLoginInfo(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  416. ret := jsonutils.NewDict()
  417. ret.Add(jsonutils.NewString(self.Name), "username")
  418. ret.Add(jsonutils.NewString(self.Password), "password")
  419. return ret, nil
  420. }
  421. // 弹性缓存账号列表
  422. func (manager *SElasticcacheAccountManager) ListItemFilter(
  423. ctx context.Context,
  424. q *sqlchemy.SQuery,
  425. userCred mcclient.TokenCredential,
  426. input api.ElasticcacheAccountListInput,
  427. ) (*sqlchemy.SQuery, error) {
  428. var err error
  429. q, err = manager.SStatusStandaloneResourceBaseManager.ListItemFilter(ctx, q, userCred, input.StatusStandaloneResourceListInput)
  430. if err != nil {
  431. return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.ListItemFilter")
  432. }
  433. q, err = manager.SExternalizedResourceBaseManager.ListItemFilter(ctx, q, userCred, input.ExternalizedResourceBaseListInput)
  434. if err != nil {
  435. return nil, errors.Wrap(err, "SExternalizedResourceBaseManager.ListItemFilter")
  436. }
  437. q, err = manager.SElasticcacheResourceBaseManager.ListItemFilter(ctx, q, userCred, input.ElasticcacheFilterListInput)
  438. if err != nil {
  439. return nil, errors.Wrap(err, "SElasticcacheResourceBaseManager.ListItemFilter")
  440. }
  441. if len(input.AccountType) > 0 {
  442. q = q.In("account_type", input.AccountType)
  443. }
  444. if len(input.AccountPrivilege) > 0 {
  445. q = q.In("account_privilege", input.AccountPrivilege)
  446. }
  447. return q, nil
  448. }
  449. func (manager *SElasticcacheAccountManager) OrderByExtraFields(
  450. ctx context.Context,
  451. q *sqlchemy.SQuery,
  452. userCred mcclient.TokenCredential,
  453. input api.ElasticcacheAccountListInput,
  454. ) (*sqlchemy.SQuery, error) {
  455. var err error
  456. q, err = manager.SStatusStandaloneResourceBaseManager.OrderByExtraFields(ctx, q, userCred, input.StatusStandaloneResourceListInput)
  457. if err != nil {
  458. return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.OrderByExtraFields")
  459. }
  460. q, err = manager.SElasticcacheResourceBaseManager.OrderByExtraFields(ctx, q, userCred, input.ElasticcacheFilterListInput)
  461. if err != nil {
  462. return nil, errors.Wrap(err, "SElasticcacheResourceBaseManager.OrderByExtraFields")
  463. }
  464. return q, nil
  465. }
  466. func (manager *SElasticcacheAccountManager) QueryDistinctExtraField(q *sqlchemy.SQuery, field string) (*sqlchemy.SQuery, error) {
  467. var err error
  468. q, err = manager.SStatusStandaloneResourceBaseManager.QueryDistinctExtraField(q, field)
  469. if err == nil {
  470. return q, nil
  471. }
  472. q, err = manager.SElasticcacheResourceBaseManager.QueryDistinctExtraField(q, field)
  473. if err == nil {
  474. return q, nil
  475. }
  476. return q, httperrors.ErrNotFound
  477. }
  478. func (manager *SElasticcacheAccountManager) FetchCustomizeColumns(
  479. ctx context.Context,
  480. userCred mcclient.TokenCredential,
  481. query jsonutils.JSONObject,
  482. objs []interface{},
  483. fields stringutils2.SSortedStrings,
  484. isList bool,
  485. ) []api.ElasticcacheAccountDetails {
  486. rows := make([]api.ElasticcacheAccountDetails, len(objs))
  487. stdRows := manager.SStatusStandaloneResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
  488. cacheRows := manager.SElasticcacheResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList)
  489. cacheIds := make([]string, len(objs))
  490. for i := range rows {
  491. rows[i] = api.ElasticcacheAccountDetails{
  492. StatusStandaloneResourceDetails: stdRows[i],
  493. ElasticcacheResourceInfo: cacheRows[i],
  494. }
  495. account := objs[i].(*SElasticcacheAccount)
  496. cacheIds[i] = account.ElasticcacheId
  497. }
  498. caches := make(map[string]SElasticcache)
  499. err := db.FetchStandaloneObjectsByIds(ElasticcacheManager, cacheIds, &caches)
  500. if err != nil {
  501. log.Errorf("FetchStandaloneObjectsByIds fail: %v", err)
  502. return rows
  503. }
  504. virObjs := make([]interface{}, len(objs))
  505. for i := range rows {
  506. if cache, ok := caches[cacheIds[i]]; ok {
  507. virObjs[i] = &cache
  508. rows[i].ProjectId = cache.ProjectId
  509. }
  510. }
  511. projRows := ElasticcacheManager.SProjectizedResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, virObjs, fields, isList)
  512. for i := range rows {
  513. rows[i].ProjectizedResourceInfo = projRows[i]
  514. }
  515. return rows
  516. }
  517. func (manager *SElasticcacheAccountManager) ListItemExportKeys(ctx context.Context,
  518. q *sqlchemy.SQuery,
  519. userCred mcclient.TokenCredential,
  520. keys stringutils2.SSortedStrings,
  521. ) (*sqlchemy.SQuery, error) {
  522. var err error
  523. q, err = manager.SStatusStandaloneResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
  524. if err != nil {
  525. return nil, errors.Wrap(err, "SStatusStandaloneResourceBaseManager.ListItemExportKeys")
  526. }
  527. if keys.ContainsAny(manager.SElasticcacheResourceBaseManager.GetExportKeys()...) {
  528. q, err = manager.SElasticcacheResourceBaseManager.ListItemExportKeys(ctx, q, userCred, keys)
  529. if err != nil {
  530. return nil, errors.Wrap(err, "SElasticcacheResourceBaseManager.ListItemExportKeys")
  531. }
  532. }
  533. return q, nil
  534. }