forked from TencentBlueKing/bk-cmdb
/
association_mainline_inst.go
272 lines (222 loc) · 7.95 KB
/
association_mainline_inst.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
/*
* 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 (
"fmt"
"io"
"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"
)
func (cli *association) canReset(params types.ContextParams, currentInsts []inst.Inst) error {
instNames := map[string]struct{}{}
for _, currInst := range currentInsts {
currInstParentID, err := currInst.GetParentID()
if nil != err {
return err
}
// reset the child's parent
childs, err := currInst.GetMainlineChildInst()
if nil != err {
return err
}
for _, child := range childs {
instName, err := child.GetInstName()
if nil != err {
return err
}
key := fmt.Sprintf("%d_%s", currInstParentID, instName)
if _, ok := instNames[key]; ok {
errMsg := params.Err.Error(common.CCErrTopoDeleteMainLineObjectAndInstNameRepeat).Error() + " " + instName
return params.Err.New(common.CCErrTopoDeleteMainLineObjectAndInstNameRepeat, errMsg)
}
instNames[key] = struct{}{}
}
}
return nil
}
func (cli *association) ResetMainlineInstAssociatoin(params types.ContextParams, current model.Object) error {
defaultCond := &metadata.QueryInput{}
cond := condition.CreateCondition()
if current.IsCommon() {
cond.Field(common.BKObjIDField).Eq(current.GetID())
}
defaultCond.Condition = cond.ToMapStr()
// fetch all parent inst
_, currentInsts, err := cli.inst.FindInst(params, current, defaultCond, false)
if nil != err {
blog.Errorf("[operation-asst] failed to find current object(%s) inst, error info is %s", current.GetID(), err.Error())
return err
}
if err := cli.canReset(params, currentInsts); nil != err {
blog.Errorf("[operation-asst] can not be reset, error info is %s", err.Error())
return err
}
// NEED FIX: 下面循环中的continue ,会在处理实例异常的时候跳过当前拓扑的处理,此方式可能会导致某个业务拓扑失败,但是不会影响所有。
// reset the parent's inst
for _, currentInst := range currentInsts {
// delete the current inst
instID, err := currentInst.GetInstID()
if nil != err {
blog.Errorf("[operation-asst] failed to get the inst id from the inst(%#v)", currentInst.ToMapStr())
continue
}
parent, err := currentInst.GetMainlineParentInst()
if nil != err {
blog.Errorf("[operation-asst] failed to get the object(%s) mainline parent inst, the current inst(%v), error info is %s", current.GetID(), currentInst.GetValues(), err.Error())
continue
}
// reset the child's parent
childs, err := currentInst.GetMainlineChildInst()
if nil != err {
blog.Errorf("[operation-asst] failed to get the object(%s) mainline child inst, error info is %s", current.GetID(), err.Error())
continue
}
for _, child := range childs {
// set the child's parent
if err = child.SetMainlineParentInst(parent); nil != err {
blog.Errorf("[operation-asst] failed to set the object(%s) mainline child inst, error info is %s", child.GetObject().GetID(), err.Error())
continue
}
}
// delete the current inst
cond := condition.CreateCondition()
cond.Field(currentInst.GetObject().GetInstIDFieldName()).Eq(instID)
if err = cli.inst.DeleteInst(params, current, cond, false); nil != err {
blog.Errorf("[operation-asst] failed to delete the current inst(%#v), error info is %s", currentInst.ToMapStr(), err.Error())
continue
}
}
return nil
}
func (cli *association) SetMainlineInstAssociation(params types.ContextParams, parent, current, child model.Object) error {
defaultCond := &metadata.QueryInput{}
cond := condition.CreateCondition()
if parent.IsCommon() {
cond.Field(common.BKObjIDField).Eq(parent.GetID())
}
defaultCond.Condition = cond.ToMapStr()
// fetch all parent inst
_, parentInsts, err := cli.inst.FindInst(params, parent, defaultCond, false)
if nil != err {
blog.Errorf("[operation-asst] failed to find parent object(%s) inst, error info is %s", parent.GetID(), err.Error())
return err
}
// reset the parent's inst
for _, parent := range parentInsts {
id, err := parent.GetInstID()
if nil != err {
blog.Errorf("[operation-asst] failed to find the inst id, error info is %s", err.Error())
return err
}
// create the default inst
defaultInst := cli.instFactory.CreateInst(params, current)
defaultInst.SetValue(common.BKOwnerIDField, params.SupplierAccount)
defaultInst.SetValue(current.GetInstNameFieldName(), current.GetName())
defaultInst.SetValue(common.BKDefaultField, 0)
defaultInst.SetValue(common.BKInstParentStr, id)
// create the inst
if err = defaultInst.Create(); nil != err {
blog.Errorf("[operation-asst] failed to create object(%s) default inst, error info is %s", current.GetID(), err.Error())
return err
}
// reset the child's parent
childs, err := parent.GetMainlineChildInst()
if nil != err {
if io.EOF == err {
continue
}
blog.Errorf("[operation-asst] failed to get the object(%s) mainline child inst, error info is %s", parent.GetObject().GetID(), err.Error())
return err
}
for _, child := range childs {
// set the child's parent
if err = child.SetMainlineParentInst(defaultInst); nil != err {
blog.Errorf("[operation-asst] failed to set the object(%s) mainline child inst, error info is %s", child.GetObject().GetID(), err.Error())
return err
}
}
}
return nil
}
func (cli *association) constructTopo(params types.ContextParams, targetInst inst.Inst) ([]metadata.TopoInstRst, error) {
childs, err := targetInst.GetMainlineChildInst()
if nil != err {
return nil, err
}
results := []metadata.TopoInstRst{}
if 0 == len(childs) {
return []metadata.TopoInstRst{}, nil
}
for _, child := range childs {
rst := metadata.TopoInstRst{
Child: []metadata.TopoInstRst{},
}
id, err := child.GetInstID()
if nil != err {
return nil, err
}
name, err := child.GetInstName()
if nil != err {
return nil, err
}
rst.InstID = id
rst.InstName = name
rst.ObjID = child.GetObject().GetID()
rst.ObjName = child.GetObject().GetName()
childRst, err := cli.constructTopo(params, child)
if nil != err {
return nil, err
}
rst.Child = append(rst.Child, childRst...)
results = append(results, rst)
}
return results, nil
}
func (cli *association) SearchMainlineAssociationInstTopo(params types.ContextParams, obj model.Object, instID int64) ([]*metadata.TopoInstRst, error) {
cond := &metadata.QueryInput{}
cond.Condition = frtypes.MapStr{
obj.GetInstIDFieldName(): instID,
}
_, bizInsts, err := cli.inst.FindInst(params, obj, cond, false)
if nil != err {
return nil, err
}
results := make([]*metadata.TopoInstRst, 0)
for _, biz := range bizInsts {
instID, err := biz.GetInstID()
if nil != err {
return nil, err
}
instName, err := biz.GetInstName()
if nil != err {
return nil, err
}
tmp := &metadata.TopoInstRst{Child: []metadata.TopoInstRst{}}
tmp.InstID = instID
tmp.InstName = instName
tmp.ObjID = biz.GetObject().GetID()
tmp.ObjName = biz.GetObject().GetName()
rst, err := cli.constructTopo(params, biz)
if nil != err {
return nil, err
}
tmp.Child = append(tmp.Child, rst...)
results = append(results, tmp)
}
return results, nil
}