-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
association.go
249 lines (211 loc) · 10.5 KB
/
association.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
/*
* 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"
"configcenter/src/apimachinery"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/condition"
frtypes "configcenter/src/common/mapstr"
"configcenter/src/common/metadata"
metatype "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"
)
// AssociationOperationInterface association operation methods
type AssociationOperationInterface interface {
CreateMainlineAssociation(params types.ContextParams, data *metadata.Association) (model.Object, error)
DeleteMainlineAssociaton(params types.ContextParams, objID string) error
SearchMainlineAssociationTopo(params types.ContextParams, targetObj model.Object) ([]*metadata.MainlineObjectTopo, error)
SearchMainlineAssociationInstTopo(params types.ContextParams, obj model.Object, instID int64) ([]*metadata.TopoInstRst, error)
CreateCommonAssociation(params types.ContextParams, data *metadata.Association) error
DeleteAssociation(params types.ContextParams, cond condition.Condition) error
UpdateAssociation(params types.ContextParams, data frtypes.MapStr, cond condition.Condition) error
SearchObjectAssociation(params types.ContextParams, objID string) ([]metadata.Association, error)
SearchInstAssociation(params types.ContextParams, query *metadata.QueryInput) ([]metadata.InstAsst, error)
CheckBeAssociation(params types.ContextParams, obj model.Object, cond condition.Condition) error
CreateCommonInstAssociation(params types.ContextParams, data *metadata.InstAsst) error
DeleteInstAssociation(params types.ContextParams, cond condition.Condition) error
SetProxy(cls ClassificationOperationInterface, obj ObjectOperationInterface, grp GroupOperationInterface, attr AttributeOperationInterface, inst InstOperationInterface, targetModel model.Factory, targetInst inst.Factory)
}
// NewAssociationOperation create a new association operation instance
func NewAssociationOperation(client apimachinery.ClientSetInterface) AssociationOperationInterface {
return &association{
clientSet: client,
}
}
type association struct {
clientSet apimachinery.ClientSetInterface
cls ClassificationOperationInterface
obj ObjectOperationInterface
grp GroupOperationInterface
attr AttributeOperationInterface
inst InstOperationInterface
modelFactory model.Factory
instFactory inst.Factory
}
func (a *association) SetProxy(cls ClassificationOperationInterface, obj ObjectOperationInterface, grp GroupOperationInterface, attr AttributeOperationInterface, inst InstOperationInterface, targetModel model.Factory, targetInst inst.Factory) {
a.cls = cls
a.obj = obj
a.attr = attr
a.inst = inst
a.grp = grp
a.modelFactory = targetModel
a.instFactory = targetInst
}
func (a *association) SearchObjectAssociation(params types.ContextParams, objID string) ([]metadata.Association, error) {
cond := condition.CreateCondition()
cond.Field(common.BKOwnerIDField).Eq(params.SupplierAccount)
if 0 != len(objID) {
cond.Field(common.BKObjIDField).Eq(objID)
}
rsp, err := a.clientSet.ObjectController().Meta().SelectObjectAssociations(context.Background(), params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[operation-asst] failed to request object controller, error info is %s", err.Error())
return nil, params.Err.New(common.CCErrCommHTTPDoRequestFailed, err.Error())
}
if !rsp.Result {
blog.Errorf("[operation-asst] failed to search the object(%s) association info , error info is %s", objID, rsp.ErrMsg)
return nil, params.Err.New(rsp.Code, rsp.ErrMsg)
}
// DELETE: query the property name as the associated name,the follow code need to be deleted after relationships refactoring
propertyIDS := make([]string, 0)
for _, asst := range rsp.Data {
propertyIDS = append(propertyIDS, asst.ObjectAttID)
}
attrCond := condition.CreateCondition()
attrCond.Field(metadata.AttributeFieldSupplierAccount).Eq(params.SupplierAccount)
attrCond.Field(metadata.AttributeFieldPropertyID).In(propertyIDS)
attributes, err := a.attr.FindObjectAttribute(params, attrCond)
if nil != err {
blog.Errorf("[operation-asst] failed to query attributes by the condition(%v), error info is %s", attrCond.ToMapStr(), err.Error())
return rsp.Data, nil // return the origin data
}
for asstIDX, asst := range rsp.Data {
for _, attr := range attributes {
if asst.ObjectAttID == attr.GetID() {
rsp.Data[asstIDX].AsstName = attr.GetName()
}
}
}
return rsp.Data, nil
}
func (a *association) SearchInstAssociation(params types.ContextParams, query *metadata.QueryInput) ([]metadata.InstAsst, error) {
rsp, err := a.clientSet.ObjectController().Instance().SearchObjects(context.Background(), common.BKTableNameInstAsst, params.Header, query)
if nil != err {
blog.Errorf("[operation-asst] failed to request object controller, error info is %s", err.Error())
return nil, params.Err.New(common.CCErrCommHTTPDoRequestFailed, err.Error())
}
if !rsp.Result {
blog.Errorf("[operation-asst] failed to search the association info, query: %#v, error info is %s", query, rsp.ErrMsg)
return nil, params.Err.New(rsp.Code, rsp.ErrMsg)
}
var instAsst []metadata.InstAsst
for _, info := range rsp.Data.Info {
asst := metadata.InstAsst{}
if err := info.MarshalJSONInto(&asst); nil != err {
return nil, err
}
instAsst = append(instAsst, asst)
}
blog.V(4).Infof("[SearchInstAssociation] search association, condition: %#v, results: %#v, unmarshal to: %#v", query, rsp.Data.Info, instAsst)
return instAsst, nil
}
func (a *association) CreateCommonAssociation(params types.ContextParams, data *metadata.Association) error {
// check the association
cond := condition.CreateCondition()
cond.Field(metadata.AssociationFieldAssociationObjectID).Eq(data.AsstObjID)
cond.Field(metadata.AssociationFieldObjectID).Eq(data.ObjectID)
cond.Field(metadata.AssociationFieldSupplierAccount).Eq(params.SupplierAccount)
rsp, err := a.clientSet.ObjectController().Meta().SelectObjectAssociations(context.Background(), params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[operation-asst] failed to request object controller, error info is %s", err.Error())
return params.Err.New(common.CCErrCommHTTPDoRequestFailed, err.Error())
}
if !rsp.Result {
blog.Errorf("[operation-asst] failed to create the association (%#v) , error info is %s", cond.ToMapStr(), rsp.ErrMsg)
return params.Err.New(rsp.Code, rsp.ErrMsg)
}
if len(rsp.Data) > 0 {
blog.Errorf("[operation-asst] failed to create the association (%#v) , the associations %s->%s already exist ", cond.ToMapStr(), data.ObjectID, data.AsstObjID)
return params.Err.Errorf(common.CCErrTopoAssociationAlreadyExist, data.ObjectID, data.AsstObjID)
}
// create a new
rspAsst, err := a.clientSet.ObjectController().Meta().CreateObjectAssociation(context.Background(), params.Header, data)
if nil != err {
blog.Errorf("[operation-asst] failed to request object controller, error info is %s", err.Error())
return params.Err.New(common.CCErrCommHTTPDoRequestFailed, err.Error())
}
if !rspAsst.Result {
blog.Errorf("[operation-asst] failed to create the association (%#v) , error info is %s", data, rspAsst.ErrMsg)
return params.Err.New(rspAsst.Code, rspAsst.ErrMsg)
}
return nil
}
func (a *association) DeleteInstAssociation(params types.ContextParams, cond condition.Condition) error {
rsp, err := a.clientSet.ObjectController().Instance().DelObject(context.Background(), common.BKTableNameInstAsst, params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[operation-asst] failed to request object controller, error info is %s", err.Error())
return params.Err.New(common.CCErrCommHTTPDoRequestFailed, err.Error())
}
if !rsp.Result {
blog.Errorf("[operation-asst] failed to delete the inst association info , error info is %s", rsp.ErrMsg)
return params.Err.New(rsp.Code, rsp.ErrMsg)
}
return nil
}
func (a *association) CreateCommonInstAssociation(params types.ContextParams, data *metadata.InstAsst) error {
// create a new
rspAsst, err := a.clientSet.ObjectController().Instance().CreateObject(context.Background(), common.BKTableNameInstAsst, params.Header, data)
if nil != err {
blog.Errorf("[operation-asst] failed to request object controller, error info is %s", err.Error())
return params.Err.New(common.CCErrCommHTTPDoRequestFailed, err.Error())
}
if !rspAsst.Result {
blog.Errorf("[operation-asst] failed to create the association (%#v) , error info is %s", data, rspAsst.ErrMsg)
return params.Err.New(rspAsst.Code, rspAsst.ErrMsg)
}
return nil
}
func (a *association) DeleteAssociation(params types.ContextParams, cond condition.Condition) error {
// delete the object association
rsp, err := a.clientSet.ObjectController().Meta().DeleteObjectAssociation(context.Background(), 0, params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[operation-asst] failed to request object controller, error info is %s", err.Error())
return params.Err.New(common.CCErrCommHTTPDoRequestFailed, err.Error())
}
if !rsp.Result {
blog.Errorf("[operation-asst] failed to create the association (%#v) , error info is %s", cond.ToMapStr(), rsp.ErrMsg)
return params.Err.New(rsp.Code, rsp.ErrMsg)
}
return nil
}
func (a *association) UpdateAssociation(params types.ContextParams, data frtypes.MapStr, cond condition.Condition) error {
return nil
}
// CheckBeAssociation and return error if the obj has been bind
func (a *association) CheckBeAssociation(params types.ContextParams, obj model.Object, cond condition.Condition) error {
exists, err := a.SearchInstAssociation(params, &metatype.QueryInput{Condition: cond.ToMapStr()})
if nil != err {
return err
}
if len(exists) > 0 {
beAsstObject := []string{}
for _, asst := range exists {
beAsstObject = append(beAsstObject, asst.ObjectID)
}
return params.Err.Errorf(common.CCErrTopoInstHasBeenAssociation, beAsstObject)
}
return nil
}