From e660e25be9e8ad88ae3908142cb7e41addf521c3 Mon Sep 17 00:00:00 2001 From: tangbin Date: Fri, 24 Jul 2020 17:59:52 +0800 Subject: [PATCH] host upload template check --- pkg/apigateway/handler/misc.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/pkg/apigateway/handler/misc.go b/pkg/apigateway/handler/misc.go index f2b9ae366d..1af0034fb4 100644 --- a/pkg/apigateway/handler/misc.go +++ b/pkg/apigateway/handler/misc.go @@ -25,6 +25,7 @@ import ( "github.com/360EntSecGroup-Skylar/excelize" "golang.org/x/sync/errgroup" + "yunion.io/x/pkg/utils" "yunion.io/x/jsonutils" "yunion.io/x/log" @@ -57,6 +58,13 @@ const ( BATCH_HOST_REGISTER_QUANTITY_LIMITATION = 1000 ) + +var ( + BatchHostRegisterTemplate = []string{HOST_MAC, HOST_NAME, HOST_IPMI_ADDR_OPTIONAL, HOST_IPMI_USERNAME_OPTIONAL, HOST_IPMI_PASSWORD_OPTIONAL} + BatchHostISORegisterTemplate = []string{HOST_NAME, HOST_IPMI_ADDR, HOST_IPMI_USERNAME, HOST_IPMI_PASSWORD, HOST_MNG_IP_ADDR} + BatchHostPXERegisterTemplate = []string{HOST_NAME, HOST_IPMI_ADDR, HOST_IPMI_USERNAME, HOST_IPMI_PASSWORD, HOST_MNG_IP_ADDR_OPTIONAL} +) + func FetchSession(ctx context.Context, r *http.Request, apiVersion string) *mcclient.ClientSession { token := AppContextToken(ctx) session := auth.GetSession(ctx, token, FetchRegion(r), apiVersion) @@ -170,6 +178,25 @@ func (mh *MiscHandler) DoBatchHostRegister(ctx context.Context, w http.ResponseW return } + // check header line + titlesOk := false + for _, t := range [][]string{BatchHostRegisterTemplate, BatchHostISORegisterTemplate, BatchHostPXERegisterTemplate} { + if len(t) == len(rows[0]) { + for _, title := range rows[0] { + if !utils.IsInStringArray(title, t) { + break + } + } + + titlesOk = true + } + } + + if !titlesOk { + httperrors.InputParameterError(w, "template file is invalid.please check.") + return + } + paramKeys := []string{} for _, title := range rows[0] { switch title { @@ -375,21 +402,21 @@ func (mh *MiscHandler) getDownloadsHandler(ctx context.Context, w http.ResponseW var content bytes.Buffer switch template { case "BatchHostRegister": - records := [][]string{{HOST_MAC, HOST_NAME, HOST_IPMI_ADDR_OPTIONAL, HOST_IPMI_USERNAME_OPTIONAL, HOST_IPMI_PASSWORD_OPTIONAL}} + records := [][]string{BatchHostRegisterTemplate} content, err = writeXlsx("hosts", records) if err != nil { httperrors.InternalServerError(w, "internal server error") return } case "BatchHostISORegister": - records := [][]string{{HOST_NAME, HOST_IPMI_ADDR, HOST_IPMI_USERNAME, HOST_IPMI_PASSWORD, HOST_MNG_IP_ADDR}} + records := [][]string{BatchHostISORegisterTemplate} content, err = writeXlsx("hosts", records) if err != nil { httperrors.InternalServerError(w, "internal server error") return } case "BatchHostPXERegister": - records := [][]string{{HOST_NAME, HOST_IPMI_ADDR, HOST_IPMI_USERNAME, HOST_IPMI_PASSWORD, HOST_MNG_IP_ADDR_OPTIONAL}} + records := [][]string{BatchHostPXERegisterTemplate} content, err = writeXlsx("hosts", records) if err != nil { httperrors.InternalServerError(w, "internal server error")