cloudgroup_set_policies_task.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 CloudgroupSetPoliciesTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(CloudgroupSetPoliciesTask{})
  33. }
  34. func (self *CloudgroupSetPoliciesTask) taskFailed(ctx context.Context, group *models.SCloudgroup, err error) {
  35. group.SetStatus(ctx, self.GetUserCred(), apis.STATUS_UNKNOWN, err.Error())
  36. logclient.AddActionLogWithStartable(self, group, logclient.ACT_SYNC_CONF, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *CloudgroupSetPoliciesTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  40. group := obj.(*models.SCloudgroup)
  41. roles, err := group.GetCloudroles()
  42. if err != nil {
  43. self.taskFailed(ctx, group, errors.Wrapf(err, "GetCloudroles"))
  44. return
  45. }
  46. iGroup, err := group.GetICloudgroup()
  47. if err != nil {
  48. self.taskFailed(ctx, group, errors.Wrap(err, "GetICloudgroup"))
  49. return
  50. }
  51. input := struct {
  52. Add []api.SPolicy
  53. Del []api.SPolicy
  54. }{}
  55. err = self.GetParams().Unmarshal(&input)
  56. if err != nil {
  57. self.taskFailed(ctx, group, errors.Wrapf(err, "Unmarshal"))
  58. return
  59. }
  60. iRoleMap := map[string]cloudprovider.ICloudrole{}
  61. roleMap := map[string]*models.SCloudrole{}
  62. for i := range roles {
  63. roleMap[roles[i].Id] = &roles[i]
  64. iRole, err := roles[i].GetICloudrole()
  65. if err == nil {
  66. iRoleMap[roles[i].Id] = iRole
  67. }
  68. }
  69. for _, policy := range input.Add {
  70. for id, role := range iRoleMap {
  71. err := role.AttachPolicy(policy.ExternalId, cloudid.TPolicyType(policy.PolicyType))
  72. if err != nil {
  73. logclient.AddSimpleActionLog(roleMap[id], logclient.ACT_ATTACH_POLICY, err, self.GetUserCred(), false)
  74. }
  75. }
  76. err = iGroup.AttachPolicy(policy.ExternalId, cloudid.TPolicyType(policy.PolicyType))
  77. if err != nil {
  78. self.taskFailed(ctx, group, errors.Wrapf(err, "AttachPolicy %s", policy.Name))
  79. return
  80. }
  81. }
  82. for _, policy := range input.Del {
  83. for id, role := range iRoleMap {
  84. err := role.DetachPolicy(policy.ExternalId, cloudid.TPolicyType(policy.PolicyType))
  85. if err != nil {
  86. logclient.AddSimpleActionLog(roleMap[id], logclient.ACT_DETACH_POLICY, err, self.GetUserCred(), false)
  87. }
  88. }
  89. err = iGroup.DetachPolicy(policy.ExternalId, cloudid.TPolicyType(policy.PolicyType))
  90. if err != nil {
  91. self.taskFailed(ctx, group, errors.Wrapf(err, "DetachPolicy %s", policy.Name))
  92. return
  93. }
  94. }
  95. self.taskComplete(ctx, group, iGroup)
  96. }
  97. func (self *CloudgroupSetPoliciesTask) taskComplete(ctx context.Context, group *models.SCloudgroup, iGroup cloudprovider.ICloudgroup) {
  98. group.SyncCloudpolicies(ctx, self.GetUserCred(), iGroup)
  99. group.SetStatus(ctx, self.GetUserCred(), apis.STATUS_AVAILABLE, "")
  100. self.SetStageComplete(ctx, nil)
  101. }