/
result.go
372 lines (313 loc) · 10.4 KB
/
result.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
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.,
* Copyright (C) 2017,-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the ",License",); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 metadata
import (
"fmt"
"configcenter/src/common"
"configcenter/src/common/errors"
"configcenter/src/common/mapstr"
)
// BaseResp common result struct
type BaseResp struct {
Result bool `json:"result" mapstructure:"result"`
Code int `json:"bk_error_code" mapstructure:"bk_error_code"`
ErrMsg string `json:"bk_error_msg" mapstructure:"bk_error_msg"`
Permissions *IamPermission `json:"permission" mapstructure:"permission"`
}
// CCError 根据response返回的信息产生错误
func (br *BaseResp) CCError() errors.CCErrorCoder {
if br.Result {
return nil
}
return errors.New(br.Code, br.ErrMsg)
}
// New 根据response返回的信息产生错误
func (br *BaseResp) Error() error {
if br.Result {
return nil
}
return errors.New(br.Code, br.ErrMsg)
}
func (br *BaseResp) ToString() string {
return fmt.Sprintf("code:%d, message:%s", br.Code, br.ErrMsg)
}
// JsonStringResp defines a response that do not parse the data field to a struct
// but decode it to a json string if possible
type JsonStringResp struct {
BaseResp
Data string
}
// JsonCntInfoResp defines a response that do not parse the data's count and info fields
// to a struct but decode it to a json string if possible
type JsonCntInfoResp struct {
BaseResp
Data CntInfoString `json:"data"`
}
type CntInfoString struct {
Count int64 `json:"count"`
// info is a json array string field.
Info string `json:"info"`
}
type IamPermission struct {
SystemID string `json:"system_id"`
Actions []IamAction `json:"actions"`
}
type IamAction struct {
ID string `json:"id"`
RelatedResourceTypes []IamResourceType `json:"related_resource_types"`
}
type IamResourceType struct {
SystemID string `json:"system_id"`
Type string `json:"type"`
Instances [][]IamResourceInstance `json:"instances,omitempty"`
Attributes []IamResourceAttribute `json:"attributes,omitempty"`
}
type IamResourceInstance struct {
Type string `json:"type"`
ID string `json:"id"`
}
type IamResourceAttribute struct {
ID string `json:"id"`
Values []IamResourceAttributeValue `json:"values"`
}
type IamResourceAttributeValue struct {
ID string `json:"id"`
}
type IamInstanceWithCreator struct {
System string `json:"system"`
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
Creator string `json:"creator"`
Ancestors []IamInstanceAncestor `json:"ancestors,omitempty"`
}
type IamInstancesWithCreator struct {
System string `json:"system"`
Type string `json:"type"`
Creator string `json:"creator"`
Instances []IamInstance `json:"instances"`
}
type IamInstance struct {
ID string `json:"id"`
Name string `json:"name"`
Ancestors []IamInstanceAncestor `json:"ancestors,omitempty"`
}
type IamInstanceAncestor struct {
System string `json:"system"`
Type string `json:"type"`
ID string `json:"id"`
}
type IamCreatorActionPolicy struct {
Action struct {
ID string `json:"id"`
} `json:"action"`
PolicyID int64 `json:"policy_id"`
}
// Permission describes all the authorities that a user
// is need, when he attempts to operate a resource.
// Permission is used only when a user do not have the authority to
// access a resources with a action.
type Permission struct {
SystemID string `json:"system_id"`
SystemName string `json:"system_name"`
ScopeType string `json:"scope_type"`
ScopeTypeName string `json:"scope_type_name"`
ScopeID string `json:"scope_id"`
ScopeName string `json:"scope_name"`
ActionID string `json:"action_id"`
ActionName string `json:"action_name"`
// newly added two field.
ResourceTypeName string `json:"resource_type_name"`
ResourceType string `json:"resource_type"`
Resources [][]Resource `json:"resources"`
}
type Resource struct {
ResourceTypeName string `json:"resource_type_name"`
ResourceType string `json:"resource_type"`
ResourceName string `json:"resource_name"`
ResourceID string `json:"resource_id"`
}
func NewNoPermissionResp(permission *IamPermission) BaseResp {
return BaseResp{
Result: false,
Code: common.CCNoPermission,
ErrMsg: "no permissions",
Permissions: permission,
}
}
// SuccessBaseResp default result
var SuccessBaseResp = BaseResp{Result: true, Code: common.CCSuccess, ErrMsg: common.CCSuccessStr}
type SuccessResponse struct {
BaseResp `json:",inline"`
Data interface{} `json:"data"`
}
func NewSuccessResponse(data interface{}) *SuccessResponse {
return &SuccessResponse{
BaseResp: SuccessBaseResp,
Data: data,
}
}
// CreatedCount created count struct
type CreatedCount struct {
Count uint64 `json:"created_count"`
}
// UpdatedCount created count struct
type UpdatedCount struct {
Count uint64 `json:"updated_count"`
}
// UpdateAttributeIndex created bk_property_index info struct
type UpdateAttributeIndex struct {
Id int64 `json:"id"`
Index int64 `json:"index"`
}
// DeletedCount created count struct
type DeletedCount struct {
Count uint64 `json:"deleted_count"`
}
// ExceptionResult exception info
type ExceptionResult struct {
Message string `json:"message"`
Code int64 `json:"code"`
Data interface{} `json:"data"`
OriginIndex int64 `json:"origin_index"`
}
// CreatedDataResult common created result definition
type CreatedDataResult struct {
OriginIndex int64 `json:"origin_index"`
ID uint64 `json:"id"`
}
// RepeatedDataResult repeated data
type RepeatedDataResult struct {
OriginIndex int64 `json:"origin_index"`
Data mapstr.MapStr `json:"data"`
}
// UpdatedDataResult common update operation result
type UpdatedDataResult struct {
OriginIndex int64 `json:"origin_index"`
ID uint64 `json:"id"`
}
// SetDataResult common set result definition
type SetDataResult struct {
UpdatedCount `json:",inline"`
CreatedCount `json:",inline"`
Created []CreatedDataResult `json:"created"`
Updated []UpdatedDataResult `json:"updated"`
Exceptions []ExceptionResult `json:"exception"`
}
// CreateManyInfoResult create many function return this result struct
type CreateManyInfoResult struct {
Created []CreatedDataResult `json:"created"`
Repeated []RepeatedDataResult `json:"repeated"`
Exceptions []ExceptionResult `json:"exception"`
}
// CreateManyDataResult the data struct definition in create many function result
type CreateManyDataResult struct {
CreateManyInfoResult `json:",inline"`
}
// CreateOneDataResult the data struct definition in create one function result
type CreateOneDataResult struct {
Created CreatedDataResult `json:"created"`
}
// SearchResp common search response
type SearchResp struct {
BaseResp `json:",inline"`
Data SearchDataResult `json:"data"`
}
// SearchDataResult common search data result
type SearchDataResult struct {
Count int64 `json:"count"`
Info []mapstr.MapStr `json:"info"`
}
// QueryModelDataResult used to define the model query
type QueryModelDataResult struct {
Count int64 `json:"count"`
Info []Object `json:"info"`
}
// QueryModelWithAttributeDataResult used to define the model with attribute query
type QueryModelWithAttributeDataResult struct {
Count int64 `json:"count"`
Info []SearchModelInfo `json:"info"`
}
// QueryModelAttributeDataResult search model attr data result
type QueryModelAttributeDataResult struct {
Count int64 `json:"count"`
Info []Attribute `json:"info"`
}
// QueryModelAttributeGroupDataResult query model attribute group result definition
type QueryModelAttributeGroupDataResult struct {
Count int64 `json:"count"`
Info []Group `json:"info"`
}
// QueryModelClassificationDataResult query model classification result definition
type QueryModelClassificationDataResult struct {
Count int64 `json:"count"`
Info []Classification `json:"info"`
}
// SearchAssociationKindResult search association kind result definition
type SearchAssociationKindResult struct {
Count uint64 `json:"count"`
Info []AssociationKind `json:"info"`
}
// ReadModelAttrResult read model attribute api http response return result struct
type ReadModelAttrResult struct {
BaseResp `json:",inline"`
Data QueryModelAttributeDataResult `json:"data"`
}
// ReadModelClassifitionResult read model classifition api http response return result struct
type ReadModelClassifitionResult struct {
BaseResp `json:",inline"`
Data QueryModelClassificationDataResult `json:"data"`
}
// ReadModelResult read model classifition api http response return result struct
type ReadModelResult struct {
BaseResp `json:",inline"`
Data QueryModelWithAttributeDataResult `json:"data"`
}
type ReadModelAttributeGroupResult struct {
BaseResp `json:",inline"`
Data QueryModelAttributeGroupDataResult `json:"data"`
}
type ReadModelUniqueResult struct {
BaseResp `json:",inline"`
Data QueryUniqueResult `json:"data"`
}
type ReadModelAssociationResult struct {
BaseResp
Data struct {
Count uint64 `json:"count"`
Info []Association `json:"info"`
}
}
type ReadInstAssociationResult struct {
BaseResp
Data struct {
Count uint64 `json:"count"`
Info []InstAsst `json:"info"`
}
}
// OperaterException result
type OperaterException struct {
BaseResp `json:",inline"`
Data []ExceptionResult `json:"data"`
}
type Uint64DataResponse struct {
BaseResp `json:",inline"`
Data uint64 `json:"data"`
}
type TransferException struct {
HostID []int64 `json:"bk_host_id"`
ErrMsg string `json:"bk_error_msg"`
}
type TransferExceptionResult struct {
BaseResp `json:",inline"`
Data TransferException `json:"data"`
}