/
lib-reference.go
230 lines (205 loc) · 6.31 KB
/
lib-reference.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// 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 libreference
import (
"fmt"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/dop/dao"
"github.com/erda-project/erda/modules/dop/services/apierrors"
)
// LibReference 库引用 service
type LibReference struct {
db *dao.DBClient
bdl *bundle.Bundle
}
// Option 库引用选项
type Option func(*LibReference)
// New 初始化库引用 service
func New(options ...Option) *LibReference {
lr := &LibReference{}
for _, op := range options {
op(lr)
}
return lr
}
// WithDBClient 设置 db client
func WithDBClient(db *dao.DBClient) Option {
return func(lr *LibReference) {
lr.db = db
}
}
func WithBundle(bdl *bundle.Bundle) Option {
return func(lr *LibReference) {
lr.bdl = bdl
}
}
// Create 创建库引用
func (l *LibReference) Create(createReq *apistructs.LibReferenceCreateRequest) (uint64, error) {
// 参数校验
if createReq.AppID == 0 {
return 0, apierrors.ErrCreateLibReference.MissingParameter("appID")
}
if createReq.LibID == 0 {
return 0, apierrors.ErrCreateLibReference.MissingParameter("libID")
}
if createReq.LibName == "" {
return 0, apierrors.ErrCreateLibReference.MissingParameter("libName")
}
if !createReq.IsInternalClient() {
// Authorize
access, err := l.bdl.CheckPermission(&apistructs.PermissionCheckRequest{
UserID: createReq.UserID,
Scope: apistructs.AppScope,
ScopeID: createReq.AppID,
Resource: apistructs.LibReferenceResource,
Action: apistructs.CreateAction,
})
if err != nil {
return 0, err
}
if !access.Access {
return 0, apierrors.ErrCreateLibReference.AccessDenied()
}
}
// 检查库引用是否已存在
listReq := &apistructs.LibReferenceListRequest{
AppID: createReq.AppID,
LibID: createReq.LibID,
PageNo: 1,
PageSize: 1,
}
_, libReferences, err := l.db.ListLibReference(listReq)
if err != nil {
return 0, apierrors.ErrCreateLibReference.InternalError(err)
}
if len(libReferences) > 0 {
return 0, apierrors.ErrCreateLibReference.AlreadyExists()
}
// 创建审批流
approvalCreateReq := apistructs.ApproveCreateRequest{
Title: fmt.Sprintf("应用 %s 关联库 %s 申请", createReq.AppName, createReq.LibName),
Type: apistructs.ApproveLibReference,
Priority: "middle",
TargetID: createReq.AppID,
EntityID: createReq.LibID,
OrgID: createReq.OrgID,
Desc: createReq.LibDesc,
}
newApproval, err := l.bdl.CreateApprove(&approvalCreateReq, createReq.UserID)
if err != nil {
return 0, err
}
// 创建库引用
libReference := &dao.LibReference{
AppID: createReq.AppID,
LibID: createReq.LibID,
LibName: createReq.LibName,
LibDesc: createReq.LibDesc,
ApprovalID: newApproval.ID,
ApprovalStatus: apistructs.ApprovalStatusPending,
Creator: createReq.UserID,
}
if err := l.db.CreateLibReference(libReference); err != nil {
return 0, err
}
return uint64(libReference.ID), nil
}
// UpdateApprovalStatus 更新库引用审批状态
func (l *LibReference) UpdateApprovalStatus(approvalID uint64, approvalStatus apistructs.ApprovalStatus) error {
return l.db.UpdateApprovalStatusByApprovalID(approvalID, approvalStatus)
}
// Delete 删除库引用
func (l *LibReference) Delete(identityInfo apistructs.IdentityInfo, libReferenceID uint64) error {
// 检查库引用是否存在
libReference, err := l.db.GetLibReference(libReferenceID)
if err != nil {
return err
}
if libReference == nil {
return apierrors.ErrDeleteLibReference.NotFound()
}
if !identityInfo.IsInternalClient() {
// Authorize
access, err := l.bdl.CheckPermission(&apistructs.PermissionCheckRequest{
UserID: identityInfo.UserID,
Scope: apistructs.AppScope,
ScopeID: libReference.AppID,
Resource: apistructs.LibReferenceResource,
Action: apistructs.DeleteAction,
})
if err != nil {
return err
}
if !access.Access {
return apierrors.ErrDeleteLibReference.AccessDenied()
}
}
// 待审批库引用不能删除
if libReference.ApprovalStatus == apistructs.ApprovalStatusPending {
return apierrors.ErrDeleteLibReference.InvalidState("approvalStatus")
}
return l.db.DeleteLibReference(libReferenceID)
}
// List 库引用列表
func (l *LibReference) List(listReq *apistructs.LibReferenceListRequest) (*apistructs.LibReferenceListResponseData, error) {
if listReq.PageNo == 0 {
listReq.PageNo = 1
}
if listReq.PageSize == 0 {
listReq.PageSize = 20
}
if !listReq.IsInternalClient() {
// Authorize
access, err := l.bdl.CheckPermission(&apistructs.PermissionCheckRequest{
UserID: listReq.UserID,
Scope: apistructs.AppScope,
ScopeID: listReq.AppID,
Resource: apistructs.LibReferenceResource,
Action: apistructs.ListAction,
})
if err != nil {
return nil, err
}
if !access.Access {
return nil, apierrors.ErrListLibReference.AccessDenied()
}
}
total, lrs, err := l.db.ListLibReference(listReq)
if err != nil {
return nil, err
}
libReferences := make([]apistructs.LibReference, 0, len(lrs))
for _, lr := range lrs {
libReferences = append(libReferences, *l.Convert(&lr))
}
return &apistructs.LibReferenceListResponseData{
Total: total,
List: libReferences,
}, nil
}
// Convert 库引用数据结构转换
func (l *LibReference) Convert(libReference *dao.LibReference) *apistructs.LibReference {
return &apistructs.LibReference{
ID: uint64(libReference.ID),
AppID: libReference.AppID,
LibID: libReference.LibID,
LibName: libReference.LibName,
LibDesc: libReference.LibDesc,
ApprovalID: libReference.ApprovalID,
ApprovalStatus: libReference.ApprovalStatus,
Creator: libReference.Creator,
CreatedAt: &libReference.CreatedAt,
UpdatedAt: &libReference.UpdatedAt,
}
}