dbinstance_account_set_privileges_task.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/log"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/onecloud/pkg/apis/compute"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  25. "yunion.io/x/onecloud/pkg/compute/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type DBInstanceAccountSetPrivilegesTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(DBInstanceAccountSetPrivilegesTask{})
  33. }
  34. func (self *DBInstanceAccountSetPrivilegesTask) taskFailed(ctx context.Context, account *models.SDBInstanceAccount, err error) {
  35. account.SetStatus(ctx, self.UserCred, api.DBINSTANCE_USER_AVAILABLE, err.Error())
  36. db.OpsLog.LogEvent(account, db.ACT_SET_PRIVILEGES, err, self.GetUserCred())
  37. logclient.AddActionLogWithStartable(self, account, logclient.ACT_SET_PRIVILEGES, err, self.UserCred, false)
  38. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. }
  40. func (self *DBInstanceAccountSetPrivilegesTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  41. account := obj.(*models.SDBInstanceAccount)
  42. instance, err := account.GetDBInstance()
  43. if err != nil {
  44. self.taskFailed(ctx, account, errors.Wrap(err, "account.GetDBInstance"))
  45. return
  46. }
  47. iRds, err := instance.GetIDBInstance(ctx)
  48. if err != nil {
  49. self.taskFailed(ctx, account, errors.Wrap(err, "instance.GetIDBInstance"))
  50. return
  51. }
  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 {
  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. grant := map[string]string{}
  69. revoke := map[string]string{}
  70. params := self.GetParams()
  71. params.Unmarshal(&grant, "grant")
  72. params.Unmarshal(&revoke, "revoke")
  73. for db, privilege := range revoke {
  74. database, err := instance.GetDBInstanceDatabase(db)
  75. if err != nil {
  76. log.Errorf("failed to found database %s for instance %s(%s): %v", db, instance.Name, instance.Id, err)
  77. logclient.AddActionLogWithStartable(self, account, logclient.ACT_GRANT_PRIVILEGE, err.Error(), self.UserCred, false)
  78. continue
  79. }
  80. err = iAccount.RevokePrivilege(database.Name)
  81. if err != nil {
  82. log.Errorf("failed to revoke privilege %s for account %s(%s) error: %v", privilege, account.Name, account.Id, err)
  83. logclient.AddActionLogWithStartable(self, account, logclient.ACT_REVOKE_PRIVILEGE, err.Error(), self.UserCred, false)
  84. continue
  85. }
  86. dbPrivilege, _ := instance.GetDBInstancePrivilege(account.Id, database.Id)
  87. if dbPrivilege != nil {
  88. dbPrivilege.Delete(ctx, self.UserCred)
  89. }
  90. }
  91. for db, privilege := range grant {
  92. database, err := instance.GetDBInstanceDatabase(db)
  93. if err != nil {
  94. log.Errorf("failed to found database %s for instance %s(%s): %v", db, instance.Name, instance.Id, err)
  95. logclient.AddActionLogWithStartable(self, account, logclient.ACT_GRANT_PRIVILEGE, err.Error(), self.UserCred, false)
  96. continue
  97. }
  98. err = iAccount.GrantPrivilege(database.Name, privilege)
  99. if err != nil {
  100. log.Errorf("failed to grant privilege %s for account %s(%s) error: %v", privilege, account.Name, account.Id, err)
  101. logclient.AddActionLogWithStartable(self, account, logclient.ACT_GRANT_PRIVILEGE, err.Error(), self.UserCred, false)
  102. continue
  103. }
  104. pri := models.SDBInstancePrivilege{
  105. Privilege: privilege,
  106. DBInstanceaccountId: account.Id,
  107. DBInstancedatabaseId: database.Id,
  108. }
  109. pri.ExternalId = fmt.Sprintf("%s/%s/%s", account.Name, database.Name, pri.Privilege)
  110. models.DBInstancePrivilegeManager.TableSpec().Insert(ctx, &pri)
  111. logclient.AddActionLogWithStartable(self, account, logclient.ACT_GRANT_PRIVILEGE, nil, self.UserCred, true)
  112. }
  113. account.SetStatus(ctx, self.UserCred, api.DBINSTANCE_USER_AVAILABLE, "")
  114. logclient.AddActionLogWithStartable(self, account, logclient.ACT_REVOKE_PRIVILEGE, nil, self.UserCred, true)
  115. self.SetStageComplete(ctx, nil)
  116. }