/
app.go
357 lines (321 loc) · 10.8 KB
/
app.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bundle
import (
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle/apierrors"
"github.com/erda-project/erda/pkg/http/httputil"
)
// GetApp get app by id from core-service.
func (b *Bundle) GetApp(id uint64) (*apistructs.ApplicationDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var fetchResp apistructs.ApplicationFetchResponse
resp, err := hc.Get(host).Path(fmt.Sprintf("/api/applications/%d", id)).Header(httputil.InternalHeader, "bundle").Do().JSON(&fetchResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !fetchResp.Success {
return nil, toAPIError(resp.StatusCode(), fetchResp.Error)
}
return &fetchResp.Data, nil
}
func (b *Bundle) GetMyApps(userid string, orgid uint64) (*apistructs.ApplicationListResponseData, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var listResp apistructs.ApplicationListResponse
resp, err := hc.Get(host).
Path("/api/applications/actions/list-my-applications").
Header(httputil.OrgHeader, strconv.FormatUint(orgid, 10)).
Header(httputil.UserHeader, userid).
Param("pageSize", "9999").
Param("pageNo", "1").
Do().JSON(&listResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !listResp.Success {
return nil, toAPIError(resp.StatusCode(), listResp.Error)
}
return &listResp.Data, nil
}
// GetAppsByProject 根据 projectID 获取应用列表
func (b *Bundle) GetAppsByProject(projectID, orgID uint64, userID string) (*apistructs.ApplicationListResponseData, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var listResp apistructs.ApplicationListResponse
resp, err := hc.Get(host).
Path(fmt.Sprintf("/api/applications")).
Header(httputil.OrgHeader, strconv.FormatUint(orgID, 10)).
Header(httputil.UserHeader, userID).
Param("projectId", strconv.FormatUint(projectID, 10)).
Param("pageSize", "100").
Param("pageNo", "1").
Do().JSON(&listResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !listResp.Success {
return nil, toAPIError(resp.StatusCode(), listResp.Error)
}
return &listResp.Data, nil
}
// get applications by projectID and app name
func (b *Bundle) GetAppsByProjectAndAppName(projectID, orgID uint64, userID string, appName string) (*apistructs.ApplicationListResponseData, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var listResp apistructs.ApplicationListResponse
resp, err := hc.Get(host).
Path("/api/applications").
Header(httputil.OrgHeader, strconv.FormatUint(orgID, 10)).
Header(httputil.UserHeader, userID).
Param("projectId", strconv.FormatUint(projectID, 10)).
Param("pageSize", "1").
Param("pageNo", "1").
Param("name", appName).
Do().JSON(&listResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !listResp.Success {
return nil, toAPIError(resp.StatusCode(), listResp.Error)
}
return &listResp.Data, nil
}
// GetAppsByProjectSimple 根据 projectID 获取应用列表简单信息
func (b *Bundle) GetAppsByProjectSimple(projectID, orgID uint64, userID string) (*apistructs.ApplicationListResponseData, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var listResp apistructs.ApplicationListResponse
resp, err := hc.Get(host).
Path(fmt.Sprintf("/api/applications")).
Header(httputil.OrgHeader, strconv.FormatUint(orgID, 10)).
Header(httputil.UserHeader, userID).
Param("projectId", strconv.FormatUint(projectID, 10)).
Param("pageSize", "100").
Param("pageNo", "1").
Param("isSimple", "true").
Do().JSON(&listResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !listResp.Success {
return nil, toAPIError(resp.StatusCode(), listResp.Error)
}
return &listResp.Data, nil
}
// Deprecated
type AbilityAppReq struct {
OrgId uint64 `json:"orgId"`
ClusterId uint64 `json:"clusterId"`
ClusterName string `json:"clusterName"`
ApplicationName string `json:"applicationName"`
Operator string `json:"operator"`
}
// GetAppPublishItemRelationsGroupByENV 根据 appID 获取应用关联的发布内容
func (b *Bundle) GetAppPublishItemRelationsGroupByENV(appID uint64) (*apistructs.QueryAppPublishItemRelationGroupByENVResponse, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var relationResp apistructs.QueryAppPublishItemRelationGroupByENVResponse
resp, err := hc.Get(host).
Path(fmt.Sprintf("/api/applications/%d/actions/get-publish-item-relations", appID)).
Do().JSON(&relationResp)
if err != nil {
return nil, err
}
if !resp.IsOK() {
return nil, errors.Errorf("failed to fetch publish item relation, statusCode: %d", resp.StatusCode())
}
if !relationResp.Success {
return nil, errors.Errorf("failed to fetch publish item relation, err: %s", relationResp.Error.Msg)
}
return &relationResp, nil
}
// QueryAppPublishItemRelations 查询应用关联的发布内容
func (b *Bundle) QueryAppPublishItemRelations(req *apistructs.QueryAppPublishItemRelationRequest) (*apistructs.QueryAppPublishItemRelationResponse, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var relationResp apistructs.QueryAppPublishItemRelationResponse
resp, err := hc.Get(host).
Path("/api/applications/actions/query-publish-item-relations").
Param("publishItemID", strconv.FormatInt(req.PublishItemID, 10)).
Param("appID", strconv.FormatInt(req.AppID, 10)).
Param("ak", req.AK).Param("ai", req.AI).
Do().JSON(&relationResp)
if err != nil {
return nil, err
}
if !resp.IsOK() {
return nil, errors.Errorf("failed to fetch publish item relation, statusCode: %d", resp.StatusCode())
}
if !relationResp.Success {
return nil, errors.Errorf("failed to fetch publish item relation, err: %s", relationResp.Error.Msg)
}
return &relationResp, nil
}
func (b *Bundle) RemoveAppPublishItemRelations(publishItemID int64) error {
host, err := b.urls.CoreServices()
if err != nil {
return err
}
req := apistructs.RemoveAppPublishItemRelationsRequest{
PublishItemId: publishItemID,
}
hc := b.hc
var getResp apistructs.RemoveAppPublishItemRelationsResponse
resp, err := hc.Post(host).Path("/api/applications/actions/remove-publish-item-relations").
Header("Internal-Client", "bundle").JSONBody(req).
Do().JSON(&getResp)
if err != nil {
apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !getResp.Success {
return toAPIError(resp.StatusCode(), getResp.Error)
}
return nil
}
// get my apps by paging
func (b *Bundle) GetAllMyApps(userid string, orgid uint64, req apistructs.ApplicationListRequest) (*apistructs.ApplicationListResponseData, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var listResp apistructs.ApplicationListResponse
resp, err := hc.Get(host).
Path("/api/applications/actions/list-my-applications").
Header(httputil.OrgHeader, strconv.FormatUint(orgid, 10)).
Header(httputil.UserHeader, userid).
Param("pageSize", strconv.Itoa(req.PageSize)).
Param("pageNo", strconv.Itoa(req.PageNo)).
Param("name", req.Name).
Param("mode", req.Mode).
Param("q", req.Query).
Param("public", req.Public).
Param("projectId", strconv.FormatUint(req.ProjectID, 10)).
Param("isSimple", strconv.FormatBool(req.IsSimple)).
Param("orderBy", req.OrderBy).
Do().JSON(&listResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !listResp.Success {
return nil, toAPIError(resp.StatusCode(), listResp.Error)
}
return &listResp.Data, nil
}
// CreateApp create app
func (b *Bundle) CreateApp(req apistructs.ApplicationCreateRequest, userID string) (*apistructs.ApplicationDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var fetchResp apistructs.ApplicationCreateResponse
resp, err := hc.Post(host).Path("/api/applications").
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, userID).
JSONBody(&req).Do().JSON(&fetchResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() {
return nil, toAPIError(resp.StatusCode(), fetchResp.Error)
}
return &fetchResp.Data, nil
}
// UpdateApp update app
func (b *Bundle) UpdateApp(req apistructs.ApplicationUpdateRequestBody, appID uint64, userID string) (interface{}, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var fetchResp apistructs.ApplicationUpdateResponse
resp, err := hc.Put(host).Path(fmt.Sprintf("/api/applications/%d", appID)).
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, userID).
JSONBody(&req).Do().JSON(&fetchResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() {
return nil, toAPIError(resp.StatusCode(), fetchResp.Error)
}
return &fetchResp.Data, nil
}
// DeleteApp delete app
func (b *Bundle) DeleteApp(appID uint64, userID string) (*apistructs.ApplicationDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var fetchResp apistructs.ApplicationDeleteResponse
resp, err := hc.Delete(host).Path(fmt.Sprintf("/api/applications/%d", appID)).
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, userID).
Do().JSON(&fetchResp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() {
return nil, toAPIError(resp.StatusCode(), fetchResp.Error)
}
return &fetchResp.Data, nil
}
// CountAppByProID count app by proID
func (b *Bundle) CountAppByProID(proID uint64) (int64, error) {
host, err := b.urls.CoreServices()
if err != nil {
return 0, err
}
hc := b.hc
var fetchResp apistructs.CountAppResponse
resp, err := hc.Get(host).Path("/api/applications/actions/count").
Header(httputil.InternalHeader, "bundle").
Param("projectID", strconv.FormatUint(proID, 10)).
Do().JSON(&fetchResp)
if err != nil {
return 0, apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() {
return 0, toAPIError(resp.StatusCode(), fetchResp.Error)
}
return fetchResp.Data, nil
}