/
org.go
414 lines (369 loc) · 11.5 KB
/
org.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// 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/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle/apierrors"
"github.com/erda-project/erda/pkg/http/httputil"
)
// GetOrg get org by id from core-service.
func (b *Bundle) GetOrg(idOrName interface{}) (*apistructs.OrgDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var fetchResp apistructs.OrgFetchResponse
resp, err := hc.Get(host).Path(fmt.Sprintf("/api/orgs/%v", idOrName)).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
}
// GetDopOrg get org by id from dop
func (b *Bundle) GetDopOrg(idOrName interface{}) (*apistructs.OrgDTO, error) {
host, err := b.urls.DOP()
if err != nil {
return nil, err
}
hc := b.hc
var fetchResp apistructs.OrgFetchResponse
resp, err := hc.Get(host).Path(fmt.Sprintf("/api/orgs/%v", idOrName)).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
}
// ListOrgs 分页查询企业
func (b *Bundle) ListOrgs(req *apistructs.OrgSearchRequest, orgID string) (*apistructs.PagingOrgDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgSearchResponse
r, err := hc.Get(host).Path("/api/orgs").
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, req.UserID).
Header(httputil.OrgHeader, orgID).
Param("q", req.Q).
Param("org", req.Org).
Param("pageNo", strconv.Itoa(req.PageNo)).
Param("pageSize", strconv.Itoa(req.PageSize)).
Do().
JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}
// ListDopOrgs 分页查询企业
func (b *Bundle) ListDopOrgs(req *apistructs.OrgSearchRequest) (*apistructs.PagingOrgDTO, error) {
host, err := b.urls.DOP()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgSearchResponse
r, err := hc.Get(host).Path("/api/orgs").
Header(httputil.InternalHeader, "bundle").
Header("User-ID", req.UserID).
Param("q", req.Q).
Param("pageNo", strconv.Itoa(req.PageNo)).
Param("pageSize", strconv.Itoa(req.PageSize)).
Do().
JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}
// ListPublicOrgs get list of public orgs
func (b *Bundle) ListPublicOrgs(req *apistructs.OrgSearchRequest) (*apistructs.PagingOrgDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgSearchResponse
r, err := hc.Get(host).Path("/api/orgs/actions/list-public").
Header(httputil.InternalHeader, "bundle").
Header("User-ID", req.UserID).
Param("q", req.Q).
Param("pageNo", strconv.Itoa(req.PageNo)).
Param("pageSize", strconv.Itoa(req.PageSize)).
Do().
JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}
// ListDopPublicOrgs get list of public orgs form dop
func (b *Bundle) ListDopPublicOrgs(req *apistructs.OrgSearchRequest) (*apistructs.PagingOrgDTO, error) {
host, err := b.urls.DOP()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgSearchResponse
r, err := hc.Get(host).Path("/api/orgs/actions/list-public").
Header(httputil.InternalHeader, "bundle").
Header("User-ID", req.UserID).
Param("q", req.Q).
Param("pageNo", strconv.Itoa(req.PageNo)).
Param("pageSize", strconv.Itoa(req.PageSize)).
Do().
JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}
// GetOrgByDomain 通过域名获取企业
func (b *Bundle) GetOrgByDomain(domain, orgName, userID string) (*apistructs.OrgDTO, error) {
// TODO: userID should be deprecated
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgGetByDomainResponse
r, err := hc.Get(host).Path("/api/orgs/actions/get-by-domain").
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, userID). // TODO: for compatibility
Param("domain", domain).
Param("orgName", orgName).
Do().
JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return resp.Data, nil
}
// GetDopOrgByDomain 通过域名获取企业
func (b *Bundle) GetDopOrgByDomain(domain string, userID string) (*apistructs.OrgDTO, error) {
// TODO: userID should be deprecated
host, err := b.urls.DOP()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgGetByDomainResponse
r, err := hc.Get(host).Path("/api/orgs/actions/get-by-domain").
Header(httputil.InternalHeader, "bundle").
Header("User-ID", userID). // TODO: for compatibility
Param("domain", domain).
Do().
JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return resp.Data, nil
}
// CreateOrg create org
func (b *Bundle) CreateOrg(userID string, req *apistructs.OrgCreateRequest) (*apistructs.OrgDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgCreateResponse
r, err := hc.Post(host).Path("/api/orgs").
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, userID).
JSONBody(req).Do().JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}
// CreateDopOrg create org
func (b *Bundle) CreateDopOrg(userID string, req *apistructs.OrgCreateRequest) (*apistructs.OrgDTO, error) {
host, err := b.urls.DOP()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgCreateResponse
r, err := hc.Post(host).Path("/api/orgs").
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, userID).
JSONBody(req).Do().JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}
// UpdateOrg update org
func (b *Bundle) UpdateOrg(userID string, orgID int64, req *apistructs.OrgUpdateRequestBody) (*apistructs.OrgDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgUpdateResponse
r, err := hc.Put(host).Path(fmt.Sprintf("/api/orgs/%d", orgID)).
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, userID).
JSONBody(req).Do().JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}
// DeleteOrg delete org by id from core-service.
func (b *Bundle) DeleteOrg(idOrName interface{}) (*apistructs.OrgDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgDeleteResponse
r, err := hc.Delete(host).Path(fmt.Sprintf("/api/orgs/%v", idOrName)).
Header(httputil.InternalHeader, "bundle").Do().JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}
// GetOrgClusterRelationsByOrg get orgClusters relation by orgID
func (b *Bundle) GetOrgClusterRelationsByOrg(orgID uint64) ([]apistructs.OrgClusterRelationDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgClusterRelationDTOResponse
r, err := hc.Get(host).Path(fmt.Sprintf("/api/orgs/clusters/relations/%d", orgID)).
Header(httputil.InternalHeader, "bundle").Do().JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return resp.Data, nil
}
// ListOrgClusterRelation list all relations if cluster is empty, else list all relation related to this cluster
func (b *Bundle) ListOrgClusterRelation(userID, clusterName string) ([]apistructs.OrgClusterRelationDTO, error) {
host, err := b.urls.CoreServices()
if err != nil {
return nil, err
}
hc := b.hc
var resp apistructs.OrgClusterRelationDTOResponse
r, err := hc.Get(host).Path("/api/orgs/clusters/relations").
Header(httputil.UserHeader, userID).
Param("cluster", clusterName).
Do().JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return resp.Data, nil
}
// DereferenceCluster delete the relation of org and cluster
func (b *Bundle) DereferenceCluster(orgID uint64, clusterName, userID string) (string, error) {
host, err := b.urls.CoreServices()
if err != nil {
return "", err
}
hc := b.hc
var resp apistructs.DeleteOrgClusterRelationResponse
r, err := hc.Put(host).Path("/api/clusters/actions/dereference").
Header(httputil.InternalHeader, "bundle").
Header(httputil.UserHeader, userID).
Param("orgID", strconv.FormatUint(orgID, 10)).
Param("clusterName", clusterName).
Do().
JSON(&resp)
if err != nil {
return "", apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return "", toAPIError(r.StatusCode(), resp.Error)
}
return resp.Data, nil
}
// CreateOrgClusterRelationsByOrg create orgClusters relation by orgID
func (b *Bundle) CreateOrgClusterRelationsByOrg(clusterName string, userID string, orgID uint64) error {
host, err := b.urls.CoreServices()
if err != nil {
return err
}
hc := b.hc
org, err := b.GetOrg(orgID)
if err != nil {
return err
}
var createResp apistructs.OrgClusterRelationDTOCreateResponse
req := &apistructs.OrgClusterRelationCreateRequest{
OrgID: orgID,
OrgName: org.Name,
ClusterName: clusterName,
}
resp, err := hc.Post(host).Path("/api/orgs/actions/relate-cluster").
Header(httputil.UserHeader, userID).
JSONBody(req).
Do().
JSON(&createResp)
if err != nil {
return apierrors.ErrInvoke.InternalError(err)
}
if !resp.IsOK() || !createResp.Success {
return toAPIError(resp.StatusCode(), createResp.Error)
}
return nil
}