forked from TencentBlueKing/bk-cmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
296 lines (240 loc) · 10.6 KB
/
module.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
/*
* 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 compatiblev2
import (
"context"
"configcenter/src/apimachinery"
"configcenter/src/common"
"configcenter/src/common/blog"
"configcenter/src/common/condition"
"configcenter/src/common/mapstr"
"configcenter/src/common/metadata"
"configcenter/src/scene_server/topo_server/core/types"
)
// ModuleInterface module interface
type ModuleInterface interface {
UpdateMultiModule(bizID int64, moduleIDS interface{}, innerData mapstr.MapStr) error
SearchModuleByApp(query *metadata.QueryInput) (*metadata.InstResult, error)
SearchModuleBySetProperty(bizID int64, cond condition.Condition) (*metadata.InstResult, error)
AddMultiModule(bizID, setID int64, moduleNames []string, data mapstr.MapStr) error
DeleteMultiModule(bizID int64, moduleIDS []int64) error
}
// NewModule create a module instance
func NewModule(params types.ContextParams, client apimachinery.ClientSetInterface) ModuleInterface {
return &module{
params: params,
client: client,
}
}
type module struct {
params types.ContextParams
client apimachinery.ClientSetInterface
}
func (m *module) hasHost(bizID int64, moduleIDS []int64) (bool, error) {
cond := map[string][]int64{
"ApplicationID": []int64{bizID},
"ModuleID": moduleIDS,
}
rsp, err := m.client.HostController().Module().GetModulesHostConfig(context.Background(), m.params.Header, cond)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to request the object controller, error info is %s", err.Error())
return false, m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rsp.Result {
blog.Errorf("[compatiblev2-module] failed to search the host module configures, error info is %s", err.Error())
return false, m.params.Err.New(rsp.Code, rsp.ErrMsg)
}
return 0 != len(rsp.Data), nil
}
func (m *module) isValidSet(bizID, setID int64) (bool, error) {
cond := condition.CreateCondition()
cond.Field(common.BKAppIDField).Eq(bizID)
cond.Field(common.BKSetIDField).Eq(setID)
query := &metadata.QueryInput{}
query.Condition = cond.ToMapStr()
rsp, err := m.client.ObjectController().Instance().SearchObjects(context.Background(), common.BKInnerObjIDSet, m.params.Header, query)
if nil != err {
blog.Errorf("[compatiblev2-module]failed to request object controller, error info is %s", err.Error())
return false, m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rsp.Result {
blog.Errorf("[compatiblev2-module] failed to search the sets, error info is %s", rsp.ErrMsg)
return false, m.params.Err.New(rsp.Code, rsp.ErrMsg)
}
return true, nil
}
func (m *module) isRepeated(moduleName string, excludeModuleIDS interface{}) (bool, error) {
cond := condition.CreateCondition()
cond.Field(common.BKModuleNameField).Eq(moduleName)
cond.Field(common.BKModuleIDField).NotIn(excludeModuleIDS)
query := &metadata.QueryInput{}
query.Condition = cond.ToMapStr()
rsp, err := m.client.ObjectController().Instance().SearchObjects(context.Background(), common.BKInnerObjIDModule, m.params.Header, query)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to request object controller, error info is %s", err.Error())
return false, m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rsp.Result {
blog.Errorf("[compatiblev2-module] failed to search the module, error info is %s", rsp.ErrMsg)
return false, m.params.Err.New(rsp.Code, rsp.ErrMsg)
}
return 0 != rsp.Data.Count, nil
}
func (m *module) UpdateMultiModule(bizID int64, moduleIDS interface{}, innerData mapstr.MapStr) error {
// check the module name
moduleName, err := innerData.String(common.BKModuleNameField)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to parse the module name , error info is %s", err.Error())
return m.params.Err.New(common.CCErrCommParamsIsInvalid, err.Error())
}
repeated, err := m.isRepeated(moduleName, moduleIDS)
if nil != err {
return err
}
if repeated {
blog.Error("[compatiblev2-module] the module name is repeated")
return m.params.Err.Errorf(common.CCErrCommDuplicateItem, moduleName)
}
// update module
cond := condition.CreateCondition()
cond.Field(common.BKAppIDField).Eq(bizID)
cond.Field(common.BKModuleIDField).In(moduleIDS)
updateData := mapstr.New()
updateData.Set("condition", cond.ToMapStr())
updateData.Set("data", innerData)
rsp, err := m.client.ObjectController().Instance().UpdateObject(context.Background(), common.BKInnerObjIDModule, m.params.Header, updateData)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to request object controller, error info is %s", err.Error())
return m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rsp.Result {
blog.Errorf("[compatiblev2-module] failed to search the module, error info is %s", rsp.ErrMsg)
return m.params.Err.New(rsp.Code, rsp.ErrMsg)
}
return nil
}
func (m *module) SearchModuleByApp(query *metadata.QueryInput) (*metadata.InstResult, error) {
rsp, err := m.client.ObjectController().Instance().SearchObjects(context.Background(), common.BKInnerObjIDModule, m.params.Header, query)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to request object controller, error info is %s", err.Error())
return nil, m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rsp.Result {
blog.Errorf("[compatiblev2-module] failed to search the module, error info is %s", rsp.ErrMsg)
return nil, m.params.Err.New(rsp.Code, rsp.ErrMsg)
}
return &rsp.Data, nil
}
func (m *module) SearchModuleBySetProperty(bizID int64, cond condition.Condition) (*metadata.InstResult, error) {
query := &metadata.QueryInput{}
query.Condition = cond.ToMapStr()
query.Limit = common.BKNoLimit
//fmt.Println("cond:", cond.ToMapStr())
// search sets
rsp, err := m.client.ObjectController().Instance().SearchObjects(context.Background(), common.BKInnerObjIDSet, m.params.Header, query)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to request object controller, error info is %s", err.Error())
return nil, m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rsp.Result {
blog.Errorf("[compatiblev2-module] failed to search the sets, error info is %s", rsp.ErrMsg)
return nil, m.params.Err.New(rsp.Code, rsp.ErrMsg)
}
// construct the sets search condition
setIDS := make([]int64, 0)
for _, set := range rsp.Data.Info {
id, err := set.Int64(common.BKSetIDField)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to search sets, error info is %s", err.Error())
return nil, m.params.Err.New(common.CCErrCommParamsIsInvalid, err.Error())
}
setIDS = append(setIDS, id)
}
// search modules
cond = condition.CreateCondition()
cond.Field(common.BKAppIDField).Eq(bizID)
cond.Field(common.BKSetIDField).In(setIDS)
query.Condition = cond.ToMapStr()
rspModule, err := m.client.ObjectController().Instance().SearchObjects(context.Background(), common.BKInnerObjIDModule, m.params.Header, query)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to request object controller, error info is %s", err.Error())
return nil, m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rspModule.Result {
blog.Errorf("[compatiblev2-module] failed to search the sets, error info is %s", rspModule.ErrMsg)
return nil, m.params.Err.New(rspModule.Code, rspModule.ErrMsg)
}
return &rspModule.Data, nil
}
func (m *module) AddMultiModule(bizID, setID int64, moduleNames []string, data mapstr.MapStr) error {
ok, err := m.isValidSet(bizID, setID)
if nil != err {
return err
}
if !ok {
blog.Errorf("[compatiblev2-module] the set id (%d) is invalid", setID)
return m.params.Err.New(common.CCErrCommParamsIsInvalid, "the set id is invalid")
}
for _, moduleName := range moduleNames {
if len(moduleName) > 24 { // the module name lengh limit max
blog.Errorf("[compatiblev2-module] the module name is over the max length limit")
return m.params.Err.New(common.CCErrCommParamsIsInvalid, "the module name is over max limit")
}
exists, err := m.isRepeated(moduleName, []int64{})
if nil != err {
blog.Errorf("[compatiblev2-module] failed to check the module name(%s), error info is %s ", moduleNames, err.Error())
return err
}
if exists {
blog.Errorf("[compatiblev2-module] the module name (%s) is duplicated ", moduleName)
return m.params.Err.Errorf(common.CCErrCommDuplicateItem, moduleName)
}
data.Set(common.BKModuleNameField, moduleName)
data.Set(common.BKAppIDField, bizID)
data.Set(common.BKDefaultField, 0)
data.Set(common.BKOwnerIDField, common.BKDefaultOwnerID)
rsp, err := m.client.ObjectController().Instance().CreateObject(context.Background(), common.BKInnerObjIDModule, m.params.Header, data)
if nil != err {
blog.Errorf("[compatiblev2-module] failed to request object controller, error info is %s", err.Error())
return m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rsp.Result {
blog.Errorf("[compatiblev2-module] failed to add the module name, error info is %s", rsp.ErrMsg)
return m.params.Err.New(rsp.Code, rsp.ErrMsg)
}
}
return nil
}
func (m *module) DeleteMultiModule(bizID int64, moduleIDS []int64) error {
exists, err := m.hasHost(bizID, moduleIDS)
if nil != err {
return err
}
if exists {
blog.Errorf("[compatiblev2-module] failed to delete the modues(%#v) for the business(%d), which has some hosts ", moduleIDS, bizID)
return m.params.Err.Error(common.CCErrTopoHasHost)
}
cond := condition.CreateCondition()
cond.Field(common.BKAppIDField).Eq(bizID)
cond.Field(common.BKModuleIDField).In(moduleIDS)
//fmt.Println("cond:", cond.ToMapStr())
rsp, err := m.client.ObjectController().Instance().DelObject(context.Background(), common.BKInnerObjIDModule, m.params.Header, cond.ToMapStr())
if nil != err {
blog.Errorf("[compatiblev2-module] failed to request object controller, error info is %s", err.Error())
return m.params.Err.Error(common.CCErrCommHTTPDoRequestFailed)
}
if !rsp.Result {
blog.Errorf("[compatiblev2-module] failed to delete the modules(%#v), error info is %s", moduleIDS, rsp.ErrMsg)
return m.params.Err.New(rsp.Code, rsp.ErrMsg)
}
return nil
}