forked from TencentBlueKing/bk-cmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute.go
719 lines (548 loc) · 19 KB
/
attribute.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
* 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 model
import (
"context"
"encoding/json"
"io"
"configcenter/src/common/util"
"configcenter/src/apimachinery"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/condition"
frtypes "configcenter/src/common/mapstr"
metadata "configcenter/src/common/metadata"
"configcenter/src/scene_server/topo_server/core/types"
)
// Attribute attribute opeartion interface declaration
type Attribute interface {
Operation
Parse(data frtypes.MapStr) (*metadata.Attribute, error)
Origin() metadata.Attribute
IsMainlineField() bool
IsAssociationType() bool
SetSupplierAccount(supplierAccount string)
GetSupplierAccount() string
GetParentObject() (Object, error)
GetChildObject() (Object, error)
SetParentObject(objID string) error
SetChildObject(objID string) error
SetObjectID(objectID string)
GetObjectID() string
SetID(attributeID string)
GetID() string
SetName(attributeName string)
GetName() string
SetGroup(grp Group)
GetGroup() (Group, error)
SetGroupIndex(attGroupIndex int64)
GetGroupIndex() int64
SetUnint(unit string)
GetUnint() string
SetPlaceholder(placeHolder string)
GetPlaceholder() string
SetIsEditable(isEditable bool)
GetIsEditable() bool
SetIsPre(isPre bool)
GetIsPre() bool
SetIsReadOnly(isReadOnly bool)
GetIsReadOnly() bool
SetIsOnly(isOnly bool)
GetIsOnly() bool
SetIsRequired(isRequired bool)
GetIsRequired() bool
SetIsSystem(isSystem bool)
GetIsSystem() bool
SetIsAPI(isAPI bool)
GetIsAPI() bool
SetType(attributeType string)
GetType() string
SetOption(attributeOption interface{})
GetOption() interface{}
SetDescription(attributeDescription string)
GetDescription() string
SetCreator(attributeCreator string)
GetCreator() string
ToMapStr() (frtypes.MapStr, error)
}
var _ Attribute = (*attribute)(nil)
// attribute the metadata structure definition of the model attribute
type attribute struct {
FieldValid
attr metadata.Attribute
isNew bool
params types.ContextParams
clientSet apimachinery.ClientSetInterface
}
func (a *attribute) Origin() metadata.Attribute {
return a.attr
}
func (a *attribute) IsAssociationType() bool {
return util.IsAssocateProperty(a.attr.PropertyType)
}
func (a *attribute) IsMainlineField() bool {
return a.attr.PropertyID == common.BKChildStr
}
func (a *attribute) searchObjects(objID string) ([]metadata.Object, error) {
cond := condition.CreateCondition()
cond.Field(common.BKOwnerIDField).Eq(a.params.SupplierAccount).Field(common.BKObjIDField).Eq(objID)
condStr, err := cond.ToMapStr().ToJSON()
if nil != err {
return nil, err
}
rsp, err := a.clientSet.ObjectController().Meta().SelectObjects(context.Background(), a.params.Header, condStr)
if nil != err {
blog.Errorf("failed to request the object controller, error info is %s", err.Error())
return nil, a.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if common.CCSuccess != rsp.Code {
blog.Errorf("failed to search the object(%s), error info is %s", objID, rsp.ErrMsg)
return nil, a.params.Err.Error(rsp.Code)
}
return rsp.Data, nil
}
func (a *attribute) GetParentObject() (Object, error) {
cond := condition.CreateCondition()
cond.Field(metadata.AssociationFieldSupplierAccount).Eq(a.params.SupplierAccount)
cond.Field(metadata.AssociationFieldObjectID).Eq(a.attr.ObjectID)
cond.Field(metadata.AssociationFieldObjectAttributeID).Eq(a.attr.PropertyID)
rsp, err := a.clientSet.ObjectController().Meta().SelectObjectAssociations(context.Background(), a.params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[model-obj] failed to request the object controller, error info is %s", err.Error())
return nil, err
}
for _, asst := range rsp.Data {
rspRst, err := a.searchObjects(asst.ObjectID)
if nil != err {
blog.Errorf("[model-obj] failed to search the object(%s)'s parent, error info is %s", asst.ObjectID, err.Error())
return nil, err
}
objItems := CreateObject(a.params, a.clientSet, rspRst)
for _, item := range objItems { // only one object
return item, nil
}
}
return nil, io.EOF
}
func (a *attribute) GetChildObject() (Object, error) {
cond := condition.CreateCondition()
cond.Field(metadata.AssociationFieldSupplierAccount).Eq(a.params.SupplierAccount)
cond.Field(metadata.AssociationFieldAssociationObjectID).Eq(a.attr.ObjectID)
cond.Field(metadata.AssociationFieldObjectAttributeID).Eq(a.attr.PropertyID)
rsp, err := a.clientSet.ObjectController().Meta().SelectObjectAssociations(context.Background(), a.params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[model-obj] failed to request the object controller, error info is %s", err.Error())
return nil, err
}
for _, asst := range rsp.Data {
rspRst, err := a.searchObjects(asst.ObjectID)
if nil != err {
blog.Errorf("[model-obj] failed to search the object(%s)'s child, error info is %s", asst.ObjectID, err.Error())
return nil, err
}
objItems := CreateObject(a.params, a.clientSet, rspRst)
for _, item := range objItems { // only one object
return item, nil
}
}
return nil, io.EOF
}
func (a *attribute) SetParentObject(objID string) error {
cond := condition.CreateCondition()
cond.Field(metadata.AssociationFieldSupplierAccount).Eq(a.params.SupplierAccount)
cond.Field(metadata.AssociationFieldObjectAttributeID).Eq(a.attr.PropertyID)
cond.Field(metadata.AssociationFieldObjectID).Eq(a.attr.ObjectID)
rsp, err := a.clientSet.ObjectController().Meta().SelectObjectAssociations(context.Background(), a.params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[model-attr] failed to request the object controller, error info is %s", err.Error())
return a.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[model-attr] failed to search the parent association, error info is %s", rsp.ErrMsg)
return a.params.Err.Error(rsp.Code)
}
// create
if 0 == len(rsp.Data) {
asst := &metadata.Association{}
asst.OwnerID = a.params.SupplierAccount
asst.ObjectAttID = a.attr.PropertyID
asst.AsstObjID = objID
asst.ObjectID = a.attr.ObjectID
rsp, err := a.clientSet.ObjectController().Meta().CreateObjectAssociation(context.Background(), a.params.Header, asst)
if nil != err {
blog.Errorf("[model-obj] failed to request the object controller, error info is %s", err.Error())
return a.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[model-obj] failed to set the main line association parent, error info is %s", rsp.ErrMsg)
return a.params.Err.Error(rsp.Code)
}
return nil
}
// update
for _, asst := range rsp.Data {
asst.AsstObjID = objID
rsp, err := a.clientSet.ObjectController().Meta().UpdateObjectAssociation(context.Background(), asst.ID, a.params.Header, nil)
if nil != err {
blog.Errorf("[model-obj] failed to request object controller, error info is %s", err.Error())
return err
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[model-obj] failed to update the child association, error info is %s", rsp.ErrMsg)
return a.params.Err.Error(rsp.Code)
}
}
return nil
}
func (a *attribute) SetChildObject(objID string) error {
cond := condition.CreateCondition()
cond.Field(metadata.AssociationFieldSupplierAccount).Eq(a.params.SupplierAccount)
cond.Field(metadata.AssociationFieldObjectAttributeID).Eq(a.attr.PropertyID)
cond.Field(metadata.AssociationFieldAssociationObjectID).Eq(a.attr.ObjectID)
rsp, err := a.clientSet.ObjectController().Meta().SelectObjectAssociations(context.Background(), a.params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[model-attr] failed to request the object controller, error info is %s", err.Error())
return a.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[model-attr] failed to search the child association, error info is %s", rsp.ErrMsg)
return a.params.Err.Error(rsp.Code)
}
// create
if 0 == len(rsp.Data) {
asst := &metadata.Association{}
asst.OwnerID = a.params.SupplierAccount
asst.ObjectAttID = a.attr.PropertyID
asst.AsstObjID = a.attr.ObjectID
asst.ObjectID = objID
rsp, err := a.clientSet.ObjectController().Meta().CreateObjectAssociation(context.Background(), a.params.Header, asst)
if nil != err {
blog.Errorf("[model-obj] failed to request the object controller, error info is %s", err.Error())
return a.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[model-obj] failed to set the main line association parent, error info is %s", rsp.ErrMsg)
return a.params.Err.Error(rsp.Code)
}
return nil
}
// update
for _, asst := range rsp.Data {
asst.ObjectID = objID
rsp, err := a.clientSet.ObjectController().Meta().UpdateObjectAssociation(context.Background(), asst.ID, a.params.Header, nil)
if nil != err {
blog.Errorf("[model-obj] failed to request object controller, error info is %s", err.Error())
return err
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[model-obj] failed to update the child association, error info is %s", rsp.ErrMsg)
return a.params.Err.Error(rsp.Code)
}
}
return nil
}
func (a *attribute) MarshalJSON() ([]byte, error) {
return json.Marshal(a.attr)
}
func (a *attribute) Parse(data frtypes.MapStr) (*metadata.Attribute, error) {
attr, err := a.attr.Parse(data)
if nil != err {
return attr, err
}
if a.attr.IsOnly {
a.attr.IsRequired = true
}
if 0 == len(a.attr.PropertyGroup) {
a.attr.PropertyGroup = "default"
}
return nil, err
}
func (a *attribute) ToMapStr() (frtypes.MapStr, error) {
rst := metadata.SetValueToMapStrByTags(&a.attr)
return rst, nil
}
func (a *attribute) IsValid(isUpdate bool, data frtypes.MapStr) error {
if a.attr.PropertyID == common.BKChildStr || a.attr.PropertyID == common.BKInstParentStr {
return nil
}
if !isUpdate || data.Exists(metadata.AttributeFieldPropertyType) {
if _, err := a.FieldValid.Valid(a.params, data, metadata.AttributeFieldPropertyType); nil != err {
return err
}
}
if !isUpdate || data.Exists(metadata.AttributeFieldPropertyID) {
val, err := a.FieldValid.Valid(a.params, data, metadata.AttributeFieldPropertyID)
if nil != err {
return err
}
if err = a.FieldValid.ValidID(a.params, val); nil != err {
return err
}
}
if !isUpdate || data.Exists(metadata.AttributeFieldPropertyName) {
val, err := a.FieldValid.Valid(a.params, data, metadata.AttributeFieldPropertyName)
if nil != err {
return err
}
if err = a.FieldValid.ValidNameWithRegex(a.params, val); nil != err {
return err
}
}
if !isUpdate || data.Exists(metadata.AttributeFieldOption) {
propertyType, err := data.String(metadata.AttributeFieldPropertyType)
if nil != err {
return a.params.Err.New(common.CCErrCommParamsIsInvalid, err.Error())
}
if option, exists := data.Get(metadata.AttributeFieldOption); exists && (propertyType == common.FieldTypeInt || propertyType == common.FieldTypeEnum) {
if err := util.ValidPropertyOption(propertyType, option, a.params.Err); nil != err {
return err
}
}
}
return nil
}
func (a *attribute) Create() error {
if err := a.IsValid(false, a.attr.ToMapStr()); nil != err {
return err
}
// check the property id repeated
a.attr.OwnerID = a.params.SupplierAccount
exists, err := a.IsExists()
if nil != err {
return err
}
if exists {
return a.params.Err.Error(common.CCErrCommDuplicateItem)
}
// create a new record
rsp, err := a.clientSet.ObjectController().Meta().CreateObjectAtt(context.Background(), a.params.Header, &a.attr)
if nil != err {
blog.Errorf("faield to request the object controller, the error info is %s", err.Error())
return err
}
if common.CCSuccess != rsp.Code {
return err
}
a.attr.ID = rsp.Data.ID
return nil
}
func (a *attribute) Update(data frtypes.MapStr) error {
data.Remove(metadata.AttributeFieldPropertyID)
data.Remove(metadata.AttributeFieldObjectID)
data.Remove(metadata.AttributeFieldID)
if err := a.IsValid(true, data); nil != err {
return err
}
a.attr.OwnerID = a.params.SupplierAccount
exists, err := a.IsExists()
if nil != err {
return err
}
if exists {
return a.params.Err.Error(common.CCErrCommDuplicateItem)
}
rsp, err := a.clientSet.ObjectController().Meta().UpdateObjectAttByID(context.Background(), a.attr.ID, a.params.Header, data)
if nil != err {
blog.Errorf("failed to request object controller, error info is %s", err.Error())
return err
}
if common.CCSuccess != rsp.Code {
blog.Errorf("failed to update the object attribute(%s), error info is %s", a.attr.PropertyID, rsp.ErrMsg)
return a.params.Err.Error(common.CCErrTopoObjectAttributeUpdateFailed)
}
return nil
}
func (a *attribute) search(cond condition.Condition) ([]metadata.Attribute, error) {
rsp, err := a.clientSet.ObjectController().Meta().SelectObjectAttWithParams(context.Background(), a.params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("failed to request to object controller, error info is %s", err.Error())
return nil, err
}
if common.CCSuccess != rsp.Code {
blog.Errorf("failed to query the object controller, error info is %s", err.Error())
return nil, a.params.Err.Error(common.CCErrTopoObjectAttributeSelectFailed)
}
return rsp.Data, nil
}
func (a *attribute) IsExists() (bool, error) {
// check id
cond := condition.CreateCondition()
cond.Field(common.BKOwnerIDField).Eq(a.params.SupplierAccount)
cond.Field(metadata.AttributeFieldObjectID).Eq(a.attr.ObjectID)
cond.Field(metadata.AttributeFieldPropertyID).Eq(a.attr.PropertyID)
cond.Field(metadata.AttributeFieldID).NotIn([]int64{a.attr.ID})
items, err := a.search(cond)
if nil != err {
return false, err
}
if 0 != len(items) {
return true, err
}
// ceck nam
cond = condition.CreateCondition()
cond.Field(common.BKOwnerIDField).Eq(a.params.SupplierAccount)
cond.Field(metadata.AttributeFieldObjectID).Eq(a.attr.ObjectID)
cond.Field(metadata.AttributeFieldPropertyName).Eq(a.attr.PropertyName)
cond.Field(metadata.AttributeFieldID).NotIn([]int64{a.attr.ID})
items, err = a.search(cond)
if nil != err {
return false, err
}
if 0 != len(items) {
return true, err
}
return false, nil
}
func (a *attribute) Save(data frtypes.MapStr) error {
if nil != data {
if _, err := a.attr.Parse(data); nil != err {
return err
}
}
if exists, err := a.IsExists(); nil != err {
return err
} else if !exists {
return a.Create()
}
return a.Update(data)
}
func (a *attribute) SetSupplierAccount(supplierAccount string) {
a.attr.OwnerID = supplierAccount
}
func (a *attribute) GetSupplierAccount() string {
return a.attr.OwnerID
}
func (a *attribute) SetObjectID(objectID string) {
a.attr.ObjectID = objectID
}
func (a *attribute) GetObjectID() string {
return a.attr.ObjectID
}
func (a *attribute) SetID(attributeID string) {
a.attr.PropertyID = attributeID
}
func (a *attribute) GetID() string {
return a.attr.PropertyID
}
func (a *attribute) SetName(attributeName string) {
a.attr.PropertyName = attributeName
}
func (a *attribute) GetName() string {
return a.attr.PropertyName
}
func (a *attribute) SetGroup(grp Group) {
a.attr.PropertyGroup = grp.GetID()
a.attr.PropertyGroupName = grp.GetName()
}
func (a *attribute) GetGroup() (Group, error) {
cond := condition.CreateCondition()
cond.Field(metadata.GroupFieldGroupID).Eq(a.attr.PropertyGroup)
cond.Field(metadata.GroupFieldObjectID).Eq(a.attr.ObjectID)
cond.Field(metadata.GroupFieldSupplierAccount).Eq(a.attr.OwnerID)
rsp, err := a.clientSet.ObjectController().Meta().SelectPropertyGroupByObjectID(context.Background(), a.params.SupplierAccount, a.GetObjectID(), a.params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[model-grp] failed to request the object controller, error info is %s", err.Error())
return nil, a.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if common.CCSuccess != rsp.Code {
blog.Errorf("[model-grp] failed to search the group of the object(%s) by the condition (%#v), error info is %s", a.GetObjectID(), cond.ToMapStr(), rsp.ErrMsg)
return nil, a.params.Err.Error(rsp.Code)
}
if 0 == len(rsp.Data) {
return CreateGroup(a.params, a.clientSet, []metadata.Group{metadata.Group{GroupID: "default", GroupName: "Default", OwnerID: a.attr.OwnerID, ObjectID: a.attr.ObjectID}})[0], nil
}
return CreateGroup(a.params, a.clientSet, rsp.Data)[0], nil // should be one group
}
func (a *attribute) SetGroupIndex(attGroupIndex int64) {
a.attr.PropertyIndex = attGroupIndex
}
func (a *attribute) GetGroupIndex() int64 {
return a.attr.PropertyIndex
}
func (a *attribute) SetUnint(unit string) {
a.attr.Unit = unit
}
func (a *attribute) GetUnint() string {
return a.attr.Unit
}
func (a *attribute) SetPlaceholder(placeHolder string) {
a.attr.Placeholder = placeHolder
}
func (a *attribute) GetPlaceholder() string {
return a.attr.Placeholder
}
func (a *attribute) SetIsRequired(isRequired bool) {
a.attr.IsRequired = isRequired
}
func (a *attribute) GetIsRequired() bool {
return a.attr.IsRequired
}
func (a *attribute) SetIsEditable(isEditable bool) {
a.attr.IsEditable = isEditable
}
func (a *attribute) GetIsEditable() bool {
return a.attr.IsEditable
}
func (a *attribute) SetIsPre(isPre bool) {
a.attr.IsPre = isPre
}
func (a *attribute) GetIsPre() bool {
return a.attr.IsPre
}
func (a *attribute) SetIsReadOnly(isReadOnly bool) {
a.attr.IsReadOnly = isReadOnly
}
func (a *attribute) GetIsReadOnly() bool {
return a.attr.IsReadOnly
}
func (a *attribute) SetIsOnly(isOnly bool) {
a.attr.IsOnly = isOnly
}
func (a *attribute) GetIsOnly() bool {
return a.attr.IsOnly
}
func (a *attribute) SetIsSystem(isSystem bool) {
a.attr.IsSystem = isSystem
}
func (a *attribute) GetIsSystem() bool {
return a.attr.IsSystem
}
func (a *attribute) SetIsAPI(isAPI bool) {
a.attr.IsAPI = isAPI
}
func (a *attribute) GetIsAPI() bool {
return a.attr.IsAPI
}
func (a *attribute) SetType(attributeType string) {
a.attr.PropertyType = attributeType
}
func (a *attribute) GetType() string {
return a.attr.PropertyType
}
func (a *attribute) SetOption(attributeOption interface{}) {
a.attr.Option = attributeOption
}
func (a *attribute) GetOption() interface{} {
return a.attr.Option
}
func (a *attribute) SetDescription(attributeDescription string) {
a.attr.Description = attributeDescription
}
func (a *attribute) GetDescription() string {
return a.attr.Description
}
func (a *attribute) SetCreator(attributeCreator string) {
a.attr.Creator = attributeCreator
}
func (a *attribute) GetCreator() string {
return a.attr.Creator
}