elasticcache_create_task.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/cloudcommon/notifyclient"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type ElasticcacheCreateTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(ElasticcacheCreateTask{})
  32. }
  33. func (self *ElasticcacheCreateTask) taskFail(ctx context.Context, elasticcache *models.SElasticcache, err error) {
  34. elasticcache.SetStatus(ctx, self.GetUserCred(), api.ELASTIC_CACHE_STATUS_CREATE_FAILED, err.Error())
  35. db.OpsLog.LogEvent(elasticcache, db.ACT_ALLOCATE_FAIL, err, self.UserCred)
  36. logclient.AddActionLogWithStartable(self, elasticcache, logclient.ACT_CREATE, err, self.UserCred, false)
  37. notifyclient.EventNotify(ctx, self.GetUserCred(), notifyclient.SEventNotifyParam{
  38. Obj: elasticcache,
  39. Action: notifyclient.ActionCreate,
  40. IsFail: true,
  41. })
  42. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  43. }
  44. func (self *ElasticcacheCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  45. elasticcache := obj.(*models.SElasticcache)
  46. region, err := elasticcache.GetRegion()
  47. if err != nil {
  48. self.taskFail(ctx, elasticcache, errors.Wrapf(err, "GetRegion"))
  49. return
  50. }
  51. // sync security group here
  52. self.SetStage("OnSyncSecurityGroupComplete", data.(*jsonutils.JSONDict))
  53. if region.GetDriver().IsSupportedElasticcacheSecgroup() {
  54. secgroups := []string{}
  55. self.GetParams().Unmarshal(&secgroups, "secgroup_ids")
  56. secgroupInput := api.ElasticcacheSecgroupsInput{SecgroupIds: secgroups}
  57. _, err = elasticcache.ProcessElasticcacheSecgroupsInput(ctx, self.UserCred, "set", &secgroupInput)
  58. if err != nil {
  59. self.taskFail(ctx, elasticcache, errors.Wrapf(err, "ProcessElasticcacheSecgroupsInput"))
  60. return
  61. }
  62. if err := region.GetDriver().RequestSyncSecgroupsForElasticcache(ctx, self.UserCred, elasticcache, self); err != nil {
  63. self.taskFail(ctx, elasticcache, errors.Wrapf(err, "RequestSyncSecgroupsForElasticcache"))
  64. return
  65. }
  66. } else {
  67. self.OnSyncSecurityGroupComplete(ctx, elasticcache, data)
  68. }
  69. }
  70. func (self *ElasticcacheCreateTask) OnSyncSecurityGroupComplete(ctx context.Context, elasticcache *models.SElasticcache, data jsonutils.JSONObject) {
  71. region, err := elasticcache.GetRegion()
  72. if err != nil {
  73. self.taskFail(ctx, elasticcache, errors.Wrapf(err, "GetRegion"))
  74. return
  75. }
  76. self.SetStage("OnElasticcacheCreateComplete", nil)
  77. err = region.GetDriver().RequestCreateElasticcache(ctx, self.GetUserCred(), elasticcache, self, data.(*jsonutils.JSONDict))
  78. if err != nil {
  79. self.taskFail(ctx, elasticcache, errors.Wrapf(err, "RequestCreateElasticcache"))
  80. return
  81. }
  82. }
  83. func (self *ElasticcacheCreateTask) OnSyncSecurityGroupCompleteFailed(ctx context.Context, elasticcache *models.SElasticcache, reason jsonutils.JSONObject) {
  84. self.taskFail(ctx, elasticcache, fmt.Errorf("%s", reason.String()))
  85. }
  86. func (self *ElasticcacheCreateTask) OnElasticcacheCreateComplete(ctx context.Context, elasticcache *models.SElasticcache, data jsonutils.JSONObject) {
  87. elasticcache.SetStatus(ctx, self.GetUserCred(), api.ELASTIC_CACHE_STATUS_RUNNING, "")
  88. logclient.AddActionLogWithStartable(self, elasticcache, logclient.ACT_CREATE, "", self.UserCred, true)
  89. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  90. Obj: elasticcache,
  91. Action: notifyclient.ActionCreate,
  92. })
  93. self.SetStageComplete(ctx, nil)
  94. }
  95. func (self *ElasticcacheCreateTask) OnElasticcacheCreateCompleteFailed(ctx context.Context, elasticcache *models.SElasticcache, reason jsonutils.JSONObject) {
  96. self.taskFail(ctx, elasticcache, fmt.Errorf("%s", reason.String()))
  97. }