forked from TencentBlueKing/bk-cmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inst_host.go
365 lines (289 loc) · 8.48 KB
/
inst_host.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
/*
* 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 inst
import (
"fmt"
"strconv"
"strings"
cccommon "configcenter/src/common"
"configcenter/src/framework/common"
"configcenter/src/framework/core/errors"
"configcenter/src/framework/core/log"
"configcenter/src/framework/core/output/module/client"
"configcenter/src/framework/core/output/module/model"
"configcenter/src/framework/core/types"
)
var _ HostInterface = (*host)(nil)
// HostInterface the host interface
type HostInterface interface {
Maintaince
Transfer() TransferInterface
SetBusinessID(bizID int64)
SetModuleIDS(moduleIDS []int64, isIncrement bool)
GetBizs() []types.MapStr
GetSets() []types.MapStr
GetModules() []types.MapStr
GetModel() model.Model
GetInstID() (int64, error)
GetInstName() string
SetValue(key string, value interface{}) error
GetValues() (types.MapStr, error)
}
type host struct {
isIncrement bool
bizs []types.MapStr
sets []types.MapStr
modules []types.MapStr
bizID int64
moduleIDS []int64
setIDS []int64
target model.Model
datas types.MapStr
}
func (cli *host) GetBizs() []types.MapStr {
return cli.bizs
}
func (cli *host) GetSets() []types.MapStr {
return cli.sets
}
func (cli *host) GetModules() []types.MapStr {
return cli.modules
}
func (cli *host) reset() error {
// parse business
datas, err := cli.datas.MapStrArray(Business)
if nil != err {
return fmt.Errorf("failed to get biz data , error info is %s", err.Error())
}
cli.bizs = datas
for _, dataVal := range datas {
id, err := dataVal.Int64(BusinessID)
if nil != err {
return fmt.Errorf("failed to get biz id, error info is %s", err.Error())
}
cli.bizID = id
}
// parse module
datas, err = cli.datas.MapStrArray(Module)
if nil != err {
return fmt.Errorf("failed to get module data , error info is %s", err.Error())
}
cli.modules = datas
for _, dataVal := range datas {
id, err := dataVal.Int64(ModuleID)
if nil != err {
return fmt.Errorf("failed to get module id, error info is %s", err.Error())
}
cli.moduleIDS = append(cli.moduleIDS, id)
//fmt.Println("the module id:", id)
}
// parse set
datas, err = cli.datas.MapStrArray(Set)
if nil != err {
return fmt.Errorf("failed to get module data , error info is %s", err.Error())
}
cli.sets = datas
for _, dataVal := range datas {
id, err := dataVal.Int64(SetID)
if nil != err {
return fmt.Errorf("failed to get set id, error info is %s", err.Error())
}
cli.setIDS = append(cli.setIDS, id)
}
return nil
}
func (cli *host) SetBusinessID(bizID int64) {
cli.bizID = bizID
}
func (cli *host) SetModuleIDS(moduleIDS []int64, isIncrement bool) {
cli.moduleIDS = moduleIDS
cli.isIncrement = isIncrement
}
func (cli *host) GetModel() model.Model {
return cli.target
}
func (cli *host) GetInstID() (int64, error) {
return cli.datas.Int64(cccommon.BKHostIDField)
}
func (cli *host) GetInstName() string {
return cli.datas.String(cccommon.BKHostIDField)
}
func (cli *host) GetValues() (types.MapStr, error) {
return cli.datas, nil
}
func (cli *host) SetValue(key string, value interface{}) error {
cli.datas.Set(key, value)
return nil
}
func (cli *host) Transfer() TransferInterface {
return &transfer{
targetHost: cli,
}
}
func (cli *host) ResetAssociationValue() error {
attrs, err := cli.target.Attributes()
if nil != err {
return err
}
for _, attrItem := range attrs {
if attrItem.GetKey() {
if model.FieldTypeSingleAsst == attrItem.GetType() {
asstVals, err := cli.datas.MapStrArray(attrItem.GetID())
if nil != err {
return err
}
for _, val := range asstVals {
// the datas is created by find
valID, exists := val.Get(InstID)
if exists {
cli.datas.Set(attrItem.GetID(), valID)
continue
}
// default the datas is new one
valID, exists = val.Get(attrItem.GetID())
if exists {
cli.datas.Set(attrItem.GetID(), valID)
}
}
continue
}
if model.FieldTypeMultiAsst == attrItem.GetType() {
asstVals, err := cli.datas.MapStrArray(attrItem.GetID())
if nil != err {
return err
}
condVals := make([]string, 0)
for _, val := range asstVals {
valID := val.String(InstID)
if 0 != len(valID) {
condVals = append(condVals, valID)
}
}
cli.datas.Set(attrItem.GetID(), strings.Join(condVals, ","))
}
}
}
return nil
}
func (cli *host) search() ([]model.Attribute, []types.MapStr, error) {
if err := cli.ResetAssociationValue(); nil != err {
return nil, nil, err
}
attrs, err := cli.target.Attributes()
if nil != err {
return nil, nil, err
}
cond := common.CreateCondition()
for _, attrItem := range attrs {
if attrItem.GetKey() {
attrVal, exists := cli.datas.Get(attrItem.GetID())
if !exists {
return nil, nil, errors.New("the key field(" + attrItem.GetID() + ") is not set")
}
cond.Field(attrItem.GetID()).Eq(attrVal)
}
}
//log.Infof("the condition:%#v", cond.ToMapStr())
// search by condition
items, err := client.GetClient().CCV3(client.Params{SupplierAccount: cli.target.GetSupplierAccount()}).Host().SearchHost(cond)
return attrs, items, err
}
func (cli *host) IsExists() (bool, error) {
attrs, err := cli.target.Attributes()
if nil != err {
return false, err
}
cond := common.CreateCondition()
for _, attrItem := range attrs {
if !attrItem.GetKey() {
continue
}
if !cli.datas.Exists(attrItem.GetID()) {
return false, errors.New("the key field(" + attrItem.GetID() + ") is not set")
}
cond.Field(attrItem.GetID()).Eq(cli.datas[attrItem.GetID()])
}
//log.Infof("host search condition:%s %d", string(cond.ToMapStr().ToJSON()), 0)
items, err := client.GetClient().CCV3(client.Params{SupplierAccount: cli.target.GetSupplierAccount()}).Host().SearchHost(cond)
if nil != err {
return false, err
}
return 0 != len(items), nil
}
func (cli *host) Create() error {
//log.Infof("the create host:%#v", cli.datas)
//log.Infof("create the exists:%s %d", string(cli.datas.ToJSON()), 0)
if exists, err := cli.IsExists(); nil != err {
return err
} else if exists {
return nil
}
//log.Infof("the create host:%#v", cli.datas)
ids, err := client.GetClient().CCV3(client.Params{SupplierAccount: cli.target.GetSupplierAccount()}).Host().CreateHostBatch(cli.bizID, cli.moduleIDS, cli.datas)
if nil != err {
return err
}
if 0 == len(ids) {
return fmt.Errorf("the host ids is empty")
}
cli.datas.Set(HostID, ids[0])
return nil
}
func (cli *host) Update() error {
attrs, existItems, err := cli.search()
if nil != err {
return err
}
//log.Infof("update the exists:%s %d", string(cli.datas.ToJSON()), 0)
// clear the invalid data
cli.datas.ForEach(func(key string, val interface{}) {
for _, attrItem := range attrs {
if attrItem.GetID() == key {
return
}
}
//log.Infof("remove the invalid field:%s", key)
cli.datas.Remove(key)
})
cli.datas.Remove("create_time") //invalid check , need to delete
for _, existItem := range existItems {
hostID, err := existItem.Int64(HostID)
if nil != err {
return err
}
updateCond := common.CreateCondition()
updateCond.Field(ModuleID).Eq(hostID)
//log.Infof("the exists:%s %d", string(existItem.ToJSON()), hostID)
err = client.GetClient().CCV3(client.Params{SupplierAccount: cli.target.GetSupplierAccount()}).Host().UpdateHostBatch(cli.datas, strconv.Itoa(int(hostID)))
if err != nil {
log.Errorf("failed to update host, error info is %s", err.Error())
return err
}
cli.datas.Set(HostID, hostID)
if 0 != len(cli.moduleIDS) {
err = cli.Transfer().MoveToModule(cli.moduleIDS, cli.isIncrement)
if nil != err {
log.Errorf("failed to biz(%d) set modules(%#v), error info is %s", cli.bizID, cli.moduleIDS, err.Error())
return err
}
}
}
return nil
}
func (cli *host) Save() error {
if exists, err := cli.IsExists(); nil != err {
return err
} else if exists {
return cli.Update()
}
return cli.Create()
}