guestcdrom.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 models
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. )
  23. // +onecloud:swagger-gen-ignore
  24. type SGuestcdromManager struct {
  25. db.SModelBaseManager
  26. }
  27. var GuestcdromManager *SGuestcdromManager
  28. func init() {
  29. GuestcdromManager = &SGuestcdromManager{
  30. SModelBaseManager: db.NewModelBaseManager(
  31. SGuestcdrom{},
  32. "guestcdrom_tbl",
  33. "guestcdrom",
  34. "guestcdroms",
  35. ),
  36. }
  37. GuestcdromManager.SetVirtualObject(GuestcdromManager)
  38. }
  39. // +onecloud:swagger-gen-ignore
  40. type SGuestcdrom struct {
  41. db.SModelBase
  42. RowId int64 `primary:"true" auto_increment:"true" list:"user"`
  43. Ordinal int `nullable:"false" default:"0"` // = Column(Integer, nullable=False, default=0)
  44. Id string `width:"36" charset:"ascii"` // = Column(VARCHAR(36, charset='ascii'), primary_key=True)
  45. ImageId string `width:"36" charset:"ascii" nullable:"true"` // Column(VARCHAR(36, charset='ascii'), nullable=True)
  46. Name string `width:"64" charset:"ascii" nullable:"true"` // Column(VARCHAR(64, charset='ascii'), nullable=True)
  47. Path string `width:"256" charset:"ascii" nullable:"true"` // Column(VARCHAR(256, charset='ascii'), nullable=True)
  48. Size int64 `nullable:"false" default:"0"` // = Column(Integer, nullable=False, default=0)
  49. BootIndex int8 `nullable:"false" default:"-1" list:"user" update:"user"`
  50. UpdatedAt time.Time `nullable:"false" updated_at:"true" nullable:"false"`
  51. UpdateVersion int `default:"0" nullable:"false" auto_version:"true"`
  52. }
  53. func (self *SGuestcdrom) insertIso(imageId string) bool {
  54. if len(self.ImageId) == 0 {
  55. _, err := db.Update(self, func() error {
  56. self.ImageId = imageId
  57. self.Name = ""
  58. self.Path = ""
  59. self.Size = 0
  60. return nil
  61. })
  62. if err != nil {
  63. log.Errorf("insertISO saveupdate fail: %s", err)
  64. return false
  65. }
  66. return true
  67. } else {
  68. return false
  69. }
  70. }
  71. func (self *SGuestcdrom) insertIsoSucc(imageId string, path string, size int64, name string, bootIndex *int8) bool {
  72. if self.ImageId == imageId {
  73. _, err := db.Update(self, func() error {
  74. self.Name = name
  75. self.Path = path
  76. self.Size = size
  77. if bootIndex != nil {
  78. self.BootIndex = *bootIndex
  79. } else {
  80. self.BootIndex = -1
  81. }
  82. return nil
  83. })
  84. if err != nil {
  85. log.Errorf("insertIsoSucc saveUpdate fail %s", err)
  86. return false
  87. }
  88. return true
  89. } else {
  90. return false
  91. }
  92. }
  93. func (self *SGuestcdrom) ejectIso() bool {
  94. if len(self.ImageId) > 0 {
  95. _, err := db.Update(self, func() error {
  96. self.ImageId = ""
  97. self.Name = ""
  98. self.Path = ""
  99. self.Size = 0
  100. self.BootIndex = -1
  101. return nil
  102. })
  103. if err != nil {
  104. log.Errorf("ejectIso saveUpdate fail %s", err)
  105. return false
  106. }
  107. return true
  108. } else {
  109. return false
  110. }
  111. }
  112. func (self *SGuestcdrom) GetImage() (*SCachedimage, error) {
  113. if len(self.ImageId) == 0 {
  114. return nil, fmt.Errorf("empty image_id")
  115. }
  116. image, err := CachedimageManager.FetchById(self.ImageId)
  117. if err != nil {
  118. return nil, errors.Wrapf(err, "CachedimageManager.FetchById(%s)", self.ImageId)
  119. }
  120. return image.(*SCachedimage), nil
  121. }
  122. func (self *SGuestcdrom) GetDetails() string {
  123. if len(self.ImageId) > 0 {
  124. if self.Size > 0 {
  125. // Size is stored in bytes, convert to MB for display
  126. sizeMB := self.Size / 1024 / 1024
  127. return fmt.Sprintf("%s(%s/%dMB)", self.Name, self.ImageId, sizeMB)
  128. } else {
  129. return fmt.Sprintf("%s(inserting)", self.ImageId)
  130. }
  131. } else {
  132. return ""
  133. }
  134. }
  135. func (self *SGuestcdrom) SetBootIndex(bootIndex int8) error {
  136. _, err := db.Update(self, func() error {
  137. self.BootIndex = bootIndex
  138. return nil
  139. })
  140. return err
  141. }
  142. func (self *SGuestcdrom) getJsonDesc() *api.GuestcdromJsonDesc {
  143. if len(self.ImageId) > 0 && len(self.Path) > 0 {
  144. return &api.GuestcdromJsonDesc{
  145. Ordinal: self.Ordinal,
  146. ImageId: self.ImageId,
  147. Path: self.Path,
  148. Name: self.Name,
  149. Size: self.Size,
  150. BootIndex: self.BootIndex,
  151. }
  152. }
  153. return nil
  154. }