global_vpc_create_task.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 global_vpc
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  26. "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
  27. "yunion.io/x/onecloud/pkg/compute/models"
  28. "yunion.io/x/onecloud/pkg/util/logclient"
  29. )
  30. type GlobalVpcCreateTask struct {
  31. taskman.STask
  32. }
  33. func init() {
  34. taskman.RegisterTask(GlobalVpcCreateTask{})
  35. }
  36. func (self *GlobalVpcCreateTask) taskFailed(ctx context.Context, gvpc *models.SGlobalVpc, err error) {
  37. gvpc.SetStatus(ctx, self.UserCred, apis.STATUS_CREATE_FAILED, err.Error())
  38. logclient.AddActionLogWithStartable(self, gvpc, logclient.ACT_ALLOCATE, err, self.UserCred, false)
  39. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  40. }
  41. func (self *GlobalVpcCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  42. gvpc := obj.(*models.SGlobalVpc)
  43. opts := &cloudprovider.GlobalVpcCreateOptions{
  44. NAME: gvpc.Name,
  45. Desc: gvpc.Description,
  46. }
  47. log.Infof("global vpc create params: %s", jsonutils.Marshal(opts).String())
  48. provider, err := gvpc.GetDriver(ctx)
  49. if err != nil {
  50. self.taskFailed(ctx, gvpc, errors.Wrapf(err, "GetDriver"))
  51. return
  52. }
  53. iVpc, err := provider.CreateICloudGlobalVpc(opts)
  54. if err != nil {
  55. self.taskFailed(ctx, gvpc, errors.Wrapf(err, "CreateICloudGlobalVpc"))
  56. return
  57. }
  58. db.SetExternalId(gvpc, self.GetUserCred(), iVpc.GetGlobalId())
  59. cloudprovider.WaitMultiStatus(iVpc, []string{
  60. api.GLOBAL_VPC_STATUS_AVAILABLE,
  61. apis.STATUS_CREATE_FAILED,
  62. apis.STATUS_UNKNOWN,
  63. }, time.Second*5, time.Minute*10)
  64. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  65. Obj: self,
  66. Action: notifyclient.ActionCreate,
  67. })
  68. self.SetStage("OnSyncstatusComplete", nil)
  69. gvpc.StartSyncstatusTask(ctx, self.GetUserCred(), self.GetTaskId())
  70. }
  71. func (self *GlobalVpcCreateTask) OnSyncstatusComplete(ctx context.Context, gvpc *models.SGlobalVpc, data jsonutils.JSONObject) {
  72. self.SetStageComplete(ctx, nil)
  73. }
  74. func (self *GlobalVpcCreateTask) OnSyncstatusCompleteFailed(ctx context.Context, gvpc *models.SGlobalVpc, data jsonutils.JSONObject) {
  75. self.SetStageFailed(ctx, data)
  76. }