forked from TencentBlueKing/bk-cmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inst_module.go
207 lines (163 loc) · 5.01 KB
/
inst_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
/*
* 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 (
"errors"
"configcenter/src/framework/common"
"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 _ ModuleInterface = (*module)(nil)
// ModuleInterface the module interface
type ModuleInterface interface {
Maintaince
SetTopo(bizID, setID int64)
GetModel() model.Model
GetInstID() (int64, error)
GetInstName() string
SetValue(key string, value interface{}) error
GetValues() (types.MapStr, error)
}
type module struct {
bizID int64
setID int64
target model.Model
datas types.MapStr
}
func (cli *module) SetTopo(bizID, setID int64) {
cli.setID = setID
cli.bizID = bizID
}
func (cli *module) GetModel() model.Model {
return cli.target
}
func (cli *module) GetInstID() (int64, error) {
return cli.datas.Int64(ModuleID)
}
func (cli *module) GetInstName() string {
return cli.datas.String(ModuleName)
}
func (cli *module) GetValues() (types.MapStr, error) {
return cli.datas, nil
}
func (cli *module) SetValue(key string, value interface{}) error {
// TODO:需要根据model 的定义对输入的key 及value 进行校验
cli.datas.Set(key, value)
return nil
}
func (cli *module) search() ([]model.Attribute, []types.MapStr, error) {
// get the attributes
attrs, err := cli.target.Attributes()
if nil != err {
return nil, nil, err
}
// construct the condition which is used to check the if it is exists
cond := common.CreateCondition().Field(BusinessID).Eq(cli.bizID).Field(SetID).Eq(cli.setID)
// extract the required id
for _, attrItem := range attrs {
if attrItem.GetKey() {
if attrItem.GetID() == BusinessID {
if 0 >= cli.bizID {
return nil, nil, errors.New("the key field(" + attrItem.GetID() + ") is not set")
}
cond.Field(BusinessID).Eq(cli.bizID)
continue
}
if attrItem.GetID() == SetID {
if 0 >= cli.setID {
return nil, nil, errors.New("the key field(" + attrItem.GetID() + ") is not set")
}
cond.Field(SetID).Eq(cli.setID)
continue
}
attrVal := cli.datas.String(attrItem.GetID())
if 0 == len(attrVal) {
return attrs, nil, errors.New("the key field(" + attrItem.GetID() + ") is not set")
}
cond.Field(attrItem.GetID()).Eq(attrVal)
}
}
//log.Infof("the module search condition:%#v", cond.ToMapStr())
// search by condition
existItems, err := client.GetClient().CCV3(client.Params{SupplierAccount: cli.target.GetSupplierAccount()}).Module().SearchModules(cond)
return attrs, existItems, err
}
func (cli *module) IsExists() (bool, error) {
_, items, err := cli.search()
if nil != err {
return false, err
}
return 0 != len(items), nil
}
func (cli *module) Create() error {
if 0 <= cli.setID {
cli.datas.Set(ParentID, cli.setID)
cli.datas.Set(SetID, cli.setID)
}
if 0 < cli.bizID {
cli.datas.Set(BusinessID, cli.bizID)
}
moduleID, err := client.GetClient().CCV3(client.Params{SupplierAccount: cli.target.GetSupplierAccount()}).Module().CreateModule(cli.bizID, cli.setID, cli.datas)
if nil != err {
return err
}
cli.datas.Set(ModuleID, moduleID)
return nil
}
func (cli *module) Update() error {
attrs, existItems, err := cli.search()
if nil != err {
return err
}
// clear the invalid field
cli.datas.ForEach(func(key string, val interface{}) {
for _, attrItem := range attrs {
if attrItem.GetID() == key {
return
}
}
cli.datas.Remove(key)
})
if 0 < cli.setID {
cli.datas.Set(ParentID, cli.setID)
cli.datas.Set(SetID, cli.setID)
}
if 0 < cli.bizID {
cli.datas.Set(BusinessID, cli.bizID)
}
cli.datas.Remove("create_time") //invalid check , need to delete
for _, existItem := range existItems {
instID, err := existItem.Int64(ModuleID)
if nil != err {
return err
}
cli.datas.Remove(ModuleID)
err = client.GetClient().CCV3(client.Params{SupplierAccount: cli.target.GetSupplierAccount()}).Module().UpdateModule(cli.bizID, cli.setID, instID, cli.datas)
if nil != err {
log.Infof("failed to update the module (%#v), error info is %s", existItem, err.Error())
return err
}
cli.datas.Set(ModuleID, instID)
}
return nil
}
func (cli *module) Save() error {
//fmt.Println("bizID:", cli.bizID, "setID:", cli.setID)
if exists, err := cli.IsExists(); nil != err {
return err
} else if exists {
return cli.Update()
}
return cli.Create()
}