Handle db connection response on the backend
This commit is contained in:
@@ -60,6 +60,47 @@ func (ds *ZabbixDatasource) ZabbixAPIHandler(rw http.ResponseWriter, req *http.R
|
||||
writeResponse(rw, result)
|
||||
}
|
||||
|
||||
func (ds *ZabbixDatasource) DBConnectionPostProcessingHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodPost {
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
defer req.Body.Close()
|
||||
if err != nil || len(body) == 0 {
|
||||
writeError(rw, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
var reqData DBConnectionPostProcessingRequest
|
||||
err = json.Unmarshal(body, &reqData)
|
||||
if err != nil {
|
||||
ds.logger.Error("Cannot unmarshal request", "error", err.Error())
|
||||
writeError(rw, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
pluginCxt := httpadapter.PluginConfigFromContext(req.Context())
|
||||
dsInstance, err := ds.getDSInstance(pluginCxt)
|
||||
if err != nil {
|
||||
ds.logger.Error("Error loading datasource", "error", err)
|
||||
writeError(rw, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
frames, err := dsInstance.applyDataProcessing(req.Context(), &reqData.Query, reqData.Series)
|
||||
|
||||
resultJson, err := json.Marshal(frames)
|
||||
if err != nil {
|
||||
writeError(rw, http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
rw.Header().Add("Content-Type", "application/json")
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
rw.Write(resultJson)
|
||||
|
||||
}
|
||||
|
||||
func writeResponse(rw http.ResponseWriter, result *ZabbixAPIResourceResponse) {
|
||||
resultJson, err := json.Marshal(*result)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user