Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add publish status to route in manager api #450

Merged
merged 4 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/errno/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
DBRouteDeleteError = Message{"010207", "Route deletion failed: %s", 500}
DBRouteReduplicateError = Message{"010208", "Route name is reduplicate : %s", 400}
SetRouteUngroupError = Message{"010209", "Set route ungroup err", 500}
RoutePublishError = Message{"010209", "Route publish error", 400}

// 03 plugins
ApisixPluginListError = Message{"010301", "find APISIX plugin list failed: %s", 500}
Expand Down
203 changes: 174 additions & 29 deletions api/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,137 @@ func AppendRoute(r *gin.Engine) *gin.Engine {
r.GET("/apisix/admin/routes/:rid", findRoute)
r.GET("/apisix/admin/routes", listRoute)
r.PUT("/apisix/admin/routes/:rid", updateRoute)
r.PUT("/apisix/admin/routes/:rid/publish", publishRoute)
r.DELETE("/apisix/admin/routes/:rid", deleteRoute)
r.GET("/apisix/admin/notexist/routes", isRouteExist)
r.PUT("/apisix/admin/routes/:rid/offline", offlineRoute)
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
return r
}

func publishRoute(c *gin.Context) {
rid := c.Param("rid")
r := &service.Route{}
tx := conf.DB().Begin()
if err := tx.Model(&service.Route{}).Where("id = ?", rid).Update("status", true).Find(&r).Error; err != nil {
tx.Rollback()
e := errno.FromMessage(errno.RoutePublishError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
routeRequest := &service.RouteRequest{}
if err := json.Unmarshal([]byte(r.Content), routeRequest); err != nil {
tx.Rollback()
e := errno.FromMessage(errno.RoutePublishError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
routeRequest.Status = true
}
if content, err := json.Marshal(routeRequest); err != nil {
tx.Rollback()
e := errno.FromMessage(errno.RoutePublishError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
r.Content = string(content)
}
arr := service.ToApisixRequest(routeRequest)
var resp *service.ApisixRouteResponse
if resp, err = arr.Create(rid); err != nil {
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
tx.Rollback()
if httpError, ok := err.(*errno.HttpError); ok {
c.AbortWithStatusJSON(httpError.Code, httpError.Msg)
return
} else {
e := errno.FromMessage(errno.ApisixRouteCreateError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
}
} else {
resp.Node.Value.Name = r.Name
resp.Node.Value.Status = r.Status
if respStr, err := json.Marshal(resp); err != nil {
e := errno.FromMessage(errno.RoutePublishError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
r.ContentAdminApi = string(respStr)
}
}
if err := tx.Commit().Error; err == nil {
// update content_admin_api
if err := conf.DB().Model(&service.Route{}).Update(r).Error; err != nil {
e := errno.FromMessage(errno.DBRouteUpdateError, err.Error())
logger.Error(e.Msg)
}
}
}
c.Data(http.StatusOK, service.ContentType, errno.Success())
}

func offlineRoute(c *gin.Context) {
rid := c.Param("rid")
db := conf.DB()
tx := db.Begin()
route := &service.Route{}
if err := tx.Model(&service.Route{}).Where("id = ?", rid).Update(map[string]interface{}{"status": 0, "content_admin_api": ""}).First(&route).Error; err != nil {
tx.Rollback()
e := errno.FromMessage(errno.RoutePublishError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
routeRequest := &service.RouteRequest{}
if err := json.Unmarshal([]byte(route.Content), routeRequest); err != nil {
tx.Rollback()
e := errno.FromMessage(errno.RoutePublishError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
routeRequest.Status = false
}
if content, err := json.Marshal(routeRequest); err != nil {
tx.Rollback()
e := errno.FromMessage(errno.RoutePublishError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
route.Content = string(content)
}
request := &service.ApisixRouteRequest{}
if _, err := request.Delete(rid); err != nil {
tx.Rollback()
if httpError, ok := err.(*errno.HttpError); ok {
c.AbortWithStatusJSON(httpError.Code, httpError.Msg)
return
} else {
e := errno.FromMessage(errno.ApisixRouteDeleteError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
}
}
if err := tx.Model(&service.Route{}).Update(route).Error; err != nil {
tx.Rollback()
e := errno.FromMessage(errno.DBRouteUpdateError, err.Error())
logger.Error(e.Msg)
return
}
}
if err := tx.Commit().Error; err != nil {
e := errno.FromMessage(errno.ApisixRouteDeleteError, err.Error())
logger.Error(e.Msg)
}
c.Data(http.StatusOK, service.ContentType, errno.Success())
}

func isRouteExist(c *gin.Context) {
if name, exist := c.GetQuery("name"); exist {
db := conf.DB()
Expand Down Expand Up @@ -156,13 +282,19 @@ func deleteRoute(c *gin.Context) {
// delete from mysql
rd := &service.Route{}
rd.ID = uuid.FromStringOrNil(rid)
if err := db.Table("routes").Where("id=?", rid).First(&rd).Error; err != nil {
e := errno.FromMessage(errno.RouteRequestError, err.Error()+" route ID: "+rid)
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
}
if err := conf.DB().Delete(rd).Error; err != nil {
tx.Rollback()
e := errno.FromMessage(errno.DBRouteDeleteError, err.Error())
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
} else if rd.Status {
request := &service.ApisixRouteRequest{}
if _, err := request.Delete(rid); err != nil {
tx.Rollback()
Expand Down Expand Up @@ -217,10 +349,12 @@ func updateRoute(c *gin.Context) {
db := conf.DB()
arr := service.ToApisixRequest(routeRequest)
var resp *service.ApisixRouteResponse
var r *service.Route
if rd, err := service.ToRoute(routeRequest, arr, uuid.FromStringOrNil(rid), nil); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Response())
return
} else {
r = rd
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
Expand All @@ -244,7 +378,7 @@ func updateRoute(c *gin.Context) {
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
} else if rd.Status {
if resp, err = arr.Update(rid); err != nil {
tx.Rollback()
if httpError, ok := err.(*errno.HttpError); ok {
Expand All @@ -267,7 +401,7 @@ func updateRoute(c *gin.Context) {
return
}
}
if err := tx.Commit().Error; err == nil {
if err := tx.Commit().Error; err == nil && r.Status {
// update content_admin_api
if rd, err := service.ToRoute(routeRequest, arr, uuid.FromStringOrNil(rid), resp); err != nil {
e := errno.FromMessage(errno.DBRouteUpdateError, err.Error())
Expand All @@ -285,8 +419,9 @@ func updateRoute(c *gin.Context) {

func findRoute(c *gin.Context) {
rid := c.Param("rid")
route := &service.Route{}
var count int
if err := conf.DB().Table("routes").Where("id=?", rid).Count(&count).Error; err != nil {
if err := conf.DB().Table("routes").Where("id=?", rid).Count(&count).First(&route).Error; err != nil {
e := errno.FromMessage(errno.RouteRequestError, err.Error()+" route ID: "+rid)
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
Expand All @@ -299,40 +434,48 @@ func findRoute(c *gin.Context) {
return
}
}
// find from apisix
request := &service.ApisixRouteRequest{}
if response, err := request.FindById(rid); err != nil {
e := errno.FromMessage(errno.RouteRequestError, err.Error()+" route ID: "+rid)
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
return
if !route.Status {
routeRequest := &service.RouteRequest{}
if err := json.Unmarshal([]byte(route.Content), &routeRequest); err != nil {
e := errno.FromMessage(errno.RouteRequestError, " route ID: "+rid+" not exist")
logger.Error(e.Msg)
c.AbortWithStatusJSON(e.Status, e.Response())
return
} else {
routeRequest.Name = route.Name
resp, _ := json.Marshal(routeRequest)
c.Data(http.StatusOK, service.ContentType, resp)
}
} else {
// transfer response to dashboard struct
if result, err := response.Parse(); err != nil {
// find from apisix
request := &service.ApisixRouteRequest{}
if response, err := request.FindById(rid); err != nil {
e := errno.FromMessage(errno.RouteRequestError, err.Error()+" route ID: "+rid)
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
return
} else {
// need to find name from mysql temporary
route := &service.Route{}
if err := conf.DB().Table("routes").Where("id=?", rid).First(&route).Error; err != nil {
// transfer response to dashboard struct
if result, err := response.Parse(); err != nil {
e := errno.FromMessage(errno.RouteRequestError, err.Error()+" route ID: "+rid)
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusBadRequest, e.Response())
return
}
result.Name = route.Name
var script map[string]interface{}
if err = json.Unmarshal([]byte(route.Script), &script); err != nil {
script = map[string]interface{}{}
}
result.Script = script
} else {
// need to find name from mysql temporary
result.Name = route.Name
var script map[string]interface{}
if err = json.Unmarshal([]byte(route.Script), &script); err != nil {
script = map[string]interface{}{}
}
result.Script = script

result.RouteGroupId = route.RouteGroupId
result.RouteGroupName = route.RouteGroupName
resp, _ := json.Marshal(result)
c.Data(http.StatusOK, service.ContentType, resp)
result.RouteGroupId = route.RouteGroupId
result.RouteGroupName = route.RouteGroupName
result.Status = true
resp, _ := json.Marshal(result)
c.Data(http.StatusOK, service.ContentType, resp)
}
}
}
}
Expand Down Expand Up @@ -369,10 +512,12 @@ func createRoute(c *gin.Context) {
db := conf.DB()
arr := service.ToApisixRequest(routeRequest)
var resp *service.ApisixRouteResponse
var r *service.Route
if rd, err := service.ToRoute(routeRequest, arr, u4, nil); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err.Response())
return
} else {
r = rd
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
Expand All @@ -396,7 +541,7 @@ func createRoute(c *gin.Context) {
logger.Error(e.Msg)
c.AbortWithStatusJSON(http.StatusInternalServerError, e.Response())
return
} else {
} else if rd.Status {
if resp, err = arr.Create(rid); err != nil {
tx.Rollback()
if httpError, ok := err.(*errno.HttpError); ok {
Expand All @@ -410,7 +555,7 @@ func createRoute(c *gin.Context) {
}
}
}
if err := tx.Commit().Error; err == nil {
if err := tx.Commit().Error; err == nil && r.Status {
// update content_admin_api
if rd, err := service.ToRoute(routeRequest, arr, u4, resp); err != nil {
e := errno.FromMessage(errno.DBRouteUpdateError, err.Error())
Expand Down
Loading