You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/06/22 07:22:15 UTC

[GitHub] [apisix-dashboard] nic-6443 commented on a diff in pull request #2474: feat: integrate data loader interface to import

nic-6443 commented on code in PR #2474:
URL: https://github.com/apache/apisix-dashboard/pull/2474#discussion_r903374871


##########
api/internal/handler/data_loader/route_import.go:
##########
@@ -19,535 +19,317 @@ package data_loader
 import (
 	"bytes"
 	"context"
-	"encoding/json"
 	"fmt"
-	"net/http"
 	"path"
 	"reflect"
-	"regexp"
-	"strings"
 
-	"github.com/getkin/kin-openapi/openapi3"
 	"github.com/gin-gonic/gin"
+	"github.com/juliangruber/go-intersect"
+	"github.com/pkg/errors"
 	"github.com/shiningrush/droplet"
-	"github.com/shiningrush/droplet/data"
 	"github.com/shiningrush/droplet/wrapper"
 	wgin "github.com/shiningrush/droplet/wrapper/gin"
 
 	"github.com/apisix/manager-api/internal/conf"
 	"github.com/apisix/manager-api/internal/core/entity"
 	"github.com/apisix/manager-api/internal/core/store"
 	"github.com/apisix/manager-api/internal/handler"
-	"github.com/apisix/manager-api/internal/log"
-	"github.com/apisix/manager-api/internal/utils"
-	"github.com/apisix/manager-api/internal/utils/consts"
+	loader "github.com/apisix/manager-api/internal/handler/data_loader/loader"
+	"github.com/apisix/manager-api/internal/handler/data_loader/loader/openapi3"
 )
 
 type ImportHandler struct {
-	routeStore    *store.GenericStore
-	svcStore      store.Interface
-	upstreamStore store.Interface
+	routeStore        store.Interface
+	upstreamStore     store.Interface
+	serviceStore      store.Interface
+	consumerStore     store.Interface
+	sslStore          store.Interface
+	streamRouteStore  store.Interface
+	globalPluginStore store.Interface
+	pluginConfigStore store.Interface
+	protoStore        store.Interface
 }
 
 func NewImportHandler() (handler.RouteRegister, error) {
 	return &ImportHandler{
-		routeStore:    store.GetStore(store.HubKeyRoute),
-		svcStore:      store.GetStore(store.HubKeyService),
-		upstreamStore: store.GetStore(store.HubKeyUpstream),
+		routeStore:        store.GetStore(store.HubKeyRoute),
+		upstreamStore:     store.GetStore(store.HubKeyUpstream),
+		serviceStore:      store.GetStore(store.HubKeyService),
+		consumerStore:     store.GetStore(store.HubKeyConsumer),
+		sslStore:          store.GetStore(store.HubKeySsl),
+		streamRouteStore:  store.GetStore(store.HubKeyStreamRoute),
+		globalPluginStore: store.GetStore(store.HubKeyGlobalRule),
+		pluginConfigStore: store.GetStore(store.HubKeyPluginConfig),
+		protoStore:        store.GetStore(store.HubKeyProto),
 	}, nil
 }
 
-var regPathVar = regexp.MustCompile(`{[\w.]*}`)
-var regPathRepeat = regexp.MustCompile(`-APISIX-REPEAT-URI-[\d]*`)
-
 func (h *ImportHandler) ApplyRoute(r *gin.Engine) {
 	r.POST("/apisix/admin/import/routes", wgin.Wraps(h.Import,
 		wrapper.InputType(reflect.TypeOf(ImportInput{}))))
 }
 
+type ImportResult struct {
+	Total  int      `json:"total"`
+	Failed int      `json:"failed"`
+	Errors []string `json:"errors"`
+}
+
+type LoaderType string
+
 type ImportInput struct {
-	Force       bool   `auto_read:"force,query"`
+	Type        string `auto_read:"type"`
+	TaskName    string `auto_read:"task_name"`
 	FileName    string `auto_read:"_file"`
 	FileContent []byte `auto_read:"file"`
+
+	MergeMethod string `auto_read:"merge_method"`
 }
 
+const (
+	LoaderTypeOpenAPI3 LoaderType = "openapi3"
+)
+
 func (h *ImportHandler) Import(c droplet.Context) (interface{}, error) {
 	input := c.Input().(*ImportInput)
-	Force := input.Force
 
-	// file check
+	// input file content check
 	suffix := path.Ext(input.FileName)
 	if suffix != ".json" && suffix != ".yaml" && suffix != ".yml" {
-		return nil, fmt.Errorf("required file type is .yaml, .yml or .json but got: %s", suffix)
+		return nil, errors.Errorf("required file type is .yaml, .yml or .json but got: %s", suffix)
 	}
-
 	contentLen := bytes.Count(input.FileContent, nil) - 1
-	if contentLen > conf.ImportSizeLimit {
-		log.Warnf("upload file size exceeds limit: %d", contentLen)
-		return nil, fmt.Errorf("the file size exceeds the limit; limit %d", conf.ImportSizeLimit)
+	if contentLen <= 0 {
+		return nil, errors.New("uploaded file is empty")
 	}
-
-	swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(input.FileContent)
-	if err != nil {
-		return nil, err
+	if contentLen > conf.ImportSizeLimit {
+		return nil, errors.Errorf("uploaded file size exceeds the limit, limit is %d", conf.ImportSizeLimit)
 	}
 
-	if len(swagger.Paths) < 1 {
-		return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
-			consts.ErrImportFile
+	var l loader.Loader
+	switch LoaderType(input.Type) {
+	case LoaderTypeOpenAPI3:
+		l = &openapi3.Loader{
+			MergeMethod: input.MergeMethod == "true",

Review Comment:
   Why not use `bool` as type of `input.MergeMethod`directly?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org