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 2020/09/02 03:32:14 UTC

[GitHub] [apisix-dashboard] nic-chen commented on a change in pull request #432: Feat: manage api support route group

nic-chen commented on a change in pull request #432:
URL: https://github.com/apache/apisix-dashboard/pull/432#discussion_r481597737



##########
File path: api/route/route.go
##########
@@ -101,11 +101,15 @@ func listRoute(c *gin.Context) {
 		db = db.Where("upstream_nodes like ? ", "%"+ip+"%")
 		isSearch = false
 	}
+	if rgid, exist := c.GetQuery("route_group_id"); exist {
+		db = db.Where("route_group_id equal ?", rgid)

Review comment:
       does `equal` works  ? 

##########
File path: api/route/route_group.go
##########
@@ -0,0 +1,195 @@
+package route
+
+import (
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"strconv"
+
+	"github.com/apisix/manager-api/conf"
+	"github.com/apisix/manager-api/errno"
+	"github.com/apisix/manager-api/service"
+	"github.com/gin-gonic/gin"
+	uuid "github.com/satori/go.uuid"
+)
+
+func AppendRouteGroup(r *gin.Engine) *gin.Engine {
+	r.POST("/apisix/admin/routegroups", createRouteGroup)
+	r.GET("/apisix/admin/routegroups/:gid", findRouteGroup)
+	r.GET("/apisix/admin/routegroups", listRouteGroup)
+	r.GET("/apisix/admin/names/routegroups", listRouteGroupName)
+	r.PUT("/apisix/admin/routegroups/:gid", updateRouteGroup)
+	r.DELETE("/apisix/admin/routegroups/:gid", deleteRouteGroup)
+	return r
+}
+
+func createRouteGroup(c *gin.Context) {
+	u4 := uuid.NewV4()
+	gid := u4.String()
+	// todo 参数校验
+	param, exist := c.Get("requestBody")
+	if !exist || len(param.([]byte)) < 1 {
+		e := errno.FromMessage(errno.RouteRequestError, "route_group create with no post data")
+		logger.Error(e.Msg)
+		c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
+		return
+	}
+	// trans params
+	rr := &service.RouteGroupRequest{}
+	if err := rr.Parse(param); err != nil {
+		e := errno.FromMessage(errno.RouteGroupRequestError, err.Error())
+		logger.Error(e.Msg)
+		c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
+		return
+	}
+	rr.Id = gid
+	fmt.Println(rr)
+	// mysql
+	if rd, err := service.Trans2RouteGroupDao(rr); err != nil {
+		c.AbortWithStatusJSON(http.StatusInternalServerError, err.Response())
+		return
+	} else {
+		if err := rd.CreateRouteGroup(); err != nil {
+			e := errno.FromMessage(errno.DBRouteGroupError, err.Error())
+			logger.Error(e.Msg)
+			c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
+			return
+		}
+	}
+	c.Data(http.StatusOK, service.ContentType, errno.Success())
+}
+
+func findRouteGroup(c *gin.Context) {
+	gid := c.Param("gid")
+	routeGroup := &service.RouteGroupDao{}
+	if err, count := routeGroup.FindRouteGroup(gid); err != nil {
+		e := errno.FromMessage(errno.RouteGroupRequestError, err.Error()+"route_group ID:"+gid)
+		logger.Error(e.Msg)
+		c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
+		return
+	} else {
+		if count < 1 {
+			e := errno.FromMessage(errno.RouteRequestError, " route_group ID: "+gid+" not exist")
+			logger.Error(e.Msg)
+			c.AbortWithStatusJSON(e.Status, e.Response())
+			return
+		}
+	}
+	resp, _ := json.Marshal(routeGroup)
+	c.Data(http.StatusOK, service.ContentType, resp)
+}
+
+func listRouteGroup(c *gin.Context) {
+	size, _ := strconv.Atoi(c.Query("size"))
+	page, _ := strconv.Atoi(c.Query("page"))
+	if size == 0 {
+		size = 10
+	}
+	var s string
+
+	rd := &service.RouteGroupDao{}
+	routeGroupList := []service.RouteGroupDao{}
+	if search, exist := c.GetQuery("search"); exist && len(search) > 0 {
+		s = "%" + search + "%"
+	}
+	if err, count := rd.GetRouteGroupList(&routeGroupList, s, page, size); err != nil {
+		e := errno.FromMessage(errno.RouteGroupRequestError, err.Error())
+		logger.Error(e.Msg)
+		c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
+		return
+	} else {
+		result := &service.ListResponse{Count: count, Data: routeGroupList}
+		resp, _ := json.Marshal(result)
+		c.Data(http.StatusOK, service.ContentType, resp)
+	}
+}

Review comment:
       need a blank line between two functions.

##########
File path: api/service/route_group.go
##########
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package service
+
+import (
+	"encoding/json"
+	"github.com/apisix/manager-api/conf"
+	"github.com/apisix/manager-api/errno"
+	uuid "github.com/satori/go.uuid"
+)
+
+type RouteGroupDao struct {
+	Base
+	Name        string `json:"name"`
+	Description string `json:"description,omitempty"`
+}
+
+func (RouteGroupDao) TableName() string {
+	return "route_group"
+}
+
+func (rd *RouteGroupDao) CreateRouteGroup() error {
+	return conf.DB().Create(&rd).Error
+}
+
+func (rd *RouteGroupDao) FindRouteGroup(id string) (error, int) {
+	var count int
+	if err := conf.DB().Table("route_group").Where("id=?", id).Count(&count).Error; err != nil {
+		return err, 0
+	}
+	conf.DB().Table("route_group").Where("id=?", id).First(&rd)
+	return nil, count
+}
+
+func (rd *RouteGroupDao) GetRouteGroupList(routeGroupList *[]RouteGroupDao, search string, page, size int) (error, int) {
+	db := conf.DB()
+	db = db.Table(rd.TableName())
+	if len(search) != 0 {
+		db = db.Where("name like ? or description like ? ", search, search)
+	}
+	var count int
+	if err := db.Order("update_time desc").Offset((page - 1) * size).Limit(size).Find(&routeGroupList).Error; err != nil {
+		return err, 0
+	} else {
+		if err := db.Count(&count).Error; err != nil {
+			return err, 0
+		}
+		return nil, count
+	}
+}
+
+func (rd *RouteGroupDao) UpdateRouteGroup() error {
+	db := conf.DB()
+	return db.Model(&RouteGroupDao{}).Update(rd).Error
+}
+
+func (rd *RouteGroupDao) DeleteRouteGroup() error {
+	return conf.DB().Delete(&rd).Error

Review comment:
       we should check if there is a route bound before delete
   

##########
File path: api/route/route_group.go
##########
@@ -0,0 +1,195 @@
+package route
+
+import (
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"strconv"
+
+	"github.com/apisix/manager-api/conf"
+	"github.com/apisix/manager-api/errno"
+	"github.com/apisix/manager-api/service"
+	"github.com/gin-gonic/gin"
+	uuid "github.com/satori/go.uuid"
+)
+
+func AppendRouteGroup(r *gin.Engine) *gin.Engine {
+	r.POST("/apisix/admin/routegroups", createRouteGroup)
+	r.GET("/apisix/admin/routegroups/:gid", findRouteGroup)
+	r.GET("/apisix/admin/routegroups", listRouteGroup)
+	r.GET("/apisix/admin/names/routegroups", listRouteGroupName)
+	r.PUT("/apisix/admin/routegroups/:gid", updateRouteGroup)
+	r.DELETE("/apisix/admin/routegroups/:gid", deleteRouteGroup)
+	return r
+}
+
+func createRouteGroup(c *gin.Context) {
+	u4 := uuid.NewV4()
+	gid := u4.String()
+	// todo 参数校验
+	param, exist := c.Get("requestBody")
+	if !exist || len(param.([]byte)) < 1 {
+		e := errno.FromMessage(errno.RouteRequestError, "route_group create with no post data")
+		logger.Error(e.Msg)
+		c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
+		return
+	}
+	// trans params
+	rr := &service.RouteGroupRequest{}
+	if err := rr.Parse(param); err != nil {
+		e := errno.FromMessage(errno.RouteGroupRequestError, err.Error())
+		logger.Error(e.Msg)
+		c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
+		return
+	}
+	rr.Id = gid
+	fmt.Println(rr)
+	// mysql
+	if rd, err := service.Trans2RouteGroupDao(rr); err != nil {
+		c.AbortWithStatusJSON(http.StatusInternalServerError, err.Response())
+		return
+	} else {
+		if err := rd.CreateRouteGroup(); err != nil {
+			e := errno.FromMessage(errno.DBRouteGroupError, err.Error())
+			logger.Error(e.Msg)
+			c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
+			return
+		}
+	}
+	c.Data(http.StatusOK, service.ContentType, errno.Success())
+}
+
+func findRouteGroup(c *gin.Context) {
+	gid := c.Param("gid")
+	routeGroup := &service.RouteGroupDao{}
+	if err, count := routeGroup.FindRouteGroup(gid); err != nil {
+		e := errno.FromMessage(errno.RouteGroupRequestError, err.Error()+"route_group ID:"+gid)
+		logger.Error(e.Msg)
+		c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
+		return
+	} else {
+		if count < 1 {
+			e := errno.FromMessage(errno.RouteRequestError, " route_group ID: "+gid+" not exist")
+			logger.Error(e.Msg)
+			c.AbortWithStatusJSON(e.Status, e.Response())
+			return
+		}
+	}
+	resp, _ := json.Marshal(routeGroup)
+	c.Data(http.StatusOK, service.ContentType, resp)
+}
+
+func listRouteGroup(c *gin.Context) {
+	size, _ := strconv.Atoi(c.Query("size"))
+	page, _ := strconv.Atoi(c.Query("page"))
+	if size == 0 {
+		size = 10
+	}
+	var s string
+
+	rd := &service.RouteGroupDao{}
+	routeGroupList := []service.RouteGroupDao{}
+	if search, exist := c.GetQuery("search"); exist && len(search) > 0 {
+		s = "%" + search + "%"
+	}
+	if err, count := rd.GetRouteGroupList(&routeGroupList, s, page, size); err != nil {
+		e := errno.FromMessage(errno.RouteGroupRequestError, err.Error())
+		logger.Error(e.Msg)
+		c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
+		return
+	} else {
+		result := &service.ListResponse{Count: count, Data: routeGroupList}
+		resp, _ := json.Marshal(result)
+		c.Data(http.StatusOK, service.ContentType, resp)
+	}
+}
+func listRouteGroupName(c *gin.Context) {
+	db := conf.DB()

Review comment:
       i think we should make paging for this api.




----------------------------------------------------------------
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.

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