Files
opencloud/tests/ociswrapper/wrapper/handlers/handler.go
Sawjan Gurung 9945dad647 [POC][full-ci] Testing various oCIS env configurations (#6062)
* run api tests

* run ocis via wrapper

* add context, helpers and steps change ocis config

* remove separate pipelines for special test cases

* add context to behat config

* fix suite name

* set wrapper url while running tests

* fix cors allow env name

* address review

* add ociswrapper codebase

* add ci pipeline to build and cache wrapper

* add step to check ociswrapper cache

* invalidate wrapper cache and build new

* build on pipeline step

* undo unnecessary changes

* undo unnecessary changes

* fix php code

* fix ocis run command

* use tag for teardown

* exclude wrapper vendor in codacy check

* fix typos
2023-05-11 17:21:52 +05:45

69 lines
1.5 KiB
Go

package handlers
import (
"encoding/json"
"errors"
"io"
"net/http"
"ociswrapper/ocis"
)
func parseJsonBody(reqBody io.ReadCloser) (map[string]any, error) {
body, _ := io.ReadAll(reqBody)
if len(body) == 0 || !json.Valid(body) {
return nil, errors.New("Invalid json data")
}
var bodyMap map[string]any
json.Unmarshal(body, &bodyMap)
return bodyMap, nil
}
func sendResponse(res http.ResponseWriter, ocisStatus bool) {
resBody := make(map[string]string)
if ocisStatus {
res.WriteHeader(http.StatusOK)
resBody["status"] = "OK"
resBody["message"] = "oCIS server is running"
} else {
res.WriteHeader(http.StatusInternalServerError)
resBody["status"] = "ERROR"
resBody["message"] = "oCIS server error"
}
res.Header().Set("Content-Type", "application/json")
jsonResponse, _ := json.Marshal(resBody)
res.Write(jsonResponse)
}
func SetEnvHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPut {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
return
}
environments, err := parseJsonBody(req.Body)
if err != nil {
http.Error(res, "Bad request", http.StatusBadRequest)
return
}
ocisStatus := ocis.Restart(environments)
sendResponse(res, ocisStatus)
}
func RollbackHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(res, "Method not allowed", http.StatusMethodNotAllowed)
return
}
ocisStatus := ocis.Restart(nil)
sendResponse(res, ocisStatus)
}