baremetal_delete_task.go 2.5 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 baremetal
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/util/httputils"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. )
  26. type BaremetalDeleteTask struct {
  27. SBaremetalBaseTask
  28. }
  29. func init() {
  30. taskman.RegisterTask(BaremetalDeleteTask{})
  31. }
  32. func (self *BaremetalDeleteTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  33. baremetal := obj.(*models.SHost)
  34. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_DELETE, "")
  35. if !baremetal.IsBaremetalAgentReady() {
  36. self.OnDeleteBaremetalComplete(ctx, baremetal, nil)
  37. return
  38. }
  39. url := fmt.Sprintf("/baremetals/%s/delete", baremetal.Id)
  40. headers := self.GetTaskRequestHeader()
  41. self.SetStage("OnDeleteBaremetalComplete", nil)
  42. _, err := baremetal.BaremetalSyncRequest(ctx, "POST", url, headers, nil)
  43. if err != nil {
  44. if errVal, ok := err.(*httputils.JSONClientError); ok && errVal.Code == 404 {
  45. self.OnDeleteBaremetalComplete(ctx, baremetal, nil)
  46. return
  47. }
  48. log.Errorln(err.Error())
  49. self.OnFailure(ctx, baremetal, jsonutils.NewString(err.Error()))
  50. }
  51. }
  52. func (self *BaremetalDeleteTask) OnDeleteBaremetalComplete(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  53. err := baremetal.RealDelete(ctx, self.UserCred)
  54. if err != nil {
  55. log.Errorf("RealDelete fail %s", err)
  56. self.OnFailure(ctx, baremetal, jsonutils.NewString(err.Error()))
  57. return
  58. }
  59. self.SetStageComplete(ctx, nil)
  60. }
  61. func (self *BaremetalDeleteTask) OnDeleteBaremetalCompleteFailed(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  62. self.OnFailure(ctx, baremetal, body)
  63. }
  64. func (self *BaremetalDeleteTask) OnFailure(ctx context.Context, baremetal *models.SHost, body jsonutils.JSONObject) {
  65. baremetal.SetStatus(ctx, self.UserCred, api.BAREMETAL_DELETE_FAIL, body.String())
  66. self.SetStageFailed(ctx, body)
  67. }