guestfloppy.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. )
  22. // +onecloud:swagger-gen-ignore
  23. type SGuestFloppyManager struct {
  24. db.SModelBaseManager
  25. }
  26. var GuestFloppyManager *SGuestFloppyManager
  27. func init() {
  28. GuestFloppyManager = &SGuestFloppyManager{
  29. SModelBaseManager: db.NewModelBaseManager(
  30. SGuestfloppy{},
  31. "guestfloppy_tbl",
  32. "guestfloppy",
  33. "guestfloppys",
  34. ),
  35. }
  36. GuestFloppyManager.SetVirtualObject(GuestFloppyManager)
  37. }
  38. // +onecloud:swagger-gen-ignore
  39. type SGuestfloppy struct {
  40. db.SModelBase
  41. RowId int64 `primary:"true" auto_increment:"true" list:"user"`
  42. Id string `width:"36" charset:"ascii" ` // = Column(VARCHAR(36, charset='ascii'), primary_key=True)
  43. Ordinal int `nullable:"false" default:"0"` // = Column(Integer, nullable=False, default=0)
  44. ImageId string `width:"36" charset:"ascii" nullable:"true"` // Column(VARCHAR(36, charset='ascii'), nullable=True)
  45. Name string `width:"64" charset:"ascii" nullable:"true"` // Column(VARCHAR(64, charset='ascii'), nullable=True)
  46. Path string `width:"256" charset:"ascii" nullable:"true"` // Column(VARCHAR(256, charset='ascii'), nullable=True)
  47. Size int64 `nullable:"false" default:"0"` // = Column(Integer, nullable=False, default=0)
  48. UpdatedAt time.Time `nullable:"false" updated_at:"true" nullable:"false"`
  49. UpdateVersion int `default:"0" nullable:"false" auto_version:"true"`
  50. }
  51. func (self *SGuestfloppy) insertVfd(imageId string) bool {
  52. if len(self.ImageId) == 0 {
  53. _, err := db.Update(self, func() error {
  54. self.ImageId = imageId
  55. self.Name = ""
  56. self.Path = ""
  57. self.Size = 0
  58. return nil
  59. })
  60. if err != nil {
  61. log.Errorf("insertVfd saveupdate fail: %s", err)
  62. return false
  63. }
  64. return true
  65. } else {
  66. return false
  67. }
  68. }
  69. func (self *SGuestfloppy) insertVfdSucc(imageId string, path string, size int64, name string) bool {
  70. if self.ImageId == imageId {
  71. _, err := db.Update(self, func() error {
  72. self.Name = name
  73. self.Path = path
  74. self.Size = size
  75. return nil
  76. })
  77. if err != nil {
  78. log.Errorf("insertIsoSucc saveUpdate fail %s", err)
  79. return false
  80. }
  81. return true
  82. } else {
  83. return false
  84. }
  85. }
  86. func (self *SGuestfloppy) ejectVfd() bool {
  87. if len(self.ImageId) > 0 {
  88. _, err := db.Update(self, func() error {
  89. self.ImageId = ""
  90. self.Name = ""
  91. self.Path = ""
  92. self.Size = 0
  93. return nil
  94. })
  95. if err != nil {
  96. log.Errorf("ejectVfd saveUpdate fail %s", err)
  97. return false
  98. }
  99. return true
  100. } else {
  101. return false
  102. }
  103. }
  104. func (self *SGuestfloppy) GetDetails() string {
  105. if len(self.ImageId) > 0 {
  106. if self.Size > 0 {
  107. return fmt.Sprintf("%s(%s/%dMB)", self.Name, self.ImageId, self.Size)
  108. } else {
  109. return fmt.Sprintf("%s(inserting)", self.ImageId)
  110. }
  111. } else {
  112. return ""
  113. }
  114. }
  115. func (self *SGuestfloppy) getJsonDesc() *api.GuestfloppyJsonDesc {
  116. if len(self.ImageId) > 0 && len(self.Path) > 0 {
  117. return &api.GuestfloppyJsonDesc{
  118. Ordinal: self.Ordinal,
  119. ImageId: self.ImageId,
  120. Path: self.Path,
  121. Name: self.Name,
  122. Size: self.Size,
  123. }
  124. }
  125. return nil
  126. }