-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
objectatt_group_action.go
353 lines (288 loc) · 13.4 KB
/
objectatt_group_action.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
/*
* 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 service
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
restful "github.com/emicklei/go-restful"
"configcenter/src/common"
"configcenter/src/common/blog"
meta "configcenter/src/common/metadata"
"configcenter/src/common/util"
)
// CreatePropertyGroup to create property group
func (cli *Service) CreatePropertyGroup(req *restful.Request, resp *restful.Response) {
blog.Info("create property group")
// get the language
language := util.GetActionLanguage(req)
ownerID := util.GetOwnerID(req.Request.Header)
// get the error factory by the language
defErr := cli.Core.CCErr.CreateDefaultCCErrorIf(language)
// execute
// read body data
val, err := ioutil.ReadAll(req.Request.Body)
if err != nil {
blog.Error("read http request body failed, error:%s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommHTTPReadBodyFailed, err.Error())})
return
}
// parse the body data
propertyGroup := &meta.Group{}
jsErr := json.Unmarshal(val, propertyGroup)
if nil != jsErr {
blog.Error("failed to unmarshal the data, data is %s, error info is %s ", string(val), jsErr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommJSONUnmarshalFailed, jsErr.Error())})
return
}
// save the data
id, err := cli.Instance.GetIncID("cc_PropertyGroup")
if err != nil {
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupInsertFailed, err.Error())})
return
}
propertyGroup.ID = id
propertyGroup.OwnerID = ownerID
_, err = cli.Instance.Insert("cc_PropertyGroup", propertyGroup)
if nil == err && !cli.Instance.IsNotFoundErr(err) {
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp, Data: propertyGroup})
return
}
blog.Errorf("failed to insert the property group , error info is %s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupInsertFailed, err.Error())})
}
// UpdatePropertyGroup to update property group
func (cli *Service) UpdatePropertyGroup(req *restful.Request, resp *restful.Response) {
blog.Info("update property group")
// get the language
language := util.GetActionLanguage(req)
ownerID := util.GetOwnerID(req.Request.Header)
// get the error factory by the language
defErr := cli.Core.CCErr.CreateDefaultCCErrorIf(language)
// read body data
val, err := ioutil.ReadAll(req.Request.Body)
if err != nil {
blog.Error("read http request body failed, error:%s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommHTTPReadBodyFailed, err.Error())})
return
}
propertyGroup := &meta.PropertyGroupCondition{}
jsErr := json.Unmarshal(val, propertyGroup)
if nil != jsErr {
blog.Error("failed to unmarshal the data, data is %s, error info is %s ", string(val), jsErr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommJSONUnmarshalFailed, err.Error())})
return
}
propertyGroup.Condition = util.SetModOwner(propertyGroup.Condition, ownerID)
if updateerr := cli.Instance.UpdateByCondition("cc_PropertyGroup", propertyGroup.Data, propertyGroup.Condition); nil != updateerr {
blog.Error("fail update object by condition, error:%v", updateerr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupUpdateFailed, err.Error())})
return
}
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
}
// SelectGroup search groups
func (cli *Service) SelectGroup(req *restful.Request, resp *restful.Response) {
blog.Info("select property group")
// get the language
language := util.GetActionLanguage(req)
ownerID := util.GetOwnerID(req.Request.Header)
// get the error factory by the language
defErr := cli.Core.CCErr.CreateDefaultCCErrorIf(language)
defLang := cli.Core.Language.CreateDefaultCCLanguageIf(language)
// execute
value, err := ioutil.ReadAll(req.Request.Body)
if err != nil {
blog.Error("read request body failed, error information is %s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommHTTPReadBodyFailed, err.Error())})
return
}
// selector := &metadata.PropertyGroup{Page: &metadata.BasePage{Limit: common.BKNoLimit}}
condition := map[string]interface{}{}
if jsErr := json.Unmarshal([]byte(value), &condition); nil != jsErr {
blog.Error("unmarshal failed, error information %s is %s", value, jsErr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommJSONUnmarshalFailed, jsErr.Error())})
return
}
page := meta.ParsePage(condition["page"])
delete(condition, "page")
condition = util.SetQueryOwner(condition, ownerID)
results := make([]meta.Group, 0)
if selerr := cli.Instance.GetMutilByCondition(common.BKTableNamePropertyGroup, nil, condition, &results, page.Sort, page.Start, page.Limit); nil != selerr {
blog.Error("find object by selector failed, error information is %s", selerr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupSelectFailed, selerr.Error())})
return
}
// translate language
for index := range results {
results[index].GroupName = cli.TranslatePropertyGroupName(defLang, &results[index])
}
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp, Data: results})
}
// DeletePropertyGroup to update property group
func (cli *Service) DeletePropertyGroup(req *restful.Request, resp *restful.Response) {
blog.Info("delete property group")
// get the language
language := util.GetActionLanguage(req)
ownerID := util.GetOwnerID(req.Request.Header)
// get the error factory by the language
defErr := cli.Core.CCErr.CreateDefaultCCErrorIf(language)
// execute
id, conErr := strconv.Atoi(req.PathParameter("id"))
if nil != conErr {
blog.Error("id(%s) should be int value, error info is %s", req.PathParameter("id"), conErr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommParamsNeedInt, conErr.Error())})
return
}
condition := map[string]interface{}{"id": id}
condition = util.SetModOwner(condition, ownerID)
cnt, cntErr := cli.Instance.GetCntByCondition(common.BKTableNamePropertyGroup, condition)
if nil != cntErr {
blog.Error("failed to select object group by condition(%+v), error is %d", cntErr)
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupDeleteFailed, cntErr.Error())})
return
}
if 0 == cnt {
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
return
}
if delErr := cli.Instance.DelByCondition(common.BKTableNamePropertyGroup, condition); nil != delErr {
blog.Error("failed to delete property group by condition, error:%v", delErr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupDeleteFailed, delErr.Error())})
return
}
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
}
// UpdatePropertyGroupObjectAtt to update property group object attribute
func (cli *Service) UpdatePropertyGroupObjectAtt(req *restful.Request, resp *restful.Response) {
blog.Info("update property group")
// get the language
language := util.GetActionLanguage(req)
ownerID := util.GetOwnerID(req.Request.Header)
// get the error factory by the language
defErr := cli.Core.CCErr.CreateDefaultCCErrorIf(language)
// execute
// read body data
val, err := ioutil.ReadAll(req.Request.Body)
if err != nil {
blog.Error("read http request body failed, error:%s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommHTTPReadBodyFailed, err.Error())})
return
}
// decode the data struct
propertyGroupObjectAttArr := make([]meta.PropertyGroupObjectAtt, 0)
jsErr := json.Unmarshal(val, &propertyGroupObjectAttArr)
if nil != jsErr {
blog.Error("failed to unmarshal the data, data is %s, error info is %s ", string(val), jsErr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommJSONUnmarshalFailed, err.Error())})
return
}
for _, objAtt := range propertyGroupObjectAttArr {
// update the object attributes
objectAttSelector := map[string]interface{}{
common.BKOwnerIDField: objAtt.Condition.OwnerID,
common.BKObjIDField: objAtt.Condition.ObjectID,
common.BKPropertyIDField: objAtt.Condition.PropertyID,
}
objectAttValue := map[string]interface{}{
"bk_property_index": objAtt.Data.PropertyIndex,
"bk_property_group": objAtt.Data.PropertyGroupID,
}
objectAttSelector = util.SetModOwner(objectAttSelector, ownerID)
// update the object attribute
if updateerr := cli.Instance.UpdateByCondition(common.BKTableNameObjAttDes, objectAttValue, objectAttSelector); nil != updateerr {
blog.Error("fail update object by condition, error:%v", updateerr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupUpdateFailed, updateerr.Error())})
return
}
}
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
}
// DeletePropertyGroupObjectAtt to delete property group object attribute
func (cli *Service) DeletePropertyGroupObjectAtt(req *restful.Request, resp *restful.Response) {
blog.Info("delete property group object attribute")
// get the language
language := util.GetActionLanguage(req)
ownerID := util.GetOwnerID(req.Request.Header)
// get the error factory by the language
defErr := cli.Core.CCErr.CreateDefaultCCErrorIf(language)
// execute
// update the object attributes
objectAttSelector := map[string]interface{}{
common.BKOwnerIDField: req.PathParameter("owner_id"),
common.BKObjIDField: req.PathParameter("object_id"),
common.BKPropertyIDField: req.PathParameter("property_id"),
common.BKPropertyGroupField: req.PathParameter("group_id"),
}
objectAttValue := map[string]interface{}{
"bk_property_index": -1,
common.BKPropertyGroupField: "default",
}
objectAttSelector = util.SetModOwner(objectAttSelector, ownerID)
cnt, cntErr := cli.Instance.GetCntByCondition(common.BKTableNameObjAttDes, objectAttSelector)
if nil != cntErr {
blog.Error("failed to select objectatt group by condition(%+v), error is %d", cntErr)
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupDeleteFailed, cntErr.Error())})
return
}
if 0 == cnt {
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
return
}
// update the object attribute
if updateerr := cli.Instance.UpdateByCondition(common.BKTableNameObjAttDes, objectAttValue, objectAttSelector); nil != updateerr {
blog.Error("fail update object by condition, error:%v", updateerr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupUpdateFailed, cntErr.Error())})
return
}
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
}
// SelectPropertyGroupByObjectID to search
func (cli *Service) SelectPropertyGroupByObjectID(req *restful.Request, resp *restful.Response) {
blog.Info("select property group object attribute")
// get the language
language := util.GetActionLanguage(req)
ownerID := util.GetOwnerID(req.Request.Header)
// get the error factory by the language
defErr := cli.Core.CCErr.CreateDefaultCCErrorIf(language)
defLang := cli.Core.Language.CreateDefaultCCLanguageIf(language)
// execute
groupSelector := map[string]interface{}{}
if jserr := json.NewDecoder(req.Request.Body).Decode(&groupSelector); nil != jserr {
blog.Error("unmarshal failed, is %s", jserr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommJSONUnmarshalFailed, jserr.Error())})
return
}
// update the object attributes
groupSelector[common.BKOwnerIDField] = req.PathParameter("owner_id")
groupSelector[common.BKObjIDField] = req.PathParameter("object_id")
page := meta.ParsePage(groupSelector["page"])
if page.Sort == "" {
page.Sort = "bk_group_name"
}
delete(groupSelector, "page")
groupSelector = util.SetQueryOwner(groupSelector, ownerID)
blog.Debug("group property selector %+v", groupSelector)
results := make([]meta.Group, 0)
// select the object group
if selerr := cli.Instance.GetMutilByCondition(common.BKTableNamePropertyGroup, nil, groupSelector, &results, page.Sort, page.Start, page.Limit); nil != selerr {
blog.Error("select data failed, error information is %s", selerr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectPropertyGroupSelectFailed, selerr.Error())})
return
}
// translate language
for index := range results {
results[index].GroupName = cli.TranslatePropertyGroupName(defLang, &results[index])
}
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp, Data: results})
}