elasticcache_syncsecgroups_task.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 elasticcache
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/util/logclient"
  25. )
  26. type ElasticcacheSyncsecgroupsTask struct {
  27. taskman.STask
  28. }
  29. func init() {
  30. taskman.RegisterTask(ElasticcacheSyncsecgroupsTask{})
  31. }
  32. func (self *ElasticcacheSyncsecgroupsTask) taskFailed(ctx context.Context, cache *models.SElasticcache, err error) {
  33. cache.SetStatus(ctx, self.GetUserCred(), api.ELASTIC_CACHE_STATUS_SYNC_FAILED, err.Error())
  34. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  35. db.OpsLog.LogEvent(cache, db.ACT_SYNC_CONF, cache.GetShortDesc(ctx), self.GetUserCred())
  36. logclient.AddActionLogWithContext(ctx, cache, logclient.ACT_SYNC_CONF, err, self.UserCred, false)
  37. }
  38. func (self *ElasticcacheSyncsecgroupsTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  39. cache := obj.(*models.SElasticcache)
  40. region, err := cache.GetRegion()
  41. if err != nil {
  42. self.taskFailed(ctx, cache, errors.Wrapf(err, "GetRegion"))
  43. return
  44. }
  45. self.SetStage("OnElasticcacheSyncSecgroupsComplete", nil)
  46. err = region.GetDriver().RequestSyncSecgroupsForElasticcache(ctx, self.GetUserCred(), cache, self)
  47. if err != nil {
  48. self.taskFailed(ctx, cache, errors.Wrapf(err, "RequestSyncSecgroupsForElasticcache"))
  49. return
  50. }
  51. }
  52. // https://cloud.tencent.com/document/api/239/41256
  53. func (self *ElasticcacheSyncsecgroupsTask) OnElasticcacheSyncSecgroupsComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  54. cache := obj.(*models.SElasticcache)
  55. secgroups := []string{}
  56. err := data.Unmarshal(&secgroups, "ext_secgroup_ids")
  57. if err != nil {
  58. self.taskFailed(ctx, cache, errors.Wrapf(err, "get ext_secgroup_ids"))
  59. return
  60. }
  61. iregion, err := cache.GetIRegion(ctx)
  62. if err != nil {
  63. self.taskFailed(ctx, cache, errors.Wrapf(err, "GetIRegion"))
  64. return
  65. }
  66. iec, err := iregion.GetIElasticcacheById(cache.GetExternalId())
  67. if err != nil {
  68. self.taskFailed(ctx, cache, errors.Wrapf(err, "GetIElasticcacheById"))
  69. return
  70. }
  71. err = iec.UpdateSecurityGroups(secgroups)
  72. if err != nil {
  73. self.taskFailed(ctx, cache, errors.Wrapf(err, "UpdateSecurityGroups"))
  74. return
  75. }
  76. cache.SetStatus(ctx, self.UserCred, iec.GetStatus(), "UpdateSecurityGroups")
  77. self.SetStageComplete(ctx, nil)
  78. }
  79. func (self *ElasticcacheSyncsecgroupsTask) OnElasticcacheSyncSecgroupsCompleteFailed(ctx context.Context, cache *models.SElasticcache, data jsonutils.JSONObject) {
  80. self.taskFailed(ctx, cache, fmt.Errorf("%s", data.String()))
  81. }