add export, import api
This commit is contained in:
parent
350549507b
commit
cf8122fe15
|
@ -0,0 +1,21 @@
|
||||||
|
/* Copyright © INFINI Ltd. All rights reserved.
|
||||||
|
* Web: https://infinilabs.com
|
||||||
|
* Email: hello#infini.ltd */
|
||||||
|
|
||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"infini.sh/framework/core/api"
|
||||||
|
"infini.sh/framework/core/api/rbac/enum"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DataAPI struct {
|
||||||
|
api.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitAPI() {
|
||||||
|
dataApi := DataAPI{}
|
||||||
|
api.HandleAPIMethod(api.POST, "/data/export", dataApi.RequirePermission(dataApi.exportData, enum.PermissionAlertChannelRead, enum.PermissionAlertRuleRead))
|
||||||
|
api.HandleAPIMethod(api.POST, "/data/import", dataApi.RequirePermission(dataApi.importData, enum.PermissionAlertChannelWrite, enum.PermissionAlertRuleWrite))
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,108 @@
|
||||||
|
/* Copyright © INFINI Ltd. All rights reserved.
|
||||||
|
* Web: https://infinilabs.com
|
||||||
|
* Email: hello#infini.ltd */
|
||||||
|
|
||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
log "github.com/cihub/seelog"
|
||||||
|
"infini.sh/console/model"
|
||||||
|
"infini.sh/console/model/alerting"
|
||||||
|
httprouter "infini.sh/framework/core/api/router"
|
||||||
|
"infini.sh/framework/core/orm"
|
||||||
|
"infini.sh/framework/core/util"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *DataAPI) exportData(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
|
reqBody := &ExportDataRequest{}
|
||||||
|
err := h.DecodeJSON(req, &reqBody)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var resBody []ExportData
|
||||||
|
for _, meta := range reqBody.Metadatas {
|
||||||
|
result, err := getExportData(meta.Type)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resBody = append(resBody, ExportData{
|
||||||
|
Type: meta.Type,
|
||||||
|
Data: result.Result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
h.WriteJSON(w, resBody, http.StatusOK)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *DataAPI) importData(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
|
reqBody := []ExportData{}
|
||||||
|
err := h.DecodeJSON(req, &reqBody)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = indexExportData(reqBody)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.WriteAckOKJSON(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexExportData(eds []ExportData) error {
|
||||||
|
for _, ed := range eds {
|
||||||
|
var obj interface{}
|
||||||
|
switch ed.Type {
|
||||||
|
case DataTypeAlertChannel:
|
||||||
|
obj = &alerting.Channel{}
|
||||||
|
case DataTypeAlertRule:
|
||||||
|
obj = &alerting.Rule{}
|
||||||
|
case DataTypeAlertEmailServer:
|
||||||
|
obj = &model.EmailServer{}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unkonw data type: %s", ed.Type)
|
||||||
|
}
|
||||||
|
for _, row := range ed.Data {
|
||||||
|
buf := util.MustToJSONBytes(row)
|
||||||
|
err := util.FromJSONBytes(buf, obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = orm.Save(nil, obj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getExportData(typ string) (*orm.Result, error) {
|
||||||
|
var obj interface{}
|
||||||
|
switch typ {
|
||||||
|
case DataTypeAlertChannel:
|
||||||
|
obj = alerting.Channel{}
|
||||||
|
case DataTypeAlertRule:
|
||||||
|
obj = alerting.Rule{}
|
||||||
|
case DataTypeAlertEmailServer:
|
||||||
|
obj = model.EmailServer{}
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unkonw data type: %s", typ)
|
||||||
|
}
|
||||||
|
err, result := orm.Search(obj, &orm.Query{
|
||||||
|
Size: 1000,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &result, err
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* Copyright © INFINI Ltd. All rights reserved.
|
||||||
|
* Web: https://infinilabs.com
|
||||||
|
* Email: hello#infini.ltd */
|
||||||
|
|
||||||
|
package data
|
||||||
|
|
||||||
|
type ExportDataRequest struct {
|
||||||
|
Metadatas []ExportMetadata `json:"metadatas"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExportMetadata struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExportData struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Data []interface{} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
DataTypeAlertRule = "AlertRule"
|
||||||
|
DataTypeAlertChannel = "AlertChannel"
|
||||||
|
DataTypeAlertEmailServer = "EmailServer"
|
||||||
|
)
|
|
@ -1,6 +1,7 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"infini.sh/console/plugin/api/data"
|
||||||
"infini.sh/console/plugin/api/email"
|
"infini.sh/console/plugin/api/email"
|
||||||
"infini.sh/console/plugin/api/license"
|
"infini.sh/console/plugin/api/license"
|
||||||
"path"
|
"path"
|
||||||
|
@ -77,4 +78,5 @@ func Init(cfg *config.AppConfig) {
|
||||||
|
|
||||||
license.InitAPI()
|
license.InitAPI()
|
||||||
email.InitAPI()
|
email.InitAPI()
|
||||||
|
data.InitAPI()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue