mysql_machineV2.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package dao
  2. import (
  3. "go-common/app/admin/ep/merlin/model"
  4. pkgerr "github.com/pkg/errors"
  5. )
  6. // InsertMachinesV2 insert machines for v2.
  7. func (d *Dao) InsertMachinesV2(u string, gmr *model.GenMachinesRequest, pgmr *model.PaasGenMachineRequest) (ms []*model.Machine, err error) {
  8. tx := d.db.Begin()
  9. if err = tx.Error; err != nil {
  10. return
  11. }
  12. for _, pm := range pgmr.Machines {
  13. m := pm.ToMachine(u, gmr)
  14. if err = tx.Create(m).Error; err != nil {
  15. tx.Rollback()
  16. return
  17. }
  18. for _, n := range gmr.ToMachineNode(m.ID) {
  19. if err = tx.Create(n).Error; err != nil {
  20. tx.Rollback()
  21. return
  22. }
  23. }
  24. if err = tx.Create(m.ToMachineLog()).Error; err != nil {
  25. tx.Rollback()
  26. return
  27. }
  28. ms = append(ms, m)
  29. }
  30. if err = tx.Commit().Error; err != nil {
  31. tx.Rollback()
  32. }
  33. return
  34. }
  35. // UpdateStatusForMachines update status for machines.
  36. func (d *Dao) UpdateStatusForMachines(status int, ids []int64) (err error) {
  37. return pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("id IN (?)", ids).Update("status", status).Error)
  38. }
  39. // UpdateMachineStatusByName update status by name.
  40. func (d *Dao) UpdateMachineStatusByName(status int, n string) error {
  41. return pkgerr.WithStack(d.db.Model(&model.Machine{}).Where("name = ?", n).Update("status", status).Error)
  42. }