-
Notifications
You must be signed in to change notification settings - Fork 0
/
association_mainline_object.go
209 lines (176 loc) · 7.52 KB
/
association_mainline_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
/*
* 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 (
"io"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/condition"
"configcenter/src/common/errors"
"configcenter/src/common/metadata"
"configcenter/src/scene_server/topo_server/core/model"
"configcenter/src/scene_server/topo_server/core/types"
)
func (a *association) DeleteMainlineAssociaton(params types.ContextParams, objID string) error {
targetObj, err := a.obj.FindSingleObject(params, objID)
if nil != err {
blog.Errorf("[operation-asst] failed to find the target object(%s), error info is %s", objID, err.Error())
return err
}
parentObj, err := targetObj.GetMainlineParentObject()
if nil != err {
blog.Errorf("[operation-asst] failed to find the object(%s)'s parent, error info is %s", objID, err.Error())
return err
}
// update associations
childObj, err := targetObj.GetMainlineChildObject()
if nil != err && io.EOF != err {
blog.Errorf("[operation-asst] failed to find the object(%s)'s child, error info is %s", objID, err.Error())
return err
}
if err = a.ResetMainlineInstAssociatoin(params, targetObj); nil != err && io.EOF != err {
blog.Errorf("[operation-asst] failed to delete the object(%s)'s insts, error info %s", objID, err.Error())
return err
}
if nil != childObj {
// FIX: 正常情况下 childObj 不可以能为 nil,只有在拓扑异常的时候才会出现
if err = childObj.SetMainlineParentObject(parentObj.GetID()); nil != err && io.EOF != err {
blog.Errorf("[operation-asst] failed to update the association, error info is %s", err.Error())
return err
}
}
// delete objects
if err = a.obj.DeleteObject(params, targetObj.GetRecordID(), nil, false); nil != err && io.EOF != err {
blog.Errorf("[operation-asst] failed to delete the object(%s), error info is %s", targetObj.GetID(), err.Error())
return err
}
// delete this object related association.
cond := condition.CreateCondition()
cond.Field(metadata.AssociationFieldObjectID).Eq(targetObj.GetID())
cond.Field(common.BKOwnerIDField).Eq(targetObj.GetSupplierAccount())
if err = a.DeleteAssociation(params, cond); nil != err {
blog.Errorf("[operation-asst] failed to delete the association, error info is %s", err.Error())
return err
}
return nil
}
func (a *association) SearchMainlineAssociationTopo(params types.ContextParams, targetObj model.Object) ([]*metadata.MainlineObjectTopo, error) {
results := make([]*metadata.MainlineObjectTopo, 0)
for {
tmpRst := &metadata.MainlineObjectTopo{}
tmpRst.ObjID = targetObj.GetID()
tmpRst.ObjName = targetObj.GetName()
tmpRst.OwnerID = params.SupplierAccount
parentObj, err := targetObj.GetMainlineParentObject()
if nil == err {
tmpRst.PreObjID = parentObj.GetID()
tmpRst.PreObjName = parentObj.GetName()
} else if nil != err && io.EOF != err {
return nil, err
}
childObj, err := targetObj.GetMainlineChildObject()
if nil == err {
tmpRst.NextObj = childObj.GetID()
tmpRst.NextName = childObj.GetName()
} else if nil != err {
if io.EOF == err {
results = append(results, tmpRst)
return results, nil
}
return nil, err
}
results = append(results, tmpRst)
targetObj = childObj
}
}
func (a *association) CreateMainlineAssociation(params types.ContextParams, data *metadata.Association) (model.Object, error) {
// find the mainline module's head, which is biz.
bizObj, err := a.obj.FindSingleObject(params, common.BKInnerObjIDApp)
if nil != err {
blog.Errorf("[operation-asst] failed to check the mainline topo level, error info is %s", err.Error())
return nil, err
}
items, err := a.SearchMainlineAssociationTopo(params, bizObj)
if nil != err {
blog.Errorf("[operation-asst] failed to check the mainline topo level, error info is %s", err.Error())
return nil, err
}
if len(items) >= params.MaxTopoLevel {
blog.Errorf("[operation-asst] the mainline topo leve is %d, the max limit is %d", len(items), params.MaxTopoLevel)
return nil, params.Err.Error(common.CCErrTopoBizTopoLevelOverLimit)
}
// check and fetch the association object's classification
objCls, err := a.cls.FindSingleClassification(params, data.ClassificationID)
if nil != err {
blog.Errorf("[opration-asst] failed to find the single classification, error info is %s", err.Error())
return nil, params.Err.Errorf(common.CCErrCommParamsInvalid, data.ClassificationID)
}
// find the mainline parent object
parentObj, err := a.obj.FindSingleObject(params, data.AsstObjID)
switch t := err.(type) {
case nil:
default:
blog.Errorf("[operation-asst] failed to find the single object(%s), error info is %s", data.ObjectID, t.Error())
return nil, t
case errors.CCErrorCoder:
if t.GetCode() == common.CCErrTopoObjectSelectFailed {
blog.Errorf("[operation-asst] failed to find the single object(%s), error info is %s", data.ObjectID, t.Error())
return nil, t
}
}
// find the mainline child object for the parent
childObj, err := parentObj.GetMainlineChildObject()
if nil != err {
blog.Errorf("[operation-asst] failed to find the child object for the object(%s), error info is %s", parentObj.GetID(), err.Error())
return nil, err
}
// check and create the association mainline object
if err = a.obj.IsValidObject(params, data.ObjectID); nil == err {
blog.Errorf("[operation-asst] the object(%s) is duplicate", data.ObjectID)
return nil, params.Err.Errorf(common.CCErrCommDuplicateItem, data.ObjectID)
}
currentObj := a.modelFactory.CreaetObject(params)
currentObj.SetID(data.ObjectID)
currentObj.SetName(data.ObjectName)
currentObj.SetIcon(data.ObjectIcon)
currentObj.SetClassification(objCls)
if err = currentObj.Save(nil); nil != err {
blog.Errorf("[operation-asst] failed to create the object(%s), error info is %s", currentObj.GetID(), err.Error())
return nil, err
}
// create the default group
grp := currentObj.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())
return nil, err
}
// update the mainline topo inst association
if err = a.SetMainlineInstAssociation(params, parentObj, currentObj, childObj); nil != err {
blog.Errorf("[operation-asst] failed set the mainline inst association, error info is %s", err.Error())
return nil, err
}
if err = currentObj.CreateMainlineObjectAssociation(parentObj.GetID()); err != nil {
blog.Errorf("[operation-asst] create mainline object[%s] association related to object[%s] failed, err: %v",
currentObj.GetID(), parentObj.GetID(), err)
return nil, err
}
if err = childObj.UpdateMainlineObjectAssociationTo(parentObj.GetID(), currentObj.GetID()); err != nil {
blog.Errorf("[operation-asst] update mainline current object's[%s] child object[%s] association to current failed, err: %v",
currentObj.GetID(), childObj.GetID(), err)
return nil, err
}
return currentObj, nil
}