-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
object.go
590 lines (497 loc) · 18.6 KB
/
object.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* 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 operation
import (
"context"
"fmt"
"strconv"
"github.com/rs/xid"
"configcenter/src/apimachinery"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/condition"
frtypes "configcenter/src/common/mapstr"
"configcenter/src/common/metadata"
"configcenter/src/scene_server/topo_server/core/inst"
"configcenter/src/scene_server/topo_server/core/model"
"configcenter/src/scene_server/topo_server/core/types"
)
// ObjectOperationInterface object operation methods
type ObjectOperationInterface interface {
CreateObjectBatch(params types.ContextParams, data frtypes.MapStr) (frtypes.MapStr, error)
FindObjectBatch(params types.ContextParams, data frtypes.MapStr) (frtypes.MapStr, error)
CreateObject(params types.ContextParams, data frtypes.MapStr) (model.Object, error)
CanDelete(params types.ContextParams, targetObj model.Object) error
DeleteObject(params types.ContextParams, id int64, cond condition.Condition, needCheckInst bool) error
FindObject(params types.ContextParams, cond condition.Condition) ([]model.Object, error)
FindObjectTopo(params types.ContextParams, cond condition.Condition) ([]metadata.ObjectTopo, error)
FindSingleObject(params types.ContextParams, objectID string) (model.Object, error)
UpdateObject(params types.ContextParams, data frtypes.MapStr, id int64, cond condition.Condition) error
SetProxy(modelFactory model.Factory, instFactory inst.Factory, cls ClassificationOperationInterface, asst AssociationOperationInterface, inst InstOperationInterface, attr AttributeOperationInterface, grp GroupOperationInterface)
IsValidObject(params types.ContextParams, objID string) error
}
// NewObjectOperation create a new object operation instance
func NewObjectOperation(client apimachinery.ClientSetInterface) ObjectOperationInterface {
return &object{
clientSet: client,
}
}
type object struct {
clientSet apimachinery.ClientSetInterface
modelFactory model.Factory
instFactory inst.Factory
cls ClassificationOperationInterface
grp GroupOperationInterface
asst AssociationOperationInterface
inst InstOperationInterface
attr AttributeOperationInterface
}
func (o *object) SetProxy(modelFactory model.Factory, instFactory inst.Factory, cls ClassificationOperationInterface, asst AssociationOperationInterface, inst InstOperationInterface, attr AttributeOperationInterface, grp GroupOperationInterface) {
o.modelFactory = modelFactory
o.instFactory = instFactory
o.asst = asst
o.inst = inst
o.attr = attr
o.grp = grp
}
func (o *object) IsValidObject(params types.ContextParams, objID string) error {
checkObjCond := condition.CreateCondition()
checkObjCond.Field(metadata.AttributeFieldObjectID).Eq(objID)
checkObjCond.Field(metadata.AttributeFieldSupplierAccount).Eq(params.SupplierAccount)
objItems, err := o.FindObject(params, checkObjCond)
if nil != err {
blog.Errorf("[opeartion-attr] failed to check the object repeated, error info is %s", err.Error())
return params.Err.New(common.CCErrCommParamsIsInvalid, err.Error())
}
if 0 == len(objItems) {
return params.Err.New(common.CCErrCommParamsIsInvalid, fmt.Sprintf("the object id '%s' is invalid", objID))
}
return nil
}
func (o *object) CreateObjectBatch(params types.ContextParams, data frtypes.MapStr) (frtypes.MapStr, error) {
inputData := map[string]ImportObjectData{}
if err := data.MarshalJSONInto(&inputData); nil != err {
return nil, err
}
result := frtypes.New()
for objID, inputData := range inputData {
subResult := frtypes.New()
if err := o.IsValidObject(params, objID); nil != err {
blog.Error("not found the objid: %s", objID)
subResult["errors"] = fmt.Sprintf("the object(%s) is invalid", objID)
result[objID] = subResult
continue
}
// update the object's attribute
for idx, attr := range inputData.Attr {
metaAttr := metadata.Attribute{}
targetAttr, err := metaAttr.Parse(attr)
targetAttr.OwnerID = params.SupplierAccount
targetAttr.ObjectID = objID
if nil != err {
blog.Error("not found the objid: %s", objID)
subResult["errors"] = err.Error()
result[objID] = subResult
continue
}
if targetAttr.PropertyID == common.BKChildStr || targetAttr.PropertyID == common.BKInstParentStr {
continue
}
if 0 == len(targetAttr.PropertyGroupName) {
targetAttr.PropertyGroup = "Default"
}
// find group
grpCond := condition.CreateCondition()
grpCond.Field(metadata.GroupFieldObjectID).Eq(objID)
grpCond.Field(metadata.GroupFieldGroupName).Eq(targetAttr.PropertyGroupName)
grps, err := o.grp.FindObjectGroup(params, grpCond)
if nil != err {
blog.Error("not found the objid: %s", objID)
errStr := params.Lang.Languagef("import_row_int_error_str", idx, err)
subResult["errors"] = errStr
result[objID] = subResult
continue
}
if 0 != len(grps) {
targetAttr.PropertyGroup = grps[0].GetID() // should be only one group
} else {
newGrp := o.modelFactory.CreateGroup(params)
newGrp.SetName(targetAttr.PropertyGroupName)
newGrp.SetID(xid.New().String())
newGrp.SetSupplierAccount(params.SupplierAccount)
newGrp.SetObjectID(objID)
err := newGrp.Save(nil)
if nil != err {
errStr := params.Lang.Languagef("import_row_int_error_str", idx, params.Err.Error(common.CCErrTopoObjectGroupCreateFailed))
if failed, ok := subResult["insert_failed"]; ok {
failedArr := failed.([]string)
failedArr = append(failedArr, errStr)
subResult["insert_failed"] = failedArr
} else {
subResult["insert_failed"] = []string{
errStr,
}
}
result[objID] = subResult
continue
}
targetAttr.PropertyGroup = newGrp.GetID()
}
// create or update the attribute
attrID, err := attr.String(metadata.AttributeFieldPropertyID)
if nil != err {
errStr := params.Lang.Languagef("import_row_int_error_str", idx, err.Error())
if failed, ok := subResult["insert_failed"]; ok {
failedArr := failed.([]string)
failedArr = append(failedArr, errStr)
subResult["insert_failed"] = failedArr
} else {
subResult["insert_failed"] = []string{
errStr,
}
}
result[objID] = subResult
continue
}
attrCond := condition.CreateCondition()
attrCond.Field(metadata.AttributeFieldSupplierAccount).Eq(params.SupplierAccount)
attrCond.Field(metadata.AttributeFieldObjectID).Eq(objID)
attrCond.Field(metadata.AttributeFieldPropertyID).Eq(attrID)
attrs, err := o.attr.FindObjectAttribute(params, attrCond)
if nil != err {
errStr := params.Lang.Languagef("import_row_int_error_str", idx, err.Error())
if failed, ok := subResult["insert_failed"]; ok {
failedArr := failed.([]string)
failedArr = append(failedArr, errStr)
subResult["insert_failed"] = failedArr
} else {
subResult["insert_failed"] = []string{
errStr,
}
}
result[objID] = subResult
continue
}
if 0 == len(attrs) {
//fmt.Println("targetattr:", targetAttr.ToMapStr())
newAttr := o.modelFactory.CreateAttribute(params)
if err = newAttr.Save(targetAttr.ToMapStr()); nil != err {
errStr := params.Lang.Languagef("import_row_int_error_str", idx, err.Error())
if failed, ok := subResult["insert_failed"]; ok {
failedArr := failed.([]string)
failedArr = append(failedArr, errStr)
subResult["insert_failed"] = failedArr
} else {
subResult["insert_failed"] = []string{
errStr,
}
}
result[objID] = subResult
continue
}
}
for _, newAttr := range attrs {
//fmt.Println("id:", newAttr.Origin().ID, targetAttr.ToMapStr())
if err := newAttr.Update(targetAttr.ToMapStr()); nil != err {
errStr := params.Lang.Languagef("import_row_int_error_str", idx, err.Error())
if failed, ok := subResult["update_failed"]; ok {
failedArr := failed.([]string)
failedArr = append(failedArr, errStr)
subResult["update_failed"] = failedArr
} else {
subResult["update_failed"] = []string{
errStr,
}
}
result[objID] = subResult
continue
}
}
if failed, ok := subResult["success"]; ok {
failedArr := failed.([]string)
failedArr = append(failedArr, strconv.FormatInt(idx, 10))
subResult["success"] = failedArr
} else {
subResult["success"] = []string{
strconv.FormatInt(idx, 10),
}
}
result[objID] = subResult
}
}
return result, nil
}
func (o *object) FindObjectBatch(params types.ContextParams, data frtypes.MapStr) (frtypes.MapStr, error) {
cond := &ExportObjectCondition{}
if err := data.MarshalJSONInto(cond); nil != err {
return nil, err
}
result := frtypes.New()
for _, objID := range cond.ObjIDS {
obj, err := o.FindSingleObject(params, objID)
if nil != err {
return nil, err
}
attrs, err := obj.GetAttributesExceptInnerFields()
if nil != err {
return nil, err
}
result.Set(objID, frtypes.MapStr{
"attr": attrs,
})
}
return result, nil
}
func (o *object) FindSingleObject(params types.ContextParams, objectID string) (model.Object, error) {
cond := condition.CreateCondition()
cond.Field(common.BKOwnerIDField).Eq(params.SupplierAccount)
cond.Field(common.BKObjIDField).Eq(objectID)
objs, err := o.FindObject(params, cond)
if nil != err {
blog.Errorf("[api-inst] failed to find the supplier account(%s) objects(%s), error info is %s", params.SupplierAccount, objectID, err.Error())
return nil, err
}
for _, item := range objs {
return item, nil
}
return nil, params.Err.New(common.CCErrTopoObjectSelectFailed, params.Err.Errorf(common.CCErrCommParamsIsInvalid, objectID).Error())
}
func (o *object) CreateObject(params types.ContextParams, data frtypes.MapStr) (model.Object, error) {
obj := o.modelFactory.CreaetObject(params)
_, err := obj.Parse(data)
if nil != err {
blog.Errorf("[operation-obj] failed to parse the data(%#v), error info is %s", data, err.Error())
return nil, err
}
// check the classification
_, err = obj.GetClassification()
if nil != err {
blog.Errorf("[operation-obj] failed to create the object, error info is %s", err.Error())
return nil, params.Err.New(common.CCErrTopoObjectCreateFailed, err.Error())
}
// check repeated
exists, err := obj.IsExists()
if nil != err {
blog.Errorf("[operation-obj] failed to create the object(%#v), error info is %s", data, err.Error())
return nil, params.Err.New(common.CCErrTopoObjectCreateFailed, err.Error())
}
if exists {
blog.Errorf("[operation-obj] the object(%#v) is repeated", data)
return nil, params.Err.Error(common.CCErrCommDuplicateItem)
}
err = obj.Create()
if nil != err {
blog.Errorf("[operation-obj] failed to save the data(%#v), error info is %s", data, err.Error())
return nil, err
}
// create the default group
grp := obj.CreateGroup()
grp.SetDefault(true)
grp.SetIndex(-1)
grp.SetName("Default")
grp.SetID("default")
if err = grp.Save(nil); nil != err {
blog.Errorf("[operation-obj] failed to create the default group, error info is %s", err.Error())
}
// create the default inst name
attr := obj.CreateAttribute()
attr.SetIsOnly(true)
attr.SetIsPre(true)
attr.SetCreator("user")
attr.SetIsEditable(true)
attr.SetGroupIndex(-1)
attr.SetGroup(grp)
attr.SetIsRequired(true)
attr.SetType(common.FieldTypeSingleChar)
attr.SetID(obj.GetInstNameFieldName())
attr.SetName(obj.GetDefaultInstPropertyName())
if err = attr.Create(); nil != err {
blog.Errorf("[operation-obj] failed to create the default inst name field, error info is %s", err.Error())
}
return obj, nil
}
func (o *object) CanDelete(params types.ContextParams, targetObj model.Object) error {
cond := condition.CreateCondition()
cond.Field(common.BKOwnerIDField).Eq(params.SupplierAccount)
if targetObj.IsCommon() {
cond.Field(common.BKObjIDField).Eq(targetObj.GetID())
}
query := &metadata.QueryInput{}
query.Condition = cond.ToMapStr()
findInstResponse, err := o.inst.FindOriginInst(params, targetObj, query)
if nil != err {
blog.Errorf("[operation-obj] failed to check if it (%s) has some insts, error info is %s", targetObj.GetID(), err.Error())
return err
}
if 0 != findInstResponse.Count {
blog.Errorf("the object [%s] has been instantiated and cannot be deleted", targetObj.GetID())
return params.Err.Errorf(common.CCErrTopoObjectHasSomeInstsForbiddenToDelete, targetObj.GetID())
}
return nil
}
func (o *object) DeleteObject(params types.ContextParams, id int64, cond condition.Condition, needCheckInst bool) error {
if 0 < id {
cond = condition.CreateCondition()
cond.Field(metadata.ModelFieldID).Eq(id)
}
objs, err := o.FindObject(params, cond)
if nil != err {
blog.Errorf("[operation-obj] failed to find objects, the condition is (%v) error info is %s", cond, err.Error())
return err
}
for _, obj := range objs {
// check if is can be deleted
if needCheckInst {
if err := o.CanDelete(params, obj); nil != err {
return err
}
}
// delete object
attrCond := condition.CreateCondition()
attrCond.Field(common.BKOwnerIDField).Eq(params.SupplierAccount)
attrCond.Field(common.BKObjIDField).Eq(obj.GetID())
if err := o.attr.DeleteObjectAttribute(params, -1, attrCond); nil != err {
blog.Errorf("[operation-obj] failed to delete the object(%d)'s attribute, error info is %s", id, err.Error())
return err
}
if groups, err := obj.GetGroups(); err != nil {
blog.Errorf("[operation-asst] failed to get the object's groups, error info is %s", err.Error())
return err
} else {
for _, group := range groups {
if err = o.grp.DeleteObjectGroup(params, group.GetRecordID()); err != nil {
blog.Errorf("[operation-asst] failed to delete the object's groups, error info is %s", err.Error())
return err
}
}
}
rsp, err := o.clientSet.ObjectController().Meta().DeleteObject(context.Background(), obj.GetRecordID(), params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[operation-obj] failed to request the object controller, error info is %s", err.Error())
return params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[opration-obj] failed to delete the object by the condition(%#v) or the id(%d)", cond.ToMapStr(), id)
return params.Err.Error(rsp.Code)
}
}
return nil
}
func (o *object) isFrom(params types.ContextParams, fromObjID, toObjID string) (bool, error) {
asstItems, err := o.asst.SearchObjectAssociation(params, fromObjID)
if nil != err {
return false, err
}
for _, asst := range asstItems {
if asst.AsstObjID == toObjID {
return true, nil
}
}
return false, nil
}
func (o *object) FindObjectTopo(params types.ContextParams, cond condition.Condition) ([]metadata.ObjectTopo, error) {
objs, err := o.FindObject(params, cond)
if nil != err {
blog.Errorf("[operation-obj] failed to find object, error info is %s", err.Error())
return nil, err
}
results := []metadata.ObjectTopo{}
for _, obj := range objs {
asstItems, err := o.asst.SearchObjectAssociation(params, obj.GetID())
if nil != err {
return nil, err
}
for _, asst := range asstItems {
if asst.ObjectAttID == common.BKChildStr {
continue
}
cond = condition.CreateCondition()
cond.Field(common.BKObjIDField).Eq(asst.AsstObjID)
cond.Field(common.BKOwnerIDField).Eq(params.SupplierAccount)
asstObjs, err := o.FindObject(params, cond)
if nil != err {
blog.Errorf("[operation-obj] failed to find object, error info is %s", err.Error())
return nil, err
}
for _, asstObj := range asstObjs {
tmp := metadata.ObjectTopo{}
tmp.Label = asst.ObjectAttID
tmp.LabelName = asst.AsstName
tmp.From.ObjID = obj.GetID()
cls, err := obj.GetClassification()
if nil != err {
return nil, err
}
tmp.From.ClassificationID = cls.GetID()
tmp.From.Position = obj.GetPosition()
tmp.From.OwnerID = obj.GetSupplierAccount()
tmp.From.ObjName = obj.GetName()
tmp.To.OwnerID = asstObj.GetSupplierAccount()
tmp.To.ObjID = asstObj.GetID()
cls, err = asstObj.GetClassification()
if nil != err {
return nil, err
}
tmp.To.ClassificationID = cls.GetID()
tmp.To.Position = asstObj.GetPosition()
tmp.To.ObjName = asstObj.GetName()
ok, err := o.isFrom(params, asstObj.GetID(), obj.GetID())
if nil != err {
return nil, err
}
if ok {
tmp.Arrows = "to,from"
} else {
tmp.Arrows = "to"
}
results = append(results, tmp)
}
}
}
return results, nil
}
func (o *object) FindObject(params types.ContextParams, cond condition.Condition) ([]model.Object, error) {
rsp, err := o.clientSet.ObjectController().Meta().SelectObjects(context.Background(), params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[operation-obj] failed to request the object controller, error info is %s", err.Error())
return nil, params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[operation-obj] failed to search the objects by the condition(%#v) , error info is %s", cond.ToMapStr(), rsp.ErrMsg)
return nil, params.Err.Error(rsp.Code)
}
return model.CreateObject(params, o.clientSet, rsp.Data), nil
}
func (o *object) UpdateObject(params types.ContextParams, data frtypes.MapStr, id int64, cond condition.Condition) error {
obj := o.modelFactory.CreaetObject(params)
obj.SetRecordID(id)
_, err := obj.Parse(data)
if nil != err {
blog.Errorf("[operation-obj] failed to parse the data(%#v), error info is %s", data, err.Error())
return err
}
// check repeated
exists, err := obj.IsExists()
if nil != err {
blog.Errorf("[operation-obj] failed to update the object(%#v), error info is %s", data, err.Error())
return params.Err.New(common.CCErrTopoObjectCreateFailed, err.Error())
}
if exists {
blog.Errorf("[operation-obj] the object(%#v) is repeated", data)
return params.Err.Error(common.CCErrCommDuplicateItem)
}
if err = obj.Update(data); nil != err {
blog.Errorf("[operation-obj] failed to update the object(%d), the new data(%#v), error info is %s", id, data, err.Error())
return params.Err.New(common.CCErrTopoObjectUpdateFailed, err.Error())
}
return nil
}