dbinstance_account_revoke_privilege_task.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 dbinstance
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type DBInstanceAccountRevokePrivilegeTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(DBInstanceAccountRevokePrivilegeTask{})
  32. }
  33. func (self *DBInstanceAccountRevokePrivilegeTask) taskFailed(ctx context.Context, account *models.SDBInstanceAccount, err error) {
  34. account.SetStatus(ctx, self.UserCred, api.DBINSTANCE_USER_AVAILABLE, err.Error())
  35. db.OpsLog.LogEvent(account, db.ACT_REVOKE_PRIVILEGE, err, self.GetUserCred())
  36. logclient.AddActionLogWithStartable(self, account, logclient.ACT_REVOKE_PRIVILEGE, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *DBInstanceAccountRevokePrivilegeTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  40. account := obj.(*models.SDBInstanceAccount)
  41. instance, err := account.GetDBInstance()
  42. if err != nil {
  43. self.taskFailed(ctx, account, errors.Wrap(err, "account.GetDBInstance"))
  44. return
  45. }
  46. iRds, err := instance.GetIDBInstance(ctx)
  47. if err != nil {
  48. self.taskFailed(ctx, account, errors.Wrap(err, "instance.GetIDBInstance"))
  49. return
  50. }
  51. databaseStr, _ := self.GetParams().GetString("database")
  52. accounts, err := iRds.GetIDBInstanceAccounts()
  53. if err != nil {
  54. self.taskFailed(ctx, account, errors.Wrap(err, "iRds.GetIDBInstanceAccounts"))
  55. return
  56. }
  57. var iAccount cloudprovider.ICloudDBInstanceAccount = nil
  58. for _, ac := range accounts {
  59. if ac.GetName() == account.Name && ac.GetHost() == account.Host {
  60. iAccount = ac
  61. break
  62. }
  63. }
  64. if iAccount == nil {
  65. self.taskFailed(ctx, account, fmt.Errorf("failed to found iAccount by %s", account.Name))
  66. return
  67. }
  68. err = iAccount.RevokePrivilege(databaseStr)
  69. if err != nil {
  70. self.taskFailed(ctx, account, errors.Wrap(err, "iAccount.RevokePrivilege"))
  71. return
  72. }
  73. dbPrivilege, _ := instance.GetDBInstancePrivilege(account.Id, databaseStr)
  74. if dbPrivilege != nil {
  75. dbPrivilege.Delete(ctx, self.UserCred)
  76. }
  77. account.SetStatus(ctx, self.UserCred, api.DBINSTANCE_USER_AVAILABLE, "")
  78. logclient.AddActionLogWithStartable(self, account, logclient.ACT_REVOKE_PRIVILEGE, nil, self.UserCred, true)
  79. self.SetStageComplete(ctx, nil)
  80. }