/
addon_attachment.go
205 lines (184 loc) · 7.28 KB
/
addon_attachment.go
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
201
202
203
204
205
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package dbclient
import (
"time"
"github.com/pkg/errors"
"github.com/erda-project/erda/apistructs"
)
// AddonAttachment addon & runtime 关联关系
type AddonAttachment struct {
ID uint64 `gorm:"primary_key"`
InstanceID string `gorm:"type:varchar(64)"` // AddonInstance 主键
RoutingInstanceID string `gorm:"type:varchar(64)"` // AddonInstanceRouting 主键
TenantInstanceID string `gorm:"type:varchar(64)"`
Options string `gorm:"type:text"`
OrgID string
ProjectID string
ApplicationID string
RuntimeID string `gorm:"column:app_id"`
InsideAddon string `gorm:"type:varchar(1)"` // N or Y
RuntimeName string
Deleted string `gorm:"column:is_deleted"` // Y: 已删除 N: 未删除
CreatedAt time.Time `gorm:"column:create_time"`
UpdatedAt time.Time `gorm:"column:update_time"`
}
// TableName 数据库表名
func (AddonAttachment) TableName() string {
return "tb_addon_attachment"
}
// CreateAttachment insert addonAttachment
func (db *DBClient) CreateAttachment(addonAttachment *AddonAttachment) error {
return db.Create(addonAttachment).Error
}
// UpdateAttachment update addonAttachment
func (db *DBClient) UpdateAttachment(addonAttachment *AddonAttachment) error {
return db.Save(addonAttachment).Error
}
// DestroyByIntsanceID 根据instanceID逻辑删除attach信息
func (db *DBClient) DestroyByIntsanceID(instanceID string) error {
if err := db.Model(&AddonAttachment{}).
Where("instance_id = ?", instanceID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Update("is_deleted", apistructs.AddonDeleted).Error; err != nil {
return errors.Wrapf(err, "failed to delete addon attachments, instanceID: %v", instanceID)
}
return nil
}
// DeleteAttachmentsByRoutingInstanceID 根据 routingInstanceID 删除 attachment
func (db *DBClient) DeleteAttachmentsByRoutingInstanceID(routingInstanceID string) error {
if err := db.Model(&AddonAttachment{}).
Where("routing_instance_id = ?", routingInstanceID).
Update("is_deleted", apistructs.AddonDeleted).Error; err != nil {
return err
}
return nil
}
// DeleteAttachmentByRuntimeAndRoutingInstanceID 根据 runtimeID & routingInstanceID 删除 attachment
func (db *DBClient) DeleteAttachmentByRuntimeAndRoutingInstanceID(runtimeID, routingInstanceID string) error {
if err := db.Model(&AddonAttachment{}).
Where("app_id = ?", runtimeID).
Where("routing_instance_id = ?", routingInstanceID).
Update("is_deleted", apistructs.AddonDeleted).Error; err != nil {
return err
}
return nil
}
// DeleteAttachmentByRuntimeAndInstanceID 根据 runtimeID & InstanceID 删除 attachment
func (db *DBClient) DeleteAttachmentByRuntimeAndInstanceID(runtimeID, instanceID string) error {
if err := db.Model(&AddonAttachment{}).
Where("app_id = ?", runtimeID).
Where("instance_id = ?", instanceID).
Update("is_deleted", apistructs.AddonDeleted).Error; err != nil {
return err
}
return nil
}
// GetAttachmentCountByRoutingInstanceID count数据量
func (db *DBClient) GetAttachmentCountByRoutingInstanceID(routingInstanceID string) (int64, error) {
// 获取匹配搜索结果总量
var total int64
if err := db.
Where("routing_instance_id = ?", routingInstanceID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Model(&AddonAttachment{}).
Count(&total).Error; err != nil {
return 0, err
}
return total, nil
}
// GetAttachmentCountByInstanceID count数据量
func (db *DBClient) GetAttachmentCountByInstanceID(instanceID string) (int64, error) {
// 获取匹配搜索结果总量
var total int64
if err := db.
Where("instance_id = ?", instanceID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Model(&AddonAttachment{}).
Count(&total).Error; err != nil {
return 0, err
}
return total, nil
}
// GetAttachMentsByRuntimeID 根据runtimeID获取attachment信息
func (db *DBClient) GetAttachMentsByRuntimeID(runtimeID uint64) (*[]AddonAttachment, error) {
var attachments []AddonAttachment
if err := db.
Where("app_id = ?", runtimeID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Find(&attachments).Error; err != nil {
return nil, errors.Wrapf(err, "failed to get addon attachments info, runtimeID : %d",
runtimeID)
}
return &attachments, nil
}
// GetByRuntimeIDAndRoutingInstanceID 根据runtimeID、routingInstanceID获取attachment信息
func (db *DBClient) GetByRuntimeIDAndRoutingInstanceID(runtimeID, routingInstanceID string) (*[]AddonAttachment, error) {
var attachments []AddonAttachment
if err := db.
Where("app_id = ?", runtimeID).
Where("routing_instance_id = ?", routingInstanceID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Find(&attachments).Error; err != nil {
return nil, errors.Wrapf(err, "failed to get addon attachments info, runtimeID : %s, routingInstanceID : %s",
runtimeID, routingInstanceID)
}
return &attachments, nil
}
// GetByRuntimeIDAndInstanceID 根据runtimeID、instanceId获取attachment信息
func (db *DBClient) GetByRuntimeIDAndInstanceID(runtimeID, instanceID string) (*[]AddonAttachment, error) {
var attachments []AddonAttachment
if err := db.
Where("app_id = ?", runtimeID).
Where("instance_id = ?", instanceID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Find(&attachments).Error; err != nil {
return nil, errors.Wrapf(err, "failed to get addon attachments info, runtimeID : %s, instanceID : %s",
runtimeID, instanceID)
}
return &attachments, nil
}
// GetAttachmentsByInstanceID 根据instanceId获取attachment信息
func (db *DBClient) GetAttachmentsByInstanceID(instanceID string) (*[]AddonAttachment, error) {
var attachments []AddonAttachment
if err := db.
Where("instance_id = ?", instanceID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Find(&attachments).Error; err != nil {
return nil, errors.Wrapf(err, "failed to get addon attachments info, instanceID : %s",
instanceID)
}
return &attachments, nil
}
// GetAttachmentsByRoutingInstanceID 根据routingInstanceID获取attachment信息
func (db *DBClient) GetAttachmentsByRoutingInstanceID(routingInstanceID string) (*[]AddonAttachment, error) {
var attachments []AddonAttachment
if err := db.
Where("routing_instance_id = ?", routingInstanceID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Find(&attachments).Error; err != nil {
return nil, err
}
return &attachments, nil
}
// GetAttachmentsByTenantInstanceID 根据 tenantInstanceID 获取 attachment 信息
func (db *DBClient) GetAttachmentsByTenantInstanceID(tenantInstanceID string) (*[]AddonAttachment, error) {
var attachments []AddonAttachment
if err := db.
Where("tenant_instance_id = ?", tenantInstanceID).
Where("is_deleted = ?", apistructs.AddonNotDeleted).
Find(&attachments).Error; err != nil {
return nil, err
}
return &attachments, nil
}