forked from openshift-hyperfleet/hyperfleet-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_pool.go
More file actions
200 lines (175 loc) · 6.31 KB
/
Copy pathnode_pool.go
File metadata and controls
200 lines (175 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package dao
import (
"bytes"
"context"
"time"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/openshift-hyperfleet/hyperfleet-api/pkg/api"
"github.com/openshift-hyperfleet/hyperfleet-api/pkg/db"
)
type NodePoolDao interface {
Get(ctx context.Context, id string) (*api.NodePool, error)
GetForUpdate(ctx context.Context, id string) (*api.NodePool, error)
Create(ctx context.Context, nodePool *api.NodePool) (*api.NodePool, error)
Replace(ctx context.Context, nodePool *api.NodePool) (*api.NodePool, error)
Save(ctx context.Context, nodePool *api.NodePool) error
SaveStatusConditions(ctx context.Context, id string, statusConditions []byte) error
Delete(ctx context.Context, id string) error
FindByIDs(ctx context.Context, ids []string) (api.NodePoolList, error)
FindByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error)
FindSoftDeletedByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error)
SoftDeleteByOwner(ctx context.Context, ownerID string, t time.Time, deletedBy string) error
UpdateStatusConditionsByIDs(ctx context.Context, updates map[string][]byte) error
ExistsByOwner(ctx context.Context, ownerID string) (bool, error)
All(ctx context.Context) (api.NodePoolList, error)
}
var _ NodePoolDao = &sqlNodePoolDao{}
type sqlNodePoolDao struct {
sessionFactory *db.SessionFactory
}
func NewNodePoolDao(sessionFactory *db.SessionFactory) NodePoolDao {
return &sqlNodePoolDao{sessionFactory: sessionFactory}
}
func (d *sqlNodePoolDao) Get(ctx context.Context, id string) (*api.NodePool, error) {
g2 := (*d.sessionFactory).New(ctx)
var nodePool api.NodePool
if err := g2.Take(&nodePool, "id = ?", id).Error; err != nil {
return nil, err
}
return &nodePool, nil
}
func (d *sqlNodePoolDao) GetForUpdate(ctx context.Context, id string) (*api.NodePool, error) {
g2 := (*d.sessionFactory).New(ctx)
var nodePool api.NodePool
if err := g2.Clauses(clause.Locking{Strength: "UPDATE"}).Take(&nodePool, "id = ?", id).Error; err != nil {
return nil, err
}
return &nodePool, nil
}
func (d *sqlNodePoolDao) SaveStatusConditions(ctx context.Context, id string, statusConditions []byte) error {
g2 := (*d.sessionFactory).New(ctx)
result := g2.Model(&api.NodePool{}).Where("id = ?", id).Update("status_conditions", statusConditions)
if result.Error != nil {
db.MarkForRollback(ctx, result.Error)
return result.Error
}
return nil
}
func (d *sqlNodePoolDao) Create(ctx context.Context, nodePool *api.NodePool) (*api.NodePool, error) {
g2 := (*d.sessionFactory).New(ctx)
if err := g2.Omit(clause.Associations).Create(nodePool).Error; err != nil {
db.MarkForRollback(ctx, err)
return nil, err
}
return nodePool, nil
}
func (d *sqlNodePoolDao) Replace(ctx context.Context, nodePool *api.NodePool) (*api.NodePool, error) {
g2 := (*d.sessionFactory).New(ctx)
// Get the existing nodePool to compare spec
existing, err := d.Get(ctx, nodePool.ID)
if err != nil {
db.MarkForRollback(ctx, err)
return nil, err
}
// Compare spec and labels: if either changed, increment generation.
// Aggregated conditions are recomputed in the service layer.
if !bytes.Equal(existing.Spec, nodePool.Spec) || !bytes.Equal(existing.Labels, nodePool.Labels) {
nodePool.Generation = existing.Generation + 1
} else {
nodePool.Generation = existing.Generation
}
// Save the nodePool
if err := g2.Omit(clause.Associations).Save(nodePool).Error; err != nil {
db.MarkForRollback(ctx, err)
return nil, err
}
return nodePool, nil
}
func (d *sqlNodePoolDao) Save(ctx context.Context, nodePool *api.NodePool) error {
g2 := (*d.sessionFactory).New(ctx)
if err := g2.Omit(clause.Associations).Save(nodePool).Error; err != nil {
db.MarkForRollback(ctx, err)
return err
}
return nil
}
func (d *sqlNodePoolDao) Delete(ctx context.Context, id string) error {
g2 := (*d.sessionFactory).New(ctx)
if err := g2.Omit(clause.Associations).Delete(&api.NodePool{Meta: api.Meta{ID: id}}).Error; err != nil {
db.MarkForRollback(ctx, err)
return err
}
return nil
}
func (d *sqlNodePoolDao) SoftDeleteByOwner(ctx context.Context, ownerID string, t time.Time, deletedBy string) error {
g2 := (*d.sessionFactory).New(ctx)
result := g2.Model(&api.NodePool{}).
Where("owner_id = ? AND deleted_time IS NULL", ownerID).
Updates(map[string]interface{}{
"deleted_time": t,
"deleted_by": deletedBy,
"generation": gorm.Expr("generation + 1"),
})
if result.Error != nil {
db.MarkForRollback(ctx, result.Error)
return result.Error
}
return nil
}
func (d *sqlNodePoolDao) FindSoftDeletedByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error) {
g2 := (*d.sessionFactory).New(ctx)
var nodePools api.NodePoolList
if err := g2.Where("owner_id = ? AND deleted_time IS NOT NULL", ownerID).Find(&nodePools).Error; err != nil {
return nil, err
}
return nodePools, nil
}
func (d *sqlNodePoolDao) FindByIDs(ctx context.Context, ids []string) (api.NodePoolList, error) {
g2 := (*d.sessionFactory).New(ctx)
nodePools := api.NodePoolList{}
if err := g2.Where("id in (?)", ids).Find(&nodePools).Error; err != nil {
return nil, err
}
return nodePools, nil
}
func (d *sqlNodePoolDao) FindByOwner(ctx context.Context, ownerID string) (api.NodePoolList, error) {
g2 := (*d.sessionFactory).New(ctx)
var nodePools api.NodePoolList
if err := g2.Where("owner_id = ?", ownerID).Find(&nodePools).Error; err != nil {
return nil, err
}
return nodePools, nil
}
func (d *sqlNodePoolDao) UpdateStatusConditionsByIDs(ctx context.Context, updates map[string][]byte) error {
g2 := (*d.sessionFactory).New(ctx)
if len(updates) == 0 {
return nil
}
for id, statusConditions := range updates {
result := g2.Model(&api.NodePool{}).
Where("id = ?", id).
Update("status_conditions", statusConditions)
if result.Error != nil {
db.MarkForRollback(ctx, result.Error)
return result.Error
}
}
return nil
}
func (d *sqlNodePoolDao) ExistsByOwner(ctx context.Context, ownerID string) (bool, error) {
g2 := (*d.sessionFactory).New(ctx)
var count int64
if err := g2.Model(&api.NodePool{}).Where("owner_id = ?", ownerID).Limit(1).Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
func (d *sqlNodePoolDao) All(ctx context.Context) (api.NodePoolList, error) {
g2 := (*d.sessionFactory).New(ctx)
nodePools := api.NodePoolList{}
if err := g2.Find(&nodePools).Error; err != nil {
return nil, err
}
return nodePools, nil
}