guest_save_template_task.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 guest
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. billing_api "yunion.io/x/onecloud/pkg/apis/billing"
  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/mcclient/auth"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  26. "yunion.io/x/onecloud/pkg/util/logclient"
  27. )
  28. type GuestSaveTemplateTask struct {
  29. taskman.STask
  30. }
  31. func init() {
  32. taskman.RegisterTask(GuestSaveTemplateTask{})
  33. }
  34. func (self *GuestSaveTemplateTask) taskFailed(ctx context.Context, g *models.SGuest, reason jsonutils.JSONObject) {
  35. g.SetStatus(ctx, self.UserCred, api.VM_TEMPLATE_SAVE_FAILED, reason.String())
  36. logclient.AddActionLogWithStartable(self, g, logclient.ACT_SAVE_TO_TEMPLATE, reason, self.UserCred, false)
  37. self.SetStageFailed(ctx, reason)
  38. }
  39. func (self *GuestSaveTemplateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  40. g := obj.(*models.SGuest)
  41. ci := g.ToCreateInput(ctx, self.UserCred)
  42. // Information not to be saved:
  43. // 1. Expiry release information
  44. if ci.BillingType == billing_api.BILLING_TYPE_POSTPAID {
  45. ci.Duration = ""
  46. }
  47. gtName, _ := self.Params.GetString("name")
  48. genGtName, _ := self.Params.GetString("generate_name")
  49. dict := jsonutils.NewDict()
  50. if len(genGtName) > 0 {
  51. dict.Set("generate_name", jsonutils.NewString(genGtName))
  52. } else {
  53. dict.Set("name", jsonutils.NewString(gtName))
  54. }
  55. dict.Set("description", jsonutils.NewString(fmt.Sprintf("Save from Guest '%s'", g.Name)))
  56. dict.Set("content", jsonutils.Marshal(ci))
  57. session := auth.GetSession(ctx, self.UserCred, "")
  58. _, err := compute.GuestTemplate.Create(session, dict)
  59. if err != nil {
  60. self.taskFailed(ctx, g, jsonutils.NewString(err.Error()))
  61. return
  62. }
  63. logclient.AddActionLogWithStartable(self, g, logclient.ACT_SAVE_TO_TEMPLATE, "", self.UserCred, true)
  64. // sync status
  65. self.SetStageComplete(ctx, nil)
  66. g.StartSyncstatus(ctx, self.UserCred, "")
  67. }