filesystem_create_task.go 3.7 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 filesystem
  15. import (
  16. "context"
  17. "strings"
  18. "time"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  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 FileSystemCreateTask struct {
  31. taskman.STask
  32. }
  33. func init() {
  34. taskman.RegisterTask(FileSystemCreateTask{})
  35. }
  36. func (self *FileSystemCreateTask) taskFailed(ctx context.Context, fs *models.SFileSystem, err error) {
  37. fs.SetStatus(ctx, self.UserCred, api.NAS_STATUS_CREATE_FAILED, err.Error())
  38. logclient.AddActionLogWithStartable(self, fs, logclient.ACT_ALLOCATE, err, self.UserCred, false)
  39. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  40. }
  41. func (self *FileSystemCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  42. fs := obj.(*models.SFileSystem)
  43. iRegion, err := fs.GetIRegion(ctx)
  44. if err != nil {
  45. self.taskFailed(ctx, fs, errors.Wrapf(err, "fs.GetIRegion"))
  46. return
  47. }
  48. zone, _ := fs.GetZone()
  49. opts := &cloudprovider.FileSystemCraeteOptions{
  50. Name: fs.Name,
  51. Desc: fs.Description,
  52. Capacity: fs.Capacity,
  53. StorageType: fs.StorageType,
  54. Protocol: fs.Protocol,
  55. FileSystemType: fs.FileSystemType,
  56. ZoneId: strings.TrimPrefix(zone.ExternalId, iRegion.GetGlobalId()+"/"),
  57. }
  58. netId := jsonutils.GetAnyString(self.GetParams(), []string{"network_id"})
  59. if len(netId) > 0 {
  60. net, err := models.NetworkManager.FetchById(netId)
  61. if err != nil {
  62. self.taskFailed(ctx, fs, errors.Wrapf(err, "NetworkManager.FetchById(%s)", netId))
  63. return
  64. }
  65. network := net.(*models.SNetwork)
  66. opts.NetworkId = network.ExternalId
  67. vpc, _ := network.GetVpc()
  68. opts.VpcId = vpc.ExternalId
  69. }
  70. log.Infof("nas create params: %s", jsonutils.Marshal(opts).String())
  71. iFs, err := iRegion.CreateICloudFileSystem(opts)
  72. if err != nil {
  73. self.taskFailed(ctx, fs, errors.Wrapf(err, "iRegion.CreateICloudFileSystem"))
  74. return
  75. }
  76. db.SetExternalId(fs, self.GetUserCred(), iFs.GetGlobalId())
  77. cloudprovider.WaitMultiStatus(iFs, []string{api.NAS_STATUS_AVAILABLE, api.NAS_STATUS_CREATE_FAILED}, time.Second*5, time.Minute*10)
  78. tags, _ := fs.GetAllUserMetadata()
  79. if len(tags) > 0 {
  80. err = iFs.SetTags(tags, true)
  81. if err != nil {
  82. logclient.AddActionLogWithStartable(self, fs, logclient.ACT_UPDATE, errors.Wrapf(err, "SetTags"), self.UserCred, false)
  83. }
  84. }
  85. notifyclient.EventNotify(ctx, self.UserCred, notifyclient.SEventNotifyParam{
  86. Obj: self,
  87. Action: notifyclient.ActionCreate,
  88. })
  89. self.SetStage("OnSyncstatusComplete", nil)
  90. fs.StartSyncstatus(ctx, self.GetUserCred(), self.GetTaskId())
  91. }
  92. func (self *FileSystemCreateTask) OnSyncstatusComplete(ctx context.Context, fs *models.SFileSystem, data jsonutils.JSONObject) {
  93. self.SetStageComplete(ctx, nil)
  94. }
  95. func (self *FileSystemCreateTask) OnSyncstatusCompleteFailed(ctx context.Context, fs *models.SFileSystem, data jsonutils.JSONObject) {
  96. self.SetStageFailed(ctx, data)
  97. }