dbinstance_account_grant_privilege_task.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 DBInstanceAccountGrantPrivilegeTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(DBInstanceAccountGrantPrivilegeTask{})
  32. }
  33. func (self *DBInstanceAccountGrantPrivilegeTask) 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_GRANT_PRIVILEGE, err.Error(), self.GetUserCred())
  36. logclient.AddActionLogWithStartable(self, account, logclient.ACT_GRANT_PRIVILEGE, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *DBInstanceAccountGrantPrivilegeTask) 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. accounts, err := iRds.GetIDBInstanceAccounts()
  52. if err != nil {
  53. self.taskFailed(ctx, account, errors.Wrap(err, "iRds.GetIDBInstanceAccounts"))
  54. return
  55. }
  56. databaseStr, _ := self.GetParams().GetString("database")
  57. privilegeStr, _ := self.GetParams().GetString("privilege")
  58. database, err := instance.GetDBInstanceDatabase(databaseStr)
  59. if err != nil {
  60. self.taskFailed(ctx, account, errors.Wrapf(err, "instance.GetDBInstanceDatabase(%s)", databaseStr))
  61. return
  62. }
  63. var iAccount cloudprovider.ICloudDBInstanceAccount = nil
  64. for _, ac := range accounts {
  65. if ac.GetName() == account.Name {
  66. iAccount = ac
  67. }
  68. }
  69. if iAccount == nil {
  70. self.taskFailed(ctx, account, fmt.Errorf("failed to found iAccount by %s", account.Name))
  71. return
  72. }
  73. err = iAccount.GrantPrivilege(databaseStr, privilegeStr)
  74. if err != nil {
  75. self.taskFailed(ctx, account, errors.Wrap(err, "ac.GrantPrivilege"))
  76. return
  77. }
  78. privilege := models.SDBInstancePrivilege{
  79. Privilege: privilegeStr,
  80. DBInstanceaccountId: account.Id,
  81. DBInstancedatabaseId: database.Id,
  82. }
  83. privilege.ExternalId = fmt.Sprintf("%s/%s/%s", account.Name, database.Name, privilege.Privilege)
  84. models.DBInstancePrivilegeManager.TableSpec().Insert(ctx, &privilege)
  85. account.SetStatus(ctx, self.UserCred, api.DBINSTANCE_USER_AVAILABLE, "")
  86. logclient.AddActionLogWithStartable(self, account, logclient.ACT_GRANT_PRIVILEGE, nil, self.UserCred, true)
  87. self.SetStageComplete(ctx, nil)
  88. }