Merge pull request #2337 from tb365/feature/tb-itsm-post-api

add itsm upload api
This commit is contained in:
yunion-ci-robot
2019-08-16 18:10:04 +08:00
committed by GitHub
2 changed files with 54 additions and 4 deletions
+27
View File
@@ -61,6 +61,7 @@ func (h *MiscHandler) Bind(app *appsrv.Application) {
uploader := UploadHandlerInfo(POST, prefix+"uploads", FetchAuthToken(h.PostUploads))
app.AddHandler3(uploader)
app.AddHandler(GET, prefix+"downloads/<template_id>", FetchAuthToken(h.getDownloadsHandler))
app.AddHandler(POST, prefix+"piuploads", FetchAuthToken(h.postPIUploads)) // itsm process instances upload api
}
func UploadHandlerInfo(method, prefix string, handler func(context.Context, http.ResponseWriter, *http.Request)) *appsrv.SHandlerInfo {
@@ -187,6 +188,32 @@ func (mh *MiscHandler) getDownloadsHandler(ctx context.Context, w http.ResponseW
}
}
func (mh *MiscHandler) postPIUploads(ctx context.Context, w http.ResponseWriter, req *http.Request) {
// 5 MB
var maxMemory int64 = 5 << 20
if req.ContentLength > maxMemory {
httperrors.InvalidInputError(w, "request body is too large.")
return
}
if !strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data") {
httperrors.InvalidInputError(w, "invalid multipart form")
return
}
s := FetchSession(ctx, req, "")
header := http.Header{}
header.Set("Content-Type", req.Header.Get("Content-Type"))
header.Set("Content-Length", req.Header.Get("Content-Length"))
resp, err := modules.ProcessInstance.Upload(s, header, req.Body)
if err != nil {
httperrors.GeneralServerError(w, err)
return
}
appsrv.SendJSON(w, resp)
}
func writeCsv(records [][]string) (bytes.Buffer, error) {
var content bytes.Buffer
content.WriteString("\xEF\xBB\xBF") // 写入UTF-8 BOM, 防止office打开后中文乱码
@@ -14,14 +14,37 @@
package modules
var (
ProcessInstance ResourceManager
import (
"fmt"
"io"
"net/http"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient"
)
type ProcessInstanceManager struct {
ResourceManager
}
var (
ProcessInstance ProcessInstanceManager
)
func (self *ProcessInstanceManager) Upload(s *mcclient.ClientSession, header http.Header, body io.Reader) (jsonutils.JSONObject, error) {
path := fmt.Sprintf("/%s", self.URLPath())
resp, err := self.rawRequest(s, "POST", path, header, body)
_, json, err := s.ParseJSONResponse(resp, err)
if err != nil {
return nil, err
}
return json.Get(self.Keyword)
}
func init() {
ProcessInstance = NewITSMManager("process-instance", "process-instances",
ProcessInstance = ProcessInstanceManager{NewITSMManager("process-instance", "process-instances",
[]string{"id", "process_instance_id", "root_process_instance_id", "case_instance_id", "business_key", "ended", "suspended", "tenant_id"},
[]string{},
)
)}
register(&ProcessInstance)
}