scalinggroup_guest.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/tristate"
  20. "yunion.io/x/sqlchemy"
  21. "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type SScalingGroupGuestManager struct {
  26. SGuestJointsManager
  27. }
  28. var ScalingGroupGuestManager *SScalingGroupGuestManager
  29. func init() {
  30. db.InitManager(func() {
  31. ScalingGroupGuestManager = &SScalingGroupGuestManager{
  32. SGuestJointsManager: NewGuestJointsManager(
  33. SScalingGroupGuest{},
  34. "scalinggroupguests_tbl",
  35. "scalinggroupguest",
  36. "scalinggroupguests",
  37. ScalingGroupManager,
  38. ),
  39. }
  40. ScalingGroupGuestManager.SetVirtualObject(ScalingGroupGuestManager)
  41. })
  42. }
  43. type SScalingGroupGuest struct {
  44. SGuestJointsBase
  45. ScalingGroupId string `width:"36" charset:"ascii" nullable:"false"`
  46. GuestStatus string `width:"36" charset:"ascii" nullable:"false" index:"true"`
  47. Manual tristate.TriState `default:"false"`
  48. }
  49. func (sggm *SScalingGroupGuestManager) GetSlaveFieldName() string {
  50. return "scaling_group_id"
  51. }
  52. func (sggm *SScalingGroupGuestManager) Attach(ctx context.Context, scaligGroupId, guestId string, manual bool) error {
  53. sgg := &SScalingGroupGuest{
  54. SGuestJointsBase: SGuestJointsBase{
  55. GuestId: guestId,
  56. },
  57. ScalingGroupId: scaligGroupId,
  58. GuestStatus: compute.SG_GUEST_STATUS_JOINING,
  59. }
  60. if manual {
  61. sgg.Manual = tristate.True
  62. } else {
  63. sgg.Manual = tristate.False
  64. }
  65. return sggm.TableSpec().Insert(ctx, sgg)
  66. }
  67. func (sgg *SScalingGroupGuest) Detach(ctx context.Context, userCred mcclient.TokenCredential) error {
  68. return db.DetachJoint(ctx, userCred, sgg)
  69. }
  70. func (sggm *SScalingGroupGuestManager) Fetch(scalingGroupId, guestId string) ([]SScalingGroupGuest, error) {
  71. sggs := make([]SScalingGroupGuest, 0)
  72. q := sggm.Query()
  73. if len(scalingGroupId) != 0 {
  74. q = q.Equals("scaling_group_id", scalingGroupId)
  75. }
  76. if len(guestId) != 0 {
  77. q = q.Equals("guest_id", guestId)
  78. }
  79. err := db.FetchModelObjects(sggm, q, &sggs)
  80. return sggs, err
  81. }
  82. func (sgg *SScalingGroupGuest) SetGuestStatus(status string) error {
  83. if sgg.GuestStatus == status {
  84. return nil
  85. }
  86. _, err := db.Update(sgg, func() error {
  87. sgg.GuestStatus = status
  88. sgg.UpdatedAt = time.Now()
  89. sgg.UpdateVersion += 1
  90. return nil
  91. })
  92. return err
  93. }
  94. func (sggm *SScalingGroupGuestManager) NewQuery(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, useRawQuery bool) *sqlchemy.SQuery {
  95. return sggm.Query()
  96. }
  97. func (sggm *SScalingGroupGuestManager) Query(fields ...string) *sqlchemy.SQuery {
  98. return sggm.SVirtualJointResourceBaseManager.Query(fields...).NotEquals("guest_status",
  99. compute.SG_GUEST_STATUS_PENDING_REMOVE)
  100. }