image_guest_joint.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "context"
  17. "database/sql"
  18. "fmt"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/sqlchemy"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/mcclient/auth"
  24. )
  25. // +onecloud:swagger-gen-ignore
  26. type SGuestImageJointManager struct {
  27. db.SJointResourceBaseManager
  28. }
  29. type SGuestImageJoint struct {
  30. db.SJointResourceBase
  31. GuestImageId string `width:"128" charset:"ascii" create:"required"`
  32. ImageId string `width:"128" charset:"ascii" create:"required"`
  33. }
  34. var GuestImageJointManager *SGuestImageJointManager
  35. func init() {
  36. GuestImageJointManager = &SGuestImageJointManager{
  37. db.NewJointResourceBaseManager(
  38. SGuestImageJoint{},
  39. "guest_image_tbl",
  40. "guestimagejoint",
  41. "guestimagejoints",
  42. GuestImageManager,
  43. ImageManager,
  44. ),
  45. }
  46. GuestImageJointManager.SetVirtualObject(GuestImageJointManager)
  47. }
  48. func (manager *SGuestImageJointManager) InitializeData() error {
  49. q := manager.Query()
  50. guestImageQ := GuestImageManager.RawQuery().IsTrue("deleted").SubQuery()
  51. q = q.Join(guestImageQ, sqlchemy.Equals(q.Field("guest_image_id"), guestImageQ.Field("id")))
  52. guestImageJoints := make([]SGuestImageJoint, 0)
  53. err := db.FetchModelObjects(manager, q, &guestImageJoints)
  54. if err != nil {
  55. return errors.Wrap(err, "FetchModelObjects")
  56. }
  57. for i := range guestImageJoints {
  58. guestImageJoints[i].Delete(context.Background(), auth.AdminCredential())
  59. }
  60. return nil
  61. }
  62. func (gm *SGuestImageJointManager) GetByGuestImageId(guestImageId string) ([]SGuestImageJoint, error) {
  63. q := gm.Query().Equals("guest_image_id", guestImageId).Asc("row_id") // order by row_id ascending
  64. ret := make([]SGuestImageJoint, 0, 1)
  65. err := db.FetchModelObjects(gm, q, &ret)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return ret, nil
  70. }
  71. func (gm *SGuestImageJointManager) GetByImageId(imageId string) ([]SGuestImageJoint, error) {
  72. q := gm.Query().Equals("image_id", imageId)
  73. ret := make([]SGuestImageJoint, 0, 1)
  74. err := db.FetchModelObjects(gm, q, &ret)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return ret, nil
  79. }
  80. /*func (gm *SGuestImageJointManager) GetGuestImageByImageId(imageId string) (*SGuestImage, error) {
  81. gits, err := gm.GetByImageId(imageId)
  82. if err != nil {
  83. return nil, err
  84. }
  85. model, err := GuestImageManager.FetchById(gits.GuestImageId)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return model.(*SGuestImage), nil
  90. }*/
  91. /*func (gm *SGuestImageJointManager) GetImagesByFilter(guestImageId string,
  92. filter func(q *sqlchemy.SQuery) *sqlchemy.SQuery) ([]SImage, error) {
  93. giJoints, err := gm.GetByGuestImageId(guestImageId)
  94. if err != nil {
  95. return nil, errors.Wrap(err, "get joints of guest and image failed")
  96. }
  97. if len(giJoints) == 0 {
  98. return []SImage{}, nil
  99. }
  100. imageIds := make([]string, len(giJoints))
  101. for i := range giJoints {
  102. imageIds[i] = giJoints[i].ImageId
  103. }
  104. q := ImageManager.Query().In("id", imageIds)
  105. q = filter(q)
  106. images := make([]SImage, 0, len(imageIds))
  107. err = db.FetchModelObjects(ImageManager, q, &images)
  108. if err != nil && errors.Cause(err) != sql.ErrNoRows {
  109. return nil, errors.Wrap(err, "fetch images failed")
  110. }
  111. return images, nil
  112. }
  113. func (gm *SGuestImageJointManager) GetImagesByGuestImageId(guestImageId string) ([]SImage, error) {
  114. return gm.GetImagesByFilter(guestImageId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery {
  115. return q
  116. })
  117. }*/
  118. func (gt *SGuestImageJoint) GetId() string {
  119. return fmt.Sprintf("guestimage-%s-image-%s", gt.GuestImageId, gt.ImageId)
  120. }
  121. func (gt *SGuestImageJoint) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error {
  122. return db.DeleteModel(ctx, userCred, gt)
  123. }
  124. func (gt *SGuestImageJointManager) CreateGuestImageJoint(
  125. ctx context.Context,
  126. guestImageId,
  127. imageId string,
  128. ) (*SGuestImageJoint, error) {
  129. gi := SGuestImageJoint{}
  130. gi.GuestImageId = guestImageId
  131. gi.ImageId = imageId
  132. //
  133. if err := gt.TableSpec().Insert(ctx, &gi); err != nil {
  134. return nil, errors.Wrapf(err, "insert guestimage joint error")
  135. }
  136. gi.SetVirtualObject(gt)
  137. return &gi, nil
  138. }
  139. func (gt *SGuestImageJoint) GetImage() (*SImage, error) {
  140. imgObj, err := ImageManager.FetchById(gt.ImageId)
  141. if err != nil {
  142. if errors.Cause(err) == sql.ErrNoRows {
  143. err = errors.ErrNotFound
  144. }
  145. return nil, errors.Wrapf(err, "FetchByImageId %s", gt.ImageId)
  146. }
  147. return imgObj.(*SImage), nil
  148. }