clouduser_set_policies_task.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 tasks
  15. import (
  16. "context"
  17. "yunion.io/x/cloudmux/pkg/apis"
  18. "yunion.io/x/cloudmux/pkg/apis/cloudid"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/onecloud/pkg/apis/cloudid"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  25. "yunion.io/x/onecloud/pkg/cloudid/models"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type ClouduserSetPoliciesTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(ClouduserSetPoliciesTask{})
  33. }
  34. func (self *ClouduserSetPoliciesTask) taskFailed(ctx context.Context, user *models.SClouduser, err error) {
  35. user.SetStatus(ctx, self.GetUserCred(), apis.STATUS_UNKNOWN, err.Error())
  36. logclient.AddActionLogWithStartable(self, user, logclient.ACT_SYNC_CONF, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *ClouduserSetPoliciesTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  40. user := obj.(*models.SClouduser)
  41. iUser, err := user.GetIClouduser()
  42. if err != nil {
  43. self.taskFailed(ctx, user, errors.Wrap(err, "GetIClouduser"))
  44. return
  45. }
  46. input := struct {
  47. Add []api.SPolicy
  48. Del []api.SPolicy
  49. }{}
  50. err = self.GetParams().Unmarshal(&input)
  51. if err != nil {
  52. self.taskFailed(ctx, user, errors.Wrapf(err, "Unmarshal"))
  53. return
  54. }
  55. for _, policy := range input.Add {
  56. err = iUser.AttachPolicy(policy.ExternalId, cloudid.TPolicyType(policy.PolicyType))
  57. if err != nil {
  58. self.taskFailed(ctx, user, errors.Wrapf(err, "AttachPolicy %s", policy.Name))
  59. return
  60. }
  61. }
  62. for _, policy := range input.Del {
  63. err = iUser.DetachPolicy(policy.ExternalId, cloudid.TPolicyType(policy.PolicyType))
  64. if err != nil {
  65. self.taskFailed(ctx, user, errors.Wrapf(err, "DetachPolicy %s", policy.Name))
  66. return
  67. }
  68. }
  69. self.taskComplete(ctx, user, iUser)
  70. }
  71. func (self *ClouduserSetPoliciesTask) taskComplete(ctx context.Context, user *models.SClouduser, iUser cloudprovider.IClouduser) {
  72. user.SyncCloudpolicies(ctx, self.GetUserCred(), iUser)
  73. user.SetStatus(ctx, self.GetUserCred(), apis.STATUS_AVAILABLE, "")
  74. self.SetStageComplete(ctx, nil)
  75. }