elasticcache_allocate_public_connection_task.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 ElasticcacheAllocatePublicConnectionTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(ElasticcacheAllocatePublicConnectionTask{})
  32. }
  33. func (self *ElasticcacheAllocatePublicConnectionTask) taskFail(ctx context.Context, elasticcache *models.SElasticcache, err error) {
  34. elasticcache.SetStatus(ctx, self.GetUserCred(), api.ELASTIC_CACHE_STATUS_CHANGING, err.Error())
  35. db.OpsLog.LogEvent(elasticcache, db.ACT_ALLOCATE_FAIL, err, self.UserCred)
  36. logclient.AddActionLogWithStartable(self, elasticcache, logclient.ACT_ALLOCATE, err, self.UserCred, false)
  37. notifyclient.NotifySystemErrorWithCtx(ctx, elasticcache.Id, elasticcache.Name, api.ELASTIC_CACHE_STATUS_CHANGE_FAILED, err.Error())
  38. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  39. }
  40. func (self *ElasticcacheAllocatePublicConnectionTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  41. elasticcache := obj.(*models.SElasticcache)
  42. region, err := elasticcache.GetRegion()
  43. if err != nil {
  44. self.taskFail(ctx, elasticcache, errors.Wrapf(err, "GetRegion"))
  45. return
  46. }
  47. self.SetStage("OnElasticcacheAllocatePublicConnectionComplete", nil)
  48. err = region.GetDriver().RequestElasticcacheAllocatePublicConnection(ctx, self.GetUserCred(), elasticcache, self)
  49. if err != nil {
  50. self.OnElasticcacheAllocatePublicConnectionCompleteFailed(ctx, elasticcache, jsonutils.NewString(err.Error()))
  51. return
  52. }
  53. }
  54. func (self *ElasticcacheAllocatePublicConnectionTask) OnElasticcacheAllocatePublicConnectionComplete(ctx context.Context, elasticcache *models.SElasticcache, data jsonutils.JSONObject) {
  55. logclient.AddActionLogWithStartable(self, elasticcache, logclient.ACT_ALLOCATE, "allocate public connection", self.UserCred, true)
  56. self.SetStageComplete(ctx, nil)
  57. }
  58. func (self *ElasticcacheAllocatePublicConnectionTask) OnElasticcacheAllocatePublicConnectionCompleteFailed(ctx context.Context, elasticcache *models.SElasticcache, reason jsonutils.JSONObject) {
  59. self.taskFail(ctx, elasticcache, fmt.Errorf("%s", reason.String()))
  60. }