Rainbond/api/controller/event.go

208 lines
5.5 KiB
Go
Raw Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
2018-03-14 14:07:12 +08:00
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
2018-03-14 14:07:12 +08:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
2018-03-14 14:33:31 +08:00
2018-03-14 14:07:12 +08:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2018-03-14 14:33:31 +08:00
2018-03-14 14:07:12 +08:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2017-11-24 13:50:10 +08:00
package controller
import (
"io/ioutil"
2017-11-27 18:01:04 +08:00
"net/http"
"strconv"
"time"
"github.com/jinzhu/gorm"
2017-11-27 18:01:04 +08:00
"github.com/Sirupsen/logrus"
"github.com/bitly/go-simplejson"
"github.com/go-chi/chi"
"github.com/goodrain/rainbond/db"
httputil "github.com/goodrain/rainbond/util/http"
2017-11-24 13:50:10 +08:00
)
2017-12-07 09:56:08 +08:00
//Event GetLogs
2017-11-24 13:50:10 +08:00
func (e *TenantStruct) Event(w http.ResponseWriter, r *http.Request) {
// swagger:operation GET /v2/tenants/{tenant_name}/event v2 getevents
2017-11-24 13:50:10 +08:00
//
2017-11-27 18:01:04 +08:00
// 获取指定event_ids详细信息
2017-11-24 13:50:10 +08:00
//
// get events
//
// ---
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
2017-11-27 18:01:04 +08:00
b, _ := ioutil.ReadAll(r.Body)
2017-11-24 13:50:10 +08:00
defer r.Body.Close()
2017-11-27 18:01:04 +08:00
j, err := simplejson.NewJson(b)
2017-11-24 13:50:10 +08:00
if err != nil {
2017-11-27 18:01:04 +08:00
logrus.Errorf("error decode json,details %s", err.Error())
httputil.ReturnError(r, w, 400, "bad request")
2017-11-24 13:50:10 +08:00
return
}
2017-11-27 18:01:04 +08:00
eventIDS, err := j.Get("event_ids").StringArray()
2017-11-24 13:50:10 +08:00
if err != nil {
2017-11-27 18:01:04 +08:00
logrus.Errorf("error get event_id in json,details %s", err.Error())
httputil.ReturnError(r, w, 400, "bad request")
2017-11-24 13:50:10 +08:00
return
}
2018-02-06 14:59:21 +08:00
serviceEvents, err := db.GetManager().ServiceEventDao().GetEventByEventIDs(eventIDS)
if err != nil {
logrus.Warnf("can't find event by given id ,details %s", err.Error())
httputil.ReturnError(r, w, 500, err.Error())
2017-11-24 13:50:10 +08:00
}
2018-02-06 14:59:21 +08:00
httputil.ReturnSuccess(r, w, serviceEvents)
2017-11-24 13:50:10 +08:00
}
//GetNotificationEvents GetNotificationEvent
//support query from start and end time or all
// swagger:operation GET /v2/notificationEvent v2/notificationEvent getevents
//
// 获取数据中心通知事件
//
// get events
//
// ---
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func GetNotificationEvents(w http.ResponseWriter, r *http.Request) {
var startTime, endTime time.Time
start := r.FormValue("start")
end := r.FormValue("end")
if si, err := strconv.Atoi(start); err == nil {
startTime = time.Unix(int64(si), 0)
}
if ei, err := strconv.Atoi(end); err == nil {
endTime = time.Unix(int64(ei), 0)
}
res, err := db.GetManager().NotificationEventDao().GetNotificationEventByTime(startTime, endTime)
if err != nil {
httputil.ReturnError(r, w, 500, err.Error())
return
}
httputil.ReturnSuccess(r, w, res)
}
//Handle Handle
// swagger:parameters handlenotify
type Handle struct {
Body struct {
//in: body
//handle message
HandleMessage string `json:"handle_message" validate:"handle_message|required"`
}
}
//HandleNotificationEvent HandleNotificationEvent
// swagger:operation PUT /v2/notificationEvent/{hash} v2/notificationEvent handlenotify
//
// 处理通知事件
//
// get events
//
// ---
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func HandleNotificationEvent(w http.ResponseWriter, r *http.Request) {
hash := chi.URLParam(r, "hash")
if hash == "" {
httputil.ReturnError(r, w, 400, "hash id do not empty")
return
}
var handle Handle
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &handle.Body, nil)
if !ok {
return
}
event, err := db.GetManager().NotificationEventDao().GetNotificationEventByHash(hash)
if err != nil {
if err == gorm.ErrRecordNotFound {
httputil.ReturnError(r, w, 404, "not found")
return
}
httputil.ReturnError(r, w, 500, err.Error())
return
}
event.IsHandle = true
event.HandleMessage = handle.Body.HandleMessage
err = db.GetManager().NotificationEventDao().UpdateModel(event)
if err != nil {
if err == gorm.ErrRecordNotFound {
httputil.ReturnError(r, w, 404, "not found")
return
}
httputil.ReturnError(r, w, 500, err.Error())
return
}
httputil.ReturnSuccess(r, w, event)
}
//GetNotificationEvent GetNotificationEvent
// swagger:operation GET /v2/notificationEvent/{hash} v2/notificationEvent getevents
//
// 获取通知事件
//
// get events
//
// ---
// produces:
// - application/json
// - application/xml
//
// responses:
// default:
// schema:
// "$ref": "#/responses/commandResponse"
// description: 统一返回格式
func GetNotificationEvent(w http.ResponseWriter, r *http.Request) {
hash := chi.URLParam(r, "hash")
if hash == "" {
httputil.ReturnError(r, w, 400, "hash id do not empty")
return
}
event, err := db.GetManager().NotificationEventDao().GetNotificationEventByHash(hash)
if err != nil {
if err == gorm.ErrRecordNotFound {
httputil.ReturnError(r, w, 404, "not found")
return
}
httputil.ReturnError(r, w, 500, err.Error())
return
}
httputil.ReturnSuccess(r, w, event)
}