forked from TencentBlueKing/bk-cmdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_action.go
executable file
·244 lines (205 loc) · 8.9 KB
/
object_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
/*
* 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"
"time"
simplejson "github.com/bitly/go-simplejson"
"github.com/emicklei/go-restful"
"configcenter/src/common"
"configcenter/src/common/blog"
meta "configcenter/src/common/metadata"
"configcenter/src/common/util"
)
// CreateObject create a common object
func (cli *Service) CreateObject(req *restful.Request, resp *restful.Response) {
// 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)
value, 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
}
obj := &meta.Object{}
if jsErr := json.Unmarshal([]byte(value), obj); nil != jsErr {
blog.Error("failed to unmarshal the data, data is %s, error info is %s ", string(value), jsErr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommJSONUnmarshalFailed, jsErr.Error())})
return
}
// save to the storage
obj.CreateTime = new(time.Time)
*obj.CreateTime = time.Now()
obj.LastTime = new(time.Time)
*obj.LastTime = time.Now()
obj.OwnerID = ownerID
// get id
id, err := cli.Instance.GetIncID(common.BKTableNameObjDes)
if err != nil && !cli.Instance.IsNotFoundErr(err) {
blog.Error("failed to get id , error info is %s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectDBOpErrno, err.Error())})
return
}
obj.ID = id
// save
_, err = cli.Instance.Insert(common.BKTableNameObjDes, obj)
if nil == err && !cli.Instance.IsNotFoundErr(err) {
resp.WriteEntity(meta.CreateObjectResult{BaseResp: meta.SuccessBaseResp, Data: meta.RspID{ID: id}})
return
}
blog.Error("failed to insert the object, error info is %s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectDBOpErrno, err.Error())})
}
//删除Object
func (cli *Service) DeleteObject(req *restful.Request, resp *restful.Response) {
blog.Info("delete object")
// 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)
pathParameters := req.PathParameters()
appID, err := strconv.ParseInt(pathParameters["id"], 10, 64)
if nil != err {
blog.Error("failed to get params, error info is %s ", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommParamsInvalid, err.Error())})
return
}
condition := map[string]interface{}{"id": appID}
// delete object from storage
if 0 == appID {
js, err := simplejson.NewFromReader(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
}
condition, err = js.Map()
if nil != err {
blog.Error("fail to unmarshal json, error information is %s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommJSONUnmarshalFailed, err.Error())})
return
}
}
condition = util.SetModOwner(condition, ownerID)
cnt, cntErr := cli.Instance.GetCntByCondition(common.BKTableNameObjDes, condition)
if nil != cntErr && !cli.Instance.IsNotFoundErr(cntErr) {
blog.Error("failed to select object by condition(%+v), error is %d", cntErr)
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectDBOpErrno, cntErr.Error())})
return
}
if 0 == cnt {
// success
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
return
}
// execute delete command
if delErr := cli.Instance.DelByCondition(common.BKTableNameObjDes, condition); nil != delErr && !cli.Instance.IsNotFoundErr(delErr) {
blog.Error("fail to delete object by id , error information is %s", delErr.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectDBOpErrno, delErr.Error())})
return
}
// success
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
}
func (cli *Service) UpdateObject(req *restful.Request, resp *restful.Response) {
// 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)
js, err := simplejson.NewFromReader(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
}
pathParameters := req.PathParameters()
appID, err := strconv.ParseInt(pathParameters["id"], 10, 64)
if nil != err {
blog.Error("failed to get params, error info is %s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommParamsInvalid, err.Error())})
return
}
// update object into storage
js.Set(common.LastTimeField, util.GetCurrentTimeStr())
// decode json string
data, jsErr := js.Map()
if nil != jsErr {
blog.Error("unmarshal json failed, error information is %v", jsErr)
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommJSONUnmarshalFailed, jsErr.Error())})
return
}
condition := map[string]interface{}{"id": appID}
condition = util.SetModOwner(condition, ownerID)
err = cli.Instance.UpdateByCondition(common.BKTableNameObjDes, data, condition)
if nil != err && !cli.Instance.IsNotFoundErr(err) {
blog.Error("fail update object by condition, error information is %s", err.Error())
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectDBOpErrno, jsErr.Error())})
return
}
// success
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp})
}
//查询所有主机信息
func (cli *Service) SelectObjects(req *restful.Request, resp *restful.Response) {
// 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)
// decode json object
js, err := simplejson.NewFromReader(req.Request.Body)
if err != nil {
blog.Error("read request body failed, error information is %s", err.Error())
if nil != err {
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrCommHTTPReadBodyFailed, err.Error())})
return
}
}
page := meta.BasePage{Limit: common.BKNoLimit}
if pageJS, ok := js.CheckGet("page"); ok {
tmpMap, err := pageJS.Map()
if nil != err {
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectDBOpErrno, err.Error())})
return
}
page = meta.ParsePage(tmpMap)
js.Del("page")
}
results := make([]meta.Object, 0)
// select from storage
selector, err := js.Map()
if nil != err {
resp.WriteError(http.StatusBadRequest, &meta.RespError{Msg: defErr.New(common.CCErrObjectDBOpErrno, err.Error())})
return
}
selector = util.SetQueryOwner(selector, ownerID)
if selErr := cli.Instance.GetMutilByCondition(common.BKTableNameObjDes, nil, selector, &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.CCErrObjectDBOpErrno, selErr.Error())})
return
}
// translate language
for index := range results {
results[index].ObjectName = cli.TranslateObjectName(defLang, &results[index])
}
// success
resp.WriteEntity(meta.Response{BaseResp: meta.SuccessBaseResp, Data: results})
}